Added tests for util.defineSecureProp()
parent
0d84cd829a
commit
1f40665e57
33
lib/util.js
33
lib/util.js
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue