Initial unit tests
MODIFY
.travis.yml
.travis.yml
@@ -9,6 +9,8 @@ sudo: false
script:
- yarn lint-shim
+ - yarn build-shim
+ - yarn test-shim
branches:
only:

MODIFY
package.json
package.json
"scripts": {
"build": "ecmarkup spec/index.emu index.html --css ecmarkup.css --js ecmarkup.js",
"watch": "ecmarkup --watch spec/index.emu index.html --css ecmarkup.css --js ecmarkup.js",
- "build-shim-dev": "cross-env NODE_ENV=development rollup -c shim/rollup.config.js --watch",
+ "build-shim": "yarn build-shim-dev && yarn build-shim-prod",
+ "build-shim-watch": "yarn run build-shim-dev --watch",
+ "build-shim-dev": "cross-env NODE_ENV=development rollup -c shim/rollup.config.js",
"build-shim-prod": "cross-env NODE_ENV=production rollup -c shim/rollup.config.js",
- "lint-shim": "eslint ./shim/src",
- "prettier-shim": "prettier --config ./shim/.prettierrc --write ./shim/src/**/*.js"
+ "lint-shim": "eslint ./shim/src/*.js ./shim/test/*.js",
+ "prettier-shim": "prettier --config ./shim/.prettierrc --write ./shim/src/*.js ./shim/test/*.js",
+ "test-shim": "tape ./shim/test/**/*.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tc39/proposal-realms.git"
},
+ "main": "shim/src/main.js",
"keywords": [
"realms",
"ES8",
"prettier": "^1.12.1",
"rollup": "^0.58.2",
"rollup-plugin-babel-minify": "^4.0.0",
- "rollup-watch": "^3.2.2"
+ "rollup-watch": "^3.2.2",
+ "tape": "^4.9.0"
}
}

