2011-03-27 01:57:17 -04:00
|
|
|
/**
|
|
|
|
* Handles building of classes
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package core
|
2011-03-27 23:09:17 -04:00
|
|
|
*
|
|
|
|
* TODO: This module is currently being tested /indirectly/ by the class tests.
|
|
|
|
* This is because of a refactoring. All of this logic used to be part of
|
|
|
|
* the class module. Test this module directly, but keep the existing
|
|
|
|
* class tests in tact for a higher-level test.
|
2011-03-27 01:57:17 -04:00
|
|
|
*/
|
|
|
|
|
2011-03-27 02:02:04 -04:00
|
|
|
var util = require( __dirname + '/util' ),
|
2011-03-27 23:04:40 -04:00
|
|
|
member_builder = require( __dirname + '/member_builder' ),
|
|
|
|
propobj = require( __dirname + '/propobj' ),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class id counter, to be increment on each new definition
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
class_id = 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance id counter, to be incremented on each new instance
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2011-03-27 23:16:19 -04:00
|
|
|
instance_id = 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to TRUE when class is in the process of being extended to ensure that
|
|
|
|
* a constructor can be instantiated (to use as the prototype) without
|
|
|
|
* invoking the class construction logic
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2011-03-28 18:18:40 -04:00
|
|
|
extending = false,
|
|
|
|
|
2011-05-12 00:05:50 -04:00
|
|
|
/**
|
|
|
|
* A flag to let the system know that we are currently attempting to access
|
|
|
|
* a static property from within a method. This means that the caller should
|
|
|
|
* be given access to additional levels of visibility.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
sprop_internal = false,
|
|
|
|
|
2011-03-28 18:18:40 -04:00
|
|
|
/**
|
|
|
|
* Hash of reserved members
|
|
|
|
*
|
|
|
|
* These methods cannot be defined in the class. They are for internal use
|
2011-03-28 19:17:38 -04:00
|
|
|
* only. We must check both properties and methods to ensure that neither is
|
|
|
|
* defined.
|
2011-03-28 18:18:40 -04:00
|
|
|
*
|
|
|
|
* @type {Object.<string,boolean>}
|
|
|
|
*/
|
2011-03-28 19:01:58 -04:00
|
|
|
reserved_members = { '__initProps': true },
|
2011-03-28 18:21:06 -04:00
|
|
|
|
|
|
|
/**
|
2011-03-28 19:17:38 -04:00
|
|
|
* Hash of methods that must be public
|
|
|
|
*
|
|
|
|
* Notice that this is a list of /methods/, not members, because this check
|
|
|
|
* is performed only for methods. This is for performance reasons. We do not
|
|
|
|
* have a situation where we will want to check for properties as well.
|
2011-03-28 18:21:06 -04:00
|
|
|
*
|
|
|
|
* @type {Object.<string,boolean}
|
|
|
|
*/
|
2011-03-28 19:52:16 -04:00
|
|
|
public_methods = {
|
|
|
|
'__construct': true,
|
|
|
|
'toString': true,
|
|
|
|
'__toString': true,
|
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IE contains a nasty enumeration "bug" (poor implementation) that makes
|
|
|
|
* toString unenumerable. This means that, if you do obj.toString = foo,
|
|
|
|
* toString will NOT show up in `for` or hasOwnProperty(). This is a problem.
|
|
|
|
*
|
|
|
|
* This test will determine if this poor implementation exists.
|
|
|
|
*/
|
|
|
|
var enum_bug = (
|
|
|
|
Object.prototype.propertyIsEnumerable.call(
|
|
|
|
{ toString: function() {} },
|
|
|
|
'toString'
|
|
|
|
) === false
|
|
|
|
)
|
|
|
|
? true
|
|
|
|
: false
|
2011-03-27 01:57:17 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
/**
|
|
|
|
* Default class implementation
|
|
|
|
*
|
|
|
|
* @return undefined
|
|
|
|
*/
|
|
|
|
exports.ClassBase = function Class() {};
|
|
|
|
|
2011-04-13 22:38:05 -04:00
|
|
|
/**
|
|
|
|
* Default static property method
|
|
|
|
*
|
|
|
|
* This simply returns undefined, signifying that the property was not found.
|
|
|
|
*
|
|
|
|
* @param {string} prop requested property
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-04-13 23:45:26 -04:00
|
|
|
exports.ClassBase.$ = function( prop, val )
|
2011-04-13 22:38:05 -04:00
|
|
|
{
|
2011-04-13 23:45:26 -04:00
|
|
|
if ( val !== undefined )
|
|
|
|
{
|
|
|
|
throw ReferenceError(
|
|
|
|
"Cannot set value of undeclared static property '" + prop + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-04-13 22:38:05 -04:00
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-03-28 18:58:23 -04:00
|
|
|
/**
|
|
|
|
* Returns a hash of the reserved members
|
|
|
|
*
|
|
|
|
* The returned object is a copy of the original. It cannot be used to modify
|
|
|
|
* the internal list of reserved members.
|
|
|
|
*
|
2011-03-28 19:17:38 -04:00
|
|
|
* @return {Object.<string,boolean>} reserved members
|
2011-03-28 18:58:23 -04:00
|
|
|
*/
|
|
|
|
exports.getReservedMembers = function()
|
|
|
|
{
|
|
|
|
// return a copy of the reserved members
|
2011-03-28 19:01:58 -04:00
|
|
|
return util.clone( reserved_members, true );
|
2011-03-28 18:58:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-28 19:17:38 -04:00
|
|
|
/**
|
|
|
|
* Returns a hash of the forced-public methods
|
|
|
|
*
|
|
|
|
* The returned object is a copy of the original. It cannot be used to modify
|
|
|
|
* the internal list of reserved members.
|
|
|
|
*
|
|
|
|
* @return {Object.<string,boolean>} forced-public methods
|
|
|
|
*/
|
|
|
|
exports.getForcedPublicMethods = function()
|
|
|
|
{
|
|
|
|
return util.clone( public_methods, true );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
/**
|
|
|
|
* Mimics class inheritance
|
|
|
|
*
|
|
|
|
* This method will mimic inheritance by setting up the prototype with the
|
|
|
|
* provided base class (or, by default, Class) and copying the additional
|
|
|
|
* properties atop of it.
|
|
|
|
*
|
|
|
|
* The class to inherit from (the first argument) is optional. If omitted, the
|
|
|
|
* first argument will be considered to be the properties list.
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
|
|
|
exports.build = function extend()
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
// ensure we'll be permitted to instantiate abstract classes for the base
|
|
|
|
extending = true;
|
|
|
|
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
|
|
|
props = args.pop() || {},
|
|
|
|
base = args.pop() || exports.ClassBase,
|
|
|
|
prototype = new base(),
|
|
|
|
cname = '',
|
|
|
|
|
2011-04-04 23:07:01 -04:00
|
|
|
prop_init = member_builder.initMembers(),
|
|
|
|
members = member_builder.initMembers( prototype ),
|
2011-04-10 22:32:46 -04:00
|
|
|
static_members = {
|
|
|
|
methods: member_builder.initMembers(),
|
|
|
|
props: member_builder.initMembers(),
|
|
|
|
}
|
2011-03-27 23:16:19 -04:00
|
|
|
|
|
|
|
abstract_methods =
|
|
|
|
util.clone( exports.getMeta( base ).abstractMethods )
|
|
|
|
|| { __length: 0 }
|
|
|
|
;
|
|
|
|
|
2011-05-22 21:54:41 -04:00
|
|
|
// must extend from constructor or class
|
|
|
|
if ( typeof base !== 'function' )
|
|
|
|
{
|
|
|
|
throw TypeError( "Must extend from class or constructor");
|
|
|
|
}
|
|
|
|
|
2011-05-22 11:11:18 -04:00
|
|
|
// prevent extending final classes
|
|
|
|
if ( base.___$$final$$ === true )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Cannot extend final class " +
|
|
|
|
( base.___$$meta$$.name || '(anonymous)' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// grab the name, if one was provided
|
|
|
|
if ( cname = props.__name )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
// we no longer need it
|
|
|
|
delete props.__name;
|
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// IE has problems with toString()
|
|
|
|
if ( enum_bug )
|
|
|
|
{
|
|
|
|
if ( props.toString !== Object.prototype.toString )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
props.__toString = props.toString;
|
2011-03-27 23:04:40 -04:00
|
|
|
}
|
2011-03-27 23:16:19 -04:00
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// increment class identifier
|
|
|
|
class_id++;
|
|
|
|
|
|
|
|
// build the various class components (xxx: this is temporary; needs
|
|
|
|
// refactoring)
|
|
|
|
buildMembers( props,
|
|
|
|
class_id,
|
|
|
|
base,
|
|
|
|
prop_init,
|
|
|
|
abstract_methods,
|
2011-04-04 23:07:01 -04:00
|
|
|
members,
|
2011-05-30 23:03:08 -04:00
|
|
|
static_members,
|
|
|
|
function( inst )
|
|
|
|
{
|
2011-05-31 22:29:07 -04:00
|
|
|
return new_class.___$$svis$$;
|
2011-05-30 23:03:08 -04:00
|
|
|
}
|
2011-03-27 23:16:19 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// reference to the parent prototype (for more experienced users)
|
|
|
|
prototype.___$$parent$$ = base.prototype;
|
|
|
|
|
|
|
|
// set up the new class
|
|
|
|
var new_class = createCtor( cname, abstract_methods, members );
|
|
|
|
|
2011-04-05 23:11:25 -04:00
|
|
|
// closure to hold static initialization to be used later by subtypes
|
2011-05-11 17:56:48 -04:00
|
|
|
initStaticVisibilityObj( new_class, static_members );
|
2011-04-10 22:32:46 -04:00
|
|
|
var staticInit = function( ctor, inheriting )
|
2011-04-05 23:11:25 -04:00
|
|
|
{
|
2011-04-10 22:32:46 -04:00
|
|
|
attachStatic( ctor, static_members, base, inheriting );
|
2011-04-05 23:11:25 -04:00
|
|
|
}
|
2011-04-10 22:32:46 -04:00
|
|
|
staticInit( new_class, false );
|
2011-04-05 23:11:25 -04:00
|
|
|
|
2011-05-10 19:54:23 -04:00
|
|
|
attachPropInit( prototype, prop_init, members, new_class, class_id );
|
2011-03-27 23:16:19 -04:00
|
|
|
|
|
|
|
new_class.prototype = prototype;
|
|
|
|
new_class.constructor = new_class;
|
|
|
|
new_class.___$$props$$ = prop_init;
|
|
|
|
new_class.___$$methods$$ = members;
|
2011-04-05 23:11:25 -04:00
|
|
|
new_class.___$$sinit$$ = staticInit;
|
2011-03-27 23:16:19 -04:00
|
|
|
|
2011-05-22 11:11:18 -04:00
|
|
|
attachFlags( new_class, props );
|
|
|
|
|
2011-05-22 13:57:56 -04:00
|
|
|
validateAbstract( new_class, cname, abstract_methods );
|
|
|
|
|
2011-04-05 22:07:13 -04:00
|
|
|
// We reduce the overall cost of this definition by defining it on the
|
|
|
|
// prototype rather than during instantiation. While this does increase the
|
|
|
|
// amount of time it takes to access the property through the prototype
|
|
|
|
// chain, it takes much more time to define the property in this manner.
|
|
|
|
// Therefore, we can save a substantial amount of time by defining it on the
|
|
|
|
// prototype rather than on each new instance via __initProps().
|
|
|
|
util.defineSecureProp( prototype, '__self', new_class );
|
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// create internal metadata for the new class
|
|
|
|
var meta = createMeta( new_class, base );
|
|
|
|
meta.abstractMethods = abstract_methods;
|
|
|
|
meta.name = cname;
|
|
|
|
|
2011-03-29 00:08:49 -04:00
|
|
|
attachAbstract( new_class, abstract_methods );
|
2011-03-29 00:15:16 -04:00
|
|
|
attachId( new_class, class_id );
|
2011-03-29 00:08:49 -04:00
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// we're done with the extension process
|
|
|
|
extending = false;
|
|
|
|
|
2011-03-29 00:15:16 -04:00
|
|
|
return new_class;
|
2011-03-27 23:16:19 -04:00
|
|
|
};
|
2011-03-27 23:04:40 -04:00
|
|
|
|
|
|
|
|
2011-05-22 13:57:56 -04:00
|
|
|
/**
|
|
|
|
* Validates abstract class requirements
|
|
|
|
*
|
|
|
|
* @param {function()} ctor class
|
|
|
|
* @param {string} cname class name
|
|
|
|
* @param {Object} abstract_methods
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function validateAbstract( ctor, cname, abstract_methods )
|
|
|
|
{
|
|
|
|
if ( ctor.___$$abstract$$ )
|
|
|
|
{
|
|
|
|
if ( abstract_methods.__length === 0 )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Class " + ( cname || "(anonymous)" ) + " was declared as " +
|
|
|
|
"abstract, but contains no abstract members"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( abstract_methods.__length > 0 )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Class " + ( cname || "(anonymous)" ) + " contains abstract " +
|
|
|
|
"members and must therefore be declared abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
/**
|
|
|
|
* Creates the constructor for a new class
|
|
|
|
*
|
|
|
|
* This constructor will call the __constructor method for concrete classes
|
|
|
|
* and throw an exception for abstract classes (to prevent instantiation).
|
|
|
|
*
|
|
|
|
* @param {string} cname class name (may be empty)
|
|
|
|
* @param {Array.<string>} abstract_methods list of abstract methods
|
|
|
|
* @param {Object} members class members
|
|
|
|
*
|
|
|
|
* @return {Function} constructor
|
|
|
|
*/
|
|
|
|
function createCtor( cname, abstract_methods, members )
|
|
|
|
{
|
|
|
|
// concrete class
|
|
|
|
if ( abstract_methods.__length === 0 )
|
|
|
|
{
|
|
|
|
return createConcreteCtor( cname, members );
|
|
|
|
}
|
|
|
|
// abstract class
|
|
|
|
else
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
return createAbstractCtor( cname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the constructor for a new concrete class
|
|
|
|
*
|
|
|
|
* This constructor will call the __constructor method of the class, if
|
|
|
|
* available.
|
|
|
|
*
|
|
|
|
* @param {string} cname class name (may be empty)
|
|
|
|
* @param {Object} members class members
|
|
|
|
*
|
|
|
|
* @return {function()} constructor
|
|
|
|
*/
|
|
|
|
function createConcreteCtor( cname, members )
|
|
|
|
{
|
|
|
|
var args = null;
|
|
|
|
|
2011-03-29 22:04:54 -04:00
|
|
|
// constructor function to be returned (the name is set to ClassInstance
|
|
|
|
// because some debuggers (e.g. v8) will show the name of this function for
|
|
|
|
// constructor instances rather than invoking the toString() method)
|
|
|
|
var ClassInstance = function ClassInstance()
|
2011-03-27 23:16:19 -04:00
|
|
|
{
|
2011-03-29 22:04:54 -04:00
|
|
|
if ( !( this instanceof ClassInstance ) )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
// store arguments to be passed to constructor and
|
|
|
|
// instantiate new object
|
|
|
|
args = arguments;
|
2011-03-29 22:04:54 -04:00
|
|
|
return new ClassInstance();
|
2011-03-27 23:16:19 -04:00
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-05-10 23:30:32 -04:00
|
|
|
initInstance( this );
|
2011-03-27 23:16:19 -04:00
|
|
|
this.__initProps();
|
|
|
|
|
2011-05-10 23:21:12 -04:00
|
|
|
// If we're extending, we don't actually want to invoke any class
|
|
|
|
// construction logic. The above is sufficient to use this class in a
|
|
|
|
// prototype, so stop here.
|
|
|
|
if ( extending )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-10 23:34:50 -04:00
|
|
|
// generate and store unique instance id
|
|
|
|
attachInstanceId( this, ++instance_id );
|
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
// call the constructor, if one was provided
|
|
|
|
if ( this.__construct instanceof Function )
|
|
|
|
{
|
|
|
|
// note that since 'this' refers to the new class (even
|
|
|
|
// subtypes), and since we're using apply with 'this', the
|
|
|
|
// constructor will be applied to subtypes without a problem
|
|
|
|
this.__construct.apply( this, ( args || arguments ) );
|
|
|
|
args = null;
|
2011-03-27 23:04:40 -04:00
|
|
|
}
|
2011-03-27 23:16:19 -04:00
|
|
|
|
|
|
|
// attach any instance properties/methods (done after
|
|
|
|
// constructor to ensure they are not overridden)
|
|
|
|
attachInstanceOf( this );
|
|
|
|
|
|
|
|
// Provide a more intuitive string representation of the class
|
|
|
|
// instance. If a toString() method was already supplied for us,
|
|
|
|
// use that one instead.
|
|
|
|
if ( !( Object.prototype.hasOwnProperty.call(
|
|
|
|
members[ 'public' ], 'toString'
|
|
|
|
) ) )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
2011-03-27 23:16:19 -04:00
|
|
|
// use __toString if available (see enum_bug), otherwise use
|
|
|
|
// our own defaults
|
|
|
|
this.toString = members[ 'public' ].__toString
|
|
|
|
|| ( ( cname )
|
|
|
|
? function()
|
|
|
|
{
|
|
|
|
return '#<' + cname + '>';
|
|
|
|
}
|
|
|
|
: function()
|
|
|
|
{
|
|
|
|
return '#<anonymous>';
|
|
|
|
}
|
|
|
|
)
|
2011-03-27 23:04:40 -04:00
|
|
|
;
|
2011-03-27 23:16:19 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// provide a more intuitive string representation
|
2011-03-29 22:04:54 -04:00
|
|
|
ClassInstance.toString = ( cname )
|
2011-03-27 23:16:19 -04:00
|
|
|
? function() { return cname; }
|
|
|
|
: function() { return '(Class)'; }
|
|
|
|
;
|
|
|
|
|
2011-03-29 22:04:54 -04:00
|
|
|
return ClassInstance;
|
2011-03-27 23:16:19 -04:00
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-03-27 23:16:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the constructor for a new abstract class
|
|
|
|
*
|
|
|
|
* Calling this constructor will cause an exception to be thrown, as abstract
|
|
|
|
* classes cannot be instantiated.
|
|
|
|
*
|
|
|
|
* @param {string} cname class name (may be empty)
|
|
|
|
*
|
|
|
|
* @return {function()} constructor
|
|
|
|
*/
|
|
|
|
function createAbstractCtor( cname )
|
|
|
|
{
|
|
|
|
var __abstract_self = function()
|
|
|
|
{
|
|
|
|
if ( !extending )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Abstract class " + ( cname || '(anonymous)' ) +
|
|
|
|
" cannot be instantiated"
|
|
|
|
);
|
2011-03-27 23:04:40 -04:00
|
|
|
}
|
2011-03-27 23:16:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
__abstract_self.toString = ( cname )
|
|
|
|
? function()
|
|
|
|
{
|
|
|
|
return cname;
|
|
|
|
}
|
|
|
|
: function()
|
|
|
|
{
|
|
|
|
return '(AbstractClass)';
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
return __abstract_self;
|
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
function buildMembers(
|
2011-04-04 23:07:01 -04:00
|
|
|
props, class_id, base, prop_init, abstract_methods, members,
|
2011-05-30 23:03:08 -04:00
|
|
|
static_members, staticInstLookup
|
2011-03-27 01:57:17 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
var hasOwn = Array.prototype.hasOwnProperty,
|
2011-04-10 22:32:46 -04:00
|
|
|
defs = {},
|
|
|
|
|
|
|
|
smethods = static_members.methods,
|
|
|
|
sprops = static_members.props
|
|
|
|
;
|
2011-03-27 01:57:17 -04:00
|
|
|
|
|
|
|
util.propParse( props, {
|
|
|
|
each: function( name, value, keywords )
|
|
|
|
{
|
|
|
|
// disallow use of our internal __initProps() method
|
2011-03-28 19:01:58 -04:00
|
|
|
if ( reserved_members[ name ] === true )
|
2011-03-27 01:57:17 -04:00
|
|
|
{
|
2011-03-28 18:18:40 -04:00
|
|
|
throw Error(
|
2011-03-27 01:57:17 -04:00
|
|
|
( ( cname ) ? cname + '::' : '' ) +
|
2011-03-28 18:18:40 -04:00
|
|
|
( name + " is reserved" )
|
2011-03-27 01:57:17 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a member was defined multiple times in the same class
|
|
|
|
// declaration, throw an error
|
|
|
|
if ( hasOwn.call( defs, name ) )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Cannot redefine method '" + name + "' in same declaration"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep track of the definitions (only during class declaration)
|
|
|
|
// to catch duplicates
|
|
|
|
defs[ name ] = 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
property: function( name, value, keywords )
|
|
|
|
{
|
2011-05-19 19:07:31 -04:00
|
|
|
var dest = ( keywordStatic( keywords ) ) ? sprops : prop_init;
|
2011-04-04 23:07:01 -04:00
|
|
|
|
2011-03-27 01:57:17 -04:00
|
|
|
// build a new property, passing in the other members to compare
|
|
|
|
// against for preventing nonsensical overrides
|
|
|
|
member_builder.buildProp(
|
2011-04-04 23:07:01 -04:00
|
|
|
dest, null, name, value, keywords, base
|
2011-03-27 01:57:17 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
getter: function( name, value, keywords )
|
|
|
|
{
|
2011-05-19 19:07:31 -04:00
|
|
|
var dest = ( keywordStatic( keywords ) ) ? smethods : members;
|
2011-04-05 00:08:39 -04:00
|
|
|
|
2011-03-27 01:57:17 -04:00
|
|
|
member_builder.buildGetter(
|
2011-06-12 00:36:52 -04:00
|
|
|
dest, null, name, value, keywords, base
|
2011-03-27 01:57:17 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
setter: function( name, value, keywords )
|
|
|
|
{
|
2011-05-19 19:07:31 -04:00
|
|
|
var dest = ( keywordStatic( keywords ) ) ? smethods : members;
|
2011-04-05 00:08:39 -04:00
|
|
|
|
2011-03-27 01:57:17 -04:00
|
|
|
member_builder.buildSetter(
|
2011-06-12 00:36:52 -04:00
|
|
|
dest, null, name, value, keywords, base
|
2011-03-27 01:57:17 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
method: function( name, func, is_abstract, keywords )
|
|
|
|
{
|
2011-05-30 23:03:08 -04:00
|
|
|
var is_static = keywordStatic( keywords ),
|
|
|
|
dest = ( is_static ) ? smethods : members,
|
|
|
|
instLookup = ( is_static )
|
|
|
|
? staticInstLookup
|
|
|
|
: getMethodInstance
|
|
|
|
;
|
2011-04-04 23:07:01 -04:00
|
|
|
|
2011-03-27 01:57:17 -04:00
|
|
|
// constructor check
|
2011-03-28 19:17:38 -04:00
|
|
|
if ( public_methods[ name ] === true )
|
2011-03-27 01:57:17 -04:00
|
|
|
{
|
|
|
|
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
|
|
|
|
{
|
2011-03-28 18:21:06 -04:00
|
|
|
throw TypeError(
|
|
|
|
name + " must be public"
|
|
|
|
);
|
2011-03-27 01:57:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
member_builder.buildMethod(
|
2011-05-30 23:03:08 -04:00
|
|
|
dest, null, name, func, keywords, instLookup,
|
2011-03-27 01:57:17 -04:00
|
|
|
class_id, base
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( is_abstract )
|
|
|
|
{
|
|
|
|
abstract_methods[ name ] = true;
|
|
|
|
abstract_methods.__length++;
|
|
|
|
}
|
|
|
|
else if ( ( hasOwn.call( abstract_methods, name ) )
|
|
|
|
&& ( is_abstract === false )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// if this was a concrete method, then it should no longer
|
|
|
|
// be marked as abstract
|
|
|
|
delete abstract_methods[ name ];
|
|
|
|
abstract_methods.__length--;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
} );
|
2011-03-27 23:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 19:07:31 -04:00
|
|
|
/**
|
|
|
|
* Determines if the given keywords should result in a static member
|
|
|
|
*
|
|
|
|
* A member will be considered static if the static or const keywords are given.
|
|
|
|
*
|
|
|
|
* @param {Object} keywords keywords to scan
|
|
|
|
*
|
|
|
|
* @return {bool} true if to be static, otherwise false
|
|
|
|
*/
|
|
|
|
function keywordStatic( keywords )
|
|
|
|
{
|
|
|
|
return ( keywords[ 'static' ] || keywords[ 'const' ] )
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
/**
|
|
|
|
* Attaches __initProps() method to the class prototype
|
|
|
|
*
|
|
|
|
* The __initProps() method will initialize class properties for that instance,
|
|
|
|
* ensuring that their data is not shared with other instances (this is not a
|
|
|
|
* problem with primitive data types).
|
|
|
|
*
|
|
|
|
* The method will also initialize any parent properties (recursive) to ensure
|
|
|
|
* that subtypes do not have a referencing issue, and subtype properties take
|
|
|
|
* precedence over those of the parent.
|
|
|
|
*
|
|
|
|
* @param {Object} prototype prototype to attach method to
|
|
|
|
* @param {Object} properties properties to initialize
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
2011-05-10 19:54:23 -04:00
|
|
|
* @param {function() ctor class
|
|
|
|
* @param {number} cid class id
|
2011-04-05 22:07:13 -04:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-05-10 19:54:23 -04:00
|
|
|
function attachPropInit( prototype, properties, members, ctor, cid )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
|
|
|
util.defineSecureProp( prototype, '__initProps', function( inherit )
|
|
|
|
{
|
2011-04-02 10:58:26 -04:00
|
|
|
// defaults to false
|
2011-03-27 23:04:40 -04:00
|
|
|
inherit = !!inherit;
|
|
|
|
|
|
|
|
var iid = this.__iid;
|
|
|
|
|
|
|
|
// first initialize the parent's properties, so that ours will overwrite
|
|
|
|
// them
|
|
|
|
var parent_init = prototype.___$$parent$$.__initProps;
|
|
|
|
if ( parent_init instanceof Function )
|
|
|
|
{
|
|
|
|
// call the parent prop_init, letting it know that it's been
|
|
|
|
// inherited so that it does not initialize private members or
|
|
|
|
// perform other unnecessary tasks
|
|
|
|
parent_init.call( this, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
// this will return our property proxy, if supported by our environment,
|
|
|
|
// otherwise just a normal object with everything merged in
|
|
|
|
var inst_props = propobj.createPropProxy(
|
|
|
|
this, this.___$$vis$$, properties[ 'public' ]
|
|
|
|
);
|
|
|
|
|
2011-04-02 10:58:26 -04:00
|
|
|
// Copies all public and protected members into inst_props and stores
|
|
|
|
// private in a separate object, which adds inst_props to its prototype
|
|
|
|
// chain and is returned. This is stored in a property referenced by the
|
|
|
|
// class id, so that the private members can be swapped on each method
|
|
|
|
// request, depending on calling context.
|
2011-03-30 00:55:27 -04:00
|
|
|
var vis = this.___$$vis$$[ cid ] = propobj.setup(
|
2011-03-27 23:04:40 -04:00
|
|
|
inst_props, properties, members
|
|
|
|
);
|
2011-03-30 00:55:27 -04:00
|
|
|
|
2011-05-09 23:09:32 -04:00
|
|
|
// give internal methods access to protected/private static members
|
2011-05-10 19:54:23 -04:00
|
|
|
vis.__self = ctor.___$$svis$$;
|
2011-05-09 23:09:32 -04:00
|
|
|
|
2011-03-30 00:55:27 -04:00
|
|
|
// provide a means to access the actual instance (rather than the
|
|
|
|
// property/visibility object) internally (this will translate to
|
2011-04-03 11:57:15 -04:00
|
|
|
// this.__inst from within a method), but only if we're on our final
|
2011-03-30 23:31:46 -04:00
|
|
|
// object (not a parent)
|
2011-04-05 22:09:02 -04:00
|
|
|
if ( !inherit )
|
2011-03-30 23:31:46 -04:00
|
|
|
{
|
2011-04-03 11:57:15 -04:00
|
|
|
util.defineSecureProp( vis, '__inst', this );
|
2011-03-30 23:31:46 -04:00
|
|
|
}
|
2011-03-27 23:04:40 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-09 23:09:32 -04:00
|
|
|
/**
|
|
|
|
* Creates and populates the static visibility object
|
|
|
|
*
|
2011-05-10 19:54:23 -04:00
|
|
|
* @param {function()} class
|
2011-05-09 23:09:32 -04:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-05-30 20:10:52 -04:00
|
|
|
function initStaticVisibilityObj( ctor )
|
2011-05-09 23:09:32 -04:00
|
|
|
{
|
|
|
|
// the object will simply be another layer in the prototype chain to
|
|
|
|
// prevent protected/private members from being mixed in with the public
|
|
|
|
var sobj = function() {};
|
2011-05-10 19:54:23 -04:00
|
|
|
sobj.prototype = ctor;
|
2011-05-09 23:09:32 -04:00
|
|
|
|
|
|
|
var sobji = new sobj();
|
|
|
|
|
|
|
|
// override __self on the instance's visibility object, giving internal
|
|
|
|
// methods access to the restricted static methods
|
2011-05-10 19:54:23 -04:00
|
|
|
ctor.___$$svis$$ = sobji;
|
2011-05-12 00:05:50 -04:00
|
|
|
|
|
|
|
// Override the class-level accessor method to allow the system to know we
|
|
|
|
// are within a method. An internal flag is necessary, rather than using an
|
|
|
|
// argument or binding, because those two options are exploitable. An
|
|
|
|
// internal flag cannot be modified by conventional means.
|
|
|
|
sobji.$ = function()
|
|
|
|
{
|
|
|
|
sprop_internal = true;
|
|
|
|
var val = ctor.$.apply( ctor, arguments );
|
|
|
|
sprop_internal = false;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
};
|
2011-05-09 23:09:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-04 23:07:01 -04:00
|
|
|
/**
|
|
|
|
* Attaches static members to a constructor (class)
|
|
|
|
*
|
2011-04-10 22:32:46 -04:00
|
|
|
* Static methods will be assigned to the constructor itself. Properties, on the
|
|
|
|
* other hand, will be assigned to ctor.$. The reason for this is because JS
|
|
|
|
* engines pre-ES5 support no means of sharing references to primitives. Static
|
|
|
|
* properties of subtypes should share references to the static properties of
|
|
|
|
* their parents.
|
|
|
|
*
|
|
|
|
* @param {function()} ctor class
|
|
|
|
* @param {Object} members static members
|
|
|
|
* @param {function()} base base class inheriting from
|
|
|
|
* @param {boolean} inheriting true if inheriting static members,
|
|
|
|
* otherwise false (setting own static
|
|
|
|
* members)
|
2011-04-04 23:07:01 -04:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-04-10 22:32:46 -04:00
|
|
|
function attachStatic( ctor, members, base, inheriting )
|
2011-04-04 23:07:01 -04:00
|
|
|
{
|
2011-04-10 22:32:46 -04:00
|
|
|
var methods = members.methods,
|
|
|
|
props = members.props;
|
|
|
|
|
2011-05-11 18:36:49 -04:00
|
|
|
// "Inherit" the parent's static methods by running the parent's static
|
|
|
|
// initialization method. It is important that we do this before anything,
|
|
|
|
// because this will recursively inherit all members in order, permitting
|
|
|
|
// overrides.
|
2011-04-05 23:11:25 -04:00
|
|
|
var baseinit = base.___$$sinit$$;
|
|
|
|
if ( baseinit )
|
|
|
|
{
|
2011-04-10 22:32:46 -04:00
|
|
|
baseinit( ctor, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize static property if not yet defined
|
|
|
|
if ( !inheriting )
|
|
|
|
{
|
2011-04-13 22:38:05 -04:00
|
|
|
ctor.___$$sprops$$ = props;
|
2011-04-10 22:32:46 -04:00
|
|
|
|
2011-04-13 23:06:24 -04:00
|
|
|
// provide a method to access static properties
|
2011-05-11 20:53:43 -04:00
|
|
|
util.defineSecureProp( ctor, '$', function( prop, val )
|
2011-04-13 22:38:05 -04:00
|
|
|
{
|
2011-04-13 23:06:24 -04:00
|
|
|
// we use hasOwnProperty to ensure that undefined values will not
|
|
|
|
// cause us to continue checking the parent, thereby potentially
|
|
|
|
// failing to set perfectly legal values
|
2011-05-12 00:05:50 -04:00
|
|
|
var has = Object.prototype.hasOwnProperty,
|
|
|
|
found = false,
|
2011-04-13 23:35:54 -04:00
|
|
|
|
|
|
|
// Determine if we were invoked in the context of a class. If
|
|
|
|
// so, use that. Otherwise, use ourself.
|
2011-05-13 00:55:09 -04:00
|
|
|
context = ( this.___$$sprops$$ ) ? this : ctor,
|
|
|
|
|
|
|
|
// We are in a subtype if the context does not match the
|
|
|
|
// constructor. This works because, when invoked for the first
|
|
|
|
// time, this method is not bound to the constructor. In such a
|
|
|
|
// case, we default the context to the constructor and pass that
|
|
|
|
// down the line to each recursive call. Therefore, recursive
|
|
|
|
// calls to subtypes will have a context mismatch.
|
|
|
|
in_subtype = ( context !== ctor )
|
2011-04-13 23:35:54 -04:00
|
|
|
;
|
2011-04-13 23:06:24 -04:00
|
|
|
|
2011-05-12 00:05:50 -04:00
|
|
|
// Attempt to locate the property. First, we check public. If not
|
|
|
|
// available and we are internal (within a method), we can move on
|
|
|
|
// to check other levels of visibility. `found` will contain the
|
|
|
|
// visibility level the property was found in, or false.
|
|
|
|
found = has.call( props[ 'public' ], prop ) && 'public';
|
|
|
|
if ( !found && sprop_internal )
|
|
|
|
{
|
2011-05-13 00:55:09 -04:00
|
|
|
// Check for protected/private. We only check for private
|
|
|
|
// properties if we are not currently checking the properties of
|
|
|
|
// a subtype. This works because the context is passed to each
|
|
|
|
// recursive call.
|
|
|
|
found = has.call( props[ 'protected' ], prop ) && 'protected'
|
|
|
|
|| !in_subtype
|
|
|
|
&& has.call( props[ 'private' ], prop ) && 'private'
|
|
|
|
;
|
2011-05-12 00:05:50 -04:00
|
|
|
}
|
|
|
|
|
2011-04-13 23:06:24 -04:00
|
|
|
// if we don't own the property, let the parent(s) handle it
|
2011-05-12 00:05:50 -04:00
|
|
|
if ( found === false )
|
2011-04-13 23:06:24 -04:00
|
|
|
{
|
2011-04-13 23:35:54 -04:00
|
|
|
return base.$.apply( context, arguments );
|
2011-04-13 23:06:24 -04:00
|
|
|
}
|
|
|
|
|
2011-05-19 19:48:47 -04:00
|
|
|
var prop_item = props[ found ][ prop ];
|
|
|
|
|
2011-04-13 23:06:24 -04:00
|
|
|
// if a value was provided, this method should be treated as a
|
2011-04-13 23:52:25 -04:00
|
|
|
// setter rather than a getter (we *must* test using
|
|
|
|
// arguments.length to ensure that setting to undefined works)
|
|
|
|
if ( arguments.length > 1 )
|
2011-04-13 23:06:24 -04:00
|
|
|
{
|
2011-05-19 19:48:47 -04:00
|
|
|
// if const, disallow modification
|
|
|
|
if ( prop_item[ 1 ][ 'const' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot modify constant property '" + prop + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
prop_item[ 0 ] = val;
|
2011-04-13 23:35:54 -04:00
|
|
|
return context;
|
2011-04-13 23:06:24 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// return the value
|
2011-05-19 19:48:47 -04:00
|
|
|
return prop_item[ 0 ];
|
2011-04-13 23:06:24 -04:00
|
|
|
}
|
2011-05-11 20:53:43 -04:00
|
|
|
} );
|
2011-04-05 23:11:25 -04:00
|
|
|
}
|
|
|
|
|
2011-04-14 20:28:42 -04:00
|
|
|
// copy over public static methods
|
2011-04-10 22:32:46 -04:00
|
|
|
util.copyTo( ctor, methods[ 'public' ], true );
|
2011-05-11 17:56:48 -04:00
|
|
|
util.copyTo( ctor.___$$svis$$, methods[ 'protected' ], true );
|
2011-05-11 20:10:10 -04:00
|
|
|
|
|
|
|
// private methods should not be inherited by subtypes
|
|
|
|
if ( !inheriting )
|
|
|
|
{
|
|
|
|
util.copyTo( ctor.___$$svis$$, methods[ 'private' ], true );
|
|
|
|
}
|
2011-04-04 23:07:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
/**
|
|
|
|
* Initializes class metadata for the given class
|
|
|
|
*
|
|
|
|
* @param {Class} func class to initialize metadata for
|
|
|
|
* @param {Class} cparent class parent
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function createMeta( func, cparent )
|
|
|
|
{
|
|
|
|
var id = func.__cid,
|
|
|
|
parent_meta = ( ( cparent.__cid )
|
|
|
|
? exports.getMeta( cparent )
|
|
|
|
: undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
// copy the parent prototype's metadata if it exists (inherit metadata)
|
|
|
|
if ( parent_meta )
|
|
|
|
{
|
|
|
|
func.___$$meta$$ = util.clone( parent_meta, true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create empty
|
|
|
|
func.___$$meta$$ = {
|
|
|
|
implemented: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the metadata in the prototype as well (inconsiderable overhead;
|
|
|
|
// it's just a reference)
|
|
|
|
func.prototype.___$$meta$$ = func.___$$meta$$;
|
|
|
|
|
|
|
|
return func.___$$meta$$;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns reference to metadata for the requested class
|
|
|
|
*
|
|
|
|
* Since a reference is returned (rather than a copy), the returned object can
|
|
|
|
* be modified to alter the metadata.
|
|
|
|
*
|
|
|
|
* @param {Class} cls class from which to retrieve metadata
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
exports.getMeta = function( cls )
|
|
|
|
{
|
|
|
|
return cls.___$$meta$$ || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches an instance identifier to a class instance
|
|
|
|
*
|
|
|
|
* @param {Object} instance class instance
|
|
|
|
* @param {number} iid instance id
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachInstanceId( instance, iid )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( instance, '__iid', iid );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes class instance
|
|
|
|
*
|
2011-05-10 23:30:32 -04:00
|
|
|
* This process will create the instance visibility object that will contain
|
|
|
|
* private and protected members. The class instance is part of the prototype
|
|
|
|
* chain. This will be passed to all methods when invoked, permitting them to
|
|
|
|
* access the private and protected members while keeping them encapsulated.
|
2011-03-27 23:04:40 -04:00
|
|
|
*
|
|
|
|
* For each instance, there is always a base. The base will contain a proxy to
|
|
|
|
* the public members on the instance itself. The base will also contain all
|
|
|
|
* protected members.
|
|
|
|
*
|
|
|
|
* Atop the base object is a private member object, with the base as its
|
|
|
|
* prototype. There exists a private member object for the instance itself and
|
|
|
|
* one for each supertype. This is stored by the class id (cid) as the key. This
|
|
|
|
* permits the private member object associated with the class of the method
|
|
|
|
* call to be bound to that method. For example, if a parent method is called,
|
|
|
|
* that call must be invoked in the context of the parent, so the private
|
|
|
|
* members of the parent must be made available.
|
|
|
|
*
|
|
|
|
* The resulting structure looks something like this:
|
|
|
|
* class_instance = { iid: { cid: {} } }
|
|
|
|
*
|
|
|
|
* @param {Object} instance instance to initialize
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-05-10 23:30:32 -04:00
|
|
|
function initInstance( instance )
|
2011-03-27 23:04:40 -04:00
|
|
|
{
|
|
|
|
var prot = function() {};
|
|
|
|
prot.prototype = instance;
|
|
|
|
|
|
|
|
// add the visibility objects to the data object for this class instance
|
|
|
|
instance.___$$vis$$ = new prot();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches partially applied isInstanceOf() method to class instance
|
|
|
|
*
|
|
|
|
* @param {Object} instance class instance to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachInstanceOf( instance )
|
|
|
|
{
|
|
|
|
var method = function( type )
|
|
|
|
{
|
|
|
|
return module.exports.isInstanceOf( type, instance );
|
|
|
|
};
|
|
|
|
|
|
|
|
util.defineSecureProp( instance, 'isInstanceOf', method );
|
|
|
|
util.defineSecureProp( instance, 'isA', method );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance object associated with the given method
|
|
|
|
*
|
|
|
|
* The instance object contains the protected members. This object can be passed
|
|
|
|
* as the context when calling a method in order to give that method access to
|
|
|
|
* those members.
|
|
|
|
*
|
|
|
|
* One level above the instance object on the prototype chain is the object
|
|
|
|
* containing the private members. This is swappable, depending on the class id
|
|
|
|
* associated with the provided method call. This allows methods that were not
|
|
|
|
* overridden by the subtype to continue to use the private members of the
|
|
|
|
* supertype.
|
|
|
|
*
|
|
|
|
* @param {function()} inst instance that the method is being called from
|
|
|
|
* @param {number} cid class id
|
|
|
|
*
|
|
|
|
* @return {Object,null} instance object if found, otherwise null
|
|
|
|
*/
|
|
|
|
function getMethodInstance( inst, cid )
|
|
|
|
{
|
|
|
|
var iid = inst.__iid,
|
|
|
|
data = inst.___$$vis$$;
|
|
|
|
|
|
|
|
return ( iid && data )
|
|
|
|
? data[ cid ]
|
2011-05-30 23:03:08 -04:00
|
|
|
: null
|
2011-03-27 23:04:40 -04:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the class is an instance of the given type
|
|
|
|
*
|
|
|
|
* The given type can be a class, interface, trait or any other type of object.
|
|
|
|
* It may be used in place of the 'instanceof' operator and contains additional
|
|
|
|
* enhancements that the operator is unable to provide due to prototypal
|
|
|
|
* restrictions.
|
|
|
|
*
|
|
|
|
* @param {Object} type expected type
|
|
|
|
* @param {Object} instance instance to check
|
|
|
|
*
|
|
|
|
* @return {boolean} true if instance is an instance of type, otherwise false
|
|
|
|
*/
|
|
|
|
exports.isInstanceOf = function( type, instance )
|
|
|
|
{
|
|
|
|
var meta, implemented, i;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-05-22 21:05:46 -04:00
|
|
|
// check prototype chain (will throw an error if type is not a
|
2011-03-27 23:04:40 -04:00
|
|
|
// constructor (function)
|
|
|
|
if ( instance instanceof type )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( e ) {}
|
|
|
|
|
|
|
|
// if no metadata is available, then our remaining checks cannot be
|
|
|
|
// performed
|
|
|
|
if ( !instance.__cid || !( meta = exports.getMeta( instance ) ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
implemented = meta.implemented;
|
|
|
|
i = implemented.length;
|
|
|
|
|
|
|
|
// check implemented interfaces
|
|
|
|
while ( i-- )
|
|
|
|
{
|
|
|
|
if ( implemented[ i ] === type )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2011-03-27 01:57:17 -04:00
|
|
|
};
|
2011-03-27 23:04:40 -04:00
|
|
|
|
2011-03-29 00:08:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches isAbstract() method to the class
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
* @param {Array} methods abstract method names
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachAbstract( func, methods )
|
|
|
|
{
|
|
|
|
var is_abstract = ( methods.__length > 0 ) ? true: false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the class contains abstract methods (and is therefore
|
|
|
|
* abstract)
|
|
|
|
*
|
|
|
|
* @return {Boolean} true if class is abstract, otherwise false
|
|
|
|
*/
|
|
|
|
util.defineSecureProp( func, 'isAbstract', function()
|
|
|
|
{
|
|
|
|
return is_abstract;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-29 00:15:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches the unique id to the class and its prototype
|
|
|
|
*
|
|
|
|
* The unique identifier is used internally to match a class and its instances
|
|
|
|
* with the class metadata. Exposing the id breaks encapsulation to a degree,
|
|
|
|
* but is a lesser evil when compared to exposing all metadata.
|
|
|
|
*
|
|
|
|
* @param {function()} ctor constructor (class) to attach method to
|
|
|
|
* @param {number} id id to assign
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachId( ctor, id )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( ctor, '__cid', id );
|
|
|
|
util.defineSecureProp( ctor.prototype, '__cid', id );
|
|
|
|
}
|
|
|
|
|
2011-05-22 11:11:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets class flags
|
|
|
|
*
|
|
|
|
* @param {Class} ctor class to flag
|
|
|
|
* @param {Object} props class properties
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachFlags( ctor, props )
|
|
|
|
{
|
|
|
|
ctor.___$$final$$ = !!( props.___$$final$$ );
|
2011-05-22 13:57:56 -04:00
|
|
|
ctor.___$$abstract$$ = !!( props.___$$abstract$$ );
|
2011-05-22 11:11:18 -04:00
|
|
|
|
|
|
|
// The properties are no longer needed. Set to undefined rather than delete
|
|
|
|
// (v8 performance)
|
2011-05-22 13:57:56 -04:00
|
|
|
props.___$$final$$ = props.___$$abstract$$ = undefined;
|
2011-05-22 11:11:18 -04:00
|
|
|
}
|
|
|
|
|