Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

vm.Script inheritance error #555

Open
jdalton opened this issue Jun 22, 2018 · 5 comments
Open

vm.Script inheritance error #555

jdalton opened this issue Jun 22, 2018 · 5 comments

Comments

@jdalton
Copy link
Member

jdalton commented Jun 22, 2018

In node-chakracore 10.1.0 the following throws

class S extends vm.Script {}
for (const n of Reflect.ownKeys(vm.Script.prototype)) S.prototype[n] = vm.Script.prototype[n]
Object.setPrototypeOf(S.prototype, null)
new S('1+2').runInThisContext() // throws

It does not throw in Node 10.x in v8

@MSLaguana
Copy link
Contributor

This is somewhat interesting; the native code checks that the script object (the new S in rhis case) is an instance of ContextifyScript. For us, we do that via the javascript semantics of instanceof, and due to your manipulations of the prototype that instanceof check fails (in both v8 and chakracore) but v8's native implementation of ContextifyScript::HasInstance uses different internal behaviors to do with function templates to determine that the object was originally related to ContextifyScript.

@MSLaguana
Copy link
Contributor

@jdalton do you have a scenario where you wanted to do something like this and expected it to work?

I did check the other case, where you take a non-vm.Script object and set its prototype to vm.Script.prototype, and that ends up erroring out with "Illegal Invocation" in both v8 and chakra. I think that the requirement from native code here is just that we can get access to the wrapped native object that is created as part of ContextifyScript (which is the superclass of vm.Script), but I don't believe that with chakra/chakrashim we can currently tell how an object was constructed.

@jdalton
Copy link
Member Author

jdalton commented Jun 25, 2018

I ran into some code working in node-V8 and not in node-chakracore. The workaround was to set the prototype to that of the ContextifyScript prototype instead of null.

@MSLaguana
Copy link
Contributor

I'm curious what the intent of the code was; why was the code removing the prototype?

@jdalton
Copy link
Member Author

jdalton commented Jun 25, 2018

Creating a "safe" reference to the vm module here the safe() helper will null the prototype.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants