Skip to content

Commit

Permalink
Merge pull request #65 from jfparadis/jfparadis/master/fix-mangling
Browse files Browse the repository at this point in the history
Resolve issue #60 with mangled variable.
  • Loading branch information
jfparadis authored Oct 1, 2019
2 parents 98516d3 + f26d939 commit c3d104d
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/simple.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<header>
<script src="../dist/realms-shim.umd.js"></script>
<script src="../dist/realms-shim.umd.min.js"></script>
</header>
<body>
<h1>Realm Shim</h1>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -50,7 +51,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"
},
Expand Down
10 changes: 2 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand All @@ -45,4 +39,4 @@ export default () => {
output,
plugins
};
}
};
3 changes: 1 addition & 2 deletions src/repair/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/unsafeRec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ 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)');
// (0, eval) => (0, _<something>.e)
src = src.replace(/\(0, _[^.]+\.e\)/g, '(0, eval)');

// Restore Reflect which is modified by esm module.
// Reflect => _<something>.e.Reflect
src = src.replace(/_[^.]+\.g\.Reflect/g, 'Reflect');

// Remove code coverage which is injected by nyc module.
src = src.replace(/cov_[^+]+\+\+[;,]/g, '');

/* END_TESTS_ONLY */
return src;
}
29 changes: 29 additions & 0 deletions test-integration/realm.cjs.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
28 changes: 28 additions & 0 deletions test-integration/realm.esm-min.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
28 changes: 28 additions & 0 deletions test-integration/realm.esm.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
28 changes: 28 additions & 0 deletions test-integration/realm.umd-min.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
28 changes: 28 additions & 0 deletions test-integration/realm.umd.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
14 changes: 9 additions & 5 deletions test/module/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'); }`
);
});

0 comments on commit c3d104d

Please sign in to comment.