From b9c041ccf8d3a83cae33b34fee830685a63e954c Mon Sep 17 00:00:00 2001 From: Hank Jacobs Date: Sun, 12 May 2019 15:18:51 -0700 Subject: [PATCH 1/2] better support for comparisons and instanceof within worker scripts --- jest-env.js | 9 -------- lib/__tests__/runtime.test.js | 28 ++++++++++++++++++++++++ lib/runtime.js | 15 +++++++++++++ package-lock.json | 41 ++++++++++------------------------- package.json | 2 +- 5 files changed, 55 insertions(+), 40 deletions(-) delete mode 100644 jest-env.js diff --git a/jest-env.js b/jest-env.js deleted file mode 100644 index 442342c..0000000 --- a/jest-env.js +++ /dev/null @@ -1,9 +0,0 @@ -// fixes https://github.com/facebook/jest/issues/3186 -const NodeEnvironment = require('jest-environment-node') - -module.exports = class ArrayBufferEnvironment extends NodeEnvironment { - constructor (config) { - super(config) - this.global.ArrayBuffer = ArrayBuffer - } -} diff --git a/lib/__tests__/runtime.test.js b/lib/__tests__/runtime.test.js index 419b9a4..cd5fb10 100644 --- a/lib/__tests__/runtime.test.js +++ b/lib/__tests__/runtime.test.js @@ -1,4 +1,5 @@ const runtime = require('../runtime') +const vm = require('vm') describe('runtime', () => { test('context allows overriding of properties via bindings', async () => { @@ -31,10 +32,37 @@ describe('runtime', () => { 'TextEncoder', 'atob', 'btoa', + 'ArrayBuffer', + 'Int8Array', + 'Uint8Array', + 'Uint8ClampedArray', + 'Int16Array', + 'Uint16Array', + 'Int32Array', + 'Uint32Array', + 'Float32Array', + 'Float64Array', + 'Object', + 'constructor' ].sort() const dummyFactory = Symbol('dummy factory') const got = Object.getOwnPropertyNames(new runtime.Context(() => {}, dummyFactory)).sort() expect(got).toEqual(expected) }) + + test('instanceof from different realm works', () => { + const dummyFactory = Symbol('dummy factory') + const bindings = { + instanceOfObject: (obj) => { return obj instanceof Object}, + isArrayBufferView: (ab) => { return ArrayBuffer.isView(ab)}, + } + const context = new runtime.Context(() => {}, dummyFactory, bindings) + + const objTest = vm.runInNewContext("instanceOfObject(new Object())", context) + const arrayBufferViewTest = vm.runInNewContext("isArrayBufferView(new Uint16Array())", context) + + expect(objTest).toEqual(true) + expect(arrayBufferViewTest).toEqual(true) + }) }) diff --git a/lib/runtime.js b/lib/runtime.js index b2462fc..50bb9da 100644 --- a/lib/runtime.js +++ b/lib/runtime.js @@ -29,6 +29,21 @@ class Context { this.TextEncoder = TextEncoder this.atob = atob this.btoa = btoa + + // These are necessary to use "instanceof" within a vm + this.ArrayBuffer = ArrayBuffer + this.Int8Array = Int8Array + this.Uint8Array = Uint8Array + this.Uint8ClampedArray = Uint8ClampedArray + this.Int16Array = Int16Array + this.Uint16Array = Uint16Array + this.Int32Array = Int32Array + this.Uint32Array = Uint32Array + this.Float32Array = Float32Array + this.Float64Array = Float64Array + this.Object = Object + this.constructor = constructor + Object.assign(this, bindings) } } diff --git a/package-lock.json b/package-lock.json index f322f2f..2b3ae2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2799,8 +2799,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2821,14 +2820,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2843,20 +2840,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2973,8 +2967,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2986,7 +2979,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3001,7 +2993,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3009,14 +3000,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3035,7 +3024,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3116,8 +3104,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -3129,7 +3116,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3215,8 +3201,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3252,7 +3237,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3272,7 +3256,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3316,14 +3299,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, diff --git a/package.json b/package.json index 537b1f0..bdb0c9f 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "typescript": "^3.2.2" }, "jest": { - "testEnvironment": "./jest-env", + "testEnvironment": "node", "automock": false, "setupTestFrameworkScriptFile": "./jest-setup-framework.js", "testPathIgnorePatterns": [ From 78b22987cc74a3e9d9c98065caf9190187d2adf0 Mon Sep 17 00:00:00 2001 From: Hank Jacobs Date: Sun, 12 May 2019 15:52:19 -0700 Subject: [PATCH 2/2] lint fixes --- lib/__tests__/runtime.test.js | 12 ++++++------ lib/runtime.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/__tests__/runtime.test.js b/lib/__tests__/runtime.test.js index dcb2c11..1c2637e 100644 --- a/lib/__tests__/runtime.test.js +++ b/lib/__tests__/runtime.test.js @@ -44,7 +44,7 @@ describe('runtime', () => { 'Float32Array', 'Float64Array', 'Object', - 'constructor' + 'constructor', ].sort() const dummyFactory = Symbol('dummy factory') @@ -55,13 +55,13 @@ describe('runtime', () => { test('instanceof from different realm works', () => { const dummyFactory = Symbol('dummy factory') const bindings = { - instanceOfObject: (obj) => { return obj instanceof Object}, - isArrayBufferView: (ab) => { return ArrayBuffer.isView(ab)}, + instanceOfObject: (obj) => { return obj instanceof Object }, + isArrayBufferView: (ab) => { return ArrayBuffer.isView(ab) }, } const context = new runtime.Context(() => {}, dummyFactory, bindings) - - const objTest = vm.runInNewContext("instanceOfObject(new Object())", context) - const arrayBufferViewTest = vm.runInNewContext("isArrayBufferView(new Uint16Array())", context) + + const objTest = vm.runInNewContext('instanceOfObject(new Object())', context) + const arrayBufferViewTest = vm.runInNewContext('isArrayBufferView(new Uint16Array())', context) expect(objTest).toEqual(true) expect(arrayBufferViewTest).toEqual(true) diff --git a/lib/runtime.js b/lib/runtime.js index 0082b17..65af6cc 100644 --- a/lib/runtime.js +++ b/lib/runtime.js @@ -31,7 +31,7 @@ class Context { this.atob = atob this.btoa = btoa - // These are necessary to use "instanceof" within a vm + // These are necessary to use "instanceof" within a vm this.ArrayBuffer = ArrayBuffer this.Int8Array = Int8Array this.Uint8Array = Uint8Array