Skip to content

Commit 804bb22

Browse files
committed
vm: refactor vm module
Switch to the more efficient module.exports = {} pattern. PR-URL: #11392 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 55d202a commit 804bb22

8 files changed

+66
-54
lines changed

lib/vm.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,11 @@ Script.prototype.runInContext = function(contextifiedSandbox, options) {
3434
};
3535

3636
Script.prototype.runInNewContext = function(sandbox, options) {
37-
var context = exports.createContext(sandbox);
37+
var context = createContext(sandbox);
3838
return this.runInContext(context, options);
3939
};
4040

41-
exports.Script = Script;
42-
43-
exports.createScript = function(code, options) {
44-
return new Script(code, options);
45-
};
46-
47-
exports.createContext = function(sandbox) {
41+
function createContext(sandbox) {
4842
if (sandbox === undefined) {
4943
sandbox = {};
5044
} else if (binding.isContext(sandbox)) {
@@ -53,28 +47,11 @@ exports.createContext = function(sandbox) {
5347

5448
binding.makeContext(sandbox);
5549
return sandbox;
56-
};
57-
58-
exports.runInDebugContext = function(code) {
59-
return binding.runInDebugContext(code);
60-
};
61-
62-
exports.runInContext = function(code, contextifiedSandbox, options) {
63-
var script = new Script(code, options);
64-
return script.runInContext(contextifiedSandbox, options);
65-
};
66-
67-
exports.runInNewContext = function(code, sandbox, options) {
68-
var script = new Script(code, options);
69-
return script.runInNewContext(sandbox, options);
70-
};
71-
72-
exports.runInThisContext = function(code, options) {
73-
var script = new Script(code, options);
74-
return script.runInThisContext(options);
75-
};
50+
}
7651

77-
exports.isContext = binding.isContext;
52+
function createScript(code, options) {
53+
return new Script(code, options);
54+
}
7855

7956
// Remove all SIGINT listeners and re-attach them after the wrapped function
8057
// has executed, so that caught SIGINT are handled by the listeners again.
@@ -100,3 +77,31 @@ function sigintHandlersWrap(fn, thisArg, argsArray) {
10077
}
10178
}
10279
}
80+
81+
function runInDebugContext(code) {
82+
return binding.runInDebugContext(code);
83+
}
84+
85+
function runInContext(code, contextifiedSandbox, options) {
86+
return createScript(code, options)
87+
.runInContext(contextifiedSandbox, options);
88+
}
89+
90+
function runInNewContext(code, sandbox, options) {
91+
return createScript(code, options).runInNewContext(sandbox, options);
92+
}
93+
94+
function runInThisContext(code, options) {
95+
return createScript(code, options).runInThisContext(options);
96+
}
97+
98+
module.exports = {
99+
Script,
100+
createContext,
101+
createScript,
102+
runInDebugContext,
103+
runInContext,
104+
runInNewContext,
105+
runInThisContext,
106+
isContext: binding.isContext
107+
};

test/message/eval_messages.out

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
with(this){__filename}
44
^^^^
55
SyntaxError: Strict mode code may not include a with statement
6-
at Object.exports.runInThisContext (vm.js:*)
6+
at createScript (vm.js:*)
7+
at Object.runInThisContext (vm.js:*)
78
at Object.<anonymous> ([eval]-wrapper:*:*)
89
at Module._compile (module.js:*:*)
910
at Immediate.<anonymous> (bootstrap_node.js:*:*)
@@ -15,10 +16,11 @@ SyntaxError: Strict mode code may not include a with statement
1516
[eval]:1
1617
throw new Error("hello")
1718
^
19+
1820
Error: hello
1921
at [eval]:1:7
2022
at ContextifyScript.Script.runInThisContext (vm.js:*)
21-
at Object.exports.runInThisContext (vm.js:*)
23+
at Object.runInThisContext (vm.js:*)
2224
at Object.<anonymous> ([eval]-wrapper:*:*)
2325
at Module._compile (module.js:*:*)
2426
at Immediate.<anonymous> (bootstrap_node.js:*:*)
@@ -28,10 +30,11 @@ Error: hello
2830
[eval]:1
2931
throw new Error("hello")
3032
^
33+
3134
Error: hello
3235
at [eval]:1:7
3336
at ContextifyScript.Script.runInThisContext (vm.js:*)
34-
at Object.exports.runInThisContext (vm.js:*)
37+
at Object.runInThisContext (vm.js:*)
3538
at Object.<anonymous> ([eval]-wrapper:*:*)
3639
at Module._compile (module.js:*:*)
3740
at Immediate.<anonymous> (bootstrap_node.js:*:*)
@@ -42,20 +45,23 @@ Error: hello
4245
[eval]:1
4346
var x = 100; y = x;
4447
^
48+
4549
ReferenceError: y is not defined
4650
at [eval]:1:16
4751
at ContextifyScript.Script.runInThisContext (vm.js:*)
48-
at Object.exports.runInThisContext (vm.js:*)
52+
at Object.runInThisContext (vm.js:*)
4953
at Object.<anonymous> ([eval]-wrapper:*:*)
5054
at Module._compile (module.js:*:*)
5155
at Immediate.<anonymous> (bootstrap_node.js:*:*)
5256
at runCallback (timers.js:*:*)
5357
at tryOnImmediate (timers.js:*:*)
5458
at processImmediate [as _immediateCallback] (timers.js:*:*)
59+
5560
[eval]:1
5661
var ______________________________________________; throw 10
5762
^
5863
10
64+
5965
[eval]:1
6066
var ______________________________________________; throw 10
6167
^

test/message/stdin_messages.out

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[stdin]
2-
32
[stdin]:1
43
with(this){__filename}
54
^^^^
65
SyntaxError: Strict mode code may not include a with statement
7-
at Object.exports.runInThisContext (vm.js:*)
6+
at createScript (vm.js:*)
7+
at Object.runInThisContext (vm.js:*)
88
at Object.<anonymous> ([stdin]-wrapper:*:*)
99
at Module._compile (module.js:*:*)
1010
at Immediate.<anonymous> (bootstrap_node.js:*:*)
@@ -13,43 +13,43 @@ SyntaxError: Strict mode code may not include a with statement
1313
at processImmediate [as _immediateCallback] (timers.js:*:*)
1414
42
1515
42
16-
1716
[stdin]:1
1817
throw new Error("hello")
1918
^
19+
2020
Error: hello
21-
at [stdin]:1:*
21+
at [stdin]:1:7
2222
at ContextifyScript.Script.runInThisContext (vm.js:*)
23-
at Object.exports.runInThisContext (vm.js:*)
23+
at Object.runInThisContext (vm.js:*)
2424
at Object.<anonymous> ([stdin]-wrapper:*:*)
2525
at Module._compile (module.js:*:*)
2626
at Immediate.<anonymous> (bootstrap_node.js:*:*)
2727
at runCallback (timers.js:*:*)
2828
at tryOnImmediate (timers.js:*:*)
2929
at processImmediate [as _immediateCallback] (timers.js:*:*)
30-
3130
[stdin]:1
3231
throw new Error("hello")
3332
^
33+
3434
Error: hello
3535
at [stdin]:1:*
3636
at ContextifyScript.Script.runInThisContext (vm.js:*)
37-
at Object.exports.runInThisContext (vm.js:*)
37+
at Object.runInThisContext (vm.js:*)
3838
at Object.<anonymous> ([stdin]-wrapper:*:*)
3939
at Module._compile (module.js:*:*)
4040
at Immediate.<anonymous> (bootstrap_node.js:*:*)
4141
at runCallback (timers.js:*:*)
4242
at tryOnImmediate (timers.js:*:*)
4343
at processImmediate [as _immediateCallback] (timers.js:*:*)
4444
100
45-
4645
[stdin]:1
4746
var x = 100; y = x;
4847
^
48+
4949
ReferenceError: y is not defined
5050
at [stdin]:1:16
5151
at ContextifyScript.Script.runInThisContext (vm.js:*)
52-
at Object.exports.runInThisContext (vm.js:*)
52+
at Object.runInThisContext (vm.js:*)
5353
at Object.<anonymous> ([stdin]-wrapper:*:*)
5454
at Module._compile (module.js:*:*)
5555
at Immediate.<anonymous> (bootstrap_node.js:*:*)

