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' ),
|
|
|
|
member_builder = require( './member_builder' );
|
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 = {};
|
|
|
|
|
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-01-10 19:00:26 -05:00
|
|
|
var args = Array.prototype.slice.call( arguments );
|
2011-01-03 23:25:27 -05:00
|
|
|
|
2011-01-10 19:00:26 -05:00
|
|
|
// apply to an empty (new) object
|
2011-03-03 19:43:20 -05:00
|
|
|
args.unshift( module.exports.extend() );
|
2010-12-29 23:33:30 -05:00
|
|
|
|
2011-01-10 19:00:26 -05:00
|
|
|
return implement.apply( this, args );
|
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(
|
|
|
|
"Expecting two arguments for named Class definition; " +
|
|
|
|
arguments.length + " given."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the name to the definition
|
|
|
|
def.__name = name;
|
|
|
|
|
|
|
|
// the definition must be an object
|
|
|
|
if ( typeof def !== 'object' )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Unexpected value for named class definition"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return extend( 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-01-04 00:37:54 -05:00
|
|
|
var class_id = 0;
|
|
|
|
|
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
|
|
|
|
2010-12-19 13:54:31 -05:00
|
|
|
var properties = {},
|
2011-01-24 23:28:48 -05:00
|
|
|
members = member_builder.initMembers(
|
|
|
|
prototype, prototype, 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-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' )
|
|
|
|
{
|
|
|
|
throw new Error( "__initProps is a reserved method" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
member_builder.buildProp(
|
|
|
|
members, null, name, value, keywords
|
|
|
|
);
|
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(
|
|
|
|
members, null, name, func, keywords
|
|
|
|
);
|
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-03 22:33:18 -05:00
|
|
|
var new_class = createCtor( cname, abstract_methods );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2010-12-28 22:08:30 -05:00
|
|
|
attachPropInit( prototype, properties );
|
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
|
2010-11-15 07:07:49 -05:00
|
|
|
*
|
|
|
|
* @return {Function} constructor
|
|
|
|
*/
|
2011-03-03 22:33:18 -05:00
|
|
|
function createCtor( cname, abstract_methods )
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// provide a more intuitive string representation of the class
|
|
|
|
// instance
|
|
|
|
this.toString = ( cname )
|
|
|
|
? function()
|
|
|
|
{
|
2011-03-03 23:00:59 -05:00
|
|
|
return '[object #<' + cname + '>]';
|
2011-03-03 22:53:20 -05:00
|
|
|
}
|
|
|
|
: function()
|
|
|
|
{
|
2011-03-03 23:00:59 -05:00
|
|
|
return '[object #<anonymous>]';
|
2011-03-03 22:53:20 -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-03 22:33:18 -05:00
|
|
|
throw Error( "Abstract classes 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 = {},
|
|
|
|
base = args.shift(),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachPropInit( prototype, properties )
|
2010-12-16 23:37:18 -05:00
|
|
|
{
|
|
|
|
util.defineSecureProp( prototype, '__initProps', function()
|
|
|
|
{
|
|
|
|
// first initialize the parent's properties, so that ours will overwrite
|
|
|
|
// them
|
|
|
|
var parent_init = prototype.parent.__initProps;
|
|
|
|
if ( parent_init instanceof Function )
|
|
|
|
{
|
|
|
|
parent_init.call( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize each of the properties for this instance to
|
|
|
|
// ensure we're not sharing prototype values
|
|
|
|
for ( prop in properties )
|
|
|
|
{
|
|
|
|
// initialize the value with a clone to ensure that they do
|
|
|
|
// not share references (and therefore, data)
|
|
|
|
this[ prop ] = util.clone( properties[ prop ] );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
*
|
|
|
|
* @param {function()} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachImplement( func )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, 'implement', function()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments );
|
|
|
|
args.unshift( func );
|
|
|
|
|
|
|
|
return implement.apply( this, args );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
parent_meta = ( ( parent_id ) ? getMeta( parent_id) : undefined );
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|