2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Contains basic inheritance mechanism
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This file is part of ease.js.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* 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.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* 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
|
2010-11-10 17:41:12 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package core
|
|
|
|
*/
|
|
|
|
|
2011-01-21 00:03:41 -05:00
|
|
|
var util = require( './util' ),
|
2011-03-06 18:19:19 -05:00
|
|
|
member_builder = require( './member_builder' ),
|
|
|
|
propobj = require( './propobj' )
|
|
|
|
;
|
2010-11-15 23:22:24 -05:00
|
|
|
|
2011-01-03 23:41:45 -05:00
|
|
|
/**
|
|
|
|
* Stores class metadata internally (ensures data is encapsulated)
|
|
|
|
*
|
2011-01-04 00:37:54 -05:00
|
|
|
* The data in this object is hashed a class id.
|
2011-01-03 23:41:45 -05:00
|
|
|
*
|
2011-01-04 00:37:54 -05:00
|
|
|
* @type {Object.<number, { implemented: Array.<string> }>}
|
2011-01-03 23:41:45 -05:00
|
|
|
*/
|
|
|
|
var class_meta = {};
|
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
/**
|
|
|
|
* Stores class instance visibility object
|
|
|
|
*
|
|
|
|
* For each instance id, an object exists that contains the private and
|
|
|
|
* protected members.
|
|
|
|
*
|
|
|
|
* @type {Object.<number, Object>}
|
|
|
|
*/
|
|
|
|
var class_instance = {};
|
|
|
|
|
2011-03-06 12:39:26 -05:00
|
|
|
/*
|
2011-03-06 12:38:31 -05: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
|
|
|
|
;
|
|
|
|
|
2010-11-10 17:41:12 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
/**
|
|
|
|
* This module may be invoked in order to provide a more natural looking class
|
|
|
|
* definition mechanism
|
|
|
|
*
|
|
|
|
* This may not be used to extend existing classes. To extend an existing class,
|
|
|
|
* use the class's extend() method. If unavailable (or extending a non-ease.js
|
|
|
|
* class/object), use the module's extend() method.
|
|
|
|
*
|
2011-03-04 00:17:25 -05:00
|
|
|
* @param {string=} name optional name
|
|
|
|
* @param {Object} def class definition
|
2011-03-03 19:43:20 -05:00
|
|
|
*
|
|
|
|
* @return {Class} new class
|
|
|
|
*/
|
2011-03-03 22:33:18 -05:00
|
|
|
module.exports = function()
|
2011-03-03 19:43:20 -05:00
|
|
|
{
|
2011-03-04 19:49:15 -05:00
|
|
|
var type = typeof arguments[ 0 ],
|
|
|
|
result = null
|
2011-03-04 00:24:42 -05:00
|
|
|
;
|
2011-03-03 22:33:18 -05:00
|
|
|
|
2011-03-04 00:24:42 -05:00
|
|
|
switch ( type )
|
2011-03-03 19:43:20 -05:00
|
|
|
{
|
2011-03-04 00:24:42 -05:00
|
|
|
// anonymous class
|
|
|
|
case 'object':
|
2011-03-04 19:49:15 -05:00
|
|
|
result = createAnonymousClass.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
2011-03-03 19:43:20 -05:00
|
|
|
|
2011-03-04 00:24:42 -05:00
|
|
|
// named class
|
|
|
|
case 'string':
|
2011-03-04 19:49:15 -05:00
|
|
|
result = createNamedClass.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// we don't know what to do!
|
|
|
|
throw TypeError(
|
|
|
|
"Expecting anonymous class definition or named class definition"
|
|
|
|
);
|
2011-03-03 19:43:20 -05:00
|
|
|
}
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
return result;
|
2011-03-03 19:43:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-11-10 23:30:30 -05:00
|
|
|
/**
|
|
|
|
* Creates a class, inheriting either from the provided base class or the
|
|
|
|
* default base class
|
|
|
|
*
|
|
|
|
* @param {Object} base object to extend (extends Class by default)
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.extend = function( base )
|
2010-11-10 23:30:30 -05:00
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-11-10 23:30:30 -05:00
|
|
|
|
|
|
|
|
2010-12-29 23:33:30 -05:00
|
|
|
/**
|
|
|
|
* Implements an interface or set of interfaces
|
|
|
|
*
|
|
|
|
* @param {...Interface} interfaces interfaces to implement
|
|
|
|
*
|
|
|
|
* @return {Class} new class containing interface abstractions
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.implement = function()
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-03-05 02:59:21 -05:00
|
|
|
// implement on empty base
|
|
|
|
return createImplement(
|
|
|
|
module.exports.extend(),
|
|
|
|
Array.prototype.slice.call( arguments )
|
|
|
|
);
|
2010-12-29 23:33:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-28 20:58:42 -05:00
|
|
|
/**
|
|
|
|
* Determines whether the provided object is a class created through ease.js
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to test
|
|
|
|
*
|
|
|
|
* @return {boolean} true if class (created through ease.js), otherwise false
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isClass = function( obj )
|
2010-12-28 20:58:42 -05:00
|
|
|
{
|
|
|
|
obj = obj || {};
|
|
|
|
|
|
|
|
return ( obj.prototype instanceof Class )
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the provided object is an instance of a class created
|
|
|
|
* through ease.js
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to test
|
|
|
|
*
|
|
|
|
* @return {boolean} true if instance of class (created through ease.js),
|
|
|
|
* otherwise false
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isClassInstance = function( obj )
|
2010-12-28 20:58:42 -05:00
|
|
|
{
|
|
|
|
obj = obj || {};
|
|
|
|
|
|
|
|
return ( obj instanceof Class )
|
|
|
|
? true
|
|
|
|
: false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-29 21:13:21 -05: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
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isInstanceOf = function( type, instance )
|
2010-12-29 21:13:21 -05:00
|
|
|
{
|
2011-01-03 23:41:45 -05:00
|
|
|
var meta, implemented, i;
|
|
|
|
|
2010-12-29 21:13:21 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// check prototype chain (with throw an error if type is not a
|
|
|
|
// constructor (function)
|
|
|
|
if ( instance instanceof type )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( e ) {}
|
|
|
|
|
2011-01-03 23:41:45 -05:00
|
|
|
// if no metadata is available, then our remaining checks cannot be
|
|
|
|
// performed
|
2011-01-04 00:37:54 -05:00
|
|
|
if ( !instance.__cid || !( meta = getMeta( instance.__cid ) ) )
|
2011-01-03 23:41:45 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
implemented = meta.implemented;
|
|
|
|
i = implemented.length;
|
|
|
|
|
|
|
|
// check implemented interfaces
|
|
|
|
while ( i-- )
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-01-03 23:41:45 -05:00
|
|
|
if ( implemented[ i ] === type )
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-01-03 23:41:45 -05:00
|
|
|
return true;
|
2010-12-29 23:33:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-29 21:13:21 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-29 21:37:11 -05:00
|
|
|
/**
|
|
|
|
* Alias for isInstanceOf()
|
|
|
|
*
|
|
|
|
* May read better in certain situations (e.g. Cat.isA( Mammal )) and more
|
|
|
|
* accurately conveys the act of inheritance, implementing interfaces and
|
|
|
|
* traits, etc.
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isA = module.exports.isInstanceOf;
|
2010-12-29 21:37:11 -05:00
|
|
|
|
|
|
|
|
2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Default class implementation
|
|
|
|
*
|
|
|
|
* @return undefined
|
|
|
|
*/
|
2010-11-10 23:35:57 -05:00
|
|
|
function Class() {};
|
2010-11-10 17:41:12 -05:00
|
|
|
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
/**
|
|
|
|
* Creates a new anonymous Class from the given class definition
|
|
|
|
*
|
|
|
|
* @param {Object} def class definition
|
|
|
|
*
|
|
|
|
* @return {Class} new anonymous class
|
|
|
|
*/
|
|
|
|
function createAnonymousClass( def )
|
|
|
|
{
|
|
|
|
// ensure we have the proper number of arguments (if they passed in
|
|
|
|
// too many, it may signify that they don't know what they're doing,
|
|
|
|
// and likely they're not getting the result they're looking for)
|
|
|
|
if ( arguments.length > 1 )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Expecting one argument for anonymous Class definition; " +
|
|
|
|
arguments.length + " given."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return extend( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new named Class from the given class definition
|
|
|
|
*
|
|
|
|
* @param {string} name class name
|
|
|
|
* @param {Object} def class definition
|
|
|
|
*
|
|
|
|
* @return {Class} new named class
|
|
|
|
*/
|
|
|
|
function createNamedClass( name, def )
|
|
|
|
{
|
|
|
|
// if too many arguments were provided, it's likely that they're
|
|
|
|
// expecting some result that they're not going to get
|
|
|
|
if ( arguments.length > 2 )
|
|
|
|
{
|
|
|
|
throw Error(
|
2011-03-05 12:56:14 -05:00
|
|
|
"Expecting two arguments for definition of named Class '" + name +
|
|
|
|
"'; " + arguments.length + " given."
|
2011-03-04 19:49:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-05 00:33:47 -05:00
|
|
|
// if no definition was given, return a staging object, to apply the name to
|
|
|
|
// the class once it is actually created
|
|
|
|
if ( def === undefined )
|
|
|
|
{
|
|
|
|
return createStaging( name );
|
|
|
|
}
|
2011-03-04 19:49:15 -05:00
|
|
|
// the definition must be an object
|
2011-03-05 00:33:47 -05:00
|
|
|
else if ( typeof def !== 'object' )
|
2011-03-04 19:49:15 -05:00
|
|
|
{
|
|
|
|
throw TypeError(
|
2011-03-05 12:56:14 -05:00
|
|
|
"Unexpected value for definition of named Class '" + name +
|
|
|
|
"'; object expected"
|
2011-03-04 19:49:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:44:19 -05:00
|
|
|
// add the name to the definition
|
|
|
|
def.__name = name;
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
return extend( def );
|
|
|
|
}
|
2010-11-14 22:07:04 -05:00
|
|
|
|
|
|
|
|
2011-03-05 00:33:47 -05:00
|
|
|
/**
|
|
|
|
* Creates a staging object to stage a class name
|
|
|
|
*
|
|
|
|
* The class name will be applied to the class generated by operations performed
|
|
|
|
* on the staging object. This allows applying names to classes that need to be
|
|
|
|
* extended or need to implement interfaces.
|
|
|
|
*
|
|
|
|
* @param {string} cname desired class name
|
|
|
|
*
|
|
|
|
* @return {Object} object staging the given class name
|
|
|
|
*/
|
|
|
|
function createStaging( cname )
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
extend: function()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.apply( arguments );
|
|
|
|
|
|
|
|
// extend() takes a maximum of two arguments. If only one
|
|
|
|
// argument is provided, then it is to be the class definition.
|
|
|
|
// Otherwise, the first argument is the supertype and the second
|
|
|
|
// argument is the class definition. Either way you look at it,
|
|
|
|
// the class definition is always the final argument.
|
|
|
|
//
|
|
|
|
// We want to add the name to the definition.
|
|
|
|
args[ args.length - 1 ].__name = cname;
|
|
|
|
|
|
|
|
return extend.apply( null, args );
|
|
|
|
},
|
2011-03-05 03:22:45 -05:00
|
|
|
|
|
|
|
implement: function()
|
|
|
|
{
|
|
|
|
// implement on empty base, providing the class name to be used once
|
|
|
|
// extended
|
|
|
|
return createImplement(
|
|
|
|
extend( {} ),
|
|
|
|
Array.prototype.slice.call( arguments ),
|
|
|
|
cname
|
|
|
|
);
|
|
|
|
},
|
2011-03-05 00:33:47 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-05 02:59:21 -05:00
|
|
|
/**
|
|
|
|
* Creates an intermediate object to permit implementing interfaces
|
|
|
|
*
|
|
|
|
* This object defers processing until extend() is called. This intermediate
|
|
|
|
* object ensures that a usable class is not generated until after extend() is
|
|
|
|
* called, as it does not make sense to create a class without any
|
|
|
|
* body/definition.
|
|
|
|
*
|
2011-03-05 03:22:45 -05:00
|
|
|
* @param {Object} base base class to implement atop of
|
|
|
|
* @param {Array} ifaces interfaces to implement
|
|
|
|
* @param {string=} cname optional class name once extended
|
2011-03-05 02:59:21 -05:00
|
|
|
*
|
|
|
|
* @return {Object} intermediate implementation object
|
|
|
|
*/
|
2011-03-05 03:22:45 -05:00
|
|
|
function createImplement( base, ifaces, cname )
|
2011-03-05 02:59:21 -05:00
|
|
|
{
|
2011-03-05 03:03:50 -05:00
|
|
|
ifaces.push( base );
|
2011-03-05 02:59:21 -05:00
|
|
|
|
|
|
|
// Defer processing until after extend(). This also ensures that implement()
|
|
|
|
// returns nothing usable.
|
|
|
|
return {
|
|
|
|
extend: function( def )
|
|
|
|
{
|
2011-03-05 03:22:45 -05:00
|
|
|
// if a name was provided, use it
|
|
|
|
if ( cname )
|
|
|
|
{
|
|
|
|
def.__name = cname;
|
|
|
|
}
|
|
|
|
|
2011-03-05 02:59:21 -05:00
|
|
|
return extend.apply( null, [
|
|
|
|
implement.apply( this, ifaces ),
|
|
|
|
def
|
|
|
|
] );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2010-11-14 22:07:04 -05:00
|
|
|
|
|
|
|
|
2010-11-15 06:54:37 -05:00
|
|
|
/**
|
2010-11-15 07:07:49 -05:00
|
|
|
* Creates extend function
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2010-11-15 07:07:49 -05:00
|
|
|
* The 'extending' parameter is used to override the functionality of abstract
|
|
|
|
* class constructors, allowing them to be instantiated for use in a subclass's
|
|
|
|
* prototype.
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2010-11-15 07:07:49 -05:00
|
|
|
* @param {boolean} extending whether a class is currently being extended
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2010-11-15 07:07:49 -05:00
|
|
|
* @return {Function} extend function
|
2010-11-15 06:54:37 -05:00
|
|
|
*/
|
2010-11-15 07:07:49 -05:00
|
|
|
var extend = ( function( extending )
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
2011-03-02 20:43:24 -05:00
|
|
|
var class_id = 0,
|
|
|
|
instance_id = 0;
|
2011-01-04 00:37:54 -05:00
|
|
|
|
2010-11-15 07:07:49 -05: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
|
|
|
|
*/
|
|
|
|
return function extend()
|
|
|
|
{
|
|
|
|
// 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() || Class,
|
2011-03-01 12:11:36 -05:00
|
|
|
prototype = new base(),
|
2011-03-03 22:33:18 -05:00
|
|
|
cname = '',
|
2011-03-01 12:11:36 -05:00
|
|
|
|
|
|
|
hasOwn = Array.prototype.hasOwnProperty;
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2011-01-27 22:35:40 -05:00
|
|
|
var properties = {},
|
|
|
|
prop_init = member_builder.initMembers(),
|
2011-03-02 20:43:24 -05:00
|
|
|
members = member_builder.initMembers( prototype ),
|
2010-12-16 23:18:30 -05:00
|
|
|
|
2011-01-25 00:24:50 -05:00
|
|
|
abstract_methods =
|
|
|
|
util.clone( getMeta( base.__cid ).abstractMethods )
|
2011-01-25 00:13:47 -05:00
|
|
|
|| { __length: 0 }
|
|
|
|
;
|
2011-01-24 20:58:58 -05:00
|
|
|
|
2011-03-03 22:33:18 -05:00
|
|
|
// grab the name, if one was provided
|
|
|
|
if ( cname = props.__name )
|
|
|
|
{
|
|
|
|
// we no longer need it
|
|
|
|
delete props.__name;
|
|
|
|
}
|
|
|
|
|
2011-03-06 12:38:31 -05:00
|
|
|
// IE has problems with toString()
|
|
|
|
if ( enum_bug )
|
|
|
|
{
|
|
|
|
if ( props.toString !== Object.prototype.toString )
|
|
|
|
{
|
|
|
|
props.__toString = props.toString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-24 23:28:48 -05:00
|
|
|
util.propParse( props, {
|
|
|
|
each: function( name, value, keywords )
|
2010-12-16 23:55:56 -05:00
|
|
|
{
|
|
|
|
// disallow use of our internal __initProps() method
|
|
|
|
if ( name === '__initProps' )
|
|
|
|
{
|
2011-03-05 13:13:53 -05:00
|
|
|
throw new Error(
|
|
|
|
( ( cname ) ? cname + '::' : '' ) +
|
|
|
|
"__initProps is a reserved method"
|
|
|
|
);
|
2010-12-16 23:55:56 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-01-24 23:28:48 -05:00
|
|
|
property: function( name, value, keywords )
|
2010-12-16 23:18:30 -05:00
|
|
|
{
|
|
|
|
properties[ name ] = value;
|
2011-01-24 23:28:48 -05:00
|
|
|
|
2011-01-27 22:35:40 -05:00
|
|
|
// build a new property, passing in the other members to compare
|
|
|
|
// against for preventing nonsensical overrides
|
2011-01-24 23:28:48 -05:00
|
|
|
member_builder.buildProp(
|
2011-01-27 22:35:40 -05:00
|
|
|
prop_init, null, name, value, keywords, members
|
2011-01-24 23:28:48 -05:00
|
|
|
);
|
2010-12-16 23:18:30 -05:00
|
|
|
},
|
2010-12-19 00:11:39 -05:00
|
|
|
|
2011-01-24 23:28:48 -05:00
|
|
|
getter: function( name, value, keywords )
|
2010-12-19 00:11:39 -05:00
|
|
|
{
|
2011-01-24 23:28:48 -05:00
|
|
|
member_builder.buildGetter(
|
|
|
|
members, null, name, value, keywords
|
|
|
|
);
|
|
|
|
},
|
2010-12-19 00:11:39 -05:00
|
|
|
|
2011-01-24 23:28:48 -05:00
|
|
|
setter: function( name, value, keywords )
|
|
|
|
{
|
|
|
|
member_builder.buildSetter(
|
|
|
|
members, null, name, value, keywords
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
method: function( name, func, is_abstract, keywords )
|
|
|
|
{
|
|
|
|
member_builder.buildMethod(
|
2011-03-02 20:43:24 -05:00
|
|
|
members, null, name, func, keywords, getMethodInstance
|
2011-01-24 23:28:48 -05:00
|
|
|
);
|
2011-01-24 20:58:58 -05:00
|
|
|
|
2011-01-24 23:28:48 -05:00
|
|
|
if ( is_abstract )
|
2010-12-19 00:11:39 -05:00
|
|
|
{
|
2011-01-25 00:13:47 -05:00
|
|
|
abstract_methods[ name ] = true;
|
|
|
|
abstract_methods.__length++;
|
2010-12-19 00:11:39 -05:00
|
|
|
}
|
2011-03-01 12:11:36 -05:00
|
|
|
else if ( ( hasOwn.call( abstract_methods, name ) )
|
2011-01-24 23:28:48 -05:00
|
|
|
&& ( is_abstract === false )
|
|
|
|
)
|
2011-01-24 20:58:58 -05:00
|
|
|
{
|
|
|
|
// if this was a concrete method, then it should no longer
|
|
|
|
// be marked as abstract
|
2011-01-25 00:13:47 -05:00
|
|
|
delete abstract_methods[ name ];
|
|
|
|
abstract_methods.__length--;
|
2011-01-24 20:58:58 -05:00
|
|
|
}
|
2010-12-19 00:11:39 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
methodOverride: function( name, pre, func )
|
|
|
|
{
|
|
|
|
return util.overrideMethod(
|
2010-12-19 13:54:31 -05:00
|
|
|
name, pre, func, abstract_methods
|
2010-12-19 00:11:39 -05:00
|
|
|
);
|
|
|
|
},
|
2010-12-16 23:18:30 -05:00
|
|
|
} );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
|
|
|
// reference to the parent prototype (for more experienced users)
|
|
|
|
prototype.parent = base.prototype;
|
|
|
|
|
|
|
|
// set up the new class
|
2011-03-05 23:11:13 -05:00
|
|
|
var new_class = createCtor( cname, abstract_methods, members );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
attachPropInit( prototype, prop_init, members );
|
2010-12-16 23:37:18 -05:00
|
|
|
|
2010-11-15 07:07:49 -05:00
|
|
|
new_class.prototype = prototype;
|
|
|
|
new_class.constructor = new_class;
|
|
|
|
|
2011-01-04 00:37:54 -05:00
|
|
|
// important: call after setting prototype
|
|
|
|
setupProps( new_class, abstract_methods, ++class_id );
|
|
|
|
|
2010-11-15 23:22:24 -05:00
|
|
|
// lock down the new class (if supported) to ensure that we can't add
|
|
|
|
// members at runtime
|
2010-12-01 20:41:54 -05:00
|
|
|
util.freeze( new_class );
|
2010-11-15 23:22:24 -05:00
|
|
|
|
2011-01-03 23:41:45 -05:00
|
|
|
// create internal metadata for the new class
|
2011-01-25 00:24:50 -05:00
|
|
|
var meta = createMeta( new_class, base.prototype.__cid );
|
|
|
|
meta.abstractMethods = abstract_methods;
|
2011-03-03 22:33:18 -05:00
|
|
|
meta.name = cname;
|
2011-01-03 23:41:45 -05:00
|
|
|
|
2010-11-15 07:07:49 -05:00
|
|
|
// we're done with the extension process
|
|
|
|
extending = false;
|
|
|
|
|
|
|
|
return new_class;
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-11-15 07:07:49 -05: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).
|
|
|
|
*
|
2011-03-03 22:33:18 -05:00
|
|
|
* @param {string} cname class name (may be empty)
|
2010-12-19 13:54:31 -05:00
|
|
|
* @param {Array.<string>} abstract_methods list of abstract methods
|
2011-03-05 23:11:13 -05:00
|
|
|
* @param {Object} members class members
|
2010-11-15 07:07:49 -05:00
|
|
|
*
|
|
|
|
* @return {Function} constructor
|
|
|
|
*/
|
2011-03-05 23:11:13 -05:00
|
|
|
function createCtor( cname, abstract_methods, members )
|
2010-11-15 07:07:49 -05:00
|
|
|
{
|
|
|
|
// concrete class
|
2011-01-25 00:13:47 -05:00
|
|
|
if ( abstract_methods.__length === 0 )
|
2010-11-15 07:07:49 -05:00
|
|
|
{
|
2010-12-28 19:05:53 -05:00
|
|
|
var args = null;
|
2010-12-29 09:43:00 -05:00
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
// constructor function to be returned
|
2010-12-29 09:43:00 -05:00
|
|
|
var __self = function()
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
2010-12-28 19:05:53 -05:00
|
|
|
if ( !( this instanceof __self ) )
|
|
|
|
{
|
|
|
|
// store arguments to be passed to constructor and
|
|
|
|
// instantiate new object
|
|
|
|
args = arguments;
|
|
|
|
return new __self();
|
|
|
|
}
|
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
// generate and store unique instance id
|
|
|
|
attachInstanceId( this, ++instance_id, __self );
|
|
|
|
|
|
|
|
initInstance( instance_id, this );
|
2010-12-16 23:37:18 -05:00
|
|
|
this.__initProps();
|
2010-12-16 23:18:30 -05:00
|
|
|
|
|
|
|
// call the constructor, if one was provided
|
2010-11-15 06:54:37 -05:00
|
|
|
if ( this.__construct instanceof Function )
|
|
|
|
{
|
2010-12-23 15:57:45 -05:00
|
|
|
// 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
|
2010-12-28 19:05:53 -05:00
|
|
|
this.__construct.apply( this, ( args || arguments ) );
|
|
|
|
args = null;
|
2010-11-15 06:54:37 -05:00
|
|
|
}
|
2010-12-29 21:32:16 -05:00
|
|
|
|
|
|
|
// attach any instance properties/methods (done after
|
|
|
|
// constructor to ensure they are not overridden)
|
|
|
|
attachInstanceOf( this );
|
2011-03-03 22:53:20 -05:00
|
|
|
|
2011-03-05 23:11:13 -05:00
|
|
|
// 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-06 12:38:31 -05:00
|
|
|
// use __toString if available (see enum_bug), otherwise use
|
|
|
|
// our own defaults
|
|
|
|
this.toString = members[ 'public' ].__toString
|
|
|
|
|| ( ( cname )
|
|
|
|
? function()
|
|
|
|
{
|
|
|
|
return '[object #<' + cname + '>]';
|
|
|
|
}
|
|
|
|
: function()
|
|
|
|
{
|
|
|
|
return '[object #<anonymous>]';
|
|
|
|
}
|
|
|
|
)
|
2011-03-05 23:11:13 -05:00
|
|
|
;
|
|
|
|
}
|
2010-11-15 07:07:49 -05:00
|
|
|
};
|
2010-12-29 09:43:00 -05:00
|
|
|
|
2011-01-17 20:20:39 -05:00
|
|
|
// provide a more intuitive string representation
|
2011-03-03 22:33:18 -05:00
|
|
|
__self.toString = ( cname )
|
2011-03-03 23:00:59 -05:00
|
|
|
? function() { return '[object Class <' + cname + '>]'; }
|
|
|
|
: function() { return '[object Class]'; }
|
2011-03-03 22:33:18 -05:00
|
|
|
;
|
2011-01-17 20:20:39 -05:00
|
|
|
|
2010-12-29 09:43:00 -05:00
|
|
|
return __self;
|
2010-11-15 07:07:49 -05:00
|
|
|
}
|
|
|
|
// abstract class
|
|
|
|
else
|
|
|
|
{
|
2011-01-17 20:22:30 -05:00
|
|
|
var __abstract_self = function()
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
|
|
|
if ( !extending )
|
|
|
|
{
|
2011-03-05 13:13:53 -05:00
|
|
|
throw Error(
|
|
|
|
"Abstract class " + ( cname || '(anonymous)' ) +
|
|
|
|
" cannot be instantiated"
|
|
|
|
);
|
2010-11-15 06:54:37 -05:00
|
|
|
}
|
2010-11-15 07:07:49 -05:00
|
|
|
};
|
2011-01-17 20:22:30 -05:00
|
|
|
|
2011-03-03 22:33:18 -05:00
|
|
|
__abstract_self.toString = ( cname )
|
2011-03-03 23:00:59 -05:00
|
|
|
? function()
|
|
|
|
{
|
|
|
|
return '[object AbstractClass <' + cname + '>]';
|
|
|
|
}
|
|
|
|
: function()
|
|
|
|
{
|
|
|
|
return '[object AbstractClass]';
|
|
|
|
}
|
2011-03-03 22:33:18 -05:00
|
|
|
;
|
2011-01-17 20:22:30 -05:00
|
|
|
|
|
|
|
return __abstract_self;
|
2010-11-15 07:07:49 -05:00
|
|
|
}
|
|
|
|
}
|
2010-12-18 08:24:15 -05:00
|
|
|
} )( false );
|
2010-11-15 06:54:37 -05:00
|
|
|
|
|
|
|
|
2011-01-10 19:00:26 -05:00
|
|
|
/**
|
|
|
|
* Implements interface(s) into an object
|
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* This will copy all of the abstract methods from the interface and merge it
|
|
|
|
* into the given object.
|
2011-01-10 19:00:26 -05:00
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* @param {Object} base base object
|
2011-01-10 19:00:26 -05:00
|
|
|
* @param {...Interface} interfaces interfaces to implement into dest
|
|
|
|
*
|
|
|
|
* @return {Object} destination object with interfaces implemented
|
|
|
|
*/
|
|
|
|
var implement = function()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
2011-01-10 19:56:09 -05:00
|
|
|
dest = {},
|
2011-03-05 03:03:50 -05:00
|
|
|
base = args.pop(),
|
2011-01-10 19:00:26 -05:00
|
|
|
len = args.length,
|
|
|
|
arg = null,
|
|
|
|
|
|
|
|
abstract_list = [],
|
|
|
|
implemented = [];
|
|
|
|
|
|
|
|
// add each of the interfaces
|
|
|
|
for ( var i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
arg = args[ i ];
|
|
|
|
|
|
|
|
// copy all interface methods to the class (does not yet deep copy)
|
2011-01-24 23:38:27 -05:00
|
|
|
util.propParse( arg.prototype, {
|
|
|
|
method: function( name, func, is_abstract, keywords )
|
|
|
|
{
|
|
|
|
dest[ name ] = func;
|
|
|
|
},
|
|
|
|
} );
|
2011-01-10 19:00:26 -05:00
|
|
|
implemented.push( arg );
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:56:09 -05:00
|
|
|
// create a new class with the implemented abstract methods
|
2011-03-03 19:43:20 -05:00
|
|
|
var class_new = module.exports.extend( base, dest );
|
2011-01-10 19:00:26 -05:00
|
|
|
getMeta( class_new.__cid ).implemented = implemented;
|
|
|
|
|
|
|
|
return class_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 23:29:28 -05:00
|
|
|
/**
|
2010-11-14 00:49:52 -05:00
|
|
|
* Sets up common properties for the provided function (class)
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2011-03-03 22:33:18 -05:00
|
|
|
* @param {function()} func function (class) to set up
|
2010-12-19 13:54:31 -05:00
|
|
|
* @param {Array.<string>} abstract_methods list of abstract method names
|
2011-01-04 00:37:54 -05:00
|
|
|
* @param {number} class_id unique id to assign to class
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-01-04 00:37:54 -05:00
|
|
|
function setupProps( func, abstract_methods, class_id )
|
2010-11-14 00:49:52 -05:00
|
|
|
{
|
2010-12-28 22:08:30 -05:00
|
|
|
attachAbstract( func, abstract_methods );
|
|
|
|
attachExtend( func );
|
2011-01-10 19:56:09 -05:00
|
|
|
attachImplement( func );
|
2011-01-04 00:37:54 -05:00
|
|
|
attachId( func, class_id );
|
2010-11-14 00:49:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
/**
|
|
|
|
* Initializes class instance
|
|
|
|
*
|
|
|
|
* This process will create the instance visibility object containing 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.
|
|
|
|
*
|
|
|
|
* @param {number} iid instance id
|
|
|
|
* @param {Object} instance instance to initialize
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function initInstance( iid, instance )
|
|
|
|
{
|
|
|
|
var prot = function() {};
|
|
|
|
prot.prototype = instance;
|
|
|
|
|
|
|
|
// add the visibility objects to the data object for this class instance
|
|
|
|
class_instance[ iid ] = new prot();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-16 23:37:18 -05:00
|
|
|
/**
|
2010-12-16 23:56:35 -05:00
|
|
|
* Attaches __initProps() method to the class prototype
|
2010-12-16 23:37:18 -05:00
|
|
|
*
|
2010-12-16 23:56:35 -05:00
|
|
|
* The __initProps() method will initialize class properties for that instance,
|
2010-12-16 23:37:18 -05:00
|
|
|
* ensuring that their data is not shared with other instances (this is not a
|
|
|
|
* problem with primitive data types).
|
|
|
|
*
|
2010-12-16 23:56:35 -05:00
|
|
|
* The __initProps() method will also initialize any parent properties
|
2010-12-16 23:37:18 -05:00
|
|
|
* (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
|
|
|
|
*
|
2011-03-02 20:43:24 -05:00
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
2010-12-16 23:37:18 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-02 20:43:24 -05:00
|
|
|
function attachPropInit( prototype, properties, members )
|
2010-12-16 23:37:18 -05:00
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
util.defineSecureProp( prototype, '__initProps', function( inherit )
|
2010-12-16 23:37:18 -05:00
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
// defaults to false
|
|
|
|
inherit = !!inherit;
|
|
|
|
|
2010-12-16 23:37:18 -05:00
|
|
|
// first initialize the parent's properties, so that ours will overwrite
|
|
|
|
// them
|
|
|
|
var parent_init = prototype.parent.__initProps;
|
|
|
|
if ( parent_init instanceof Function )
|
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
// 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 );
|
2010-12-16 23:37:18 -05:00
|
|
|
}
|
|
|
|
|
2011-03-06 21:48:18 -05:00
|
|
|
var inst_props = propobj.createPropProxy(
|
|
|
|
this, class_instance[ this.__iid ], properties[ 'public' ]
|
|
|
|
);
|
|
|
|
|
2011-03-06 22:43:14 -05:00
|
|
|
class_instance[ this.__iid ] = inst_props;
|
|
|
|
|
2011-03-07 00:10:12 -05:00
|
|
|
propobj.setup( inst_props, properties, members );
|
2010-12-16 23:37:18 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-14 01:10:55 -05:00
|
|
|
/**
|
|
|
|
* Attaches isAbstract() method to the class
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
* @param {Array} methods abstract method names
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachAbstract( func, methods )
|
2010-11-14 01:10:55 -05:00
|
|
|
{
|
2011-01-25 00:13:47 -05:00
|
|
|
var is_abstract = ( methods.__length > 0 ) ? true: false;
|
2010-11-14 01:10:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the class contains abstract methods (and is therefore
|
|
|
|
* abstract)
|
|
|
|
*
|
|
|
|
* @return {Boolean} true if class is abstract, otherwise false
|
|
|
|
*/
|
2010-12-01 20:38:50 -05:00
|
|
|
util.defineSecureProp( func, 'isAbstract', function()
|
2010-11-14 01:10:55 -05:00
|
|
|
{
|
|
|
|
return is_abstract;
|
2010-11-14 20:31:23 -05:00
|
|
|
});
|
2010-11-14 01:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-14 00:49:52 -05:00
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (class)
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
2010-11-10 23:29:28 -05:00
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachExtend( func )
|
2010-11-10 17:41:12 -05:00
|
|
|
{
|
2010-11-10 21:10:31 -05:00
|
|
|
/**
|
|
|
|
* Shorthand for extending classes
|
|
|
|
*
|
2010-12-01 23:19:59 -05:00
|
|
|
* This method can be invoked on the object, rather than having to call
|
2010-11-10 21:10:31 -05:00
|
|
|
* Class.extend( this ).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to add to extended class
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2010-12-01 20:38:50 -05:00
|
|
|
util.defineSecureProp( func, 'extend', function( props )
|
2010-11-10 23:28:20 -05:00
|
|
|
{
|
|
|
|
return extend( this, props );
|
2010-11-14 22:07:04 -05:00
|
|
|
});
|
2010-11-10 21:10:31 -05:00
|
|
|
}
|
2010-11-14 21:50:56 -05:00
|
|
|
|
2010-12-29 21:32:16 -05:00
|
|
|
|
2011-01-10 19:56:09 -05:00
|
|
|
/**
|
|
|
|
* Attaches implement method to the given function (class)
|
|
|
|
*
|
2011-03-05 02:59:21 -05:00
|
|
|
* Please see the implement() export of this module for more information.
|
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* @param {function()} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachImplement( func )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, 'implement', function()
|
|
|
|
{
|
2011-03-05 02:59:21 -05:00
|
|
|
return createImplement(
|
|
|
|
func,
|
|
|
|
Array.prototype.slice.call( arguments )
|
|
|
|
);
|
2011-01-10 19:56:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 00:37:54 -05: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} func function (class) to attach method to
|
|
|
|
* @param {number} id id to assign
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachId( func, id )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, '__cid', id );
|
|
|
|
util.defineSecureProp( func.prototype, '__cid', id );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-29 23:33:30 -05:00
|
|
|
/**
|
2011-01-03 23:41:45 -05:00
|
|
|
* Attaches partially applied isInstanceOf() method to class instance
|
2010-12-29 23:33:30 -05:00
|
|
|
*
|
2011-01-03 23:41:45 -05:00
|
|
|
* @param {Object} instance class instance to attach method to
|
2010-12-29 23:33:30 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-01-03 23:41:45 -05:00
|
|
|
function attachInstanceOf( instance )
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-01-03 23:41:45 -05:00
|
|
|
var method = function( type )
|
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
return module.exports.isInstanceOf( type, instance );
|
2011-01-03 23:41:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
util.defineSecureProp( instance, 'isInstanceOf', method );
|
|
|
|
util.defineSecureProp( instance, 'isA', method );
|
2010-12-29 23:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-29 21:32:16 -05:00
|
|
|
/**
|
2011-01-03 23:41:45 -05:00
|
|
|
* Initializes class metadata for the given class
|
2010-12-29 21:32:16 -05:00
|
|
|
*
|
2011-01-04 00:37:54 -05:00
|
|
|
* @param {Class} func class to initialize metadata for
|
2010-12-29 21:32:16 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-01-04 00:37:54 -05:00
|
|
|
function createMeta( func, parent_id )
|
2010-12-29 21:32:16 -05:00
|
|
|
{
|
2011-01-04 00:37:54 -05:00
|
|
|
var id = func.__cid,
|
2011-03-02 20:43:24 -05:00
|
|
|
parent_meta = ( ( parent_id ) ? getMeta( parent_id ) : undefined );
|
2011-01-04 00:37:54 -05:00
|
|
|
|
|
|
|
// copy the parent prototype's metadata if it exists (inherit metadata)
|
|
|
|
if ( parent_meta )
|
|
|
|
{
|
2011-01-25 00:19:48 -05:00
|
|
|
class_meta[ id ] = util.clone( parent_meta, true );
|
2011-01-04 00:37:54 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create empty
|
|
|
|
class_meta[ id ] = {
|
|
|
|
implemented: [],
|
|
|
|
};
|
|
|
|
}
|
2011-01-25 00:24:50 -05:00
|
|
|
|
|
|
|
return class_meta[ id ];
|
2011-01-03 23:41:45 -05:00
|
|
|
}
|
2010-12-29 21:37:11 -05:00
|
|
|
|
2011-01-03 23:41:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2011-01-04 00:37:54 -05:00
|
|
|
* @param {number} id id of class to retrieve metadata for
|
2011-01-03 23:41:45 -05:00
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2011-01-04 00:37:54 -05:00
|
|
|
function getMeta( id )
|
2011-01-03 23:41:45 -05:00
|
|
|
{
|
2011-01-25 00:24:50 -05:00
|
|
|
return class_meta[ id ] || {};
|
2010-12-29 21:32:16 -05:00
|
|
|
}
|
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance object associated with the given method
|
|
|
|
*
|
|
|
|
* The instance object contains the protected and private members. This object
|
|
|
|
* can be passed as the context when calling a method in order to give that
|
|
|
|
* method access to those members.
|
|
|
|
*
|
|
|
|
* @param {function()} method method to look up instance object for
|
|
|
|
*
|
|
|
|
* @return {Object,null} instance object if found, otherwise null
|
|
|
|
*/
|
|
|
|
function getMethodInstance( method )
|
|
|
|
{
|
|
|
|
var iid = method.__iid,
|
|
|
|
data = class_instance[ method.__iid ];
|
|
|
|
|
|
|
|
return ( iid && data )
|
|
|
|
? data
|
|
|
|
: null
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|