docs: parsingContext option for vm.compileFunction is a little unclear #23194
Closed
Description
- Version: v10.10.0
- Platform: All Platforms
- Subsystem: vm
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.
- I initially thought the
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.
- I'm not fully sure of how the context changes the behaviour of the function (if at all). I initially thought It was similar to how
runInContext
works, i.e. whatever I put in the context, will be available globally inside the code. Take this example
const vm = require('vm')
const parsingContext = vm.createContext({name: 'world'})
const code = `return 'hello ' + name`
const fn = vm.compileFunction(code, [], { parsingContext })
console.log(fn())
I thought perhaps the name
property would be available inside the function at runtime but I guess I'm wrong. Instead I see this error:
:1
return 'hello ' + name
^
ReferenceError: name is not defined
at <anonymous>:1:19
at Object.<anonymous> (/Users/dara/mydev/vmtest/index.js:9:13)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:279:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)
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!
Metadata
Assignees
Labels
No labels