Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit 93f853d

Browse files
authored
Merge pull request #85 from dollarshaveclub/realm-comparison
Better support for comparisons and instanceof
2 parents ebff750 + 78b2298 commit 93f853d

File tree

5 files changed

+55
-40
lines changed

5 files changed

+55
-40
lines changed

jest-env.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/__tests__/runtime.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const runtime = require('../runtime')
2+
const vm = require('vm')
23

34
describe('runtime', () => {
45
test('context allows overriding of properties via bindings', async () => {
@@ -32,10 +33,37 @@ describe('runtime', () => {
3233
'TextEncoder',
3334
'atob',
3435
'btoa',
36+
'ArrayBuffer',
37+
'Int8Array',
38+
'Uint8Array',
39+
'Uint8ClampedArray',
40+
'Int16Array',
41+
'Uint16Array',
42+
'Int32Array',
43+
'Uint32Array',
44+
'Float32Array',
45+
'Float64Array',
46+
'Object',
47+
'constructor',
3548
].sort()
3649

3750
const dummyFactory = Symbol('dummy factory')
3851
const got = Object.getOwnPropertyNames(new runtime.Context(() => {}, dummyFactory)).sort()
3952
expect(got).toEqual(expected)
4053
})
54+
55+
test('instanceof from different realm works', () => {
56+
const dummyFactory = Symbol('dummy factory')
57+
const bindings = {
58+
instanceOfObject: (obj) => { return obj instanceof Object },
59+
isArrayBufferView: (ab) => { return ArrayBuffer.isView(ab) },
60+
}
61+
const context = new runtime.Context(() => {}, dummyFactory, bindings)
62+
63+
const objTest = vm.runInNewContext('instanceOfObject(new Object())', context)
64+
const arrayBufferViewTest = vm.runInNewContext('isArrayBufferView(new Uint16Array())', context)
65+
66+
expect(objTest).toEqual(true)
67+
expect(arrayBufferViewTest).toEqual(true)
68+
})
4169
})

lib/runtime.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ class Context {
3030
this.TextEncoder = TextEncoder
3131
this.atob = atob
3232
this.btoa = btoa
33+
34+
// These are necessary to use "instanceof" within a vm
35+
this.ArrayBuffer = ArrayBuffer
36+
this.Int8Array = Int8Array
37+
this.Uint8Array = Uint8Array
38+
this.Uint8ClampedArray = Uint8ClampedArray
39+
this.Int16Array = Int16Array
40+
this.Uint16Array = Uint16Array
41+
this.Int32Array = Int32Array
42+
this.Uint32Array = Uint32Array
43+
this.Float32Array = Float32Array
44+
this.Float64Array = Float64Array
45+
this.Object = Object
46+
this.constructor = constructor
47+
3348
Object.assign(this, bindings)
3449
}
3550
}

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"typescript": "^3.2.2"
5858
},
5959
"jest": {
60-
"testEnvironment": "./jest-env",
60+
"testEnvironment": "node",
6161
"automock": false,
6262
"setupTestFrameworkScriptFile": "./jest-setup-framework.js",
6363
"testPathIgnorePatterns": [

0 commit comments

Comments
 (0)