2010-12-27 20:56:36 -05:00
|
|
|
/**
|
|
|
|
* Property keyword parser module
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2010, 2011, 2012, 2013 Mike Gerwitz
|
2010-12-27 20:56:36 -05:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2010-12-27 20:56:36 -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-27 20:56:36 -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-27 20:56:36 -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-27 20:56:36 -05:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
2011-05-22 22:11:57 -04:00
|
|
|
/**
|
|
|
|
* Known (permitted) keywords
|
|
|
|
* @type {Object.<string,boolean>}
|
|
|
|
*/
|
|
|
|
var _keywords = {
|
|
|
|
'public': true,
|
|
|
|
'protected': true,
|
|
|
|
'private': true,
|
|
|
|
'static': true,
|
|
|
|
'abstract': true,
|
|
|
|
'const': true,
|
2011-06-08 01:11:53 -04:00
|
|
|
'virtual': true,
|
2011-08-04 00:32:10 -04:00
|
|
|
'override': true,
|
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
|
|
|
'proxy': true,
|
2011-05-22 22:11:57 -04:00
|
|
|
};
|
|
|
|
|
2010-12-27 20:56:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses property keywords
|
|
|
|
*
|
|
|
|
* @param {string} prop property string, which may contain keywords
|
|
|
|
*
|
|
|
|
* @return {{name: string, keywords: Object.<string, boolean>}}
|
|
|
|
*/
|
2010-12-27 23:12:37 -05:00
|
|
|
exports.parseKeywords = function ( prop )
|
2010-12-27 20:56:36 -05:00
|
|
|
{
|
|
|
|
var name = prop,
|
|
|
|
keywords = [],
|
|
|
|
keyword_obj = {};
|
|
|
|
|
|
|
|
prop = ''+( prop );
|
|
|
|
|
2010-12-27 23:04:50 -05:00
|
|
|
// only perform parsing if the string contains a space
|
|
|
|
if ( / /.test( prop ) )
|
2010-12-27 20:56:36 -05:00
|
|
|
{
|
2010-12-27 23:04:50 -05:00
|
|
|
// the keywords are all words, except for the last, which is the
|
|
|
|
// property name
|
2011-01-18 19:33:33 -05:00
|
|
|
keywords = prop.split( /\s+/ );
|
2010-12-27 23:04:50 -05:00
|
|
|
name = keywords.pop();
|
2010-12-27 20:56:36 -05:00
|
|
|
|
2011-05-22 22:11:57 -04:00
|
|
|
var i = keywords.length,
|
|
|
|
keyword = '';
|
|
|
|
|
2010-12-27 23:04:50 -05:00
|
|
|
while ( i-- )
|
|
|
|
{
|
2011-05-22 22:11:57 -04:00
|
|
|
keyword = keywords[ i ];
|
|
|
|
|
|
|
|
// ensure the keyword is recognized
|
|
|
|
if ( !_keywords[ keyword ] )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Unexpected keyword for '" + name + "': " + keyword
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
keyword_obj[ keyword ] = true;
|
2010-12-27 20:56:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
keywords: keyword_obj,
|
|
|
|
};
|
|
|
|
}
|