2010-12-01 19:27:40 -05:00
|
|
|
/**
|
|
|
|
* Contains interface module
|
|
|
|
*
|
2013-12-20 00:49:06 -05:00
|
|
|
* Copyright (C) 2010, 2011, 2012, 2013 Mike Gerwitz
|
2010-12-01 19:27:40 -05:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2010-12-01 19:27:40 -05:00
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* 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-12-01 19:27:40 -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
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2010-12-01 19:27:40 -05:00
|
|
|
*
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -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-12-01 19:27:40 -05:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
2011-03-27 02:02:04 -04:00
|
|
|
var util = require( __dirname + '/util' ),
|
2011-11-02 23:28:23 -04:00
|
|
|
|
|
|
|
MethodWrapperFactory = require( __dirname + '/MethodWrapperFactory' ),
|
|
|
|
wrappers = require( __dirname + '/MethodWrappers' ).standard,
|
|
|
|
|
|
|
|
member_builder = require( __dirname + '/MemberBuilder' )(
|
|
|
|
MethodWrapperFactory( wrappers.wrapNew ),
|
|
|
|
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 ),
|
2011-11-02 23:28:23 -04:00
|
|
|
require( __dirname + '/MemberBuilderValidator' )()
|
|
|
|
),
|
|
|
|
|
|
|
|
Class = require( __dirname + '/class' )
|
|
|
|
;
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
|
2011-03-03 19:08:24 -05:00
|
|
|
/**
|
|
|
|
* This module may be invoked in order to provide a more natural looking
|
|
|
|
* interface definition
|
|
|
|
*
|
|
|
|
* Only new interfaces may be created using this method. They cannot be
|
|
|
|
* extended. To extend an existing interface, call its extend() method, or use
|
|
|
|
* the extend() method of this module.
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @param {string|Object} namedef optional name or definition
|
|
|
|
* @param {Object=} def interface definition if first arg is name
|
2011-03-03 19:08:24 -05:00
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function|Object} new interface or staging object
|
2011-03-03 19:08:24 -05:00
|
|
|
*/
|
2011-12-13 21:19:14 -05:00
|
|
|
module.exports = function( namedef, def )
|
2011-03-03 19:08:24 -05:00
|
|
|
{
|
2011-12-13 21:19:14 -05:00
|
|
|
var type = ( typeof namedef ),
|
2011-03-04 23:35:28 -05:00
|
|
|
result = null
|
2011-03-04 00:24:42 -05:00
|
|
|
;
|
2011-03-04 00:19:02 -05:00
|
|
|
|
2011-03-04 00:24:42 -05:00
|
|
|
switch ( type )
|
2011-03-04 00:19:02 -05:00
|
|
|
{
|
2011-03-04 00:24:42 -05:00
|
|
|
// anonymous interface
|
|
|
|
case 'object':
|
2011-03-04 23:35:28 -05:00
|
|
|
result = createAnonymousInterface.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
// named class
|
|
|
|
case 'string':
|
2011-03-04 23:35:28 -05:00
|
|
|
result = createNamedInterface.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// we don't know what to do!
|
|
|
|
throw TypeError(
|
|
|
|
"Expecting anonymous interface definition or named " +
|
|
|
|
"interface definition"
|
|
|
|
);
|
2011-03-03 19:08:24 -05:00
|
|
|
}
|
|
|
|
|
2011-03-04 23:35:28 -05:00
|
|
|
return result;
|
2011-03-03 19:08:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-01 19:27:40 -05:00
|
|
|
/**
|
|
|
|
* Creates an interface
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function} extended interface
|
2010-12-01 19:27:40 -05:00
|
|
|
*/
|
2011-03-03 19:08:24 -05:00
|
|
|
module.exports.extend = function()
|
2010-12-01 19:27:40 -05:00
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
|
2011-03-03 23:59:37 -05:00
|
|
|
/**
|
|
|
|
* Determines whether the provided object is an interface created through
|
|
|
|
* ease.js
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to test
|
|
|
|
*
|
|
|
|
* @return {boolean} true if interface (created through ease.js), otherwise
|
|
|
|
* false
|
|
|
|
*/
|
|
|
|
module.exports.isInterface = function( obj )
|
|
|
|
{
|
|
|
|
obj = obj || {};
|
|
|
|
|
|
|
|
return ( obj.prototype instanceof Interface )
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-01 19:27:40 -05:00
|
|
|
/**
|
|
|
|
* Default interface implementation
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function Interface() {}
|
|
|
|
|
|
|
|
|
2011-03-04 23:35:28 -05:00
|
|
|
/**
|
|
|
|
* Creates a new anonymous Interface from the given interface definition
|
|
|
|
*
|
|
|
|
* @param {Object} def interface definition
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function} new anonymous interface
|
2011-03-04 23:35:28 -05:00
|
|
|
*/
|
|
|
|
function createAnonymousInterface( 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 Interface definition; " +
|
|
|
|
arguments.length + " given."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return extend( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new named interface from the given interface definition
|
|
|
|
*
|
|
|
|
* @param {string} name interface name
|
|
|
|
* @param {Object} def interface definition
|
|
|
|
*
|
2011-12-13 21:19:14 -05:00
|
|
|
* @return {Function} new named interface
|
2011-03-04 23:35:28 -05:00
|
|
|
*/
|
|
|
|
function createNamedInterface( name, def )
|
|
|
|
{
|
2011-03-04 23:43:30 -05:00
|
|
|
// 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:57:21 -05:00
|
|
|
"Expecting two arguments for definition of named Interface '" +
|
|
|
|
name + "'; " + arguments.length + " given."
|
2011-03-04 23:43:30 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the definition must be an object
|
|
|
|
if ( typeof def !== 'object' )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
2011-03-05 12:57:21 -05:00
|
|
|
"Unexpected value for definition of named Interface '" +
|
|
|
|
name + "'; object expected"
|
2011-03-04 23:43:30 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:35:28 -05:00
|
|
|
// add the name to the definition
|
|
|
|
def.__name = name;
|
|
|
|
|
|
|
|
return extend( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
var extend = ( function( extending )
|
2010-12-01 19:27:40 -05:00
|
|
|
{
|
2010-12-29 22:40:23 -05:00
|
|
|
return function extend()
|
|
|
|
{
|
|
|
|
// ensure we'll be permitted to instantiate interfaces for the base
|
|
|
|
extending = true;
|
2010-12-01 19:27:40 -05:00
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
|
|
|
props = args.pop() || {},
|
|
|
|
base = args.pop() || Interface,
|
2011-01-24 23:35:45 -05:00
|
|
|
prototype = new base(),
|
2011-03-04 00:19:02 -05:00
|
|
|
iname = '',
|
2011-01-24 23:35:45 -05:00
|
|
|
|
|
|
|
members = member_builder.initMembers(
|
|
|
|
prototype, prototype, prototype
|
|
|
|
)
|
|
|
|
;
|
2010-12-28 21:56:55 -05:00
|
|
|
|
2011-03-04 00:19:02 -05:00
|
|
|
// grab the name, if one was provided
|
|
|
|
if ( iname = props.__name )
|
|
|
|
{
|
|
|
|
// we no longer need it
|
|
|
|
delete props.__name;
|
|
|
|
}
|
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
// sanity check
|
|
|
|
inheritCheck( prototype );
|
2010-12-01 23:01:20 -05:00
|
|
|
|
2011-03-05 21:46:44 -05:00
|
|
|
var new_interface = createInterface( iname );
|
2010-12-29 22:40:23 -05:00
|
|
|
|
2011-11-19 22:17:59 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
util.propParse( props, {
|
|
|
|
assumeAbstract: true,
|
|
|
|
|
|
|
|
property: function()
|
2011-03-19 00:58:42 -04:00
|
|
|
{
|
2011-11-19 22:17:59 -05:00
|
|
|
// should never get to this point because of assumeAbstract
|
|
|
|
throw TypeError( 'Unexpected internal error' );
|
|
|
|
},
|
|
|
|
|
|
|
|
getset: function()
|
|
|
|
{
|
|
|
|
// should never get to this point because of assumeAbstract
|
|
|
|
throw TypeError( 'Unexpected internal error' );
|
|
|
|
},
|
|
|
|
|
|
|
|
method: function( name, value, is_abstract, keywords )
|
|
|
|
{
|
|
|
|
// all members must be public
|
|
|
|
if ( keywords[ 'protected' ] || keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
iname + " member " + name + " must be public"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
member_builder.buildMethod(
|
|
|
|
members, null, name, value, keywords
|
2011-03-19 00:58:42 -04:00
|
|
|
);
|
2011-11-19 22:17:59 -05:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
// alter the message to include our name
|
|
|
|
e.message = "Failed to define interface " +
|
|
|
|
( ( iname ) ? iname : '(anonymous)' ) + ": " + e.message
|
|
|
|
;
|
2011-03-19 00:58:42 -04:00
|
|
|
|
2011-11-19 22:17:59 -05:00
|
|
|
// re-throw
|
|
|
|
throw e;
|
|
|
|
}
|
2010-12-29 22:40:23 -05:00
|
|
|
|
|
|
|
attachExtend( new_interface );
|
2011-03-04 00:19:02 -05:00
|
|
|
attachStringMethod( new_interface, iname );
|
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
new_interface.prototype = prototype;
|
|
|
|
new_interface.constructor = new_interface;
|
|
|
|
|
|
|
|
// freeze the interface (preventing additions), if supported
|
|
|
|
util.freeze( new_interface );
|
2010-12-01 23:01:20 -05:00
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
// we're done; let's not allow interfaces to be instantiated anymore
|
|
|
|
extending = false;
|
2010-12-01 19:27:40 -05:00
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
return new_interface;
|
|
|
|
};
|
2010-12-01 19:27:40 -05:00
|
|
|
|
2010-12-29 22:40:23 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new interface constructor function
|
|
|
|
*
|
2011-03-05 21:46:44 -05:00
|
|
|
* @param {string=} iname interface name
|
|
|
|
*
|
2010-12-29 22:40:23 -05:00
|
|
|
* @return {function()}
|
|
|
|
*/
|
2011-03-05 21:46:44 -05:00
|
|
|
function createInterface( iname )
|
2010-12-29 22:40:23 -05:00
|
|
|
{
|
|
|
|
return function()
|
|
|
|
{
|
|
|
|
// allows us to extend the interface without throwing an exception
|
|
|
|
// (since the prototype requires an instance)
|
|
|
|
if ( !extending )
|
|
|
|
{
|
|
|
|
// only called if someone tries to create a new instance of an
|
|
|
|
// interface
|
2011-03-05 21:46:44 -05:00
|
|
|
throw Error(
|
2013-04-13 12:34:49 -04:00
|
|
|
"Interface " + ( ( iname ) ? ( iname + ' ' ) : '' ) +
|
2011-03-05 21:46:44 -05:00
|
|
|
" cannot be instantiated"
|
|
|
|
);
|
2010-12-29 22:40:23 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} )( false );
|
2010-12-01 19:27:40 -05:00
|
|
|
|
2010-12-01 19:38:30 -05:00
|
|
|
|
2010-12-28 21:56:55 -05:00
|
|
|
/**
|
|
|
|
* Assures that the parent object is a valid object to inherit from
|
|
|
|
*
|
|
|
|
* This method allows inheriting from any object (note that it will likely cause
|
|
|
|
* errors if not an interface), but will place restrictions on objects like
|
|
|
|
* Classes that do not make sense to inherit from. This will provide a more
|
|
|
|
* friendly error, with suggestions on how to resolve the issue, rather than a
|
|
|
|
* cryptic error resulting from inheritance problems.
|
|
|
|
*
|
|
|
|
* This method will throw an exception if there is a violation.
|
|
|
|
*
|
|
|
|
* @param {Object} prototype prototype to check for inheritance flaws
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function inheritCheck( prototype )
|
|
|
|
{
|
|
|
|
// if we're inheriting from another interface, then we're good
|
2011-11-19 22:05:18 -05:00
|
|
|
if ( !( prototype instanceof Interface ) )
|
2010-12-28 21:56:55 -05:00
|
|
|
{
|
2011-11-19 22:05:18 -05:00
|
|
|
throw new TypeError( "Interfaces may only extend other interfaces" );
|
2010-12-28 21:56:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 23:27:31 -05:00
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (interface)
|
|
|
|
*
|
|
|
|
* @param {Function} func function (interface) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachExtend( func )
|
2010-12-01 23:27:31 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Shorthand for extending interfaces
|
|
|
|
*
|
|
|
|
* This method can be invoked on the object, rather than having to call
|
|
|
|
* Interface.extend( this ).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to add to extended interface
|
|
|
|
*
|
|
|
|
* @return {Object} extended interface
|
|
|
|
*/
|
|
|
|
util.defineSecureProp( func, 'extend', function( props )
|
|
|
|
{
|
|
|
|
return extend( this, props );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-04 00:19:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides more sane/useful output when interface is converted to a string
|
|
|
|
*
|
|
|
|
* @param {Object} func interface
|
|
|
|
* @param {string=} iname interface name
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachStringMethod( func, iname )
|
|
|
|
{
|
|
|
|
func.toString = ( iname )
|
|
|
|
? function() { return '[object Interface <' + iname + '>]'; }
|
|
|
|
: function() { return '[object Interface]'; }
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|