2010-11-15 23:44:15 -05:00
|
|
|
/**
|
|
|
|
* Contains utilities functions shared by modules
|
|
|
|
*
|
|
|
|
* 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 propParseKeywords = require( __dirname + '/prop_parser' ).parseKeywords;
|
2010-12-27 20:56:36 -05:00
|
|
|
|
2010-11-15 23:44:15 -05:00
|
|
|
|
2010-12-20 13:43:12 -05:00
|
|
|
/**
|
2011-03-07 09:03:03 -05:00
|
|
|
* Whether we can actually define properties, or we need to fall back
|
|
|
|
*
|
|
|
|
* This check actually attempts to set a property and fails if there's an error.
|
|
|
|
* This is needed because IE8 has a broken implementation, yet still defines
|
|
|
|
* Object.defineProperty for use with DOM elements. Just another day in the life
|
|
|
|
* of a web developer.
|
|
|
|
*
|
|
|
|
* This test is only performed once, when the module is first loaded. Don't
|
|
|
|
* expect a performance hit from it.
|
|
|
|
*
|
2010-12-20 13:43:12 -05:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2011-03-07 09:03:03 -05:00
|
|
|
var can_define_prop = ( function()
|
|
|
|
{
|
|
|
|
if ( typeof Object.defineProperty === 'function' )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// perform test, primarily for IE8
|
|
|
|
Object.defineProperty( {}, 'x', {} );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch ( e ) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} )();
|
2010-12-20 13:43:12 -05:00
|
|
|
|
2010-11-15 23:44:15 -05:00
|
|
|
|
2010-12-01 19:27:40 -05:00
|
|
|
/**
|
2010-12-01 20:41:54 -05:00
|
|
|
* Freezes an object if freezing is supported
|
2010-12-01 19:27:40 -05:00
|
|
|
*
|
2010-12-01 20:41:54 -05:00
|
|
|
* @param {Object} obj object to freeze
|
|
|
|
*
|
|
|
|
* @return {Object} object passed to function
|
2010-12-01 19:27:40 -05:00
|
|
|
*/
|
2011-05-15 19:12:01 -04:00
|
|
|
exports.freeze = ( typeof Object.freeze === 'function' )
|
|
|
|
? Object.freeze
|
|
|
|
: function( obj )
|
2010-12-01 20:41:54 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-05-15 19:12:01 -04:00
|
|
|
;
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
|
2010-12-20 13:43:12 -05:00
|
|
|
/**
|
2011-03-07 09:03:03 -05:00
|
|
|
* Gets/sets whether the system needs to fall back to defining properties in a
|
|
|
|
* normal manner when use of Object.defineProperty() is requested
|
2010-12-20 13:43:12 -05:00
|
|
|
*
|
|
|
|
* This will be set by default if the JS engine does not support the
|
2011-03-23 21:03:19 -04:00
|
|
|
* Object.defineProperty method from ECMAScript 5.
|
2010-12-20 13:43:12 -05:00
|
|
|
*
|
|
|
|
* @param {boolean=} val value, if used as setter
|
|
|
|
*
|
|
|
|
* @return {boolean|Object} current value if getter, self if setter
|
|
|
|
*/
|
2011-03-07 09:03:03 -05:00
|
|
|
exports.definePropertyFallback = function( val )
|
2010-12-20 13:43:12 -05:00
|
|
|
{
|
|
|
|
if ( val === undefined )
|
|
|
|
{
|
2011-03-07 09:03:03 -05:00
|
|
|
return !can_define_prop;
|
2010-12-20 13:43:12 -05:00
|
|
|
}
|
|
|
|
|
2011-03-07 09:03:03 -05:00
|
|
|
can_define_prop = !val;
|
2010-12-21 22:41:50 -05:00
|
|
|
exports.defineSecureProp = getDefineSecureProp();
|
|
|
|
|
2010-12-20 13:43:12 -05:00
|
|
|
return exports;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-01 20:38:50 -05:00
|
|
|
/**
|
|
|
|
* Attempts to define a non-enumerable, non-writable and non-configurable
|
|
|
|
* property on the given object
|
|
|
|
*
|
|
|
|
* If the operation is unsupported, a normal property will be set.
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to set property on
|
|
|
|
* @param {string} prop name of property to set
|
|
|
|
* @param {mixed} value value to set
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-21 22:41:50 -05:00
|
|
|
exports.defineSecureProp = getDefineSecureProp();
|
2010-12-01 20:38:50 -05:00
|
|
|
|
|
|
|
|
2010-12-16 23:15:42 -05:00
|
|
|
/**
|
|
|
|
* Clones an object
|
|
|
|
*
|
2011-01-09 01:38:40 -05:00
|
|
|
* @param {Object} data object to clone
|
|
|
|
* @param {boolean} deep perform deep clone (defaults to shallow)
|
2010-12-16 23:15:42 -05:00
|
|
|
*
|
|
|
|
* @return {Object} cloned object
|
|
|
|
*/
|
2011-01-09 01:38:40 -05:00
|
|
|
exports.clone = function clone( data, deep )
|
2010-12-16 23:15:42 -05:00
|
|
|
{
|
2011-01-09 01:38:40 -05:00
|
|
|
deep = !!deep;
|
|
|
|
|
2010-12-16 23:15:42 -05:00
|
|
|
if ( data instanceof Array )
|
|
|
|
{
|
2011-01-09 01:38:40 -05:00
|
|
|
if ( !deep )
|
|
|
|
{
|
|
|
|
// return a copy of the array
|
|
|
|
return data.slice( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're performing a deep clone, we have to loop through each of the
|
|
|
|
// elements of the array and clone them
|
|
|
|
var ret = [];
|
|
|
|
for ( var i = 0, len = data.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
// clone this element
|
|
|
|
ret.push( clone( data[ i ], deep ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-12-16 23:15:42 -05:00
|
|
|
}
|
2011-04-05 23:47:08 -04:00
|
|
|
else if ( typeof data === 'function' )
|
|
|
|
{
|
|
|
|
// It is pointless to clone a function. Even if we did clone those that
|
|
|
|
// support toSource(), they'd still do the same damn thing.
|
|
|
|
return data;
|
|
|
|
}
|
2010-12-16 23:15:42 -05:00
|
|
|
else if ( data instanceof Object )
|
|
|
|
{
|
2010-12-21 10:09:18 -05:00
|
|
|
var newobj = {},
|
|
|
|
hasOwn = Object.prototype.hasOwnProperty;
|
2010-12-16 23:15:42 -05:00
|
|
|
|
|
|
|
// copy data to the new object
|
|
|
|
for ( prop in data )
|
|
|
|
{
|
2010-12-21 10:09:18 -05:00
|
|
|
if ( hasOwn.call( data, prop ) )
|
2010-12-16 23:15:42 -05:00
|
|
|
{
|
2011-01-09 01:46:46 -05:00
|
|
|
newobj[ prop ] = ( deep )
|
|
|
|
? clone( data[ prop ] )
|
|
|
|
: data[ prop ]
|
|
|
|
;
|
2010-12-16 23:15:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// primitive type; cloning unnecessary
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-05 00:04:55 -04:00
|
|
|
/**
|
|
|
|
* Copies properties from one object to another
|
|
|
|
*
|
|
|
|
* This method is designed to support very basic object extensions. The
|
|
|
|
* destination argument is first to allow extending an object without using the
|
|
|
|
* full-blown class system.
|
|
|
|
*
|
2011-04-05 23:38:13 -04:00
|
|
|
* If a deep copy is not performed, all values will be copied by reference.
|
|
|
|
*
|
|
|
|
* @param {Object} dest destination object
|
|
|
|
* @param {Object} src source object
|
|
|
|
* @param {boolean} deep perform deep copy (slower)
|
2011-04-05 00:04:55 -04:00
|
|
|
*
|
|
|
|
* @return {Object} dest
|
|
|
|
*/
|
2011-04-05 23:38:13 -04:00
|
|
|
exports.copyTo = function( dest, src, deep )
|
2011-04-05 00:04:55 -04:00
|
|
|
{
|
2011-04-05 23:38:13 -04:00
|
|
|
deep = !!deep;
|
|
|
|
|
2011-04-05 00:04:55 -04:00
|
|
|
var get, set, data;
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
if ( !( dest instanceof Object ) || !( src instanceof Object ) )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Must provide both source and destination objects"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// slower; supports getters/setters
|
|
|
|
if ( can_define_prop )
|
|
|
|
{
|
|
|
|
for ( prop in src )
|
|
|
|
{
|
|
|
|
data = Object.getOwnPropertyDescriptor( src, prop );
|
|
|
|
|
|
|
|
if ( data.get || data.set )
|
|
|
|
{
|
2011-04-05 23:38:13 -04:00
|
|
|
// Define the property the slower way (only needed for
|
|
|
|
// getters/setters). We don't have to worry about cloning in
|
|
|
|
// this case, since getters/setters are methods.
|
2011-04-05 00:04:55 -04:00
|
|
|
Object.defineProperty( dest, prop, data );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-05 23:38:13 -04:00
|
|
|
// normal copy; cloned if deep, otherwise by reference
|
|
|
|
dest[ prop ] = ( deep )
|
|
|
|
? exports.clone( src[ prop ], true )
|
|
|
|
: src[ prop ]
|
|
|
|
;
|
2011-04-05 00:04:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// quick (keep if statement out of the loop)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( prop in src )
|
|
|
|
{
|
2011-04-05 23:38:13 -04:00
|
|
|
// normal copy; cloned if deep, otherwise by reference
|
|
|
|
dest[ prop ] = ( deep )
|
|
|
|
? exports.clone( src[ prop ], true )
|
|
|
|
: src[ prop ]
|
|
|
|
;
|
2011-04-05 00:04:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return dest for convenience (and to feel useful about ourselves)
|
|
|
|
return dest;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-04 13:59:06 -05:00
|
|
|
/**
|
|
|
|
* Parses object properties to determine how they should be interpreted in an
|
|
|
|
* Object Oriented manner
|
|
|
|
*
|
|
|
|
* @param {Object} data properties with names as the key
|
|
|
|
* @param {Object} options parser options and callbacks
|
|
|
|
*
|
|
|
|
* @return undefined
|
|
|
|
*/
|
2010-12-05 20:50:13 -05:00
|
|
|
exports.propParse = function( data, options )
|
2010-12-04 13:59:06 -05:00
|
|
|
{
|
2011-01-09 19:44:09 -05:00
|
|
|
// todo: profile; function calls are more expensive than if statements, so
|
|
|
|
// it's probably a better idea not to use fvoid
|
2011-01-09 19:41:47 -05:00
|
|
|
var fvoid = function() {},
|
2010-12-28 22:08:30 -05:00
|
|
|
callbackEach = options.each || undefined,
|
|
|
|
callbackProp = options.property || fvoid,
|
|
|
|
callbackMethod = options.method || fvoid,
|
|
|
|
callbackGetter = options.getter || fvoid,
|
|
|
|
callbackSetter = options.setter || fvoid,
|
|
|
|
keywordParser = options.keywordParser || propParseKeywords,
|
2010-12-27 20:49:59 -05:00
|
|
|
|
|
|
|
hasOwn = Object.prototype.hasOwnProperty,
|
|
|
|
|
|
|
|
parse_data = {},
|
|
|
|
name = '',
|
|
|
|
keywords = {},
|
|
|
|
value = null,
|
|
|
|
getter = false,
|
|
|
|
setter = false;
|
2010-12-04 13:59:06 -05:00
|
|
|
|
|
|
|
// for each of the given properties, determine what type of property we're
|
|
|
|
// dealing with (in the classic OO sense)
|
|
|
|
for ( prop in data )
|
|
|
|
{
|
2010-12-18 07:07:27 -05:00
|
|
|
// ignore properties of instance prototypes
|
2010-12-21 10:09:18 -05:00
|
|
|
if ( !( hasOwn.call( data, prop ) ) )
|
2010-12-18 07:07:27 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-07 22:44:47 -05:00
|
|
|
// retrieve getters/setters, if supported
|
|
|
|
if ( can_define_prop )
|
|
|
|
{
|
|
|
|
var prop_desc = Object.getOwnPropertyDescriptor( data, prop );
|
|
|
|
getter = prop_desc.get;
|
|
|
|
setter = prop_desc.set;
|
|
|
|
}
|
|
|
|
|
2010-12-27 20:49:59 -05:00
|
|
|
value = data[ prop ];
|
|
|
|
|
2010-12-28 22:08:30 -05:00
|
|
|
parse_data = keywordParser( prop ) || {};
|
2010-12-27 20:49:59 -05:00
|
|
|
name = parse_data.name || prop;
|
|
|
|
keywords = parse_data.keywords || {};
|
2010-12-04 13:59:06 -05:00
|
|
|
|
2010-12-27 22:11:37 -05:00
|
|
|
if ( keywords['abstract'] )
|
|
|
|
{
|
2010-12-27 22:30:28 -05:00
|
|
|
if ( !( value instanceof Array ) )
|
2010-12-27 22:11:37 -05:00
|
|
|
{
|
2010-12-27 22:30:28 -05:00
|
|
|
throw TypeError(
|
|
|
|
"Missing parameter list for abstract method: " + name
|
|
|
|
);
|
2010-12-27 22:11:37 -05:00
|
|
|
}
|
2010-12-27 22:30:28 -05:00
|
|
|
|
|
|
|
value = exports.createAbstractMethod.apply( this, value );
|
2010-12-27 22:11:37 -05:00
|
|
|
}
|
|
|
|
|
2010-12-27 22:20:29 -05:00
|
|
|
// if an 'each' callback was provided, pass the data before parsing it
|
2010-12-28 22:08:30 -05:00
|
|
|
if ( callbackEach )
|
2010-12-27 22:20:29 -05:00
|
|
|
{
|
2011-03-18 23:42:07 -04:00
|
|
|
callbackEach.call( callbackEach, name, value, keywords );
|
2010-12-27 22:20:29 -05:00
|
|
|
}
|
|
|
|
|
2010-12-05 20:50:13 -05:00
|
|
|
// getter/setter
|
|
|
|
if ( getter || setter )
|
|
|
|
{
|
2011-01-24 23:27:55 -05:00
|
|
|
callbackGetter.call( callbackGetter, name, getter, keywords );
|
|
|
|
callbackSetter.call( callbackSetter, name, setter, keywords );
|
2010-12-05 20:50:13 -05:00
|
|
|
}
|
|
|
|
// method
|
2010-12-27 22:32:30 -05:00
|
|
|
else if ( value instanceof Function )
|
2010-12-04 13:59:06 -05:00
|
|
|
{
|
2010-12-28 22:08:30 -05:00
|
|
|
callbackMethod.call(
|
|
|
|
callbackMethod,
|
2010-12-27 20:49:59 -05:00
|
|
|
name,
|
2010-12-07 00:46:50 -05:00
|
|
|
value,
|
2011-01-17 22:24:02 -05:00
|
|
|
exports.isAbstractMethod( value ),
|
|
|
|
keywords
|
2010-12-07 00:46:50 -05:00
|
|
|
);
|
2010-12-04 13:59:06 -05:00
|
|
|
}
|
2010-12-05 20:50:13 -05:00
|
|
|
// simple property
|
2010-12-04 13:59:06 -05:00
|
|
|
else
|
|
|
|
{
|
2011-01-17 22:24:02 -05:00
|
|
|
callbackProp.call( callbackProp, name, value, keywords );
|
2010-12-04 13:59:06 -05:00
|
|
|
}
|
|
|
|
}
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-12-04 13:59:06 -05:00
|
|
|
|
|
|
|
|
2010-12-01 20:45:27 -05:00
|
|
|
/**
|
|
|
|
* Creates an abstract method
|
|
|
|
*
|
|
|
|
* Abstract methods must be implemented by a subclass and cannot be called
|
|
|
|
* directly. If a class contains a single abstract method, the class itself is
|
|
|
|
* considered to be abstract and cannot be instantiated. It may only be
|
|
|
|
* extended.
|
|
|
|
*
|
2010-12-01 21:13:51 -05:00
|
|
|
* @param {...string} definition function definition that concrete
|
|
|
|
* implementations must follow
|
2010-12-01 20:45:27 -05:00
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
*/
|
2010-12-01 21:13:51 -05:00
|
|
|
exports.createAbstractMethod = function()
|
2010-12-01 20:45:27 -05:00
|
|
|
{
|
2010-12-01 21:13:51 -05:00
|
|
|
var definition = Array.prototype.slice.call( arguments );
|
|
|
|
|
2010-12-01 20:45:27 -05:00
|
|
|
var method = function()
|
|
|
|
{
|
|
|
|
throw new Error( "Cannot call abstract method" );
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.defineSecureProp( method, 'abstractFlag', true );
|
|
|
|
exports.defineSecureProp( method, 'definition', definition );
|
2011-01-24 23:28:48 -05:00
|
|
|
exports.defineSecureProp( method, '__length', arguments.length );
|
2010-12-01 20:45:27 -05:00
|
|
|
|
|
|
|
return method;
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-12-01 20:45:27 -05:00
|
|
|
|
|
|
|
|
2010-12-01 21:00:15 -05:00
|
|
|
/**
|
|
|
|
* Determines if the given function is an abstract method
|
|
|
|
*
|
|
|
|
* @param {Function} function function to inspect
|
|
|
|
*
|
|
|
|
* @return {boolean} true if function is an abstract method, otherwise false
|
|
|
|
*/
|
|
|
|
exports.isAbstractMethod = function( func )
|
|
|
|
{
|
|
|
|
return ( ( func instanceof Function ) && ( func.abstractFlag === true ) )
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-12-01 21:00:15 -05:00
|
|
|
|
2010-12-01 20:45:27 -05:00
|
|
|
|
2010-11-15 23:44:15 -05:00
|
|
|
/**
|
|
|
|
* Shrinks an array, removing undefined elements
|
|
|
|
*
|
|
|
|
* Pushes all items onto a new array, removing undefined elements. This ensures
|
|
|
|
* that the length of the array represents correctly the number of elements in
|
|
|
|
* the array.
|
|
|
|
*
|
|
|
|
* @param {Array} items array to shrink
|
|
|
|
*
|
|
|
|
* @return {Array} shrunken array
|
|
|
|
*/
|
2010-12-18 09:38:58 -05:00
|
|
|
exports.arrayShrink = function( items )
|
2010-11-15 23:44:15 -05:00
|
|
|
{
|
|
|
|
// copy the methods into a new array by pushing them onto it, to ensure
|
|
|
|
// the length property of the array will work properly
|
|
|
|
var arr_new = [];
|
|
|
|
for ( var i = 0, len = items.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
var item = items[ i ];
|
|
|
|
if ( item === undefined )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr_new.push( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr_new;
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-11-15 23:44:15 -05:00
|
|
|
|
2010-12-21 22:41:50 -05:00
|
|
|
|
2011-06-11 21:47:57 -04:00
|
|
|
/**
|
|
|
|
* Uses Object.getOwnPropertyDescriptor if available, otherwise provides our own
|
|
|
|
* implementation to fall back on
|
|
|
|
*
|
|
|
|
* If the environment does not support retrieving property descriptors (ES5),
|
|
|
|
* then the following will be true:
|
|
|
|
* - get/set will always be undefined
|
|
|
|
* - writable, enumerable and configurable will always be true
|
|
|
|
* - value will be the value of the requested property on the given object
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to check property on
|
|
|
|
* @param {string} prop property to retrieve descriptor for
|
|
|
|
*
|
|
|
|
* @return {Object} descriptor for requested property or undefined if not found
|
|
|
|
*/
|
|
|
|
exports.getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
|
|
|
|
|| function( obj, prop )
|
|
|
|
{
|
|
|
|
if ( !Object.prototype.hasOwnProperty.call( obj, prop ) )
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback response
|
|
|
|
return {
|
|
|
|
get: undefined,
|
|
|
|
set: undefined,
|
|
|
|
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
|
|
|
|
value: obj[ prop ],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Travels down the prototype chain of the given object in search of the
|
|
|
|
* requested property and returns its descriptor
|
|
|
|
*
|
|
|
|
* This operates as Object.getOwnPropertyDescriptor(), except that it traverses
|
|
|
|
* the prototype chain. For environments that do not support __proto__, it will
|
|
|
|
* not traverse the prototype chain and essentially serve as an alias for
|
|
|
|
* getOwnPropertyDescriptor().
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to check property on
|
|
|
|
* @param {string} prop property to retrieve descriptor for
|
|
|
|
*
|
|
|
|
* @return {Object} descriptor for requested property or undefined if not found
|
|
|
|
*/
|
|
|
|
exports.getPropertyDescriptor = function( obj, prop )
|
|
|
|
{
|
|
|
|
// note that this uses util's function, not Object's
|
|
|
|
var desc = exports.getOwnPropertyDescriptor( obj, prop );
|
|
|
|
|
|
|
|
// if we didn't find a descriptor and a prototype is available, recurse down
|
|
|
|
// the prototype chain
|
|
|
|
if ( !desc && obj.__proto__ )
|
|
|
|
{
|
|
|
|
return exports.getPropertyDescriptor( obj.__proto__, prop );
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the descriptor or undefined if no prototype is available
|
|
|
|
return desc;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether or not the getPropertyDescriptor method is capable of
|
|
|
|
* traversing the prototype chain
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
exports.defineSecureProp( exports.getPropertyDescriptor, 'canTraverse',
|
|
|
|
( {}.__proto__ ) ? true : false
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2010-12-21 22:41:50 -05:00
|
|
|
/**
|
|
|
|
* Appropriately returns defineSecureProp implementation to avoid check on each
|
|
|
|
* invocation
|
|
|
|
*
|
|
|
|
* @return {function( Object, string, * )}
|
|
|
|
*/
|
|
|
|
function getDefineSecureProp()
|
|
|
|
{
|
|
|
|
// falls back to simply defining a normal property
|
|
|
|
var fallback = function( obj, prop, value )
|
|
|
|
{
|
|
|
|
obj[ prop ] = value;
|
|
|
|
};
|
|
|
|
|
2011-03-07 09:03:03 -05:00
|
|
|
if ( !can_define_prop )
|
2010-12-21 22:41:50 -05:00
|
|
|
{
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// uses ECMAScript 5's Object.defineProperty() method
|
|
|
|
return function( obj, prop, value )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Object.defineProperty( obj, prop,
|
|
|
|
{
|
|
|
|
value: value,
|
|
|
|
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
2010-12-22 22:17:07 -05:00
|
|
|
// let's not have this happen again, as repeatedly throwing
|
|
|
|
// exceptions will do nothing but slow down the system
|
2011-03-07 09:03:03 -05:00
|
|
|
exports.definePropertyFallback( true );
|
2010-12-22 22:17:07 -05:00
|
|
|
|
2010-12-21 22:41:50 -05:00
|
|
|
// if there's an error (ehem, IE8), fall back
|
|
|
|
fallback( obj, prop, value );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|