diff --git a/lib/util.js b/lib/util.js index 265d545..ff58df8 100644 --- a/lib/util.js +++ b/lib/util.js @@ -32,6 +32,14 @@ var getset = ( Object.prototype.__defineGetter__ === undefined ) : 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 @@ -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 * property on the given object @@ -67,7 +98,7 @@ exports.freeze = function( obj ) */ exports.defineSecureProp = function( obj, prop, value ) { - if ( Object.defineProperty === undefined ) + if ( secure_fallback ) { obj[ prop ] = value; }