Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Node issue #1140: Fix incorrect dispatch of vm.runInContext for argument "filename" #1141

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions doc/api/vm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ requires a separate process.
In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr
and throws an exception.

### vm.runInContext(code, context, [filename])

`vm.runInContext` compiles `code` to run in context `context` as if it were loaded from `filename`,
then runs it and returns the result. A (V8) context comprises a global object, together with a
set of built-in objects and functions. Running code does not have access to local scope and
the global object held within `context` will be used as the global object for `code`.
`filename` is optional.

Example: compile and execute code in a existing context.

var util = require('util'),
vm = require('vm'),
initSandbox = {
animal: 'cat',
count: 2
},
context = vm.createContext(initSandbox);

vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
console.log(util.inspect(context));

// { animal: 'cat', count: 3, name: 'CATT' }

Note that `createContext` will perform a shallow clone of the supplied sandbox object in order to
initialise the global object of the freshly constructed context.

Note that running untrusted code is a tricky business requiring great care. To prevent accidental
global variable leakage, `vm.runInContext` is quite useful, but safely running untrusted code
requires a separate process.

In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr
and throws an exception.

### vm.createContext([initSandbox])

`vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent
call to `vm.runInContext`. A (V8) context comprises a global object together with a set of
build-in objects and functions. The optional argument `initSandbox` will be shallow-copied
to seed the initial contents of the global object used by the context.

### vm.createScript(code, [filename])

Expand Down
2 changes: 1 addition & 1 deletion src/node_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) {
}

const int filename_index = sandbox_index +
(context_flag == newContext ? 1 : 0);
(context_flag == thisContext? 0 : 1);
Local<String> filename = args.Length() > filename_index
? args[filename_index]->ToString()
: String::New("evalmachine.<anonymous>");
Expand Down
12 changes: 12 additions & 0 deletions test/simple/test-script-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ assert.equal('lala', context.thing);

// Issue GH-227:
Script.runInNewContext('', null, 'some.js');

// Issue GH-1140:
common.debug('test runInContext signature');
var gh1140Exception;
try {
Script.runInContext('throw new Error()', context, 'expected-filename.js');
}
catch (e) {
gh1140Exception = e;
assert.ok(/expected-filename/.test(e.stack), 'expected appearance of filename in Error stack');
}
assert.ok(gh1140Exception, 'expected exception from runInContext signature test');