镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

vm: filename option to vm.runInNewContext() ignored #27055

Description

@bnoordhuis

This should work according to the docs but currently doesn't:

require('vm').runInNewContext(`throw Error('boom')`, {filename: 'boom.js'})

It shows up as evalmachine.<anonymous> instead of boom.js.

Confirmed with master and all release lines. It's apparently not a recent regression.

Regression test:

diff --git a/test/parallel/test-vm-run-in-new-context.js b/test/parallel/test-vm-run-in-new-context.js
index 51577668cf..6c8cc921a7 100644
--- a/test/parallel/test-vm-run-in-new-context.js
+++ b/test/parallel/test-vm-run-in-new-context.js
@@ -72,14 +72,13 @@ global.gc();
 fn();
 // Should not crash
 
-{
+for (const cb of [(code, filename) => vm.runInNewContext(code, {}, filename),
+                  (code, filename) => vm.runInNewContext(code, { filename })]) {
   // Verify that providing a custom filename as a string argument works.
   const code = 'throw new Error("foo");';
   const file = 'test_file.vm';
 
-  assert.throws(() => {
-    vm.runInNewContext(code, {}, file);
-  }, (err) => {
+  assert.throws(() => cb(code, file), (err) => {
     const lines = err.stack.split('\n');
 
     assert.strictEqual(lines[0].trim(), `${file}:1`);

Activity

  1. bnoordhuis commented on Apr 2, 2019

    @bnoordhuis
    MemberAuthor

    Looking at it from another perspective, maybe it's just a documentation issue? From vm.md:

    ## vm.runInNewContext(code[, sandbox[, options]])

    To me that reads as saying the sandbox argument can be omitted but I think it should be interpreted as "sandbox can be omitted only when options is"? It's not super clear, you can't really divine it from the description either.

  2. bnoordhuis commented on Apr 2, 2019

    @bnoordhuis
    MemberAuthor

    Thinking about it more, I don't see a way of making it work the way I expected it to (no way to know if the second arg is the sandbox or the options object in a two arg call) so I'll go ahead and close this out and think about how to improve the docs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    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