MODIFY
shim/.eslintrc
shim/.eslintrc
"prettier"
],
"rules": {
+ "import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
"prettier/prettier": "error",
// Overriden
"import/named": "error",

ADD
shim/test/262/create-proto-from-ctor-realm-array.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+test('esid: sec-array.prototype.concat', t => {
+ t.plan(2);
+
+ const array = [];
+ const OArray = new Realm().global.Array;
+ let callCount = 0;
+ const speciesDesc = {
+ get() {
+ callCount += 1;
+ }
+ };
+
+ array.constructor = OArray;
+
+ Object.defineProperty(Array, Symbol.species, speciesDesc);
+ Object.defineProperty(OArray, Symbol.species, speciesDesc);
+
+ const result = array.concat();
+
+ t.equal(Object.getPrototypeOf(result), Array.prototype);
+ t.equal(callCount, 0, 'Species constructor is not referenced');
+});

ADD
shim/test/262/define-own-prop-length-overflow-realm.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+test('esid: sec-array-exotic-objects-defineownproperty-p-desc', t => {
+ t.plan(1);
+
+ const other = new Realm().global;
+ const OArray = other.Array;
+ const array = new OArray();
+
+ t.throws(() => {
+ array.length = 4294967296;
+ }, RangeError);
+});

ADD
shim/test/262/global.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+test('esid: sec-performeval', t => {
+ t.plan(1);
+
+ const other = new Realm().global;
+ const otherEval = other.eval;
+
+ otherEval('var x = 23;');
+ t.equal(typeof x, 'undefined');
+ // t.equal(other.x, 23); // not supported
+});

ADD
shim/test/262/proto-from-ctor-realm.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+test('esid: sec-array-constructor-array', t => {
+ t.plan(1);
+
+ const other = new Realm().global;
+ const C = new other.Function();
+ C.prototype = null;
+
+ const o = Reflect.construct(Array, [], C);
+
+ t.equal(Object.getPrototypeOf(o), other.Array.prototype);
+});
+
+test('esid: sec-array.of', t => {
+ t.plan(1);
+
+ const other = new Realm().global;
+ const C = new other.Function();
+ C.prototype = null;
+
+ const a = Array.from.call(C, []);
+
+ t.equal(Object.getPrototypeOf(a), other.Object.prototype);
+});
+
+test('esid: sec-array.of', t => {
+ t.plan(1);
+
+ const other = new Realm().global;
+ const C = new other.Function();
+ C.prototype = null;
+
+ const a = Array.of.call(C, 1, 2, 3);
+
+ t.equal(Object.getPrototypeOf(a), other.Object.prototype);
+});

ADD
shim/test/realm/confinement.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+test('confinement evaluation strict mode', t => {
+ t.plan(2);
+
+ const r = new Realm();
+
+ t.equal(r.evaluate('(function() { return this })()'), undefined);
+ t.equal(r.evaluate('(new Function("return this"))()'), undefined);
+});
+
+test('confinement evaluation constructor', t => {
+ t.plan(2);
+
+ const r = new Realm();
+
+ t.throws(() => {
+ r.evaluate('({}).constructor.constructor("return this")()');
+ }, r.global.Error);
+ t.throws(() => {
+ r.evaluate('Error.__proto__.constructor("return this")()');
+ }, r.global.Error);
+});
+
+test('confinement evaluation eval', t => {
+ t.plan(2);
+
+ const r = new Realm();
+
+ // Strict mode
+ t.equal(r.evaluate('(0, eval)("this")'), r.global);
+ t.equal(r.evaluate('var evil = eval; evil("this")'), r.global);
+});

ADD
shim/test/realm/identity-compartment.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+// JSON is an ordinary intrinsic
+test('identity JSON', t => {
+ t.plan(4);
+
+ const r1 = new Realm();
+ const r2 = new r1.global.Realm({ intrinsics: 'inherit' });
+
+ t.equal(r2.evaluate('JSON'), r2.evaluate('JSON'));
+ t.equal(r2.evaluate('JSON'), r2.evaluate('eval("JSON")'));
+ t.notEqual(r2.evaluate('JSON'), JSON);
+ t.equal(r2.evaluate('JSON'), r1.evaluate('JSON'));
+});
+
+// Realm is a facade root-realm-specific
+test('identity Realm', t => {
+ t.plan(8);
+
+ const r1 = new Realm();
+ const r2 = new r1.global.Realm({ intrinsics: 'inherit' });
+
+ t.ok(r2.evaluate('Realm instanceof Function'));
+ t.ok(r2.evaluate('Realm instanceof Object'));
+ t.ok(r2.evaluate('Realm') instanceof r1.evaluate('Function'));
+ t.ok(r2.evaluate('Realm') instanceof r1.evaluate('Object'));
+ t.notOk(r2.evaluate('Realm') instanceof Function);
+ t.notOk(r2.evaluate('Realm') instanceof Object);
+ t.equal(r2.evaluate('Realm'), r1.evaluate('Realm'));
+ t.notEqual(r2.evaluate('Realm'), Realm);
+});
+
+// eval is realm-specific
+test('identity eval', t => {
+ t.plan(8);
+
+ const r1 = new Realm();
+ const r2 = new r1.global.Realm({ intrinsics: 'inherit' });
+
+ t.ok(r2.evaluate('eval instanceof Function'));
+ t.ok(r2.evaluate('eval instanceof Object'));
+ t.ok(r2.evaluate('eval') instanceof r1.evaluate('Function'));
+ t.ok(r2.evaluate('eval') instanceof r1.evaluate('Object'));
+ t.notOk(r2.evaluate('eval') instanceof Function);
+ t.notOk(r2.evaluate('eval') instanceof Object);
+ t.notEqual(r2.evaluate('eval'), r1.evaluate('eval'));
+ t.notEqual(r2.evaluate('eval'), eval);
+});
+
+// Function is realm-specific
+test('identity Function', t => {
+ t.plan(8);
+
+ const r1 = new Realm();
+ const r2 = new r1.global.Realm({ intrinsics: 'inherit' });
+
+ t.ok(r2.evaluate('Function instanceof Function'));
+ t.ok(r2.evaluate('Function instanceof Object'));
+ t.ok(r2.evaluate('Function') instanceof r1.evaluate('Function'));
+ t.ok(r2.evaluate('Function') instanceof r1.evaluate('Object'));
+ t.notOk(r2.evaluate('Function') instanceof Function);
+ t.notOk(r2.evaluate('Function') instanceof Object);
+ t.notEqual(r2.evaluate('Function'), r1.evaluate('Function'));
+ t.notEqual(r2.evaluate('Function'), Function);
+});

ADD
shim/test/realm/identity-single.js
+const Realm = require('../../dist/realm-shim.js');
+const test = require('tape');
+
+// JSON is an ordinary intrinsic
+test('identity JSON', t => {
+ t.plan(5);
+
+ const r = new Realm();
+
+ t.equal(r.evaluate('JSON'), r.evaluate('JSON'));
+ t.equal(r.evaluate('JSON'), r.evaluate('eval("JSON")'));
+ t.equal(r.evaluate('JSON'), r.evaluate('(new Function("return JSON"))()'));
+ t.equal(r.evaluate('JSON'), r.global.JSON);
+ t.notEqual(r.evaluate('JSON'), JSON);
+});
+
+// Realm is a facade root-realm-specific
+test('identity Realm', t => {
+ t.plan(5);
+
+ const r = new Realm();
+
+ t.ok(r.evaluate('Realm instanceof Function'));
+ t.ok(r.evaluate('Realm instanceof Object'));
+ t.notOk(r.evaluate('Realm') instanceof Function);
+ t.notOk(r.evaluate('Realm') instanceof Object);
+ t.notEqual(r.evaluate('Realm'), Realm);
+});
+
+// eval is realm-specific
+test('identity eval', t => {
+ t.plan(3);
+
+ const r = new Realm();
+
+ t.ok(r.evaluate('eval instanceof Function'));
+ t.ok(r.evaluate('eval instanceof Object'));
+ t.notEqual(r.evaluate('eval'), eval);
+});
+
+// Function is realm-specific
+test('identity Function', t => {
+ t.plan(4);
+
+ const r = new Realm();
+
+ t.ok(r.evaluate('Function instanceof Function'));
+ t.ok(r.evaluate('Function instanceof Object'));
+ t.notOk(r.evaluate('Function') instanceof Function);
+ t.notOk(r.evaluate('Function') instanceof Object);
+});

Initial unit tests (#101)