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-06 22:43:14 -05:00
|
|
|
var util = require( './util' ),
|
|
|
|
|
|
|
|
// whether or not we support defining properties through
|
|
|
|
// Object.defineProperty()
|
|
|
|
defprop = ( Object.defineProperty !== undefined ) ? true : false;
|
|
|
|
;
|
2011-03-06 18:19:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
exports.setup = function( base, dest, properties, members )
|
|
|
|
{
|
|
|
|
var prop_pub = properties[ 'public' ],
|
2011-03-06 23:03:39 -05:00
|
|
|
prop_prot = properties[ 'protected' ],
|
2011-03-06 23:56:19 -05:00
|
|
|
prop_priv = properties[ 'private' ],
|
|
|
|
|
|
|
|
methods_protected = members[ 'protected' ],
|
|
|
|
methods_private = members[ 'private' ]
|
2011-03-06 18:19:19 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
// initialize each of the properties for this instance to
|
|
|
|
// ensure we're not sharing references to prototype values
|
2011-03-06 23:56:19 -05:00
|
|
|
doSetup( dest, prop_pub );
|
|
|
|
|
|
|
|
doSetup( dest, prop_prot, methods_protected );
|
|
|
|
doSetup( dest, prop_priv, methods_private );
|
|
|
|
};
|
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
|
|
|
|
*
|
|
|
|
* @param {Object} dest destination object
|
|
|
|
* @param {Object} properties properties to copy
|
|
|
|
* @param {Object=} methods methods to copy
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function doSetup( dest, properties, methods )
|
|
|
|
{
|
|
|
|
var hasOwn = Array.prototype.hasOwnProperty;
|
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 ) )
|
|
|
|
{
|
|
|
|
dest[ method_name ] = methods[ method_name ];
|
|
|
|
}
|
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 ) )
|
|
|
|
{
|
|
|
|
dest[ prop ] = util.clone( properties[ prop ] );
|
|
|
|
}
|
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;
|
|
|
|
};
|
|
|
|
|