2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Contains basic inheritance mechanism
|
|
|
|
*
|
2016-07-15 00:15:11 -04:00
|
|
|
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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
|
|
|
*
|
2014-01-15 23:56:00 -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 General Public License for more details.
|
2010-11-10 22:07:03 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-10 17:41:12 -05:00
|
|
|
*/
|
|
|
|
|
2014-06-11 21:49:00 -04:00
|
|
|
/**
|
|
|
|
* Console to use for logging
|
|
|
|
*
|
|
|
|
* This reference allows an alternative console to be used. Must contain
|
|
|
|
* warn() or log() methods.
|
|
|
|
*
|
|
|
|
* TODO: This needs to be moved into a facade, once more refactoring can be
|
|
|
|
* done; it was moved out of warn during its refactoring.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
var _console = ( typeof console !== 'undefined' ) ? console : undefined;
|
|
|
|
|
2014-04-20 21:11:30 -04:00
|
|
|
var util = require( './util' ),
|
|
|
|
ClassBuilder = require( './ClassBuilder' ),
|
2015-05-24 00:29:55 -04:00
|
|
|
Interface = require( './interface' ),
|
2011-08-09 17:27:26 -04:00
|
|
|
|
2014-07-06 22:57:06 -04:00
|
|
|
warn = require( './warn' ),
|
|
|
|
Warning = warn.Warning,
|
|
|
|
log_handler = warn.LogHandler( _console ),
|
2011-11-05 12:07:52 -04:00
|
|
|
|
2014-04-20 21:11:30 -04:00
|
|
|
MethodWrapperFactory = require( './MethodWrapperFactory' ),
|
|
|
|
wrappers = require( './MethodWrappers' ).standard,
|
2011-08-31 00:24:19 -04:00
|
|
|
|
2011-08-09 17:27:26 -04:00
|
|
|
class_builder = ClassBuilder(
|
2014-07-06 22:57:06 -04:00
|
|
|
log_handler,
|
2014-04-20 21:11:30 -04:00
|
|
|
require( './MemberBuilder' )(
|
2011-08-31 00:24:19 -04:00
|
|
|
MethodWrapperFactory( wrappers.wrapNew ),
|
2011-11-02 23:28:23 -04:00
|
|
|
MethodWrapperFactory( wrappers.wrapOverride ),
|
Added `proxy' keyword support
The concept of proxy methods will become an important, core concept in ease.js
that will provide strong benefits for creating decorators and proxies, removing
boilerplate code and providing useful metadata to the system. Consider the
following example:
Class( 'Foo',
{
// ...
'public performOperation': function( bar )
{
this._doSomethingWith( bar );
return this;
},
} );
Class( 'FooDecorator',
{
'private _foo': null,
// ...
'public performOperation': function( bar )
{
return this._foo.performOperation( bar );
},
} );
In the above example, `FooDecorator` is a decorator for `Foo`. Assume that the
`getValueOf()` method is undecorated and simply needs to be proxied to its
component --- an instance of `Foo`. (It is not uncommon that a decorator, proxy,
or related class will alter certain functionality while leaving much of it
unchanged.) In order to do so, we can use this generic, boilerplate code
return this.obj.func.apply( this.obj, arguments );
which would need to be repeated again and again for *each method that needs to
be proxied*. We also have another problem --- `Foo.getValueOf()` returns
*itself*, which `FooDecorator` *also* returns. This breaks encapsulation, so we
instead need to return ourself:
'public performOperation': function( bar )
{
this._foo.performOperation( bar );
return this;
},
Our boilerplate code then becomes:
var ret = this.obj.func.apply( this.obj, arguments );
return ( ret === this.obj )
? this
: ret;
Alternatively, we could use the `proxy' keyword:
Class( 'FooDecorator2',
{
'private _foo': null,
// ...
'public proxy performOperation': '_foo',
} );
`FooDecorator2.getValueOf()` and `FooDecorator.getValueOf()` both perform the
exact same task --- proxy the entire call to another object and return its
result, unless the result is the component, in which case the decorator itself
is returned.
Proxies, as of this commit, accomplish the following:
- All arguments are forwarded to the destination
- The return value is forwarded to the caller
- If the destination returns a reference to itself, it will be replaced with
a reference to the caller's context (`this`).
- If the call is expected to fail, either because the destination is not an
object or because the requested method is not a function, a useful error
will be immediately thrown (rather than the potentially cryptic one that
would otherwise result, requiring analysis of the stack trace).
N.B. As of this commit, static proxies do not yet function properly.
2012-05-02 13:26:47 -04:00
|
|
|
MethodWrapperFactory( wrappers.wrapProxy ),
|
2014-04-20 21:11:30 -04:00
|
|
|
require( './MemberBuilderValidator' )(
|
2011-11-05 12:07:52 -04:00
|
|
|
function( warning )
|
|
|
|
{
|
2014-07-06 22:57:06 -04:00
|
|
|
log_handler.handle( Warning( warning ) );
|
2011-11-05 12:07:52 -04:00
|
|
|
}
|
|
|
|
)
|
2011-08-31 00:24:19 -04:00
|
|
|
),
|
2014-04-20 21:11:30 -04:00
|
|
|
require( './VisibilityObjectFactoryFactory' )
|
2016-07-15 00:15:11 -04:00
|
|
|
.fromEnvironment(),
|
|
|
|
require( './ctor/ErrorCtor' )( Error )
|
2011-08-09 17:27:26 -04:00
|
|
|
)
|
2011-03-06 12:38:31 -05:00
|
|
|
;
|
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
var _nullf = function() { return null; }
|
|
|
|
|
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-12-13 21:19:14 -05:00
|
|
|
* @param {string|Object} namedef optional name or definition
|
|
|
|
* @param {Object=} def class definition if first argument is name
|
2011-03-03 19:43:20 -05:00
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function|Object} new class or staging object
|
2011-03-03 19:43:20 -05:00
|
|
|
*/
|
2011-12-13 21:19:14 -05:00
|
|
|
module.exports = function( namedef, def )
|
2011-03-03 19:43:20 -05:00
|
|
|
{
|
2011-12-13 21:19:14 -05:00
|
|
|
var type = ( typeof namedef ),
|
2014-03-29 00:40:36 -04:00
|
|
|
result = null,
|
|
|
|
args = [],
|
|
|
|
i = arguments.length
|
2011-03-04 00:24:42 -05:00
|
|
|
;
|
2011-03-03 22:33:18 -05:00
|
|
|
|
2014-03-29 00:40:36 -04:00
|
|
|
// passing arguments object prohibits optimizations in v8
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
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':
|
2014-03-29 00:40:36 -04:00
|
|
|
result = createAnonymousClass.apply( null, args );
|
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':
|
2014-03-29 00:40:36 -04:00
|
|
|
result = createNamedClass.apply( null, args );
|
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
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @param {Function|Object} baseordfn parent or definition object
|
|
|
|
* @param {Object=} dfn definition object if parent provided
|
2010-11-10 23:30:30 -05:00
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function} extended class
|
2010-11-10 23:30:30 -05:00
|
|
|
*/
|
2014-03-29 00:40:36 -04:00
|
|
|
module.exports.extend = extend;
|
2010-11-10 23:30:30 -05:00
|
|
|
|
|
|
|
|
2010-12-29 23:33:30 -05:00
|
|
|
/**
|
|
|
|
* Implements an interface or set of interfaces
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @param {...Function} interfaces interfaces to implement
|
2010-12-29 23:33:30 -05:00
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Object} intermediate interface object
|
2010-12-29 23:33:30 -05:00
|
|
|
*/
|
2011-12-13 21:19:14 -05:00
|
|
|
module.exports.implement = function( interfaces )
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-03-05 02:59:21 -05:00
|
|
|
// implement on empty base
|
|
|
|
return createImplement(
|
2011-03-16 19:24:02 -04:00
|
|
|
null,
|
2011-03-05 02:59:21 -05:00
|
|
|
Array.prototype.slice.call( arguments )
|
|
|
|
);
|
2010-12-29 23:33:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
/**
|
|
|
|
* Mix a trait into a class
|
|
|
|
*
|
|
|
|
* The ultimate intent of this depends on the ultimate `extend' call---if it
|
|
|
|
* extends another class, then the traits will be mixed into that class;
|
|
|
|
* otherwise, the traits will be mixed into the base class. In either case,
|
|
|
|
* a final `extend' call is necessary to complete the definition. An attempt
|
|
|
|
* to instantiate the return value before invoking `extend' will result in
|
|
|
|
* an exception.
|
|
|
|
*
|
|
|
|
* @param {Array.<Function>} traits traits to mix in
|
|
|
|
*
|
|
|
|
* @return {Function} staging object for class definition
|
|
|
|
*/
|
2014-01-21 22:57:04 -05:00
|
|
|
module.exports.use = function( traits )
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [], i = arguments.length;
|
|
|
|
while( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
2014-01-21 22:57:04 -05:00
|
|
|
// consume traits onto an empty base
|
2014-03-29 00:40:36 -04:00
|
|
|
return createUse( _nullf, args );
|
2014-01-21 22:57:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-29 10:47:12 -04:00
|
|
|
var _dummyclass = { prototype: {} };
|
|
|
|
var _dummyinst = { constructor: { prototype: {} } };
|
|
|
|
|
2010-12-28 20:58:42 -05:00
|
|
|
/**
|
|
|
|
* Determines whether the provided object is a class created through ease.js
|
|
|
|
*
|
2014-07-07 00:11:58 -04:00
|
|
|
* TODO: delegate to ClassBuilder
|
|
|
|
*
|
2010-12-28 20:58:42 -05:00
|
|
|
* @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
|
|
|
{
|
2014-04-29 10:47:12 -04:00
|
|
|
obj = obj || _dummyclass;
|
|
|
|
|
2014-07-07 00:11:58 -04:00
|
|
|
var meta = ClassBuilder.getMeta( obj );
|
|
|
|
|
|
|
|
// TODO: we're checking a random field on the meta object; do something
|
|
|
|
// proper
|
|
|
|
return ( ( ( meta !== null ) && meta.implemented )
|
2014-04-29 10:47:12 -04:00
|
|
|
|| ( obj.prototype instanceof ClassBuilder.ClassBase ) )
|
2010-12-28 20:58:42 -05:00
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the provided object is an instance of a class created
|
|
|
|
* through ease.js
|
|
|
|
*
|
2014-07-07 00:11:58 -04:00
|
|
|
* TODO: delegate to ClassBuilder
|
|
|
|
*
|
2010-12-28 20:58:42 -05:00
|
|
|
* @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
|
|
|
{
|
2014-04-29 10:47:12 -04:00
|
|
|
obj = obj || _dummyinst;
|
|
|
|
|
2014-07-07 00:11:58 -04:00
|
|
|
// if the constructor is a class, then we must be an instance!
|
|
|
|
return module.exports.isClass( obj.constructor );
|
2010-12-28 20:58:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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-08-09 17:27:26 -04:00
|
|
|
module.exports.isInstanceOf = ClassBuilder.isInstanceOf;
|
2010-12-29 21:13:21 -05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
/**
|
|
|
|
* Creates a new anonymous Class from the given class definition
|
|
|
|
*
|
|
|
|
* @param {Object} def class definition
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function} new anonymous class
|
2011-03-04 19:49:15 -05:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function|Object} new named class or staging object if definition
|
|
|
|
* was not provided
|
2011-03-04 19:49:15 -05:00
|
|
|
*/
|
|
|
|
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-16 17:50:11 -04:00
|
|
|
"Expecting at most 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()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
2011-03-05 00:33:47 -05:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
2011-03-05 03:22:45 -05:00
|
|
|
// implement on empty base, providing the class name to be used once
|
|
|
|
// extended
|
2014-03-29 00:40:36 -04:00
|
|
|
return createImplement( null, args, cname );
|
2011-03-05 03:22:45 -05:00
|
|
|
},
|
2014-02-10 23:33:07 -05:00
|
|
|
|
|
|
|
use: function()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
|
|
|
return createUse( _nullf, args );
|
2014-02-10 23:33:07 -05:00
|
|
|
},
|
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-16 18:18:33 -04:00
|
|
|
* @param {Object} base base class to implement atop of, or null
|
2011-03-05 03:22:45 -05:00
|
|
|
* @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
|
|
|
{
|
|
|
|
// Defer processing until after extend(). This also ensures that implement()
|
|
|
|
// returns nothing usable.
|
2014-03-09 21:13:11 -04:00
|
|
|
var partial = {
|
2011-03-16 18:18:33 -04:00
|
|
|
extend: function()
|
2011-03-05 02:59:21 -05:00
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var an = arguments.length,
|
|
|
|
def = arguments[ an - 1 ],
|
|
|
|
ext_base = ( an > 1 ) ? arguments[ an - 2 ] : null
|
2011-03-16 18:18:33 -04:00
|
|
|
;
|
|
|
|
|
2011-03-16 19:50:47 -04:00
|
|
|
// if any arguments remain, then they likely misunderstood what this
|
|
|
|
// method does
|
2014-03-29 00:40:36 -04:00
|
|
|
if ( an > 2 )
|
2011-03-16 19:50:47 -04:00
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Expecting no more than two arguments for extend()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-16 19:06:16 -04:00
|
|
|
// if a base was already provided for extending, don't allow them to
|
|
|
|
// give us yet another one (doesn't make sense)
|
|
|
|
if ( base && ext_base )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Cannot override parent " + base.toString() + " with " +
|
|
|
|
ext_base.toString() + " via extend()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-05 03:22:45 -05:00
|
|
|
// if a name was provided, use it
|
|
|
|
if ( cname )
|
|
|
|
{
|
|
|
|
def.__name = cname;
|
|
|
|
}
|
|
|
|
|
2011-03-16 18:18:33 -04:00
|
|
|
// If a base was provided when createImplement() was called, use
|
|
|
|
// that. Otherwise, use the extend() base passed to this function.
|
|
|
|
// If neither of those are available, extend from an empty class.
|
|
|
|
ifaces.push( base || ext_base || extend( {} ) );
|
|
|
|
|
2011-05-22 13:57:56 -04:00
|
|
|
return extend.call( null,
|
2011-03-05 02:59:21 -05:00
|
|
|
implement.apply( this, ifaces ),
|
|
|
|
def
|
2011-05-22 13:57:56 -04:00
|
|
|
);
|
2011-03-05 02:59:21 -05:00
|
|
|
},
|
2014-03-08 23:52:47 -05:00
|
|
|
|
2014-03-09 21:13:11 -04:00
|
|
|
// TODO: this is a naive implementation that works, but could be
|
|
|
|
// much more performant (it creates a subtype before mixing in)
|
2014-03-08 23:52:47 -05:00
|
|
|
use: function()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var traits = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
// passing arguments object prohibits optimizations in v8
|
|
|
|
while ( i-- ) traits[ i ] = arguments[ i ];
|
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
return createUse(
|
2014-03-09 21:13:11 -04:00
|
|
|
function() { return partial.__createBase(); },
|
2014-03-08 23:52:47 -05:00
|
|
|
traits
|
|
|
|
);
|
|
|
|
},
|
2014-03-09 21:13:11 -04:00
|
|
|
|
|
|
|
// allows overriding default behavior
|
|
|
|
__createBase: function()
|
|
|
|
{
|
|
|
|
return partial.extend( {} );
|
|
|
|
},
|
2011-03-05 02:59:21 -05:00
|
|
|
};
|
2014-03-09 21:13:11 -04:00
|
|
|
|
|
|
|
return partial;
|
2011-03-05 02:59:21 -05:00
|
|
|
}
|
2010-11-14 22:07:04 -05:00
|
|
|
|
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
/**
|
|
|
|
* Create a staging object representing an eventual mixin
|
|
|
|
*
|
|
|
|
* This staging objects prepares a class definition for trait mixin. In
|
|
|
|
* particular, the returned staging object has the following features:
|
|
|
|
* - invoking it will, if mixing into an existing (non-base) class without
|
|
|
|
* subclassing, immediately complete the mixin and instantiate the
|
|
|
|
* generated class;
|
|
|
|
* - calling `use' has the effect of chaining mixins, stacking them atop
|
|
|
|
* of one-another; and
|
|
|
|
* - invoking `extend' will immediately complete the mixin, resulting in a
|
|
|
|
* subtype of the base.
|
|
|
|
*
|
|
|
|
* Mixins are performed lazily---the actual mixin will not take place until
|
|
|
|
* the final `extend' call, which may be implicit by invoking the staging
|
|
|
|
* object (performing instantiation).
|
|
|
|
*
|
|
|
|
* The third argument determines whether or not a final `extend' call must
|
|
|
|
* be explicit: in this case, any instantiation attempts will result in an
|
|
|
|
* exception being thrown.
|
|
|
|
*
|
2015-05-24 00:29:55 -04:00
|
|
|
* This staging object may be used as a base for extending. Note, however,
|
|
|
|
* that its metadata are unavailable at the time of definition---its
|
|
|
|
* contents are marked as "lazy" and must be processed using the mixin's
|
|
|
|
* eventual metadata.
|
|
|
|
*
|
2014-03-08 23:52:47 -05:00
|
|
|
* @param {function()} basef returns base from which to lazily
|
|
|
|
* extend
|
|
|
|
* @param {Array.<Function>} traits traits to mix in
|
|
|
|
* @param {boolean} nonbase extending from a non-base class
|
|
|
|
* (setting will permit instantiation
|
|
|
|
* with implicit extend)
|
|
|
|
*
|
|
|
|
* @return {Function} staging object for mixin
|
2015-10-25 22:15:34 -04:00
|
|
|
*
|
|
|
|
* @throws {TypeError} when object is not a trait
|
2014-03-08 23:52:47 -05:00
|
|
|
*/
|
|
|
|
function createUse( basef, traits, nonbase )
|
2014-01-21 22:57:04 -05:00
|
|
|
{
|
2015-10-25 22:15:34 -04:00
|
|
|
_validateTraits( traits );
|
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
// invoking the partially applied class will immediately complete its
|
|
|
|
// definition and instantiate it with the provided constructor arguments
|
2014-02-10 23:14:05 -05:00
|
|
|
var partial = function()
|
2015-05-24 00:29:55 -04:00
|
|
|
{
|
|
|
|
return partialClass()
|
|
|
|
.apply( null, arguments );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var partialClass = function()
|
2014-02-10 23:14:05 -05:00
|
|
|
{
|
2014-03-08 23:52:47 -05:00
|
|
|
// this argument will be set only in the case where an existing
|
|
|
|
// (non-base) class is extended, meaning that an explict Class or
|
|
|
|
// AbstractClass was not provided
|
|
|
|
if ( !( nonbase ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot instantiate incomplete class definition; did " +
|
|
|
|
"you forget to call `extend'?"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-24 00:29:55 -04:00
|
|
|
return createMixedClass( basef(), traits );
|
2014-02-10 23:14:05 -05:00
|
|
|
};
|
2014-01-21 22:57:04 -05:00
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
// otherwise, its definition is deferred until additional context is
|
|
|
|
// given during the extend operation
|
2014-02-10 23:14:05 -05:00
|
|
|
partial.extend = function()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var an = arguments.length,
|
|
|
|
dfn = arguments[ an - 1 ],
|
|
|
|
ext_base = ( an > 1 ) ? arguments[ an - 2 ] : null,
|
2014-03-08 23:52:47 -05:00
|
|
|
base = basef();
|
2014-01-21 22:57:04 -05:00
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
// extend the mixed class, which ensures that all super references
|
|
|
|
// are properly resolved
|
|
|
|
return extend.call( null,
|
|
|
|
createMixedClass( ( base || ext_base ), traits ),
|
|
|
|
dfn
|
|
|
|
);
|
|
|
|
};
|
2014-02-08 00:29:25 -05:00
|
|
|
|
2014-03-05 23:42:48 -05:00
|
|
|
// syntatic sugar to avoid the aruduous and seemingly pointless `extend'
|
|
|
|
// call simply to mix in another trait
|
|
|
|
partial.use = function()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
2014-03-08 23:52:47 -05:00
|
|
|
return createUse(
|
|
|
|
function()
|
|
|
|
{
|
2014-03-09 21:13:11 -04:00
|
|
|
return partial.__createBase();
|
2014-03-08 23:52:47 -05:00
|
|
|
},
|
2014-03-29 00:40:36 -04:00
|
|
|
args,
|
2014-03-08 23:52:47 -05:00
|
|
|
nonbase
|
|
|
|
);
|
2014-03-05 23:42:48 -05:00
|
|
|
};
|
|
|
|
|
2014-03-09 21:13:11 -04:00
|
|
|
// allows overriding default behavior
|
|
|
|
partial.__createBase = function()
|
|
|
|
{
|
|
|
|
return partial.extend( {} );
|
|
|
|
};
|
|
|
|
|
2015-05-24 00:29:55 -04:00
|
|
|
partial.asPrototype = function()
|
|
|
|
{
|
|
|
|
return partialClass().asPrototype();
|
|
|
|
};
|
|
|
|
|
|
|
|
partial.__isInstanceOf = Interface.isInstanceOf;
|
|
|
|
|
|
|
|
// allow the system to recognize this object as a viable base for
|
|
|
|
// extending, but mark the metadata as lazy: since we defer all
|
|
|
|
// processing for mixins, we cannot yet know all metadata
|
|
|
|
// TODO: `_lazy' is a kluge
|
|
|
|
ClassBuilder.masquerade( partial );
|
|
|
|
ClassBuilder.getMeta( partial )._lazy = true;
|
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
return partial;
|
|
|
|
}
|
2014-02-08 00:29:25 -05:00
|
|
|
|
2014-02-10 23:14:05 -05:00
|
|
|
|
2015-10-25 22:15:34 -04:00
|
|
|
/**
|
|
|
|
* Verify that each object in TRAITS will be able to be mixed in
|
|
|
|
*
|
|
|
|
* TODO: Use Trait.isTrait; we have circular dependency issues at the moment
|
|
|
|
* preventing that; refactoring is needed.
|
|
|
|
*
|
|
|
|
* @param {Array} traits objects to validate
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*
|
|
|
|
* @throws {TypeError} when object is not a trait
|
|
|
|
*/
|
|
|
|
function _validateTraits( traits )
|
|
|
|
{
|
|
|
|
for ( var t in traits )
|
|
|
|
{
|
|
|
|
if ( typeof traits[ t ].__mixin !== 'function' )
|
|
|
|
{
|
|
|
|
throw TypeError( "Cannot mix in non-trait " + t );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
function createMixedClass( base, traits )
|
|
|
|
{
|
|
|
|
// generated definition for our [abstract] class that will mix in each
|
|
|
|
// of the provided traits; it will automatically be marked as abstract
|
|
|
|
// if needed
|
|
|
|
var dfn = { ___$$auto$abstract$$: true };
|
|
|
|
|
|
|
|
// this object is used as a class-specific context for storing trait
|
|
|
|
// data; it will be encapsulated within a ctor closure and will not be
|
|
|
|
// attached to any class
|
|
|
|
var tc = [];
|
|
|
|
|
|
|
|
// "mix" each trait into the class definition object
|
|
|
|
for ( var i = 0, n = traits.length; i < n; i++ )
|
|
|
|
{
|
Support for stacked mixins
The concept of stacked traits already existed in previous commits, but until
now, mixins could not be stacked without some ugly errors. This also allows
mixins to be stacked atop of themselves, duplicating their effect. This
would naturally have limited use, but it's there.
This differs slightly from Scala. For example, consider this ease.js mixin:
C.use( T ).use( T )()
This is perfectly valid---it has the effect of stacking T twice. In reality,
ease.js is doing this:
- C' = C.use( T );
- new C'.use( T );
That is, it each call to `use' creates another class with T mixed in.
Scala, on the other hand, complains in this situation:
new C with T with T
will produce an error stating that "trait T is inherited twice". You can
work around this, however, by doing this:
class Ca extends T
new Ca with T
In fact, this is precisely what ease.js is doing, as mentioned above; the
"use.use" syntax is merely shorthand for this:
new C.use( T ).extend( {} ).use( T )
Just keep that in mind.
2014-03-06 01:51:49 -05:00
|
|
|
traits[ i ].__mixin( dfn, tc, ( base || ClassBuilder.ClassBase ) );
|
2014-02-16 23:23:11 -05:00
|
|
|
}
|
2014-02-10 23:14:05 -05:00
|
|
|
|
2014-02-16 23:23:11 -05:00
|
|
|
// create the mixed class from the above generated definition
|
|
|
|
var C = extend.call( null, base, dfn ),
|
|
|
|
meta = ClassBuilder.getMeta( C );
|
|
|
|
|
|
|
|
// add each trait to the list of implemented types so that the
|
|
|
|
// class is considered to be of type T in traits
|
2014-02-28 23:55:24 -05:00
|
|
|
var impl = meta.implemented;
|
2014-02-16 23:23:11 -05:00
|
|
|
for ( var i = 0, n = traits.length; i < n; i++ )
|
|
|
|
{
|
2014-02-28 23:55:24 -05:00
|
|
|
impl.push( traits[ i ] );
|
|
|
|
traits[ i ].__mixinImpl( impl );
|
2014-02-16 23:23:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return C;
|
2014-01-21 22:57:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-15 06:54:37 -05:00
|
|
|
/**
|
2011-03-27 23:04:40 -04:00
|
|
|
* Mimics class inheritance
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* 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.
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* The class to inherit from (the first argument) is optional. If omitted, the
|
|
|
|
* first argument will be considered to be the properties list.
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @param {Function|Object} _ parent or definition object
|
|
|
|
* @param {Object=} __ definition object if parent was provided
|
|
|
|
*
|
|
|
|
* @return {Function} extended class
|
2010-11-15 06:54:37 -05:00
|
|
|
*/
|
2011-12-13 21:19:14 -05:00
|
|
|
function extend( _, __ )
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [],
|
|
|
|
i = arguments.length;
|
|
|
|
|
|
|
|
// passing arguments object prohibits optimizations in v8
|
|
|
|
while ( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
// set up the new class
|
2014-03-29 00:40:36 -04:00
|
|
|
var new_class = class_builder.build.apply( class_builder, args );
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2011-03-29 00:15:16 -04:00
|
|
|
// set up some additional convenience props
|
|
|
|
setupProps( new_class );
|
2010-12-29 09:43:00 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
// lock down the new class (if supported) to ensure that we can't add
|
|
|
|
// members at runtime
|
|
|
|
util.freeze( new_class );
|
2011-01-17 20:20:39 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
return new_class;
|
|
|
|
}
|
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-12-13 21:19:14 -05:00
|
|
|
* @param {Object} baseobj base object
|
|
|
|
* @param {...Function} interfaces interfaces to implement into dest
|
2011-01-10 19:00:26 -05:00
|
|
|
*
|
|
|
|
* @return {Object} destination object with interfaces implemented
|
|
|
|
*/
|
2011-12-13 21:19:14 -05:00
|
|
|
var implement = function( baseobj, interfaces )
|
2011-01-10 19:00:26 -05:00
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var an = arguments.length,
|
2011-01-10 19:56:09 -05:00
|
|
|
dest = {},
|
2014-03-29 00:40:36 -04:00
|
|
|
base = arguments[ an - 1 ],
|
2011-01-10 19:00:26 -05:00
|
|
|
arg = null,
|
|
|
|
|
2011-05-22 13:57:56 -04:00
|
|
|
implemented = [],
|
|
|
|
make_abstract = false
|
|
|
|
;
|
2011-01-10 19:00:26 -05:00
|
|
|
|
|
|
|
// add each of the interfaces
|
2014-03-29 00:40:36 -04:00
|
|
|
for ( var i = 0; i < ( an - 1 ); i++ )
|
2011-01-10 19:00:26 -05:00
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
arg = arguments[ i ];
|
2011-01-10 19:00:26 -05:00
|
|
|
|
|
|
|
// 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 )
|
|
|
|
{
|
2011-06-08 01:10:44 -04:00
|
|
|
dest[ 'abstract ' + name ] = func.definition;
|
2011-05-22 13:57:56 -04:00
|
|
|
make_abstract = true;
|
2011-01-24 23:38:27 -05:00
|
|
|
},
|
|
|
|
} );
|
2011-01-10 19:00:26 -05:00
|
|
|
implemented.push( arg );
|
|
|
|
}
|
|
|
|
|
2011-05-22 13:57:56 -04:00
|
|
|
// xxx: temporary
|
|
|
|
if ( make_abstract )
|
|
|
|
{
|
|
|
|
dest.___$$abstract$$ = true;
|
|
|
|
}
|
|
|
|
|
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-08-09 17:27:26 -04:00
|
|
|
ClassBuilder.getMeta( class_new ).implemented = implemented;
|
2011-01-10 19:00:26 -05:00
|
|
|
|
|
|
|
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-29 00:15:16 -04:00
|
|
|
* @param {function()} func function (class) to set up
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-29 00:15:16 -04:00
|
|
|
function setupProps( func )
|
2010-11-14 00:49:52 -05:00
|
|
|
{
|
2010-12-28 22:08:30 -05:00
|
|
|
attachExtend( func );
|
2011-01-10 19:56:09 -05:00
|
|
|
attachImplement( func );
|
2014-02-10 23:11:34 -05:00
|
|
|
attachUse( func );
|
2010-11-14 00:49:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (class)
|
|
|
|
*
|
2015-10-26 22:46:08 -04:00
|
|
|
* This is a shorthand method that can be invoked on the object, rather than
|
|
|
|
* having to call Class.extend( this ).
|
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @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-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()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [], i = arguments.length;
|
|
|
|
while( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
|
|
|
return createImplement( func, args );
|
2011-01-10 19:56:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-10 23:11:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches use method to the given function (class)
|
|
|
|
*
|
|
|
|
* Please see the `use' export of this module for more information.
|
|
|
|
*
|
|
|
|
* @param {function()} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachUse( func )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, 'use', function()
|
|
|
|
{
|
2014-03-29 00:40:36 -04:00
|
|
|
var args = [], i = arguments.length;
|
|
|
|
while( i-- ) args[ i ] = arguments[ i ];
|
|
|
|
|
|
|
|
return createUse( function() { return func; }, args, true );
|
2014-02-10 23:11:34 -05:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|