@@ -65,6 +65,45 @@ requires a separate process.
6565In case of syntax error in ` code ` , ` vm.runInNewContext ` emits the syntax error to stderr
6666and throws an exception.
6767
68+ ### vm.runInContext(code, context, [ filename] )
69+
70+ ` vm.runInContext ` compiles ` code ` to run in context ` context ` as if it were loaded from ` filename ` ,
71+ then runs it and returns the result. A (V8) context comprises a global object, together with a
72+ set of built-in objects and functions. Running code does not have access to local scope and
73+ the global object held within ` context ` will be used as the global object for ` code ` .
74+ ` filename ` is optional.
75+
76+ Example: compile and execute code in a existing context.
77+
78+ var util = require('util'),
79+ vm = require('vm'),
80+ initSandbox = {
81+ animal: 'cat',
82+ count: 2
83+ },
84+ context = vm.createContext(initSandbox);
85+
86+ vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
87+ console.log(util.inspect(context));
88+
89+ // { animal: 'cat', count: 3, name: 'CATT' }
90+
91+ Note that ` createContext ` will perform a shallow clone of the supplied sandbox object in order to
92+ initialise the global object of the freshly constructed context.
93+
94+ Note that running untrusted code is a tricky business requiring great care. To prevent accidental
95+ global variable leakage, ` vm.runInContext ` is quite useful, but safely running untrusted code
96+ requires a separate process.
97+
98+ In case of syntax error in ` code ` , ` vm.runInContext ` emits the syntax error to stderr
99+ and throws an exception.
100+
101+ ### vm.createContext([ initSandbox] )
102+
103+ ` vm.createContext ` creates a new context which is suitable for use as the 2nd argument of a subsequent
104+ call to ` vm.runInContext ` . A (V8) context comprises a global object together with a set of
105+ build-in objects and functions. The optional argument ` initSandbox ` will be shallow-copied
106+ to seed the initial contents of the global object used by the context.
68107
69108### vm.createScript(code, [ filename] )
70109
0 commit comments