From 6c899e06673e3c21aa4bf99085b8d5784af7f991 Mon Sep 17 00:00:00 2001 From: JF Paradis Date: Mon, 30 Sep 2019 20:21:06 -0700 Subject: [PATCH 1/3] Prevent mangling of variables. https://github.com/Agoric/realms-shim/issues/60 Changes: - Keep cleanupSource for builds. - Use minimized UMD in demo. --- examples/simple.html | 2 +- package.json | 1 - rollup.config.js | 10 ++-------- src/repair/functions.js | 3 +-- src/unsafeRec.js | 2 +- src/utilities.js | 3 --- 6 files changed, 5 insertions(+), 16 deletions(-) diff --git a/examples/simple.html b/examples/simple.html index dc4a6d7..7c63e59 100644 --- a/examples/simple.html +++ b/examples/simple.html @@ -1,6 +1,6 @@
- +

Realm Shim

diff --git a/package.json b/package.json index a13ef90..c22fad0 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "prettier": "^1.18.2", "rollup": "^1.21.4", "rollup-plugin-babel-minify": "^9.1.0", - "rollup-plugin-strip-code": "^0.2.7", "sinon": "^7.5.0", "tape": "^4.11.0" }, diff --git a/rollup.config.js b/rollup.config.js index 1d540e1..d98ad42 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,5 +1,4 @@ import minify from 'rollup-plugin-babel-minify'; -import stripCode from 'rollup-plugin-strip-code'; export default () => { const isProduction = process.env.NODE_ENV === 'production'; @@ -19,12 +18,7 @@ export default () => { } ]; - const plugins = [ - stripCode({ - start_comment: 'START_TESTS_ONLY', - end_comment: 'END_TESTS_ONLY' - }) - ]; + const plugins = []; if (isProduction) { plugins.push( @@ -45,4 +39,4 @@ export default () => { output, plugins }; -} +}; diff --git a/src/repair/functions.js b/src/repair/functions.js index 9044a03..4a75982 100644 --- a/src/repair/functions.js +++ b/src/repair/functions.js @@ -18,7 +18,6 @@ */ // todo: this file should be moved out to a separate repo and npm module. -const globalEval = eval; export function repairFunctions() { const { defineProperties, getPrototypeOf, setPrototypeOf } = Object; @@ -35,7 +34,7 @@ export function repairFunctions() { let FunctionInstance; try { // eslint-disable-next-line no-new-func - FunctionInstance = globalEval(declaration); + FunctionInstance = (0, eval)(declaration); } catch (e) { if (e instanceof SyntaxError) { // Prevent failure on platforms where async and/or generators diff --git a/src/unsafeRec.js b/src/unsafeRec.js index 970ac60..4c05b18 100644 --- a/src/unsafeRec.js +++ b/src/unsafeRec.js @@ -94,7 +94,7 @@ const repairAccessorsShim = cleanupSource( `"use strict"; (${repairAccessors})();` ); const repairFunctionsShim = cleanupSource( - `"use strict"; const globalEval = eval; (${repairFunctions})();` + `"use strict"; (${repairFunctions})();` ); // Create a new unsafeRec from a brand new context, with new intrinsics and a diff --git a/src/utilities.js b/src/utilities.js index 085c5bc..99c8955 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -29,14 +29,11 @@ export function assert(condition, message) { // Remove code modifications. export function cleanupSource(src) { - /* START_TESTS_ONLY */ - // Restore eval which is modified by esm module. src = src.replace(/\(0,[^)]+\)/g, '(0, eval)'); // Remove code coverage which is injected by nyc module. src = src.replace(/cov_[^+]+\+\+[;,]/g, ''); - /* END_TESTS_ONLY */ return src; } From 2d9cd9870d6d2b0fb0f1dcec809afac3b1130f24 Mon Sep 17 00:00:00 2001 From: JF Paradis Date: Tue, 1 Oct 2019 08:54:43 -0700 Subject: [PATCH 2/3] Add integration tests --- package.json | 3 ++- test-integration/realm.cjs.test.js | 29 ++++++++++++++++++++++++++ test-integration/realm.esm-min.test.js | 28 +++++++++++++++++++++++++ test-integration/realm.esm.test.js | 28 +++++++++++++++++++++++++ test-integration/realm.umd-min.test.js | 28 +++++++++++++++++++++++++ test-integration/realm.umd.test.js | 28 +++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 test-integration/realm.cjs.test.js create mode 100644 test-integration/realm.esm-min.test.js create mode 100644 test-integration/realm.esm.test.js create mode 100644 test-integration/realm.umd-min.test.js create mode 100644 test-integration/realm.umd.test.js diff --git a/package.json b/package.json index c22fad0..5484ca3 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "build": "npm run shim:build", "shim:lint": "eslint ./src ./test ./examples", "shim:prettier": "prettier --config ./.prettierrc --write ./src/**/**/*.js ./test/**/**/*.js ./examples/**/**/*.js", - "shim:test": "tape -r esm ./test/**/**/*.js", + "shim:test": "tape -r esm './test/**/*.js'", + "shim:test-integration": "tape -r esm './test-integration/**/*.js'", "shim:coverage": "nyc tape -r esm ./test/**/**/*.js", "shim:coveralls": "nyc report --reporter=text-lcov | coveralls", "shim:build": "npm run shim:build:dev && npm run shim:build:prod", diff --git a/test-integration/realm.cjs.test.js b/test-integration/realm.cjs.test.js new file mode 100644 index 0000000..9e3ffed --- /dev/null +++ b/test-integration/realm.cjs.test.js @@ -0,0 +1,29 @@ +import test from 'tape'; + +const Realm = require('../dist/realms-shim.cjs.js'); + +test('new Realm', t => { + t.plan(1); + + t.throws(() => new Realm(), TypeError, 'new Real() should throws'); +}); + +test('Realm.makeRootRealm()', t => { + t.plan(3); + + const r = Realm.makeRootRealm(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof r.global.Object); + t.equal(r.evaluate('42'), 42); +}); + +test('Realm.makeCompartment()', t => { + t.plan(3); + + const r = Realm.makeCompartment(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof Object); + t.equal(r.evaluate('42'), 42); +}); diff --git a/test-integration/realm.esm-min.test.js b/test-integration/realm.esm-min.test.js new file mode 100644 index 0000000..54d7037 --- /dev/null +++ b/test-integration/realm.esm-min.test.js @@ -0,0 +1,28 @@ +import test from 'tape'; +import Realm from '../dist/realms-shim.esm.min'; + +test('new Realm', t => { + t.plan(1); + + t.throws(() => new Realm(), TypeError, 'new Real() should throws'); +}); + +test('Realm.makeRootRealm()', t => { + t.plan(3); + + const r = Realm.makeRootRealm(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof r.global.Object); + t.equal(r.evaluate('42'), 42); +}); + +test('Realm.makeCompartment()', t => { + t.plan(3); + + const r = Realm.makeCompartment(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof Object); + t.equal(r.evaluate('42'), 42); +}); diff --git a/test-integration/realm.esm.test.js b/test-integration/realm.esm.test.js new file mode 100644 index 0000000..3032ea8 --- /dev/null +++ b/test-integration/realm.esm.test.js @@ -0,0 +1,28 @@ +import test from 'tape'; +import Realm from '../dist/realms-shim.esm'; + +test('new Realm', t => { + t.plan(1); + + t.throws(() => new Realm(), TypeError, 'new Real() should throws'); +}); + +test('Realm.makeRootRealm()', t => { + t.plan(3); + + const r = Realm.makeRootRealm(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof r.global.Object); + t.equal(r.evaluate('42'), 42); +}); + +test('Realm.makeCompartment()', t => { + t.plan(3); + + const r = Realm.makeCompartment(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof Object); + t.equal(r.evaluate('42'), 42); +}); diff --git a/test-integration/realm.umd-min.test.js b/test-integration/realm.umd-min.test.js new file mode 100644 index 0000000..9829296 --- /dev/null +++ b/test-integration/realm.umd-min.test.js @@ -0,0 +1,28 @@ +import test from 'tape'; +const Realm = require('../dist/realms-shim.umd.min.js'); + +test('new Realm', t => { + t.plan(1); + + t.throws(() => new Realm(), TypeError, 'new Real() should throws'); +}); + +test('Realm.makeRootRealm()', t => { + t.plan(3); + + const r = Realm.makeRootRealm(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof r.global.Object); + t.equal(r.evaluate('42'), 42); +}); + +test('Realm.makeCompartment()', t => { + t.plan(3); + + const r = Realm.makeCompartment(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof Object); + t.equal(r.evaluate('42'), 42); +}); diff --git a/test-integration/realm.umd.test.js b/test-integration/realm.umd.test.js new file mode 100644 index 0000000..0800a03 --- /dev/null +++ b/test-integration/realm.umd.test.js @@ -0,0 +1,28 @@ +import test from 'tape'; +const Realm = require('../dist/realms-shim.umd.js'); + +test('new Realm', t => { + t.plan(1); + + t.throws(() => new Realm(), TypeError, 'new Real() should throws'); +}); + +test('Realm.makeRootRealm()', t => { + t.plan(3); + + const r = Realm.makeRootRealm(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof r.global.Object); + t.equal(r.evaluate('42'), 42); +}); + +test('Realm.makeCompartment()', t => { + t.plan(3); + + const r = Realm.makeCompartment(); + + t.ok(r instanceof Realm); + t.ok(r.global instanceof Object); + t.equal(r.evaluate('42'), 42); +}); From f26d939d3f90156b0f267be5e5fce73a8e0862a4 Mon Sep 17 00:00:00 2001 From: JF Paradis Date: Tue, 1 Oct 2019 10:23:34 -0700 Subject: [PATCH 3/3] More strict cleanupSource() --- src/utilities.js | 7 ++++++- test/module/utilities.js | 14 +++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/utilities.js b/src/utilities.js index 99c8955..9084bc0 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -30,7 +30,12 @@ export function assert(condition, message) { // Remove code modifications. export function cleanupSource(src) { // Restore eval which is modified by esm module. - src = src.replace(/\(0,[^)]+\)/g, '(0, eval)'); + // (0, eval) => (0, _.e) + src = src.replace(/\(0, _[^.]+\.e\)/g, '(0, eval)'); + + // Restore Reflect which is modified by esm module. + // Reflect => _.e.Reflect + src = src.replace(/_[^.]+\.g\.Reflect/g, 'Reflect'); // Remove code coverage which is injected by nyc module. src = src.replace(/cov_[^+]+\+\+[;,]/g, ''); diff --git a/test/module/utilities.js b/test/module/utilities.js index 5ca57ba..956f136 100644 --- a/test/module/utilities.js +++ b/test/module/utilities.js @@ -65,14 +65,18 @@ test('assert', t => { }); test('cleanupSource', t => { - t.plan(2); + t.plan(3); + t.equal( + cleanupSource(`function() { return (0, _123.e)('true'); }`), + `function() { return (0, eval)('true'); }` + ); t.equals( + cleanupSource(`function() { const { apply } = _123.g.Reflect; }`), + `function() { const { apply } = Reflect; }` + ); + t.equal( cleanupSource(`function() { cov_2kmyol0g2w[0]++;return true; }`), 'function() { return true; }' ); - t.equals( - cleanupSource(`function() { return (0, _123)('true'); }`), - `function() { return (0, eval)('true'); }` - ); });