FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

vm: pass correct context to CompileFunctionInContext in CompileFunction by darahayes · Pull Request #23206 · nodejs/node · GitHub

/ node Public

vm: pass correct context to CompileFunctionInContext in CompileFunction - #23206

Closed
darahayes wants to merge 1 commit into
nodejs:masterfrom
darahayes:master
Closed

vm: pass correct context to CompileFunctionInContext in CompileFunction#23206
darahayes wants to merge 1 commit into
nodejs:masterfrom
darahayes:master

Conversation

darahayes commented Oct 1, 2018
edited
Loading

Copy link
Copy Markdown

Fixes: #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

nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. vm Issues and PRs related to the vm subsystem. labels Oct 1, 2018

ryzokuken left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks great! Could you please add a test?

darahayes commented Oct 1, 2018
edited
Loading

Copy link
Copy Markdown
Author

@ryzokuken I'm also wondering. In these lines:

https://github.com/nodejs/node/pull/23206/files#diff-0cf206672499c2f86db4ffb0cc5b668bR1041

and

https://github.com/nodejs/node/pull/23206/files#diff-0cf206672499c2f86db4ffb0cc5b668bR1052

should we also be checking against parsing_context instead of context? I'm not actually 100% sure what this check is doing but it looks like we're checking the wrong object.

Sorry for the slowness here, I've never actually written c++ before and I'm not exactly familiar with the v8 APIs. I am trying though 😄

Copy link
Copy Markdown
Author

@ryzokuken I'd be happy to help write a test. Is it good enough to test this from the JS layer or should the test be written in C++? Sorry for all the questions!

devsnek commented Oct 1, 2018

Copy link
Copy Markdown
Member

@darahayes

In these lines ... should we also be checking against parsing_context instead of context?

Nope. Those get calls should happen in whatever the current context is.

Copy link
Copy Markdown
Author

@devsnek thanks. Now I think understand what's happening in both of those loops. We're basically checking (by name) to see if that particular value exists in the current context, and if so, then we can add it right?

Working on some small tests now!

Copy link
Copy Markdown
Author

@ryzokuken just added some small test cases. Hope they're okay :)

darahayes changed the title Pass correct context to CompileFunctionInContext in vm.compileFunction vm: pass correct context to CompileFunctionInContext in CompileFunction Oct 1, 2018

Copy link
Copy Markdown
Contributor

@darahayes

Is it good enough to test this from the JS layer or should the test be written in C++?

JS tests should be fine. Thanks.

ryzokuken left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The tests look great, can you confirm that they all pass with the changed code?

Copy link
Copy Markdown
Author

@ryzokuken using ./configure && make -j4 test shows everything passes.

Additionally, I was able to run the individual test file I modified with python tools/test.py -J --mode=release parallel/test-vm-basic.

Surely this would be running in the Travis build too right?

mscdex commented Oct 1, 2018

Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Contributor

@darahayes It would, yes! The CI failures are probably not related to your change, and it will be fixed soon.

Copy link
Copy Markdown
Author

pinging @ryzokuken, just wondering is there any more work to be done on my side to get this merged? Thanks!

Copy link
Copy Markdown
Member

@darahayes while we are at it could you rebase on master to make CI easier?

Copy link
Copy Markdown
Author

@lundibundi just rebased!

Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Contributor

CI is green! If nobody lands with in the next few hours, I will.

ryzokuken left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Final LG.

Copy link
Copy Markdown
Contributor

@darahayes shucks, the other two commits aren't fixups. Would you mind squashing the three commits into one?

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

Copy link
Copy Markdown
Author

@ryzokuken looks like the build failed for some unrelated reason. Some timeout. Is it possible to rerun it and see what happens?

Copy link
Copy Markdown
Contributor

@darahayes timeout in not ok 519 parallel/test-worker-memory indeed. Weird that it'd happen only on Windows. Could you check if it's a known flake?

Copy link
Copy Markdown
Contributor

@darahayes here we go: #23277

Copy link
Copy Markdown
Contributor

/cc @Trott

TimothyGu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Now that parsingContext is also the context/realm in which the function is created, the name becomes a misnomer. For v8.Script, the parsingContext option only controls behavior seen through the Inspector API, as the created script is not bound to a V8 context. I suggest we change the name to context or something similar, if we can still do so.

