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

can't assign function to global variable ( code execution happens inside script.runInContext) #2094

Closed
joxerTMD opened this issue Aug 2, 2019 · 2 comments

Comments

@joxerTMD
Copy link

joxerTMD commented Aug 2, 2019

Hi i'm facing a very strange behavior and would be glad for some help or explanation

  • Node.js Version: v12.1.0
  • OS:
    System Version: macOS 10.13.6 (17G2208)
    Kernel Version: Darwin 17.7.0
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant):

This code fails during execution (i'm trying to assign a function inside isolated context)

const vm = require('vm')
const codeString = `
'use strict';
exports = function someFunc() {}
`
const s = new vm.Script(codeString);
s.runInContext(vm.createContext({
    exports: {}
}));

The error is:

ReferenceError: exports is not defined
    at evalmachine.<anonymous>:3:9
    at Script.runInContext (vm.js:134:20)
    at Object.<anonymous>

looks like its somehow looses context passed with

vm.createContext({
    exports: {}
})

however this code completes with no errors ( assign someString instead of function)...

const vm = require('vm')
const codeString = `
'use strict';
exports = 'someString'
`
const s = new vm.Script(codeString);
s.runInContext(vm.createContext({
    exports: {}
}));
@Hakerh400
Copy link

That is a known limitation. See relevant discussions: #5344, #12300, #5679. It was partially fixed in #16487, but it still does not work for functions (in node_contextify.cc on line 411 - !is_function check). This issue is much more complex than it seems and fixing it would probably require refactoring the whole underlying logic of the vm module. There was a tracking issue #6283, but it has been closed with some unresolved cases.

@gireeshpunathil
Copy link
Member

closing as answered, pls reopen if there is anything outstanding.

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

No branches or pull requests

3 participants