test/message/undefined_reference_in_new_context.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
before
2-
32
evalmachine.<anonymous>:1
43
foo.bar = 5;
54
^
5+
66
ReferenceError: foo is not defined
77
at evalmachine.<anonymous>:1:1
88
at ContextifyScript.Script.runInContext (vm.js:*)
99
at ContextifyScript.Script.runInNewContext (vm.js:*)
10-
at Object.exports.runInNewContext (vm.js:*)
10+
at Object.runInNewContext (vm.js:*)
1111
at Object.<anonymous> (*test*message*undefined_reference_in_new_context.js:*)
1212
at Module._compile (module.js:*)
1313
at *..js (module.js:*)

test/message/vm_display_runtime_error.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
beginning
2-
32
test.vm:1
43
throw new Error("boo!")
54
^
5+
66
Error: boo!
77
at test.vm:1:7
88
at ContextifyScript.Script.runInThisContext (vm.js:*)
9-
at Object.exports.runInThisContext (vm.js:*)
9+
at Object.runInThisContext (vm.js:*)
1010
at Object.<anonymous> (*test*message*vm_display_runtime_error.js:*)
1111
at Module._compile (module.js:*)
1212
at Object.Module._extensions..js (module.js:*)
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
beginning
2-
32
foo.vm:1
43
var 4;
54
^
65
SyntaxError: Unexpected number
7-
at Object.exports.runInThisContext (vm.js:*)
6+
at createScript (vm.js:*)
7+
at Object.runInThisContext (vm.js:*)
88
at Object.<anonymous> (*test*message*vm_display_syntax_error.js:*)
99
at Module._compile (module.js:*)
1010
at Object.Module._extensions..js (module.js:*)
@@ -13,12 +13,12 @@ SyntaxError: Unexpected number
1313
at Function.Module._load (module.js:*)
1414
at Module.runMain (module.js:*)
1515
at run (bootstrap_node.js:*)
16-
at startup (bootstrap_node.js:*)
1716
test.vm:1
1817
var 5;
1918
^
2019
SyntaxError: Unexpected number
21-
at Object.exports.runInThisContext (vm.js:*)
20+
at createScript (vm.js:*)
21+
at Object.runInThisContext (vm.js:*)
2222
at Object.<anonymous> (*test*message*vm_display_syntax_error.js:*)
2323
at Module._compile (module.js:*)
2424
at Object.Module._extensions..js (module.js:*)
@@ -27,4 +27,3 @@ SyntaxError: Unexpected number
2727
at Function.Module._load (module.js:*)
2828
at Module.runMain (module.js:*)
2929
at run (bootstrap_node.js:*)
30-
at startup (bootstrap_node.js:*)

test/message/vm_dont_display_runtime_error.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ middle
33
test.vm:1
44
throw new Error("boo!")
55
^
6+
67
Error: boo!
78
at test.vm:1:7
89
at ContextifyScript.Script.runInThisContext (vm.js:*)
9-
at Object.exports.runInThisContext (vm.js:*)
10+
at Object.runInThisContext (vm.js:*)
1011
at Object.<anonymous> (*test*message*vm_dont_display_runtime_error.js:*)
1112
at Module._compile (module.js:*)
1213
at Object.Module._extensions..js (module.js:*)

test/message/vm_dont_display_syntax_error.out

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ middle
33
test.vm:1
44
var 5;
55
^
6+
67
SyntaxError: Unexpected number
7-
at Object.exports.runInThisContext (vm.js:*)
8+
at createScript (vm.js:*)
9+
at Object.runInThisContext (vm.js:*)
810
at Object.<anonymous> (*test*message*vm_dont_display_syntax_error.js:*)
911
at Module._compile (module.js:*)
1012
at Object.Module._extensions..js (module.js:*)
@@ -13,4 +15,3 @@ SyntaxError: Unexpected number
1315
at Function.Module._load (module.js:*)
1416
at Module.runMain (module.js:*)
1517
at run (bootstrap_node.js:*)
16-
at startup (bootstrap_node.js:*)

0 commit comments

Comments
 (0)