diff --git a/polyfills/duktape-error-setter-nonwritable.js b/polyfills/duktape-error-setter-nonwritable.js new file mode 100644 index 00000000..236c706b --- /dev/null +++ b/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 } + }); +})(); diff --git a/polyfills/duktape-error-setter-writable.js b/polyfills/duktape-error-setter-writable.js new file mode 100644 index 00000000..5d487cdf --- /dev/null +++ b/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 });') }, + }); +})(); diff --git a/util/make_dist.sh b/util/make_dist.sh index c12e6d8a..3bc20541 100644 --- a/util/make_dist.sh +++ b/util/make_dist.sh @@ -261,6 +261,8 @@ for i in \ object-assign.js \ performance-now.js \ duktape-isfastint.js \ + duktape-error-setter-writable.js \ + duktape-error-setter-nonwritable.js \ ; do cp polyfills/$i $DIST/polyfills/ done