Skip to content

vm: pass correct context to CompileFunctionInContext in CompileFunction

Rodrigo Muino Tomonari requested to merge github/fork/darahayes/master into master

Fixes: https://github.com/nodejs/node/issues/23194

The ContextifyContext::CompileFunction function was accidentally passing the wrong context variable into ScriptCompiler::CompileFunctionInContext this resulted in the incorrect context being passed to the compiled function.

You can quickly test this with the following code:

const vm = require('vm')

const name = 'world'
const parsingContext = vm.createContext({ name: 'world' })

const code = `return 'hello ' + name`

const fn = vm.compileFunction(code, [], { parsingContext })

console.log(fn())

With Node v10.10.0 I get the following output:

:1
return 'hello ' + name
                  ^

ReferenceError: name is not defined
    at <anonymous>:1:19
    at Object.<anonymous> (/Users/dara/mydev/vmtest/index.js:10: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)

With the proposed changes, the output is hello world.

I also edited the description of the parsingContext option in the docs to explicitly use the word contextified. The docs use this word everywhere else when referring to a context object.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Merge request reports

Loading