1
0
Fork 0

Added tests for util.defineSecureProp()

closure/master
Mike Gerwitz 2010-12-20 13:43:12 -05:00
parent 0d84cd829a
commit 1f40665e57
1 changed files with 32 additions and 1 deletions

View File

@ -32,6 +32,14 @@ var getset = ( Object.prototype.__defineGetter__ === undefined )
: true : true
; ;
/**
* Whether we can actually define secure properties, or we need to fall back
* @type {boolean}
*/
var secure_fallback = ( Object.defineProperty instanceof Function )
? false
: true;
/** /**
* Freezes an object if freezing is supported * Freezes an object if freezing is supported
@ -53,6 +61,29 @@ exports.freeze = function( obj )
} }
/**
* Gets/sets whether the system needs to fall back to defining normal properties
* when a secure property is requested
*
* This will be set by default if the JS engine does not support the
* Object.defineProperty method from EcmaScript 5.
*
* @param {boolean=} val value, if used as setter
*
* @return {boolean|Object} current value if getter, self if setter
*/
exports.secureFallback = function( val )
{
if ( val === undefined )
{
return secure_fallback;
}
secure_fallback = !!val;
return exports;
};
/** /**
* Attempts to define a non-enumerable, non-writable and non-configurable * Attempts to define a non-enumerable, non-writable and non-configurable
* property on the given object * property on the given object
@ -67,7 +98,7 @@ exports.freeze = function( obj )
*/ */
exports.defineSecureProp = function( obj, prop, value ) exports.defineSecureProp = function( obj, prop, value )
{ {
if ( Object.defineProperty === undefined ) if ( secure_fallback )
{ {
obj[ prop ] = value; obj[ prop ] = value;
} }