Skip to content

Broken example for vm.runInDebugContext(code) #4862

Description

@MylesBorins

The current example fails

const Debug = vm.runInDebugContext('Debug');
Debug.scripts().forEach(function(script) { console.log(script.name); });

The error I receive is

Debug.scripts().forEach(function(script) {
      ^
illegal access

Will do more research and post in this thread

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    vmIssues and PRs related to the vm subsystem.
    on Jan 25, 2016
  2. MylesBorins commented on Jan 25, 2016

    @MylesBorins
    ContributorAuthor

    /cc @nodejs/documentation

  3. bnoordhuis commented on Jan 25, 2016

    @bnoordhuis
    Member

    Oh, that's because you need to start the debugger first now before you have access to Debug.scripts(). I'd swap it out for a different example.

  4. bnoordhuis commented on Jan 25, 2016

    @bnoordhuis
    Member

    Like this:

    > vm.runInDebugContext('Debug').findScript(process.exit).name
    'node.js'
    
    > vm.runInDebugContext('Debug').findScript(process.emit).name
    'events.js'
    
  5. MylesBorins commented on Jan 25, 2016

    @MylesBorins
    ContributorAuthor

    @bnoordhuis can you share an example of running arbitrary in a vm context?

    I think I can get a test for #4819 by running some code in a debug context, but perhaps I am missing some... context (sorry couldn't help it).

    When I try something like

    const Debug = vm.runInDebugContext('var util = require("util");');
    

    I get

    undefined:1
    var util = require("util");
               ^
    ReferenceError: require is not defined
        at <anonymous>:1:12

    As the definition of runInDebugContext is vm.runInDebugContext(code) I thought I could feed arbitrary code. It appears that this context does not by default have access to all the node bits in the main context (this is a guess).

    I'll dig into the c++ and report back

  6. bnoordhuis commented on Jan 25, 2016

    @bnoordhuis
    Member

    As the definition of runInDebugContext is vm.runInDebugContext(code) I thought I could feed arbitrary code. It appears that this context does not by default have access to all the node bits in the main context (this is a guess).

    That's correct. It's a clean slate JS VM with debugger bits but no node bits. It's similar to vm.runInNewContext(code) in that respect, except that doesn't even have debugger bits.

  7. MylesBorins commented on Jan 25, 2016

    @MylesBorins
    ContributorAuthor

    @bnoordhuis ahhh. That makes a lot of sense. I'll dig into the debugger source and see how it sets up the environment. That is a bit out of scope for this issue though, so I'll try and put together a working example for VM and perhaps some documentation about the context

  8. self-assigned this
    on Jan 25, 2016
  9. yorickvP commented on May 14, 2016

    @yorickvP

    Did you figure out how to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docIssues and PRs related to Node.js documentation.vmIssues and PRs related to the vm subsystem.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions