Browse Source

Polyfills for error .fileName etc writing behavior

- One polyfill to ensure Duktape 1.3.0 behavior

- One polyfill to ensure Duktape 1.4.0 behavior

If the write behavior change is an issue, you can use the first polyfill
to bring back Duktape 1.3.0 behavior.
pull/390/head
Sami Vaarala 9 years ago
parent
commit
e0b793c991
  1. 20
      polyfills/duktape-error-setter-nonwritable.js
  2. 19
      polyfills/duktape-error-setter-writable.js
  3. 2
      util/make_dist.sh

20
polyfills/duktape-error-setter-nonwritable.js

@ -0,0 +1,20 @@
/*
* Ensure Error .fileName, .lineNumber, and .stack are not directly writable,
* but can be written using Object.defineProperty(). This matches Duktape
* 1.3.0 and prior.
*
* See: https://github.com/svaarala/duktape/pull/390.
*/
(function () {
var err = new Error('test');
err.fileName = 999;
if (err.fileName !== 999) { return; } // already non-writable
var fn = new Function(''); // nop
Object.defineProperties(Error.prototype, {
fileName: { set: fn },
lineNumber: { set: fn },
stack: { set: fn }
});
})();

19
polyfills/duktape-error-setter-writable.js

@ -0,0 +1,19 @@
/*
* Ensure Error .fileName, .lineNumber, and .stack are directly writable
* without having to use Object.defineProperty(). This matches Duktape
* 1.4.0 behavior.
*
* See: https://github.com/svaarala/duktape/pull/390.
*/
(function () {
var err = new Error('test');
err.fileName = 999;
if (err.fileName === 999) { return; } // already writable
Object.defineProperties(Error.prototype, {
fileName: { set: new Function('v', 'Object.defineProperty(this, "fileName", { value: v, writable: true, enumerable: false, configurable: true });') },
lineNumber: { set: new Function('v', 'Object.defineProperty(this, "lineNumber", { value: v, writable: true, enumerable: false, configurable: true });') },
stack: { set: new Function('v', 'Object.defineProperty(this, "stack", { value: v, writable: true, enumerable: false, configurable: true });') },
});
})();

2
util/make_dist.sh

@ -261,6 +261,8 @@ for i in \
object-assign.js \ object-assign.js \
performance-now.js \ performance-now.js \
duktape-isfastint.js \ duktape-isfastint.js \
duktape-error-setter-writable.js \
duktape-error-setter-nonwritable.js \
; do ; do
cp polyfills/$i $DIST/polyfills/ cp polyfills/$i $DIST/polyfills/
done done

Loading…
Cancel
Save