2011-03-06 18:19:19 -05:00
|
|
|
/**
|
|
|
|
* Contains property object generator
|
|
|
|
*
|
|
|
|
* 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-27 02:02:04 -04:00
|
|
|
var util = require( __dirname + '/util' ),
|
2011-03-06 22:43:14 -05:00
|
|
|
|
|
|
|
// whether or not we support defining properties through
|
|
|
|
// Object.defineProperty()
|
2011-03-07 09:05:05 -05:00
|
|
|
defprop = !( util.definePropertyFallback() );
|
2011-03-06 22:43:14 -05:00
|
|
|
;
|
2011-03-06 18:19:19 -05:00
|
|
|
|
|
|
|
|
2011-03-07 00:10:12 -05:00
|
|
|
/**
|
|
|
|
* Sets up properties (non-inheriting)
|
|
|
|
*
|
2011-03-12 23:48:38 -05:00
|
|
|
* This includes all members (including private). Private members will be set up
|
|
|
|
* in a separate object, so that they can be easily removed from the mix. That
|
|
|
|
* object will include the destination object in the prototype, so that the
|
|
|
|
* access should be transparent. This object is returned.
|
2011-03-07 00:10:12 -05:00
|
|
|
*
|
|
|
|
* @param {Object} dest destination object
|
|
|
|
* @param {Object} properties properties to copy
|
|
|
|
* @param {Object=} methods methods to copy
|
|
|
|
*
|
2011-03-12 23:48:38 -05:00
|
|
|
* @return {Object} object containing private members and dest as prototype
|
2011-03-07 00:10:12 -05:00
|
|
|
*/
|
|
|
|
exports.setup = function( dest, properties, methods )
|
|
|
|
{
|
2011-03-13 21:47:40 -04:00
|
|
|
var obj = dest;
|
|
|
|
|
2011-03-12 23:48:38 -05:00
|
|
|
// this constructor is an extra layer atop of the destination object, which
|
|
|
|
// will contain the private methods
|
2011-03-13 21:47:40 -04:00
|
|
|
if ( defprop )
|
|
|
|
{
|
|
|
|
var obj_ctor = function() {};
|
|
|
|
obj_ctor.prototype = dest;
|
2011-03-12 23:48:38 -05:00
|
|
|
|
2011-03-13 21:47:40 -04:00
|
|
|
obj = new obj_ctor();
|
2011-04-02 10:58:26 -04:00
|
|
|
|
|
|
|
// all private protected proxies need to be proxied from the private
|
|
|
|
// object (which will be passed as the context) to the object containing
|
|
|
|
// protected values
|
|
|
|
exports.createPropProxy( dest, obj, properties[ 'protected' ] );
|
2011-03-13 21:47:40 -04:00
|
|
|
}
|
2011-03-12 23:48:38 -05:00
|
|
|
|
2011-03-13 03:55:43 -04:00
|
|
|
// initialize each of the properties for this instance to
|
|
|
|
// ensure we're not sharing references to prototype values
|
|
|
|
doSetup( dest, properties[ 'public' ] );
|
2011-04-01 06:28:45 -04:00
|
|
|
|
|
|
|
// Do the same for protected, but only if they do not exist already in
|
|
|
|
// public. The reason for this is because the property object is laid /atop/
|
|
|
|
// of the public members, meaning that a parent's protected members will
|
|
|
|
// take precedence over a subtype's overriding /public/ members. Uh oh.
|
2011-04-13 14:48:20 -04:00
|
|
|
doSetup( dest,
|
|
|
|
properties[ 'protected' ],
|
|
|
|
methods[ 'protected' ],
|
|
|
|
'public'
|
|
|
|
);
|
2011-03-06 23:56:19 -05:00
|
|
|
|
2011-03-07 00:10:12 -05:00
|
|
|
// then add the private parts
|
2011-03-12 23:48:38 -05:00
|
|
|
doSetup( obj, properties[ 'private' ], methods[ 'private' ] );
|
|
|
|
|
|
|
|
return obj;
|
2011-03-06 23:56:19 -05:00
|
|
|
};
|
2011-03-06 18:19:19 -05:00
|
|
|
|
2011-03-06 23:56:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up destination object by copying over properties and methods
|
|
|
|
*
|
2011-04-13 19:53:22 -04:00
|
|
|
* @param {Object} dest destination object
|
|
|
|
* @param {Object} properties properties to copy
|
|
|
|
* @param {Object=} methods methods to copy
|
|
|
|
* @param {boolean} unless_keyword do not set if keyword is set on existing
|
|
|
|
* method
|
2011-03-06 23:56:19 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-04-13 14:48:20 -04:00
|
|
|
function doSetup( dest, properties, methods, unless_keyword )
|
2011-03-06 23:56:19 -05:00
|
|
|
{
|
2011-04-13 14:48:20 -04:00
|
|
|
var hasOwn = Array.prototype.hasOwnProperty,
|
|
|
|
pre = null;
|
2011-03-06 18:19:19 -05:00
|
|
|
|
|
|
|
// copy over the methods
|
2011-03-06 23:56:19 -05:00
|
|
|
if ( methods !== undefined )
|
2011-03-06 23:03:39 -05:00
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
for ( method_name in methods )
|
2011-03-06 23:03:39 -05:00
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
if ( hasOwn.call( methods, method_name ) )
|
|
|
|
{
|
2011-04-13 14:48:20 -04:00
|
|
|
pre = dest[ method_name ];
|
|
|
|
|
2011-04-01 06:28:45 -04:00
|
|
|
// If requested, do not copy the method over if it already
|
|
|
|
// exists in the destination object. Don't use hasOwn here;
|
|
|
|
// unnecessary overhead and we want to traverse any prototype
|
|
|
|
// chains. We do not check the public object directly, for
|
|
|
|
// example, because we need a solution that will work if a proxy
|
|
|
|
// is unsupported by the engine.
|
2011-04-13 14:48:20 -04:00
|
|
|
//
|
|
|
|
// Also note that we need to allow overriding if it exists in
|
|
|
|
// the protected object (we can override protected with
|
|
|
|
// protected). This is the *last* check to ensure a performance
|
|
|
|
// hit is incured *only* if we're overriding protected with
|
|
|
|
// protected.
|
|
|
|
if ( !unless_keyword
|
|
|
|
|| ( pre === undefined )
|
|
|
|
|| !( pre.___$$keywords$$[ unless_keyword ] )
|
|
|
|
)
|
2011-04-01 06:28:45 -04:00
|
|
|
{
|
|
|
|
dest[ method_name ] = methods[ method_name ];
|
|
|
|
}
|
2011-03-06 23:56:19 -05:00
|
|
|
}
|
2011-03-06 23:03:39 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-06 18:19:19 -05:00
|
|
|
|
2011-03-06 23:03:39 -05:00
|
|
|
// initialize private/protected properties and store in instance data
|
2011-03-06 23:56:19 -05:00
|
|
|
for ( prop in properties )
|
2011-03-06 18:19:19 -05:00
|
|
|
{
|
2011-03-06 23:56:19 -05:00
|
|
|
if ( hasOwn.call( properties, prop ) )
|
|
|
|
{
|
2011-05-19 19:48:47 -04:00
|
|
|
dest[ prop ] = util.clone( properties[ prop ][ 0 ] );
|
2011-03-06 23:56:19 -05:00
|
|
|
}
|
2011-03-06 23:03:39 -05:00
|
|
|
}
|
2011-03-06 23:56:19 -05:00
|
|
|
}
|
2011-03-06 21:48:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a proxy for all given properties to the given base
|
|
|
|
*
|
|
|
|
* The proxy uses getters/setters to forward all calls to the base. The
|
|
|
|
* destination object will be used as the proxy. All properties within props
|
|
|
|
* will be used proxied.
|
|
|
|
*
|
|
|
|
* To summarize: for each property in props, all gets and sets will be forwarded
|
|
|
|
* to base.
|
|
|
|
*
|
|
|
|
* @param {Object} base object to proxy to
|
|
|
|
* @param {Object} dest object to treat as proxy (set getters/setters on)
|
|
|
|
* @param {Object} props properties to proxy
|
|
|
|
*
|
|
|
|
* @return {Object} returns dest
|
|
|
|
*/
|
|
|
|
exports.createPropProxy = function( base, dest, props )
|
|
|
|
{
|
|
|
|
var hasOwn = Object.prototype.hasOwnProperty;
|
|
|
|
|
2011-03-06 22:43:14 -05:00
|
|
|
if ( !defprop )
|
|
|
|
{
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2011-03-06 21:48:18 -05:00
|
|
|
for ( prop in props )
|
|
|
|
{
|
|
|
|
if ( !( hasOwn.call( props, prop ) ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
( function( prop )
|
|
|
|
{
|
2011-03-06 22:16:50 -05:00
|
|
|
// just in case it's already defined, so we don't throw an error
|
|
|
|
dest[ prop ] = undefined;
|
|
|
|
|
2011-03-06 21:48:18 -05:00
|
|
|
// public properties, when set internally, must forward to the
|
|
|
|
// actual variable
|
2011-03-06 22:16:50 -05:00
|
|
|
Object.defineProperty( dest, prop, {
|
|
|
|
set: function( val )
|
|
|
|
{
|
|
|
|
base[ prop ] = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function()
|
|
|
|
{
|
|
|
|
return base[ prop ];
|
|
|
|
},
|
2011-03-06 21:48:18 -05:00
|
|
|
|
2011-03-06 22:16:50 -05:00
|
|
|
enumerable: true,
|
2011-03-06 21:48:18 -05:00
|
|
|
} );
|
|
|
|
} ).call( null, prop );
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
};
|
2011-03-06 18:19:19 -05:00
|
|
|
|
2011-03-06 22:43:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether property proxying is supported
|
|
|
|
*
|
|
|
|
* Proxying is done via getters and setters. If the JS engine doesn't support
|
|
|
|
* them (pre-ES5), then the proxy will not work.
|
|
|
|
*
|
|
|
|
* @return {boolean} true if supported, otherwise false
|
|
|
|
*/
|
|
|
|
exports.supportsPropProxy = function()
|
|
|
|
{
|
|
|
|
return defprop;
|
|
|
|
};
|
|
|
|
|