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
|
|
|
|
*/
|
|
|
|
|
2010-12-01 20:41:54 -05:00
|
|
|
var util = require( './util' );
|
2010-11-15 23:22:24 -05:00
|
|
|
|
2010-11-10 17:41:12 -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
|
|
|
|
*/
|
|
|
|
exports.extend = function( base )
|
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 20:45:27 -05:00
|
|
|
exports.abstractMethod = util.createAbstractMethod;
|
2010-11-14 00:47:27 -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
|
|
|
|
|
|
|
|
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
|
|
|
{
|
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,
|
|
|
|
prototype = new base();
|
|
|
|
|
|
|
|
// copy the given properties into the new prototype
|
|
|
|
var result_data = {
|
|
|
|
abstractMethods: ( base.abstractMethods || [] ).slice()
|
|
|
|
};
|
2010-12-16 23:18:30 -05:00
|
|
|
|
|
|
|
var properties = {};
|
2010-12-19 00:11:39 -05:00
|
|
|
util.propCopy( props, prototype, {
|
2010-12-16 23:55:56 -05:00
|
|
|
each: function( name, value )
|
|
|
|
{
|
|
|
|
// disallow use of our internal __initProps() method
|
|
|
|
if ( name === '__initProps' )
|
|
|
|
{
|
|
|
|
throw new Error( "__initProps is a reserved method" );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.performDefault( name, value );
|
|
|
|
},
|
|
|
|
|
2010-12-16 23:18:30 -05:00
|
|
|
property: function( name, value )
|
|
|
|
{
|
|
|
|
properties[ name ] = value;
|
2010-12-16 23:51:22 -05:00
|
|
|
this.performDefault( name, value );
|
2010-12-16 23:18:30 -05:00
|
|
|
},
|
2010-12-19 00:11:39 -05:00
|
|
|
|
|
|
|
method: function( name, func, is_abstract )
|
|
|
|
{
|
|
|
|
var pre = prototype[ name ];
|
|
|
|
|
|
|
|
if ( ( pre === undefined ) && is_abstract )
|
|
|
|
{
|
|
|
|
result_data.abstractMethods.push( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.performDefault( name, func, is_abstract );
|
|
|
|
},
|
|
|
|
|
|
|
|
methodOverride: function( name, pre, func )
|
|
|
|
{
|
|
|
|
return util.overrideMethod(
|
2010-12-19 00:12:41 -05:00
|
|
|
name, pre, func, result_data.abstractMethods
|
2010-12-19 00:11:39 -05:00
|
|
|
);
|
|
|
|
},
|
2010-12-16 23:18:30 -05:00
|
|
|
} );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2010-12-19 00:11:39 -05:00
|
|
|
result_data.abstractMethods = util.arrayShrink( result_data.abstractMethods );
|
|
|
|
|
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
|
2010-12-16 23:37:18 -05:00
|
|
|
var new_class = create_ctor( result_data );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
|
|
|
setup_props( new_class, result_data );
|
2010-12-16 23:37:18 -05:00
|
|
|
attach_prop_init( prototype, properties );
|
|
|
|
|
2010-11-15 07:07:49 -05:00
|
|
|
new_class.prototype = prototype;
|
|
|
|
new_class.constructor = new_class;
|
|
|
|
|
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
|
|
|
|
2010-11-15 07:07:49 -05:00
|
|
|
// we're done with the extension process
|
|
|
|
extending = false;
|
|
|
|
|
|
|
|
return new_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {Object} result_data data from property copy operation
|
|
|
|
*
|
|
|
|
* @return {Function} constructor
|
|
|
|
*/
|
2010-12-16 23:18:30 -05:00
|
|
|
function create_ctor( result_data, properties )
|
2010-11-15 07:07:49 -05:00
|
|
|
{
|
|
|
|
// concrete class
|
|
|
|
if ( result_data.abstractMethods.length === 0 )
|
|
|
|
{
|
|
|
|
return function()
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
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 )
|
|
|
|
{
|
|
|
|
this.__construct.apply( this, arguments );
|
|
|
|
}
|
2010-11-15 07:07:49 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
// abstract class
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return function ()
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
|
|
|
if ( !extending )
|
|
|
|
{
|
|
|
|
throw new Error( "Abstract classes cannot be instantiated" );
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2010-11-14 01:10:55 -05:00
|
|
|
* @param {Function} func function (class) to attach properties to
|
|
|
|
* @param {Object} class_data information about the class
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-11-14 01:10:55 -05:00
|
|
|
function setup_props( func, class_data )
|
2010-11-14 00:49:52 -05:00
|
|
|
{
|
2010-11-14 01:10:55 -05:00
|
|
|
attach_abstract( func, class_data.abstractMethods );
|
2010-11-14 00:49:52 -05:00
|
|
|
attach_extend( func );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
function attach_prop_init( prototype, properties )
|
|
|
|
{
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
function attach_abstract( 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
|
|
|
|
*/
|
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:18:49 -05:00
|
|
|
|
2010-11-14 20:30:33 -05:00
|
|
|
// attach the list of abstract methods to the class (make the copy of the
|
|
|
|
// methods to ensure that they won't be gc'd or later modified and screw up
|
|
|
|
// the value)
|
2010-12-01 20:38:50 -05:00
|
|
|
util.defineSecureProp( func, 'abstractMethods', methods );
|
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-11-10 23:35:57 -05:00
|
|
|
function attach_extend( 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
|
|
|
|