Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ({}).constructor===Object #1047

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions packages/vm/src/edge-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,31 @@ const transferableConstructors = [
'TypeError',
] as const

function patchInstanceOf(item: string, ctx: any) {
const patchedPrototypes = new Set<(typeof transferableConstructors)[number]>([
'Array',
'Object',
'RegExp',
])

function patchInstanceOf(
item: (typeof transferableConstructors)[number],
ctx: any,
) {
// @ts-ignore
ctx[Symbol.for(`node:${item}`)] = eval(item)

return runInContext(
const patchPrototype = !patchedPrototypes.has(item)
? ''
: `Object.assign(globalThis.${item}.prototype, {
get constructor() {
return proxy;
}
})`

runInContext(
`
globalThis.${item} = new Proxy(${item}, {
(() => {
const proxy = new Proxy(${item}, {
get(target, prop, receiver) {
if (prop === Symbol.hasInstance && receiver === globalThis.${item}) {
const nodeTarget = globalThis[Symbol.for('node:${item}')];
Expand All @@ -137,8 +155,22 @@ function patchInstanceOf(item: string, ctx: any) {
}

return Reflect.get(target, prop, receiver);
},
construct(target, args, newTarget) {
const value = Reflect.construct(target, args, newTarget)
Object.defineProperty(value, 'constructor', {
value: proxy,
writable: true,
configurable: true,
enumerable: false,
})
return value;
}
})

globalThis.${item} = proxy;
${patchPrototype}
})()
`,
ctx,
)
Expand Down
29 changes: 29 additions & 0 deletions packages/vm/tests/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ it('handles prototype chain correctly', () => {
})
})

describe('.constructor ===', () => {
describe('created inside the vm', () => {
test('new Object().constructor === Object', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`new Object().constructor === Object`)).toBe(true)
})
test('({}).constructor === Object', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`({}).constructor === Object`)).toBe(true)
})
test('new Array().constructor === Array', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`new Array().constructor === Array`)).toBe(true)
})
test('[].constructor === Array', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`[].constructor === Array`)).toBe(true)
})
test('new RegExp("").constructor === Array', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`new RegExp("").constructor === RegExp`)).toBe(true)
})
test('[].constructor === Array', () => {
const vm = new EdgeVM()
expect(vm.evaluate(`/./.constructor === RegExp`)).toBe(true)
})
})
})

describe('instanceof overriding', () => {
test('binary array created outside of the VM is `instanceof` Object inside the VM', () => {
const vm = new EdgeVM()
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/tests/integration/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('Error maintains a stack trace', async () => {
vm.evaluate(`(${fn})()`)

expect(log).toHaveBeenCalledTimes(1)
expect(log.mock.lastCall[0]).toMatch(/^Error: hello, world!\s+at fn/m)
expect(log.mock.lastCall[0]).toMatch(/^Error: hello, world!\s+at Object/m)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is my main concern 👀

})

test('additional error properties', async () => {
Expand Down
Loading