2011-01-18 23:47:58 -05:00
|
|
|
/**
|
|
|
|
* Handles building members (properties, methods)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* @package core
|
|
|
|
*/
|
|
|
|
|
2011-03-29 22:02:42 -04:00
|
|
|
var util = require( __dirname + '/util' ),
|
|
|
|
|
2011-07-06 19:34:35 -04:00
|
|
|
Warning = require( __dirname + '/warn' ).Warning,
|
|
|
|
|
2011-03-18 23:42:07 -04:00
|
|
|
fallback = util.definePropertyFallback(),
|
2011-01-21 23:16:20 -05:00
|
|
|
visibility = [ 'public', 'protected', 'private' ];
|
2011-01-21 21:43:18 -05:00
|
|
|
|
2011-01-18 23:47:58 -05:00
|
|
|
|
2011-01-20 23:53:00 -05:00
|
|
|
/**
|
|
|
|
* Initializes member object
|
|
|
|
*
|
|
|
|
* The member object contains members for each level of visibility (public,
|
|
|
|
* protected and private).
|
|
|
|
*
|
2011-01-21 00:09:26 -05:00
|
|
|
* @param {Object} mpublic default public members
|
|
|
|
* @param {Object} mprotected default protected members
|
|
|
|
* @param {Object} mprivate default private members
|
|
|
|
*
|
2011-01-20 23:53:00 -05:00
|
|
|
* @return {{public: Object, protected: Object, private: Object}}
|
|
|
|
*/
|
2011-01-20 23:56:39 -05:00
|
|
|
exports.initMembers = function( mpublic, mprotected, mprivate )
|
2011-01-20 23:53:00 -05:00
|
|
|
{
|
|
|
|
return {
|
2011-01-20 23:56:39 -05:00
|
|
|
'public': mpublic || {},
|
|
|
|
'protected': mprotected || {},
|
|
|
|
'private': mprivate || {},
|
2011-01-20 23:53:00 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-21 20:54:55 -05:00
|
|
|
/**
|
|
|
|
* Copies a method to the appropriate member prototype, depending on
|
|
|
|
* visibility, and assigns necessary metadata from keywords
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object} meta metadata container
|
|
|
|
* @param {string} name property name
|
|
|
|
* @param {*} value property value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
2011-03-02 20:43:24 -05:00
|
|
|
|
|
|
|
* @param {Object=} instCallback function to call in order to retrieve
|
|
|
|
* object to bind 'this' keyword to
|
2011-03-13 04:51:00 -04:00
|
|
|
* @param {number} cid class id
|
2011-01-21 20:54:55 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-02 20:43:24 -05:00
|
|
|
exports.buildMethod = function(
|
2011-03-18 23:42:07 -04:00
|
|
|
members, meta, name, value, keywords, instCallback, cid, base
|
2011-03-02 20:43:24 -05:00
|
|
|
)
|
2011-01-21 20:54:55 -05:00
|
|
|
{
|
2011-03-18 23:42:07 -04:00
|
|
|
// TODO: We can improve performance by not scanning each one individually
|
|
|
|
// every time this method is called
|
2011-08-04 00:32:10 -04:00
|
|
|
var prev_data = scanMembers( members, name, base ),
|
|
|
|
prev = ( prev_data ) ? prev_data.member : null,
|
|
|
|
prev_keywords = ( prev && prev.___$$keywords$$ ),
|
|
|
|
dest = getMemberVisibility( members, keywords );
|
2011-05-22 11:25:03 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
// ensure that the declaration is valid (keywords make sense, argument
|
|
|
|
// length, etc)
|
2011-08-04 00:32:10 -04:00
|
|
|
validateMethod( keywords, prev_data, prev_keywords, value, name );
|
2011-05-22 11:25:03 -04:00
|
|
|
|
|
|
|
// we might be overriding an existing method
|
|
|
|
if ( prev )
|
|
|
|
{
|
2011-08-04 00:32:10 -04:00
|
|
|
// by default, perform method hiding, even if the keyword was not
|
|
|
|
// provided (the keyword simply suppresses the warning)
|
|
|
|
var operation = hideMethod;
|
|
|
|
|
|
|
|
// TODO: warning if no super method when override keyword provided
|
|
|
|
if ( keywords[ 'override' ] || prev_keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
// override the method
|
|
|
|
operation = overrideMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest[ name ] = operation( prev, value, instCallback, cid );
|
2011-05-22 11:25:03 -04:00
|
|
|
}
|
|
|
|
else if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
// we do not want to wrap abstract methods, since they are not callable
|
|
|
|
dest[ name ] = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are not overriding the method, so simply copy it over, wrapping it
|
|
|
|
// to ensure privileged calls will work properly
|
|
|
|
dest[ name ] = overrideMethod( value, null, instCallback, cid );
|
|
|
|
}
|
|
|
|
|
|
|
|
// store keywords for later reference (needed for pre-ES5 fallback)
|
|
|
|
dest[ name ].___$$keywords$$ = keywords;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a method declaration, ensuring that keywords are valid, overrides
|
|
|
|
* make sense, etc.
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
2011-08-04 00:32:10 -04:00
|
|
|
* @param {Object} prev_data data of member being overridden
|
|
|
|
* @param {Object} prev_keywords keywords of member being overridden
|
|
|
|
* @param {*} value property value
|
|
|
|
* @param {string} name property name
|
2011-05-22 11:25:03 -04:00
|
|
|
*/
|
2011-08-04 00:32:10 -04:00
|
|
|
function validateMethod( keywords, prev_data, prev_keywords, value, name )
|
2011-05-22 11:25:03 -04:00
|
|
|
{
|
2011-08-04 00:32:10 -04:00
|
|
|
var prev = ( prev_data ) ? prev_data.member : null;
|
2011-01-21 21:53:31 -05:00
|
|
|
|
2011-05-22 11:11:18 -04:00
|
|
|
if ( keywords[ 'abstract' ] )
|
2011-03-29 23:39:49 -04:00
|
|
|
{
|
2011-05-22 11:11:18 -04:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
}
|
2011-03-29 23:39:49 -04:00
|
|
|
}
|
|
|
|
|
2011-05-19 07:54:51 -04:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:21:26 -04: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-06-30 23:00:13 -04:00
|
|
|
// do not allow overriding getters/setters
|
|
|
|
if ( prev_data && ( prev_data.get || prev_data.set ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override getter/setter '" + name + "' with method"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-01-27 22:35:40 -05:00
|
|
|
// search for any previous instances of this member
|
2011-03-18 23:42:07 -04:00
|
|
|
if ( prev )
|
2011-01-21 21:53:31 -05:00
|
|
|
{
|
2011-01-24 23:30:32 -05:00
|
|
|
// disallow overriding properties with methods
|
|
|
|
if ( !( prev instanceof Function ) )
|
|
|
|
{
|
2011-01-24 23:57:19 -05:00
|
|
|
throw TypeError(
|
2011-01-24 23:30:32 -05:00
|
|
|
"Cannot override property '" + name + "' with method"
|
|
|
|
);
|
|
|
|
}
|
2011-01-21 21:53:31 -05:00
|
|
|
|
2011-08-04 00:44:20 -04:00
|
|
|
// disallow overriding non-virtual methods
|
|
|
|
if ( keywords[ 'override' ] && !( prev_keywords[ 'virtual' ] ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override non-virtual method '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-01-24 23:52:06 -05:00
|
|
|
// do not allow overriding concrete methods with abstract
|
|
|
|
if ( keywords[ 'abstract' ] && !( util.isAbstractMethod( prev ) ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override concrete method '" + name + "' with " +
|
|
|
|
"abstract method"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-01-24 23:30:32 -05:00
|
|
|
// ensure parameter list is at least the length of its supertype
|
|
|
|
if ( ( value.__length || value.length )
|
|
|
|
< ( prev.__length || prev.length )
|
|
|
|
)
|
|
|
|
{
|
2011-01-24 23:57:19 -05:00
|
|
|
throw TypeError(
|
2011-01-24 23:30:32 -05:00
|
|
|
"Declaration of method '" + name + "' must be compatiable " +
|
|
|
|
"with that of its supertype"
|
|
|
|
);
|
|
|
|
}
|
2011-03-18 23:42:07 -04:00
|
|
|
|
2011-07-06 19:34:35 -04:00
|
|
|
// do not permit visibility deescalation
|
2011-03-18 23:42:07 -04:00
|
|
|
if ( prev_data.visibility < getVisibilityValue( keywords ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot de-escalate visibility of method '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
2011-07-06 19:34:35 -04:00
|
|
|
|
2011-08-03 22:40:55 -04:00
|
|
|
// if redefining a method that has already been implemented in the
|
|
|
|
// supertype, the default behavior is to "hide" the method of the
|
|
|
|
// supertype, unless otherwise specified
|
2011-07-06 19:34:35 -04:00
|
|
|
//
|
|
|
|
// IMPORTANT: do this last, to ensure we throw errors before warnings
|
2011-08-03 22:40:55 -04:00
|
|
|
if ( !( keywords[ 'new' ] || keywords[ 'override' ] ) )
|
2011-07-06 19:34:35 -04:00
|
|
|
{
|
2011-08-04 00:32:10 -04:00
|
|
|
if ( !( prev_keywords[ 'abstract' ] ) )
|
|
|
|
{
|
|
|
|
throw Warning( Error(
|
|
|
|
"Hiding method '" + name + "'; " +
|
|
|
|
"use 'new' if intended, or 'override' to override instead"
|
|
|
|
) );
|
|
|
|
}
|
2011-07-06 19:34:35 -04:00
|
|
|
}
|
2011-01-21 22:19:22 -05:00
|
|
|
}
|
2011-05-22 11:25:03 -04:00
|
|
|
}
|
2011-01-21 20:54:55 -05:00
|
|
|
|
|
|
|
|
2011-01-18 23:47:58 -05:00
|
|
|
/**
|
|
|
|
* Copies a property to the appropriate member prototype, depending on
|
|
|
|
* visibility, and assigns necessary metadata from keywords
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object} meta metadata container
|
|
|
|
* @param {string} name property name
|
|
|
|
* @param {*} value property value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
2011-01-27 22:35:40 -05:00
|
|
|
|
2011-03-18 23:42:07 -04:00
|
|
|
* @param {Object=} base optional base object to scan
|
2011-01-18 23:47:58 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-18 23:42:07 -04:00
|
|
|
exports.buildProp = function( members, meta, name, value, keywords, base )
|
2011-01-18 23:47:58 -05:00
|
|
|
{
|
2011-03-18 23:42:07 -04:00
|
|
|
// TODO: We can improve performance by not scanning each one individually
|
|
|
|
// every time this method is called
|
|
|
|
var prev_data = scanMembers( members, name, base ),
|
|
|
|
prev = ( prev_data ) ? prev_data.member : null;
|
|
|
|
|
2011-01-21 21:43:18 -05:00
|
|
|
// disallow overriding methods with properties
|
2011-03-18 23:42:07 -04:00
|
|
|
if ( prev instanceof Function )
|
2011-01-21 21:43:18 -05:00
|
|
|
{
|
|
|
|
throw new TypeError(
|
|
|
|
"Cannot override method '" + name + "' with property"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
// do not allow overriding getters/setters
|
|
|
|
if ( prev_data && ( prev_data.get || prev_data.set ) )
|
2011-07-06 19:34:35 -04:00
|
|
|
{
|
2011-06-30 23:00:13 -04:00
|
|
|
throw TypeError(
|
|
|
|
"Cannot override getter/setter '" + name + "' with property"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:42:07 -04:00
|
|
|
// do not permit visibility de-escalation
|
|
|
|
if ( prev && ( prev_data.visibility < getVisibilityValue( keywords ) ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot de-escalate visibility of property '" + name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-29 23:48:17 -04:00
|
|
|
// abstract properties do not make sense
|
|
|
|
if ( keywords[ 'abstract' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Property '" + name + "' cannot be declared as abstract"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-05-19 18:30:55 -04:00
|
|
|
if ( keywords[ 'static' ] && keywords[ 'const' ] )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Static keyword cannot be used with const for property '" +
|
|
|
|
name + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-09 19:46:13 -04:00
|
|
|
// properties are inherently virtual
|
|
|
|
if ( keywords['virtual'] )
|
|
|
|
{
|
|
|
|
throw TypeError( "Cannot declare property '" + name + "' as virtual" );
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:48:47 -04:00
|
|
|
getMemberVisibility( members, keywords )[ name ] = [ value, keywords ];
|
2011-01-20 22:11:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-20 23:50:53 -05:00
|
|
|
/**
|
|
|
|
* Copies a getter to the appropriate member prototype, depending on
|
|
|
|
* visibility, and assigns necessary metadata from keywords
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object} meta metadata container
|
|
|
|
* @param {string} name getter name
|
|
|
|
* @param {*} value getter value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
2011-06-30 23:00:13 -04:00
|
|
|
* @param {Object=} base optional base object to scan
|
|
|
|
*
|
2011-01-20 23:50:53 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-06-30 23:00:13 -04:00
|
|
|
exports.buildGetter = function( members, meta, name, value, keywords, base )
|
2011-01-20 23:50:53 -05:00
|
|
|
{
|
2011-06-30 23:00:13 -04:00
|
|
|
validateGetterSetter( members, keywords, name, base );
|
|
|
|
|
2011-03-07 22:44:47 -05:00
|
|
|
Object.defineProperty(
|
|
|
|
getMemberVisibility( members, keywords ),
|
|
|
|
name,
|
|
|
|
{
|
|
|
|
get: value,
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
// otherwise we can't add a setter to this
|
|
|
|
configurable: true,
|
|
|
|
}
|
|
|
|
);
|
2011-01-20 23:50:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a setter to the appropriate member prototype, depending on
|
|
|
|
* visibility, and assigns necessary metadata from keywords
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object} meta metadata container
|
|
|
|
* @param {string} name setter name
|
|
|
|
* @param {*} value setter value
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
2011-06-30 23:00:13 -04:00
|
|
|
* @param {Object=} base optional base object to scan
|
|
|
|
*
|
2011-01-20 23:50:53 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-06-30 23:00:13 -04:00
|
|
|
exports.buildSetter = function( members, meta, name, value, keywords, base )
|
2011-01-20 23:50:53 -05:00
|
|
|
{
|
2011-06-30 23:00:13 -04:00
|
|
|
validateGetterSetter( members, keywords, name, base );
|
|
|
|
|
2011-03-07 22:44:47 -05:00
|
|
|
Object.defineProperty(
|
|
|
|
getMemberVisibility( members, keywords ),
|
|
|
|
name,
|
|
|
|
{
|
|
|
|
set: value,
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
// otherwise we can't add a getter to this
|
|
|
|
configurable: true,
|
|
|
|
}
|
|
|
|
);
|
2011-01-20 23:50:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
/**
|
|
|
|
* Performs common validations on getters/setters
|
|
|
|
*
|
|
|
|
* If a problem is found, an exception will be thrown.
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
* @param {string} name getter/setter name
|
|
|
|
* @param {Object} base optional base to parse
|
|
|
|
*/
|
|
|
|
function validateGetterSetter( members, keywords, name, base )
|
|
|
|
{
|
|
|
|
var prev_data = scanMembers( members, name, base ),
|
|
|
|
prev = ( prev_data ) ? prev_data.member : null,
|
|
|
|
|
|
|
|
prev_keywords = ( prev && prev.___$$keywords$$ )
|
|
|
|
? prev.___$$keywords$$
|
|
|
|
: {}
|
|
|
|
;
|
|
|
|
|
|
|
|
if ( prev )
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
if ( !( prev_data.get || prev_data.set ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Cannot override method or property '" + name +
|
|
|
|
"' with getter/setter"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-20 22:11:36 -05:00
|
|
|
/**
|
|
|
|
* Returns member prototype to use for the requested visibility
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
|
|
|
* @param {Object.<string,boolean>} keywords parsed keywords
|
|
|
|
*
|
|
|
|
* @return {Object} reference to visibility of members argument to use
|
|
|
|
*/
|
|
|
|
function getMemberVisibility( members, keywords )
|
|
|
|
{
|
|
|
|
var viserr = function()
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Only one of public, protected or private may be used"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// there's cleaner ways of doing this, but consider it loop unrolling for
|
|
|
|
// performance
|
2011-01-20 21:51:35 -05:00
|
|
|
if ( keywords[ 'private' ] )
|
2011-01-20 21:46:49 -05:00
|
|
|
{
|
2011-01-20 22:11:36 -05:00
|
|
|
( keywords[ 'public' ] || keywords[ 'protected' ] ) && viserr();
|
|
|
|
return members[ 'private' ];
|
2011-01-20 21:46:49 -05:00
|
|
|
}
|
2011-01-20 21:51:35 -05:00
|
|
|
else if ( keywords[ 'protected' ] )
|
2011-01-20 21:48:09 -05:00
|
|
|
{
|
2011-01-20 22:11:36 -05:00
|
|
|
( keywords[ 'public' ] || keywords[ 'private' ] ) && viserr();
|
|
|
|
return members[ 'protected' ];
|
2011-01-20 21:48:09 -05:00
|
|
|
}
|
2011-01-20 21:50:52 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// public keyword is the default, so explicitly specifying it is only
|
|
|
|
// for clarity
|
2011-01-20 22:11:36 -05:00
|
|
|
( keywords[ 'private' ] || keywords[ 'protected' ] ) && viserr();
|
|
|
|
return members[ 'public' ];
|
2011-01-20 21:50:52 -05:00
|
|
|
}
|
2011-01-20 22:11:36 -05:00
|
|
|
}
|
2011-01-18 23:47:58 -05:00
|
|
|
|
2011-01-21 21:43:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan each level of visibility for the requested member
|
|
|
|
*
|
|
|
|
* @param {{public: Object, protected: Object, private: Object}} members
|
|
|
|
*
|
2011-01-27 22:35:40 -05:00
|
|
|
* @param {string} name member to locate
|
2011-03-18 23:42:07 -04:00
|
|
|
* @param {Object=} base optional base object to scan
|
2011-01-21 21:43:18 -05:00
|
|
|
*
|
2011-03-18 23:42:07 -04:00
|
|
|
* @return {Object} Array of member and number corresponding to visibility,
|
|
|
|
* level if located, otherwise an empty object
|
2011-01-21 21:43:18 -05:00
|
|
|
*/
|
2011-03-18 23:42:07 -04:00
|
|
|
function scanMembers( members, name, base )
|
2011-01-21 21:43:18 -05:00
|
|
|
{
|
|
|
|
var i = visibility.length,
|
|
|
|
member = null;
|
|
|
|
|
|
|
|
// locate requested member by scanning each level of visibility
|
|
|
|
while ( i-- )
|
|
|
|
{
|
2011-06-30 23:00:13 -04:00
|
|
|
var visobj = members[ visibility[ i ] ];
|
|
|
|
|
|
|
|
// In order to support getters/setters, we must go off of the
|
|
|
|
// descriptor. We must also ignore base properties (last argument), such
|
|
|
|
// as Object.prototype.toString(). However, we must still traverse the
|
|
|
|
// prototype chain.
|
|
|
|
if ( member = util.getPropertyDescriptor( visobj, name, true ) )
|
2011-01-21 21:43:18 -05:00
|
|
|
{
|
2011-06-30 23:00:13 -04:00
|
|
|
return {
|
|
|
|
get: member.get,
|
|
|
|
set: member.set,
|
|
|
|
member: member.value,
|
|
|
|
visibility: ( ( fallback ) ? 0 : i ),
|
|
|
|
};
|
2011-01-21 21:43:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 22:35:40 -05:00
|
|
|
// if a second comparison object was given, try again using it instead of
|
|
|
|
// the original members object
|
2011-03-18 23:42:07 -04:00
|
|
|
if ( base !== undefined )
|
2011-01-27 22:35:40 -05:00
|
|
|
{
|
2011-03-18 23:42:07 -04:00
|
|
|
var base_methods = base.___$$methods$$,
|
|
|
|
base_props = base.___$$props$$;
|
|
|
|
|
|
|
|
// scan the base's methods and properties, if they are available
|
|
|
|
return ( base_methods && scanMembers( base_methods, name ) )
|
|
|
|
|| ( base_props && scanMembers( base_props, name ) )
|
|
|
|
|| null
|
|
|
|
;
|
2011-01-27 22:35:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// nothing was found
|
2011-03-18 23:42:07 -04:00
|
|
|
return null;
|
2011-01-21 21:43:18 -05:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:16:20 -05:00
|
|
|
|
2011-08-04 00:32:10 -04:00
|
|
|
/**
|
|
|
|
* Hide a method with a "new" method
|
|
|
|
*/
|
|
|
|
function hideMethod( super_method, new_method, instCallback, cid )
|
|
|
|
{
|
|
|
|
// TODO: This function is currently unimplemented. It exists at present to
|
|
|
|
// provide a placeholder and ensure that the override keyword is required to
|
|
|
|
// override a parent method.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-21 23:16:20 -05:00
|
|
|
/**
|
|
|
|
* Generates a method override function
|
|
|
|
*
|
|
|
|
* The override function simply wraps the method so that its invocation will
|
|
|
|
* pass a __super property. This property may be used to invoke the overridden
|
|
|
|
* method.
|
|
|
|
*
|
|
|
|
* @param {function()} super_method method to override
|
|
|
|
* @param {function()} new_method method to override with
|
|
|
|
*
|
2011-03-02 20:43:24 -05:00
|
|
|
* @param {Object=} instCallback function to call in order to retrieve
|
|
|
|
* object to bind 'this' keyword to
|
2011-03-13 04:51:00 -04:00
|
|
|
* @param {number} cid class id
|
2011-03-02 20:43:24 -05:00
|
|
|
*
|
2011-01-21 23:16:20 -05:00
|
|
|
* @return {function()} override method
|
|
|
|
*/
|
2011-03-13 04:51:00 -04:00
|
|
|
function overrideMethod( super_method, new_method, instCallback, cid )
|
2011-01-21 23:16:20 -05:00
|
|
|
{
|
2011-03-02 20:43:24 -05:00
|
|
|
instCallback = instCallback || function() {};
|
|
|
|
|
2011-01-21 23:16:20 -05:00
|
|
|
// return a function that permits referencing the super method via the
|
|
|
|
// __super property
|
2011-03-02 20:43:24 -05:00
|
|
|
var override = null;
|
|
|
|
|
2011-03-10 12:19:39 -05:00
|
|
|
// are we overriding?
|
2011-03-02 20:43:24 -05:00
|
|
|
if ( new_method )
|
2011-01-21 23:16:20 -05:00
|
|
|
{
|
2011-03-02 20:43:24 -05:00
|
|
|
override = function()
|
|
|
|
{
|
2011-03-13 04:51:00 -04:00
|
|
|
var context = instCallback( this, cid ) || this,
|
2011-03-10 12:40:55 -05:00
|
|
|
retval = undefined
|
|
|
|
;
|
|
|
|
|
2011-03-29 22:02:42 -04:00
|
|
|
// the _super property will contain the parent method (we don't
|
|
|
|
// store the previous value for performance reasons and because,
|
|
|
|
// during conventional use, it's completely unnecessary)
|
|
|
|
context.__super = super_method;
|
2011-01-21 23:16:20 -05:00
|
|
|
|
2011-03-10 12:40:55 -05:00
|
|
|
retval = new_method.apply( context, arguments );
|
|
|
|
|
2011-03-29 22:02:42 -04:00
|
|
|
// prevent sneaky bastards from breaking encapsulation by stealing
|
|
|
|
// method references (we set to undefined rather than deleting it
|
|
|
|
// because deletion causes performance degradation within V8)
|
|
|
|
context.__super = undefined;
|
|
|
|
|
2011-03-10 12:40:55 -05:00
|
|
|
// if the value returned from the method was the context that we
|
|
|
|
// passed in, return the actual instance (to ensure we do not break
|
|
|
|
// encapsulation)
|
|
|
|
if ( retval === context )
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
2011-01-21 23:16:20 -05:00
|
|
|
|
2011-03-02 20:43:24 -05:00
|
|
|
return retval;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-10 12:19:39 -05:00
|
|
|
// we are defining a new method
|
2011-03-02 20:43:24 -05:00
|
|
|
override = function()
|
|
|
|
{
|
2011-03-13 04:51:00 -04:00
|
|
|
var context = instCallback( this, cid ) || this,
|
2011-03-10 12:19:39 -05:00
|
|
|
retval = undefined
|
|
|
|
;
|
|
|
|
|
|
|
|
// invoke the method
|
|
|
|
retval = super_method.apply( context, arguments );
|
|
|
|
|
|
|
|
// if the value returned from the method was the context that we
|
|
|
|
// passed in, return the actual instance (to ensure we do not break
|
|
|
|
// encapsulation)
|
|
|
|
if ( retval === context )
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2011-03-02 20:43:24 -05:00
|
|
|
};
|
|
|
|
}
|
2011-01-21 23:16:20 -05:00
|
|
|
|
|
|
|
// This is a trick to work around the fact that we cannot set the length
|
|
|
|
// property of a function. Instead, we define our own property - __length.
|
|
|
|
// This will store the expected number of arguments from the super method.
|
|
|
|
// This way, when a method is being overridden, we can check to ensure its
|
|
|
|
// compatibility with its super method.
|
|
|
|
util.defineSecureProp( override,
|
|
|
|
'__length',
|
|
|
|
( super_method.__length || super_method.length )
|
|
|
|
);
|
|
|
|
|
|
|
|
return override;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:42:07 -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
|
|
|
|
*/
|
|
|
|
function getVisibilityValue( keywords )
|
|
|
|
{
|
|
|
|
if ( fallback )
|
|
|
|
{
|
|
|
|
// if we have to fall back, we don't support levels of visibility
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if ( keywords[ 'protected' ] )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( keywords[ 'private' ] )
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// default is public
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|