Skip to content

Commit 7afdba6

Browse files
domenicisaacs
authored andcommittedAug 21, 2013
vm, core, module: re-do vm to fix known issues
As documented in #3042 and in [1], the existing vm implementation has many problems. All of these are solved by @brianmcd's [contextify][2] package. This commit uses contextify as a conceptual base and its code core to overhaul the vm module and fix its many edge cases and caveats. Functionally, this fixes #3042. In particular: - A context is now indistinguishable from the object it is based on (the "sandbox"). A context is simply a sandbox that has been marked by the vm module, via `vm.createContext`, with special internal information that allows scripts to be run inside of it. - Consequently, items added to the context from anywhere are immediately visible to all code that can access that context, both inside and outside the virtual machine. This commit also smooths over the API very slightly: - Parameter defaults are now uniformly triggered via `undefined`, per ES6 semantics and previous discussion at [3]. - Several undocumented and problematic features have been removed, e.g. the conflation of `vm.Script` with `vm` itself, and the fact that `Script` instances also had all static `vm` methods. The API is now exactly as documented (although arguably the existence of the `vm.Script` export is not yet documented, just the `Script` class itself). In terms of implementation, this replaces node_script.cc with node_contextify.cc, which is derived originally from [4] (see [5]) but has since undergone extensive modifications and iterations to expose the most useful C++ API and use the coding conventions and utilities of Node core. The bindings exposed by `process.binding('contextify')` (node_contextify.cc) replace those formerly exposed by `process.binding('evals')` (node_script.cc). They are: - ContextifyScript(code, [filename]), with methods: - runInThisContext() - runInContext(sandbox, [timeout]) - makeContext(sandbox) From this, the vm.js file builds the entire documented vm module API. node.js and module.js were modified to use this new native binding, or the vm module itself where possible. This introduces an extra line or two into the stack traces of module compilation (and thus into most stack traces), explaining the changed tests. The tests were also updated slightly, with all vm-related simple tests consolidated as test/simple/test-vm-* (some of them were formerly test/simple/test-script-*). At the same time they switched from `common.debug` to `console.error` and were updated to use `assert.throws` instead of rolling their own error-testing methods. New tests were also added, of course, demonstrating the new capabilities and fixes. [1]: http://nodejs.org/docs/v0.10.16/api/vm.html#vm_caveats [2]: https://github.com/brianmcd/contextify [3]: nodejs/node-v0.x-archive#5323 (comment) [4]: https://github.com/kkoopa/contextify/blob/bf123f3ef960f0943d1e30bda02e3163a004e964/src/contextify.cc [5]: https://gist.github.com/domenic/6068120
1 parent 3602d4c commit 7afdba6

26 files changed

+799
-588
lines changed
 

‎lib/module.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
var NativeModule = require('native_module');
2323
var util = NativeModule.require('util');
24-
var Script = process.binding('evals').NodeScript;
25-
var runInThisContext = Script.runInThisContext;
26-
var runInNewContext = Script.runInNewContext;
24+
var runInThisContext = require('vm').runInThisContext;
25+
var runInNewContext = require('vm').runInNewContext;
2726
var assert = require('assert').ok;
2827

2928

@@ -413,7 +412,7 @@ Module.prototype._compile = function(content, filename) {
413412
sandbox.global = sandbox;
414413
sandbox.root = root;
415414

416-
return runInNewContext(content, sandbox, filename, 0, true);
415+
return runInNewContext(content, sandbox, filename);
417416
}
418417

419418
debug('load root module');
@@ -424,13 +423,13 @@ Module.prototype._compile = function(content, filename) {
424423
global.__dirname = dirname;
425424
global.module = self;
426425

427-
return runInThisContext(content, filename, 0, true);
426+
return runInThisContext(content, filename);
428427
}
429428

430429
// create wrapper function
431430
var wrapper = Module.wrap(content);
432431

