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

Commit

Permalink
Merge pull request #85 from dollarshaveclub/realm-comparison
Browse files Browse the repository at this point in the history
Better support for comparisons and instanceof
  • Loading branch information
hankjacobs authored May 12, 2019
2 parents ebff750 + 78b2298 commit 93f853d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
9 changes: 0 additions & 9 deletions jest-env.js

This file was deleted.

28 changes: 28 additions & 0 deletions lib/__tests__/runtime.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const runtime = require('../runtime')
const vm = require('vm')

describe('runtime', () => {
test('context allows overriding of properties via bindings', async () => {
Expand Down Expand Up @@ -32,10 +33,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)
})
})
15 changes: 15 additions & 0 deletions lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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)
}
}
Expand Down
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"typescript": "^3.2.2"
},
"jest": {
"testEnvironment": "./jest-env",
"testEnvironment": "node",
"automock": false,
"setupTestFrameworkScriptFile": "./jest-setup-framework.js",
"testPathIgnorePatterns": [
Expand Down

0 comments on commit 93f853d

Please sign in to comment.