From 69e9828beb6d119a0672a06aa233d650133494cd Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 12 May 2015 23:30:14 -0400 Subject: [PATCH] Object class masquerading --- lib/ClassBuilder.js | 25 ++++++++++++++++++++++++- lib/class.js | 5 ----- test/Class/GeneralTest.js | 18 +++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/ClassBuilder.js b/lib/ClassBuilder.js index 06bac0b..241d0df 100644 --- a/lib/ClassBuilder.js +++ b/lib/ClassBuilder.js @@ -242,6 +242,30 @@ exports.getMeta = function( cls ) } +/** + * Allow OBJ to assume an identity as a class + * + * This is useful to use objects in situations where classes are expected, + * as it eliminates the need for handling of special cases. + * + * This is intended for internal use---there are no guarantees as to what + * methods ease.js may expect that a class-like object incorporate. That + * guarantee may exist in the future, but until then, stay away. + * + * @param {Object} obj object to masquerade as an ease.js class + * + * @return {Object} OBJ + */ +exports.masquerade = function( obj ) +{ + // XXX: this is duplicated; abstract + util.defineSecureProp( obj, _priv, {} ); + + createMeta( obj, exports.ClassBase ); + return obj; +}; + + /** * Determines if the class is an instance of the given type * @@ -1461,4 +1485,3 @@ function attachFlags( ctor, props ) // (v8 performance) props.___$$final$$ = props.___$$abstract$$ = undefined; } - diff --git a/lib/class.js b/lib/class.js index d7b3884..59b69e9 100644 --- a/lib/class.js +++ b/lib/class.js @@ -179,11 +179,6 @@ module.exports.isClass = function( obj ) { obj = obj || _dummyclass; - if ( !obj.prototype ) - { - return false; - } - var meta = ClassBuilder.getMeta( obj ); // TODO: we're checking a random field on the meta object; do something diff --git a/test/Class/GeneralTest.js b/test/Class/GeneralTest.js index 65746a9..0b59bbe 100644 --- a/test/Class/GeneralTest.js +++ b/test/Class/GeneralTest.js @@ -24,7 +24,8 @@ require( 'common' ).testCase( { setUp: function() { - this.Sut = this.require( 'class' ); + this.Sut = this.require( 'class' ); + this.ClassBuilder = this.require( 'ClassBuilder' ); this.Foo = this.Sut.extend( { @@ -249,6 +250,21 @@ require( 'common' ).testCase( }, + /** + * There are cases---intended for internal use---where it is beneficial + * for an object to be treated as though it were actually a class. + */ + 'Any object may masquerade as a class': function() + { + var obj = {}; + + // XXX: tightly coupled logic here; refactor things + this.ClassBuilder.masquerade( obj ); + + this.assertOk( this.Sut.isClass( obj ) ); + }, + + /** * This really should be encapsulated, probably, but it does exist for * reference.