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-01-21 23:16:20 -05:00
|
|
|
var util = require( './util' ),
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.buildMethod = function( members, meta, name, value, keywords )
|
|
|
|
{
|
2011-01-21 21:53:31 -05:00
|
|
|
var prev = scanMembers( members, name );
|
|
|
|
|
2011-01-24 23:30:32 -05: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-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-01-21 22:19:22 -05:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:16:20 -05:00
|
|
|
var dest = getMemberVisibility( members, keywords );
|
|
|
|
|
|
|
|
// we might be overriding an existing method
|
|
|
|
if ( prev )
|
|
|
|
{
|
|
|
|
// override the method
|
|
|
|
dest[ name ] = overrideMethod( prev, value );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are not overriding the method, so simply copy it over
|
|
|
|
dest[ name ] = value;
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.buildProp = function( members, meta, name, value, keywords )
|
|
|
|
{
|
2011-01-21 21:43:18 -05:00
|
|
|
// disallow overriding methods with properties
|
|
|
|
if ( scanMembers( members, name ) instanceof Function )
|
|
|
|
{
|
|
|
|
throw new TypeError(
|
|
|
|
"Cannot override method '" + name + "' with property"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-01-20 22:11:36 -05:00
|
|
|
getMemberVisibility( members, keywords )[ name ] = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.buildGetter = function( members, meta, name, value, keywords )
|
|
|
|
{
|
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
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.buildSetter = function( members, meta, name, value, keywords )
|
|
|
|
{
|
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-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
|
|
|
|
*
|
|
|
|
* @param {string} name member to locate
|
|
|
|
*
|
|
|
|
* @return {*} member, if located, otherwise undefined
|
|
|
|
*/
|
|
|
|
function scanMembers( members, name )
|
|
|
|
{
|
|
|
|
var i = visibility.length,
|
|
|
|
member = null;
|
|
|
|
|
|
|
|
// locate requested member by scanning each level of visibility
|
|
|
|
while ( i-- )
|
|
|
|
{
|
|
|
|
if ( member = members[ visibility[ i ] ][ name ] )
|
|
|
|
{
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return {function()} override method
|
|
|
|
*/
|
|
|
|
function overrideMethod( super_method, new_method )
|
|
|
|
{
|
|
|
|
// return a function that permits referencing the super method via the
|
|
|
|
// __super property
|
|
|
|
var override = function()
|
|
|
|
{
|
|
|
|
var tmp = this.__super;
|
|
|
|
|
|
|
|
// assign _super temporarily for the method invocation so
|
|
|
|
// that the method can call the parent method
|
|
|
|
this.__super = super_method;
|
|
|
|
var retval = new_method.apply( this, arguments );
|
|
|
|
this.__super = tmp;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|