433-
var compiledWrapper = runInThisContext(wrapper, filename, 0, true);
432+
var compiledWrapper = runInThisContext(wrapper, filename);
434433
if (global.v8debug) {
435434
if (!resolvedArgv) {
436435
// we enter the repl if we're not given a filename argument.

‎lib/vm.js

+40-28
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,49 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
var binding = process.binding('evals');
23-
24-
module.exports = Script;
25-
Script.Script = Script;
22+
var binding = process.binding('contextify');
23+
var Script = binding.ContextifyScript;
2624
var util = require('util');
2725

28-
function Script(code, ctx, filename) {
29-
if (!(this instanceof Script)) {
30-
return new Script(code, ctx, filename);
26+
// The binding provides a few useful primitives:
27+
// - ContextifyScript(code, [filename]), with methods:
28+
// - runInThisContext()
29+
// - runInContext(sandbox, [timeout])
30+
// - makeContext(sandbox)
31+
// From this we build the entire documented API.
32+
33+
Script.prototype.runInNewContext = function(initSandbox, timeout) {
34+
var context = exports.createContext(initSandbox);
35+
return this.runInContext(context, timeout);
36+
};
37+
38+
exports.Script = Script;
39+
40+
exports.createScript = function(code, filename) {
41+
return new Script(code, filename);
42+
};
43+
44+
exports.createContext = function(initSandbox) {
45+
if (util.isUndefined(initSandbox)) {
46+
initSandbox = {};
3147
}
3248

33-
var ns = new binding.NodeScript(code, ctx, filename);
34-
35-
// bind all methods to this Script object
36-
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
37-
if (util.isFunction(binding.NodeScript.prototype[f])) {
38-
this[f] = function() {
39-
if (!(this instanceof Script)) {
40-
throw new TypeError('invalid call to ' + f);
41-
}
42-
return ns[f].apply(ns, arguments);
43-
};
44-
}
45-
}, this);
46-
}
47-
48-
Script.createScript = function(code, ctx, name) {
49-
return new Script(code, ctx, name);
49+
binding.makeContext(initSandbox);
50+
51+
return initSandbox;
52+
};
53+
54+
exports.runInContext = function(code, sandbox, filename, timeout) {
55+
var script = exports.createScript(code, filename);
56+
return script.runInContext(sandbox, timeout);
5057
};
5158

52-
Script.createContext = binding.NodeScript.createContext;
53-
Script.runInContext = binding.NodeScript.runInContext;
54-
Script.runInThisContext = binding.NodeScript.runInThisContext;
55-
Script.runInNewContext = binding.NodeScript.runInNewContext;
59+
exports.runInNewContext = function(code, sandbox, filename, timeout) {
60+
var script = exports.createScript(code, filename);
61+
return script.runInNewContext(sandbox, timeout);
62+
};
63+
64+
exports.runInThisContext = function(code, filename, timeout) {
65+
var script = exports.createScript(code, filename);
66+
return script.runInThisContext(timeout);
67+
};

‎node.gyp

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@
9494
'src/node.cc',
9595
'src/node_buffer.cc',
9696
'src/node_constants.cc',
97+
'src/node_contextify.cc',
9798
'src/node_extensions.cc',
9899
'src/node_file.cc',
99100
'src/node_http_parser.cc',
100101
'src/node_javascript.cc',
101102
'src/node_main.cc',
102103
'src/node_os.cc',
103-
'src/node_script.cc',
104104
'src/node_stat_watcher.cc',
105105
'src/node_watchdog.cc',
106106
'src/node_zlib.cc',
@@ -120,13 +120,13 @@
120120
'src/node.h',
121121
'src/node_buffer.h',
122122
'src/node_constants.h',
123+
'src/node_contextify.h',
123124
'src/node_extensions.h',
124125
'src/node_file.h',
125126
'src/node_http_parser.h',
126127
'src/node_javascript.h',
127128
'src/node_os.h',
128129
'src/node_root_certs.h',
129-
'src/node_script.h',
130130
'src/node_version.h',
131131
'src/node_watchdog.h',
132132
'src/node_wrap.h',

‎src/node.cc

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "node_file.h"
2626
#include "node_http_parser.h"
2727
#include "node_javascript.h"
28-
#include "node_script.h"
2928
#include "node_version.h"
3029

3130
#if defined HAVE_PERFCTR

‎src/node.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@
426426
'global.require = require;\n' +
427427
'return require("vm").runInThisContext(' +
428428
JSON.stringify(body) + ', ' +
429-
JSON.stringify(name) + ', 0, true);\n';
429+
JSON.stringify(name) + ');\n';
430430
}
431431
var result = module._compile(script, name + '-wrapper');
432432
if (process._print_eval) console.log(result);
@@ -717,8 +717,11 @@
717717
// core modules found in lib/*.js. All core modules are compiled into the
718718
// node binary, so they can be loaded faster.
719719

720-
var Script = process.binding('evals').NodeScript;
721-
var runInThisContext = Script.runInThisContext;
720+
var ContextifyScript = process.binding('contextify').ContextifyScript;
721+
function runInThisContext(code, filename) {
722+
var script = new ContextifyScript(code, filename);
723+
return script.runInThisContext();
724+
}
722725

723726
function NativeModule(id) {
724727
this.filename = id + '.js';
@@ -779,7 +782,7 @@
779782
var source = NativeModule.getSource(this.id);
780783
source = NativeModule.wrap(source);
781784

782-
var fn = runInThisContext(source, this.filename, 0, true);
785+
var fn = runInThisContext(source, this.filename);
783786
fn(this.exports, NativeModule.require, this, this.filename);
784787

785788
this.loaded = true;

‎src/node_contextify.cc

+484
Large diffs are not rendered by default.

‎src/node_script.h ‎src/node_contextify.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
#ifndef SRC_NODE_SCRIPT_H_
23-
#define SRC_NODE_SCRIPT_H_
22+
#ifndef SRC_NODE_CONTEXTIFY_H_
23+
#define SRC_NODE_CONTEXTIFY_H_
2424

2525
#include "node.h"
2626
#include "node_object_wrap.h"
@@ -29,8 +29,8 @@
2929

3030
namespace node {
3131

32-
void InitEvals(v8::Handle<v8::Object> target);
32+
void InitContextify(v8::Handle<v8::Object> target);
3333

3434
} // namespace node
3535

36-
#endif // SRC_NODE_SCRIPT_H_
36+
#endif // SRC_NODE_CONTEXTIFY_H_

‎src/node_extensions.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
START \
3535
ITEM(node_buffer) \
3636
NODE_EXT_LIST_SSL(ITEM) \
37-
ITEM(node_evals) \
37+
ITEM(node_contextify) \
3838
ITEM(node_fs) \
3939
ITEM(node_http_parser) \
4040
ITEM(node_os) \

‎src/node_script.cc

-458
This file was deleted.

‎test/message/eval_messages.out

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
with(this){__filename}
55
^^^^
66
SyntaxError: Strict mode code may not include a with statement
7+
at Object.exports.createScript (vm.js:*)
8+
at Object.exports.runInThisContext (vm.js:*)
79
at Object.<anonymous> ([eval]-wrapper:*:*)
810
at Module._compile (module.js:*:*)
911
at evalScript (node.js:*:*)
@@ -17,6 +19,7 @@ throw new Error("hello")
1719
^
1820
Error: hello
1921
at [eval]:1:7
22+
at Object.exports.runInThisContext (vm.js:*)
2023
at Object.<anonymous> ([eval]-wrapper:*:*)
2124
at Module._compile (module.js:*:*)
2225
at evalScript (node.js:*:*)
@@ -28,6 +31,7 @@ throw new Error("hello")
2831
^
2932
Error: hello
3033
at [eval]:1:7
34+
at Object.exports.runInThisContext (vm.js:*)
3135
at Object.<anonymous> ([eval]-wrapper:*:*)
3236
at Module._compile (module.js:*:*)
3337
at evalScript (node.js:*:*)
@@ -40,6 +44,7 @@ var x = 100; y = x;
4044
^
4145
ReferenceError: y is not defined
4246
at [eval]:1:16
47+
at Object.exports.runInThisContext (vm.js:*)
4348
at Object.<anonymous> ([eval]-wrapper:*:*)
4449
at Module._compile (module.js:*:*)
4550
at evalScript (node.js:*:*)

‎test/message/stdin_messages.out

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
with(this){__filename}
55
^^^^
66
SyntaxError: Strict mode code may not include a with statement
7+
at Object.exports.createScript (vm.js:*)
8+
at Object.exports.runInThisContext (vm.js:*)
79
at Object.<anonymous> ([stdin]-wrapper:*:*)
810
at Module._compile (module.js:*:*)
911
at evalScript (node.js:*:*)
@@ -19,6 +21,7 @@ throw new Error("hello")
1921
^
2022
Error: hello
2123
at [stdin]:1:*
24+
at Object.exports.runInThisContext (vm.js:*)
2225
at Object.<anonymous> ([stdin]-wrapper:*:*)
2326
at Module._compile (module.js:*:*)
2427
at evalScript (node.js:*:*)
@@ -32,6 +35,7 @@ throw new Error("hello")
3235
^
3336
Error: hello
3437
at [stdin]:1:*
38+
at Object.exports.runInThisContext (vm.js:*)
3539
at Object.<anonymous> ([stdin]-wrapper:*:*)
3640
at Module._compile (module.js:*:*)
3741
at evalScript (node.js:*:*)
@@ -46,6 +50,7 @@ var x = 100; y = x;
4650
^
4751
ReferenceError: y is not defined
4852
at [stdin]:1:16
53+
at Object.exports.runInThisContext (vm.js:*)
4954
at Object.<anonymous> ([stdin]-wrapper:*:*)
5055
at Module._compile (module.js:*:*)
5156
at evalScript (node.js:*:*)

‎test/message/undefined_reference_in_new_context.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424

2525
var common = require('../common');
2626
var assert = require('assert');
27+
var vm = require('vm');
2728

2829
console.error('before');
2930

30-
var Script = process.binding('evals').NodeScript;
31-
3231
// undefined reference
33-
var script = new Script('foo.bar = 5;');
34-
script.runInNewContext();
32+
vm.runInNewContext('foo.bar = 5;');
3533

3634
console.error('after');

‎test/message/undefined_reference_in_new_context.out

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
before
22

3-
*test*message*undefined_reference_in_new_context.js:34
4-
script.runInNewContext();
5-
^
3+
evalmachine.<anonymous>:1
4+
foo.bar = 5;
5+
^
66
ReferenceError: foo is not defined
7-
at evalmachine.<anonymous>:*
7+
at evalmachine.<anonymous>:1:1
8+
at ContextifyScript.Script.runInNewContext (vm.js:*)
9+
at Object.exports.runInNewContext (vm.js:*)
810
at Object.<anonymous> (*test*message*undefined_reference_in_new_context.js:*)
911
at Module._compile (module.js:*)
1012
at *..js (module.js:*)
1113
at Module.load (module.js:*)
1214
at Function.Module._load (module.js:*:*)
1315
at Function.Module.runMain (module.js:*:*)
1416
at startup (node.js:*:*)
15-
at node.js:*:*

‎test/simple/test-debug-break-on-uncaught.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ addScenario('global.js', null, 2);
2929
addScenario('timeout.js', null, 2);
3030
addScenario('domain.js', null, 10);
3131

32-
// Exception is thrown from module.js (internal file)
32+
// Exception is thrown from vm.js via module.js (internal file)
3333
// var compiledWrapper = runInThisContext(wrapper, filename, 0, true);
34-
addScenario('parse-error.js', 'module.js', null);
34+
addScenario('parse-error.js', 'vm.js', null);
3535

3636
run();
3737

‎test/simple/test-querystring.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ var qsWeirdObjects = [
9191
];
9292
// }}}
9393

94-
var Script = require('vm').Script;
95-
var foreignObject = Script.runInContext('({"foo": ["bar", "baz"]})',
96-
Script.createContext());
94+
var vm = require('vm');
95+
var foreignObject = vm.runInNewContext('({"foo": ["bar", "baz"]})');
9796

9897
var qsNoMungeTestCases = [
9998
['', {}],

‎test/simple/test-vm-basic.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var vm = require('vm');
25+
26+
// Test 1: vm.runInNewContext
27+
var sandbox = {};
28+
var result = vm.runInNewContext(
29+
'foo = "bar"; this.typeofProcess = typeof process; typeof Object;',
30+
sandbox
31+
);
32+
assert.deepEqual(sandbox, {
33+
foo: 'bar',
34+
typeofProcess: 'undefined',
35+
});
36+
assert.strictEqual(result, 'function');
37+
38+
// Test 2: vm.runInContext
39+
var sandbox2 = { foo: 'bar' };
40+
var context = vm.createContext(sandbox2);
41+
var result = vm.runInContext(
42+
'baz = foo; this.typeofProcess = typeof process; typeof Object;',
43+
context
44+
);
45+
assert.deepEqual(sandbox2, {
46+
foo: 'bar',
47+
baz: 'bar',
48+
typeofProcess: 'undefined'
49+
});
50+
assert.strictEqual(result, 'function');
51+
52+
// Test 3: vm.runInThisContext
53+
var result = vm.runInThisContext(
54+
'vmResult = "foo"; Object.prototype.toString.call(process);'
55+
);
56+
assert.strictEqual(global.vmResult, 'foo');
57+
assert.strictEqual(result, '[object process]');
58+
delete global.vmResult;
59+
60+
// Test 4: vm.runInNewContext
61+
var result = vm.runInNewContext(
62+
'vmResult = "foo"; typeof process;'
63+
);
64+
assert.strictEqual(global.vmResult, undefined);
65+
assert.strictEqual(result, 'undefined');
66+
67+
// Test 5: vm.createContext
68+
var sandbox3 = {};
69+
var context2 = vm.createContext(sandbox3);
70+
assert.strictEqual(sandbox3, context2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var vm = require('vm');
25+
26+
var sandbox = { setTimeout: setTimeout };
27+
28+
var ctx = vm.createContext(sandbox);
29+
30+
vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx);
31+
setTimeout(function () {
32+
assert.strictEqual(sandbox.x, 3);
33+
assert.strictEqual(ctx.x, 3);
34+
}, 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var vm = require('vm');
25+
26+
var sandbox = { x: 3 };
27+
28+
var ctx = vm.createContext(sandbox);
29+
30+
assert.strictEqual(vm.runInContext('x;', ctx), 3);
31+
vm.runInContext('y = 4;', ctx);
32+
assert.strictEqual(sandbox.y, 4);
33+
assert.strictEqual(ctx.y, 4);

‎test/simple/test-script-context.js ‎test/simple/test-vm-context.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,32 @@ var vm = require('vm');
2626
var Script = vm.Script;
2727
var script = new Script('"passed";');
2828

29-
common.debug('run in a new empty context');
30-
var context = script.createContext();
29+
console.error('run in a new empty context');
30+
var context = vm.createContext();
3131
var result = script.runInContext(context);
3232
assert.equal('passed', result);
3333

34-
common.debug('create a new pre-populated context');
35-
context = script.createContext({'foo': 'bar', 'thing': 'lala'});
34+
console.error('create a new pre-populated context');
35+
context = vm.createContext({'foo': 'bar', 'thing': 'lala'});
3636
assert.equal('bar', context.foo);
3737
assert.equal('lala', context.thing);
3838

39-
common.debug('test updating context');
39+
console.error('test updating context');
4040
script = new Script('foo = 3;');
4141
result = script.runInContext(context);
4242
assert.equal(3, context.foo);
4343
assert.equal('lala', context.thing);
4444

4545
// Issue GH-227:
46-
Script.runInNewContext('', null, 'some.js');
46+
assert.throws(function () {
47+
vm.runInNewContext('', null, 'some.js');
48+
}, TypeError);
4749

4850
// Issue GH-1140:
49-
common.debug('test runInContext signature');
51+
console.error('test runInContext signature');
5052
var gh1140Exception;
5153
try {
52-
Script.runInContext('throw new Error()', context, 'expected-filename.js');
54+
vm.runInContext('throw new Error()', context, 'expected-filename.js');
5355
}
5456
catch (e) {
5557
gh1140Exception = e;
@@ -60,17 +62,13 @@ assert.ok(gh1140Exception,
6062
'expected exception from runInContext signature test');
6163

6264
// GH-558, non-context argument segfaults / raises assertion
63-
function isTypeError(o) {
64-
return o instanceof TypeError;
65-
}
66-
67-
([undefined, null, 0, 0.0, '', {}, []].forEach(function(e) {
68-
assert.throws(function() { script.runInContext(e); }, isTypeError);
69-
assert.throws(function() { vm.runInContext('', e); }, isTypeError);
70-
}));
65+
[undefined, null, 0, 0.0, '', {}, []].forEach(function(e) {
66+
assert.throws(function() { script.runInContext(e); }, TypeError);
67+
assert.throws(function() { vm.runInContext('', e); }, TypeError);
68+
});
7169

7270
// Issue GH-693:
73-
common.debug('test RegExp as argument to assert.throws');
71+
console.error('test RegExp as argument to assert.throws');
7472
script = vm.createScript('var assert = require(\'assert\'); assert.throws(' +
7573
'function() { throw "hello world"; }, /hello/);',
7674
'some.js');

‎test/simple/test-script-static-context.js ‎test/simple/test-vm-create-and-run-in-context.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
var common = require('../common');
2323
var assert = require('assert');
2424

25-
var Script = require('vm').Script;
25+
var vm = require('vm');
2626

27-
common.debug('run in a new empty context');
28-
var context = Script.createContext();
29-
var result = Script.runInContext('"passed";', context);
27+
console.error('run in a new empty context');
28+
var context = vm.createContext();
29+
var result = vm.runInContext('"passed";', context);
3030
assert.equal('passed', result);
3131

32-
common.debug('create a new pre-populated context');
33-
context = Script.createContext({'foo': 'bar', 'thing': 'lala'});
32+
console.error('create a new pre-populated context');
33+
context = vm.createContext({'foo': 'bar', 'thing': 'lala'});
3434
assert.equal('bar', context.foo);
3535
assert.equal('lala', context.thing);
3636

37-
common.debug('test updating context');
38-
result = Script.runInContext('var foo = 3;', context);
37+
console.error('test updating context');
38+
result = vm.runInContext('var foo = 3;', context);
3939
assert.equal(3, context.foo);
4040
assert.equal('lala', context.thing);
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var vm = require('vm');
25+
26+
var ctx = vm.createContext();
27+
ctx.window = ctx;
28+
29+
var thisVal = vm.runInContext('this;', ctx);
30+
var windowVal = vm.runInContext('window;', ctx);
31+
assert.strictEqual(thisVal, windowVal);

‎test/simple/test-script-new.js ‎test/simple/test-vm-new-script-new-context.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,28 @@ var Script = require('vm').Script;
2525

2626
common.globalCheck = false;
2727

28-
common.debug('run a string');
28+
console.error('run a string');
2929
var script = new Script('\'passed\';');
30-
common.debug('script created');
30+
console.error('script created');
3131
var result1 = script.runInNewContext();
3232
var result2 = script.runInNewContext();
3333
assert.equal('passed', result1);
3434
assert.equal('passed', result2);
3535

36-
common.debug('thrown error');
36+
console.error('thrown error');
3737
script = new Script('throw new Error(\'test\');');
3838
assert.throws(function() {
3939
script.runInNewContext();
40-
});
40+
}, /test/);
4141

4242

4343

44-
common.debug('undefined reference');
44+
console.error('undefined reference');
4545
var error;
4646
script = new Script('foo.bar = 5;');
47-
try {
47+
assert.throws(function () {
4848
script.runInNewContext();
49-
} catch (e) {
50-
error = e;
51-
}
52-
assert.ok(error);
53-
assert.ok(error.message.indexOf('not defined') >= 0);
54-
55-
common.debug('error.message: ' + error.message);
49+
}, /not defined/);
5650

5751

5852
hello = 5;
@@ -61,7 +55,7 @@ script.runInNewContext();
6155
assert.equal(5, hello);
6256

6357

64-
common.debug('pass values in and out');
58+
console.error('pass values in and out');
6559
code = 'foo = 1;' +
6660
'bar = 2;' +
6761
'if (baz !== 3) throw new Error(\'test fail\');';
@@ -73,21 +67,25 @@ assert.equal(1, obj.foo);
7367
assert.equal(2, obj.bar);
7468
assert.equal(2, foo);
7569

76-
common.debug('call a function by reference');
70+
console.error('call a function by reference');
7771
script = new Script('f()');
7872
function changeFoo() { foo = 100 }
7973
script.runInNewContext({ f: changeFoo });
8074
assert.equal(foo, 100);
8175

82-
common.debug('modify an object by reference');
76+
console.error('modify an object by reference');
8377
script = new Script('f.a = 2');
8478
var f = { a: 1 };
8579
script.runInNewContext({ f: f });
8680
assert.equal(f.a, 2);
8781

88-
common.debug('invalid this');
82+
assert.throws(function() {
83+
script.runInNewContext();
84+
}, /f is not defined/);
85+
86+
console.error('invalid this');
8987
assert.throws(function() {
9088
script.runInNewContext.call('\'hello\';');
91-
});
89+
}, TypeError);
9290

9391

‎test/simple/test-script-this.js ‎test/simple/test-vm-new-script-this-context.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ var Script = require('vm').Script;
2525

2626
common.globalCheck = false;
2727

28-
common.debug('run a string');
28+
console.error('run a string');
2929
var script = new Script('\'passed\';');
3030
var result = script.runInThisContext(script);
3131
assert.equal('passed', result);
3232

33-
common.debug('thrown error');
33+
console.error('thrown error');
3434
script = new Script('throw new Error(\'test\');');
3535
assert.throws(function() {
3636
script.runInThisContext(script);
@@ -42,7 +42,7 @@ script.runInThisContext(script);
4242
assert.equal(2, hello);
4343

4444

45-
common.debug('pass values');
45+
console.error('pass values');
4646
code = 'foo = 1;' +
4747
'bar = 2;' +
4848
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
@@ -54,7 +54,7 @@ assert.equal(0, obj.foo);
5454
assert.equal(2, bar);
5555
assert.equal(1, foo);
5656

57-
common.debug('call a function');
57+
console.error('call a function');
5858
f = function() { foo = 100 };
5959
script = new Script('f()');
6060
script.runInThisContext(script);

‎test/simple/test-script-static-new.js ‎test/simple/test-vm-run-in-new-context.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,42 @@
2121

2222
var common = require('../common');
2323
var assert = require('assert');
24-
var Script = require('vm').Script;
24+
var vm = require('vm');
2525

2626
common.globalCheck = false;
2727

28-
common.debug('run a string');
29-
var result = Script.runInNewContext('\'passed\';');
28+
console.error('run a string');
29+
var result = vm.runInNewContext('\'passed\';');
3030
assert.equal('passed', result);
3131

32-
common.debug('thrown error');
32+
console.error('thrown error');
3333
assert.throws(function() {
34-
Script.runInNewContext('throw new Error(\'test\');');
34+
vm.runInNewContext('throw new Error(\'test\');');
3535
});
3636

3737
hello = 5;
38-
Script.runInNewContext('hello = 2');
38+
vm.runInNewContext('hello = 2');
3939
assert.equal(5, hello);
4040

4141

42-
common.debug('pass values in and out');
42+
console.error('pass values in and out');
4343
code = 'foo = 1;' +
4444
'bar = 2;' +
4545
'if (baz !== 3) throw new Error(\'test fail\');';
4646
foo = 2;
4747
obj = { foo: 0, baz: 3 };
48-
var baz = Script.runInNewContext(code, obj);
48+
var baz = vm.runInNewContext(code, obj);
4949
assert.equal(1, obj.foo);
5050
assert.equal(2, obj.bar);
5151
assert.equal(2, foo);
5252

53-
common.debug('call a function by reference');
53+
console.error('call a function by reference');
5454
function changeFoo() { foo = 100 }
55-
Script.runInNewContext('f()', { f: changeFoo });
55+
vm.runInNewContext('f()', { f: changeFoo });
5656
assert.equal(foo, 100);
5757

58-
common.debug('modify an object by reference');
58+
console.error('modify an object by reference');
5959
var f = { a: 1 };
60-
Script.runInNewContext('f.a = 2', { f: f });
60+
vm.runInNewContext('f.a = 2', { f: f });
6161
assert.equal(f.a, 2);
6262

‎test/simple/test-script-static-this.js ‎test/simple/test-vm-static-this.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,36 @@
2121

2222
var common = require('../common');
2323
var assert = require('assert');
24-
var Script = require('vm').Script;
24+
var vm = require('vm');
2525

2626
common.globalCheck = false;
2727

28-
common.debug('run a string');
29-
var result = Script.runInThisContext('\'passed\';');
28+
console.error('run a string');
29+
var result = vm.runInThisContext('\'passed\';');
3030
assert.equal('passed', result);
3131

32-
common.debug('thrown error');
32+
console.error('thrown error');
3333
assert.throws(function() {
34-
Script.runInThisContext('throw new Error(\'test\');');
35-
});
34+
vm.runInThisContext('throw new Error(\'test\');');
35+
}, /test/);
3636

3737
hello = 5;
38-
Script.runInThisContext('hello = 2');
38+
vm.runInThisContext('hello = 2');
3939
assert.equal(2, hello);
4040

4141

42-
common.debug('pass values');
42+
console.error('pass values');
4343
code = 'foo = 1;' +
4444
'bar = 2;' +
4545
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
4646
foo = 2;
4747
obj = { foo: 0, baz: 3 };
48-
var baz = Script.runInThisContext(code);
48+
var baz = vm.runInThisContext(code);
4949
assert.equal(0, obj.foo);
5050
assert.equal(2, bar);
5151
assert.equal(1, foo);
5252

53-
common.debug('call a function');
53+
console.error('call a function');
5454
f = function() { foo = 100 };
55-
Script.runInThisContext('f()');
55+
vm.runInThisContext('f()');
5656
assert.equal(100, foo);
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.