-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
docs: parsingContext option for vm.compileFunction is a little unclear #23194
Comments
@nodejs/vm |
Hi, @darahayes 👋 I added @hashseed @addaleax do you think we should pass |
Hey @ryzokuken yeah it looks like we're creating I'm not familiar with this code but it looks like perhaps we should be passing That might explain the behaviour I see in the example I gave above? I could be totally wrong but I'll try out the change locally and see what happens. It'll be a learning experience for me either way 😊 |
So just a follow up. I cloned Node and changed the following line: Line 1059 in 1d9ec04
parsing_context instead of context and tested against two code examples.
Code Example 1const vm = require('vm')
const parsingContext = vm.createContext({name: 'world'})
const code = `return 'hello ' + name`
const fn = vm.compileFunction(code, [], { parsingContext })
console.log(fn()) Code Example 2const vm = require('vm')
const name = 'world'
const parsingContext = vm.createContext({ foo: 'bar' })
const code = `return global`
const fn = vm.compileFunction(code, [], { parsingContext })
console.log(global === fn()) Results on Node v10.10.0Example 1I get the following result:
Question: Is this expected behaviour? Example 2I get Results on locally modified Node (using
|
compiled functions won't have access to any lexically declared variables because they don't exist in any lexical scope. the |
@devsnek thanks for that clarification. I suppose that clears up the one thing I was confused about. If I'm understanding things properly though, we still may have found a bug ( |
Yes, that is correct.
I do believe that this was an oversight on my part when building this behavior and since your results confirm that it's fixed with the changes, I think it's a good idea to make a PR for the same. Thank you for your work. |
@ryzokuken thanks so much for your help. I've opened a PR which I hope is useful :) |
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: nodejs#23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
ContextifyContext::CompileFunction in src/node_contextify.cc was incorrectly passing the context variable to ScriptCompiler::CompileFunctionInContext This meant that the parsingContext option in vm.compileFunction was not being applied properly to the compiled function. fixes: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Hi there, perhaps this is not an issue. If so feel free to close. I've been playing around with the
vm
API and I found that the compileFunction docs were a little unclear.There is an option called
parsingContext
and its description is<Object> The sandbox/context in which the said function should be compiled in.
I had two problems with this.
parsingContext
might be a plain object, but it's actually supposed to be aContext
object. The reason I got confused with this is because similar options for other functions in thevm
API explicitly say the object is contextified. See the runInContext docs as an example. Perhaps the description could be updated to something like<Object> The contextified sandbox in which the said function should be compiled in.
runInContext
works, i.e. whatever I put in the context, will be available globally inside the code. Take this exampleI thought perhaps the
name
property would be available inside the function at runtime but I guess I'm wrong. Instead I see this error:Based off the description, I guess the context somehow comes into play at compile time as opposed to run time, but I'm not 100% sure how it affects the code.
I'd be super happy to open a docs PR to clarify this if I could get a little help. Cheers!
The text was updated successfully, but these errors were encountered: