2011-10-22 01:00:17 -04:00
|
|
|
/**
|
|
|
|
* Validation rules for members
|
|
|
|
*
|
2011-12-23 00:09:01 -05:00
|
|
|
* Copyright (C) 2010,2011 Mike Gerwitz
|
2011-10-22 01:00:17 -04:00
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-11-05 11:56:28 -04:00
|
|
|
module.exports = exports = function MemberBuilderValidator( warn_handler )
|
2011-10-22 01:00:17 -04:00
|
|
|
{
|
|
|
|
// permit omitting 'new' keyword
|
|
|
|
if ( !( this instanceof module.exports ) )
|
|
|
|
{
|
2011-11-05 11:56:28 -04:00
|
|
|
return new module.exports( warn_handler );
|
2011-10-22 01:00:17 -04:00
|
|
|
}
|
2011-11-05 11:56:28 -04:00
|
|
|
|
|
|
|
this._warningHandler = warn_handler || function() {};
|
2011-10-22 01:00:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a method declaration, ensuring that keywords are valid, overrides
|
|
|
|
* make sense, etc.
|
|
|
|
*
|
|
|
|
* Throws exception on validation failure
|
|
|
|
*
|
|
|
|
* @param {string} name method name
|
|
|
|
* @param {*} value method value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
|
|
|
* @param {Object} prev_data data of member being overridden
|
|
|
|
* @param {Object} prev_keywords keywords of member being overridden
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.prototype.validateMethod = function(
|
|
|
|
name, value, keywords, prev_data, prev_keywords
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var prev = ( prev_data ) ? prev_data.member : null;
|
|
|
|
|
|
|
|
if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
// do not permit private abstract methods (doesn't make sense, since
|
|
|
|
// they cannot be inherited/overridden)
|
|
|
|
if ( keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Method '" + name + "' cannot be both private and abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// const doesn't make sense for methods; they're always immutable
|
|
|
|
if ( keywords[ 'const' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare method '" + name + "' as constant; keyword is " +
|
|
|
|
"redundant"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// virtual static does not make sense, as static methods cannot be
|
|
|
|
// overridden
|
|
|
|
if ( keywords[ 'virtual' ] && ( keywords[ 'static' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare static method '" + name + "' as virtual"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not allow overriding getters/setters
|
|
|
|
if ( prev_data && ( prev_data.get || prev_data.set ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override getter/setter '" + name + "' with method"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if ( keywords[ 'proxy' ] )
|
|
|
|
{
|
|
|
|
// proxies are expected to provide the name of the destination object
|
|
|
|
if ( typeof value !== 'string' )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare proxy method '" + name + "'; string value " +
|
|
|
|
"expected"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
// proxies are always concrete
|
|
|
|
throw TypeError(
|
|
|
|
"Proxy method '" + name + "' cannot be abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 01:00:17 -04:00
|
|
|
// search for any previous instances of this member
|
|
|
|
if ( prev )
|
|
|
|
{
|
2011-12-03 00:30:22 -05:00
|
|
|
// perform this check first, as it will make more sense than those that
|
|
|
|
// follow, should this condition be satisfied
|
|
|
|
if ( prev_keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Private member name '" + name + "' conflicts with supertype"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-22 01:00:17 -04:00
|
|
|
// disallow overriding properties with methods
|
|
|
|
if ( !( typeof prev === 'function' ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override property '" + name + "' with method"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disallow overriding non-virtual methods
|
|
|
|
if ( keywords[ 'override' ] && !( prev_keywords[ 'virtual' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override non-virtual method '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not allow overriding concrete methods with abstract
|
|
|
|
if ( keywords[ 'abstract' ] && !( prev_keywords[ 'abstract' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override concrete method '" + name + "' with " +
|
|
|
|
"abstract method"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure parameter list is at least the length of its supertype
|
|
|
|
if ( ( value.__length || value.length )
|
|
|
|
< ( prev.__length || prev.length )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Declaration of method '" + name + "' must be compatible " +
|
|
|
|
"with that of its supertype"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not permit visibility deescalation
|
|
|
|
if ( this._getVisibilityValue( prev_keywords ) <
|
|
|
|
this._getVisibilityValue( keywords )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot de-escalate visibility of method '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-22 13:57:17 -04:00
|
|
|
// Disallow overriding method without override keyword (unless parent
|
|
|
|
// method is abstract). In the future, this will provide a warning to
|
|
|
|
// default to method hiding.
|
|
|
|
if ( !( keywords[ 'override' ] || prev_keywords[ 'abstract' ] ) )
|
2011-10-22 01:00:17 -04:00
|
|
|
{
|
2011-10-22 13:57:17 -04:00
|
|
|
throw TypeError(
|
|
|
|
"Attempting to override method '" + name +
|
|
|
|
"' without 'override' keyword"
|
|
|
|
);
|
2011-10-22 01:00:17 -04:00
|
|
|
}
|
|
|
|
}
|
2011-12-22 23:40:39 -05:00
|
|
|
else if ( keywords[ 'override' ] )
|
2011-11-05 11:56:28 -04:00
|
|
|
{
|
|
|
|
// using the override keyword without a super method may indicate a bug,
|
|
|
|
// but it shouldn't stop the class definition (it doesn't adversely
|
|
|
|
// affect the functionality of the class, unless of course the method
|
|
|
|
// attempts to reference a supertype)
|
|
|
|
this._warningHandler( Error(
|
|
|
|
"Method '" + name +
|
|
|
|
"' using 'override' keyword without super method"
|
|
|
|
) );
|
|
|
|
}
|
2011-10-22 01:00:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-23 00:43:08 -04:00
|
|
|
/**
|
|
|
|
* Validates a property declaration, ensuring that keywords are valid, overrides
|
|
|
|
* make sense, etc.
|
|
|
|
*
|
|
|
|
* Throws exception on validation failure
|
|
|
|
*
|
|
|
|
* @param {string} name method name
|
|
|
|
* @param {*} value method value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
|
|
|
* @param {Object} prev_data data of member being overridden
|
|
|
|
* @param {Object} prev_keywords keywords of member being overridden
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.prototype.validateProperty = function(
|
|
|
|
name, value, keywords, prev_data, prev_keywords
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var prev = ( prev_data ) ? prev_data.member : null;
|
|
|
|
|
2011-12-03 00:30:22 -05:00
|
|
|
// do not permit visibility de-escalation
|
|
|
|
if ( prev )
|
2011-10-23 00:43:08 -04:00
|
|
|
{
|
2011-12-03 00:30:22 -05:00
|
|
|
// perform this check first, as it will make more sense than those that
|
|
|
|
// follow, should this condition be satisfied
|
|
|
|
if ( prev_keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Private member name '" + name + "' conflicts with supertype"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disallow overriding methods with properties
|
|
|
|
if ( typeof prev === 'function' )
|
|
|
|
{
|
|
|
|
throw new TypeError(
|
|
|
|
"Cannot override method '" + name + "' with property"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this._getVisibilityValue( prev_keywords )
|
|
|
|
< this._getVisibilityValue( keywords )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot de-escalate visibility of property '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
2011-10-23 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// do not allow overriding getters/setters
|
|
|
|
if ( prev_data && ( prev_data.get || prev_data.set ) )
|
2011-10-23 01:14:13 -04:00
|
|
|
{
|
2011-10-23 00:43:08 -04:00
|
|
|
throw TypeError(
|
|
|
|
"Cannot override getter/setter '" + name + "' with property"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// abstract properties do not make sense
|
|
|
|
if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Property '" + name + "' cannot be declared as abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-23 01:14:13 -04:00
|
|
|
// constants are static
|
2011-10-23 00:43:08 -04:00
|
|
|
if ( keywords[ 'static' ] && keywords[ 'const' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Static keyword cannot be used with const for property '" +
|
|
|
|
name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// properties are inherently virtual
|
|
|
|
if ( keywords['virtual'] )
|
|
|
|
{
|
|
|
|
throw TypeError( "Cannot declare property '" + name + "' as virtual" );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-28 00:08:22 -04:00
|
|
|
/**
|
|
|
|
* Performs common validations on getters/setters
|
|
|
|
*
|
|
|
|
* If a problem is found, an exception will be thrown.
|
|
|
|
*
|
|
|
|
* @param {string} name getter/setter name
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.prototype.validateGetterSetter = function(
|
2011-11-05 09:40:56 -04:00
|
|
|
name, value, keywords, prev_data, prev_keywords
|
2011-10-28 00:08:22 -04:00
|
|
|
)
|
|
|
|
{
|
2011-10-30 12:06:09 -04:00
|
|
|
var prev = ( prev_data ) ? prev_data.member : null,
|
2011-11-02 23:28:23 -04:00
|
|
|
prev_gs = ( ( prev_data && ( prev_data.get || prev_data.set ) )
|
2011-11-02 23:23:13 -04:00
|
|
|
? true
|
|
|
|
: false
|
|
|
|
)
|
2011-10-28 00:08:22 -04:00
|
|
|
;
|
|
|
|
|
2011-12-22 22:05:47 -05:00
|
|
|
// abstract getters/setters are not yet supported
|
|
|
|
if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare getter/setter '" + name + "' as abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:48:17 -05:00
|
|
|
// for const getters/setters, omit the setter
|
|
|
|
if ( keywords[ 'const' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare const getter/setter '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-22 23:25:35 -05:00
|
|
|
// virtual static does not make sense, as static methods cannot be
|
|
|
|
// overridden
|
|
|
|
if ( keywords[ 'virtual' ] && ( keywords[ 'static' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot declare static method '" + name + "' as virtual"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-30 12:06:09 -04:00
|
|
|
if ( prev || prev_gs )
|
2011-10-28 00:08:22 -04:00
|
|
|
{
|
2011-12-03 00:30:22 -05:00
|
|
|
// perform this check first, as it will make more sense than those that
|
|
|
|
// follow, should this condition be satisfied
|
|
|
|
if ( prev_keywords && prev_keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Private member name '" + name + "' conflicts with supertype"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-28 00:08:22 -04:00
|
|
|
// To speed up the system we'll simply check for a getter/setter, rather
|
|
|
|
// than checking separately for methods/properties. This is at the
|
|
|
|
// expense of more detailed error messages. They'll live.
|
2011-10-30 12:06:09 -04:00
|
|
|
if ( !( prev_gs ) )
|
2011-10-28 00:08:22 -04:00
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override method or property '" + name +
|
|
|
|
"' with getter/setter"
|
|
|
|
);
|
|
|
|
}
|
2011-10-30 12:06:09 -04:00
|
|
|
|
'this' now properly binds to the private member object of the instance for getters/setters
Getters/setters did not get much attention during the initial development of
ease.js, simply because there was such a strong focus on pre-ES5
compatibility---ease.js was created for a project that strongly required it.
Given that, getters/setters were not used, since those are ES5 features. As
such, I find that two things have happened:
1. There was little incentive to provide a proper implementation; even though
I noticed the issues during the initial development, they were left
unresolved and were then forgotten about as the project lay dormant for a
while.
2. The project was dormant because it was working as intended (sure, there
are still things on the TODO-list feature-wise). Since getters/setters were
unused in the project for which ease.js was created, the bug was never
found and so never addressed.
That said, I now am using getters/setters in a project with ease.js and noticed
a very odd bug that could not be explained by that project's implementation.
Sure enough, it was an ease.js issue and this commit resolves it.
Now, there is more to be said about this commit. Mainly, it should be noted that
MemberBuilder.buildGetterSetter, when compared with its method counterpart
(buildMethod) is incomplete---it does not properly address overrides, the
abstract keyword, proxies or the possibility of method hiding. This is certainly
something that I will get to, but I want to get this fix out as soon as I can.
Since overriding ES5 getters/setters (rather than explicit methods) is more
likely to be a rarity, and since a partial fix is better than no fix, this will
likely be tagged immediately and a further fix will follow in the (hopefully
near) future.
(This is an interesting example of how glaring bugs manage to slip through the
cracks, even when the developer is initially aware of them.)
2013-01-19 20:54:30 -05:00
|
|
|
if ( !( prev_keywords && prev_keywords[ 'virtual' ] ) )
|
2011-12-22 23:10:01 -05:00
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override non-virtual getter/setter '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !( keywords[ 'override' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Attempting to override getter/setter '" + name +
|
|
|
|
"' without 'override' keyword"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-30 12:06:09 -04:00
|
|
|
// do not permit visibility de-escalation
|
2011-11-05 09:40:56 -04:00
|
|
|
if ( this._getVisibilityValue( prev_keywords || {} )
|
2011-10-30 12:06:09 -04:00
|
|
|
< this._getVisibilityValue( keywords )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot de-escalate visibility of getter/setter '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
2011-10-28 00:08:22 -04:00
|
|
|
}
|
2011-12-22 23:32:45 -05:00
|
|
|
else if ( keywords[ 'override' ] )
|
|
|
|
{
|
|
|
|
// using the override keyword without a super method may indicate a bug
|
|
|
|
// in the user's code
|
|
|
|
this._warningHandler( Error(
|
|
|
|
"Getter/setter '" + name +
|
|
|
|
"' using 'override' keyword without super getter/setter"
|
|
|
|
) );
|
|
|
|
}
|
2011-10-28 00:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-22 01:00:17 -04:00
|
|
|
/**
|
|
|
|
* Return the visibility level as a numeric value, where 0 is public and 2 is
|
|
|
|
* private
|
|
|
|
*
|
|
|
|
* @param {Object} keywords keywords to scan for visibility level
|
|
|
|
*
|
|
|
|
* @return {number} visibility level as a numeric value
|
|
|
|
*/
|
|
|
|
exports.prototype._getVisibilityValue = function( keywords )
|
|
|
|
{
|
|
|
|
if ( keywords[ 'protected' ] )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// default is public
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|