Trott commented Oct 10, 2018

Copy link
Copy Markdown
Member

/cc @Trott

Hi! Are you looking for a review from me? Or is there something else you'd like/need?

lundibundi commented Oct 10, 2018
edited
Loading

Copy link
Copy Markdown
Member

Resume CI: https://ci.nodejs.org/job/node-test-pull-request/17728/

Though, @darahayes perhaps you can come up with a better name for the context (refs #23206 (review))

lundibundi added the author ready PRs that have at least one approval, no outstanding review comments, and a CI started. label Oct 10, 2018

Copy link
Copy Markdown
Contributor

@Trott pinged you because of the flaky test, thought it'd bring this particular failure to your notice plus you'll have a better idea about the flake in general 😅.

Trott commented Oct 11, 2018

Copy link
Copy Markdown
Member

@Trott pinged you because of the flaky test, thought it'd bring this particular failure to your notice plus you'll have a better idea about the flake in general 😅.

Ah! Well, yes, that test has been observed timing out elsewhere and is reported in #23277. The Resume Build passed, so I think you can proceed.

danbev commented Oct 11, 2018

Copy link
Copy Markdown
Contributor

Landed in 8da674d.

danbev closed this Oct 11, 2018
danbev pushed a commit that referenced this pull request Oct 11, 2018
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>

Copy link
Copy Markdown
Author

Though, @darahayes perhaps you can come up with a better name for the context (refs #23206 (review))

Apologies for the delay in getting back to this. I would have preferred to get my changes right over getting them merged right now.

For v8.Script, the parsingContext option only controls behavior seen through the Inspector API, as the created script is not bound to a V8 context.

You'll have to forgive me, I'm super new to this.

  • I'm not exactly sure what the Inspector API is.
  • It might be a silly question, but is the created script not bound to the context that we pass into ScriptCompiler::CompileFunctionInContext? Or is it just compiled in that context, but at runtime, it's not bound to anything?

In any case if you folks think it's more appropriate, I'd be happy to submit a small PR that renames parsingContext to context from the JS side and the native side (and docs of course).

This would rename an option passed in from userland code, so these would be breaking changes right? Is there anything from a process or technical perspective that I need to be aware of when submitting a PR with breaking changes?

danbev commented Oct 11, 2018

Copy link
Copy Markdown
Contributor

I would have preferred to get my changes right over getting them merged right now.

Sorry about that. I saw the author ready label and thought is was good to go.

Copy link
Copy Markdown
Author

Sorry about that.

It's all good. It's really my fault because I didn't respond quick enough 😄

lundibundi commented Oct 11, 2018
edited
Loading

Copy link
Copy Markdown
Member

IMO it's not like this PR is not 'ready', that part was an addition that could happen here or in a follow-up PR as this one was opened for 'pass correct context to CompileFunctionInContext in CompileFunction'.
Also, I think that change deserves it's own PR as this one was almost ready at that point and it was unlikely that any more people would look through it lessening the possibility of coming up with a better name/other improvements connected.

Edit: also, @darahayes no worries, there is no such thing as be quick enough, there are obviously some sane limits of how much time a change might take but usually a PR is either 'needs changes' and it will wait or 'ready to go'. If the change was critical people would just use 'Request changes' to make sure this doesn't get landed before they are fixed.

Copy link
Copy Markdown
Author

Thanks @lundibundi I'm happy to create a new issue around the naming and work on a follow up PR if you think that's the way to go 😊

targos pushed a commit that referenced this pull request Oct 12, 2018
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>
jasnell pushed a commit that referenced this pull request Oct 17, 2018
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>
MylesBorins pushed a commit that referenced this pull request Oct 30, 2018
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>
codebytere mentioned this pull request Nov 27, 2018
rvagg pushed a commit that referenced this pull request Nov 28, 2018
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>
MylesBorins pushed a commit that referenced this pull request Nov 29, 2018
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>
codebytere mentioned this pull request Nov 29, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. vm Issues and PRs related to the vm subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: parsingContext option for vm.compileFunction is a little unclear

Back | FazBrowse Home | New Git URL