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..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",
@@ -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"
},
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..9084bc0 100644
--- a/src/utilities.js
+++ b/src/utilities.js
@@ -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, _.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, '');
- /* END_TESTS_ONLY */
return src;
}
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);
+});
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'); }`
- );
});