diff --git a/doc/api/_toc.markdown b/doc/api/_toc.markdown index e0a4bf45dc..bed9029155 100644 --- a/doc/api/_toc.markdown +++ b/doc/api/_toc.markdown @@ -23,7 +23,7 @@ * [Query Strings](querystring.html) * [Readline](readline.html) * [REPL](repl.html) -* [Script](script.html) +* [VM](vm.html) * [Child Processes](child_processes.html) * [Assertion Testing](assert.html) * Appendixes diff --git a/doc/api/script.markdown b/doc/api/vm.markdown similarity index 70% rename from doc/api/script.markdown rename to doc/api/vm.markdown index 9d7e4f68b4..450e511736 100644 --- a/doc/api/script.markdown +++ b/doc/api/vm.markdown @@ -2,24 +2,24 @@ You can access this module with: - var js = require('javascript'); + var vm = require('vm'); -New JavaScript code can be compiled and run immediately or compiled, saved, and run later. +JavaScript code can be compiled and run immediately or compiled, saved, and run later. -### js.runInThisContext(code, [filename]) +### vm.runInThisContext(code, [filename]) -`js.runInThisContext()` compiles `code` as if it were loaded from `filename`, +`vm.runInThisContext()` compiles `code` as if it were loaded from `filename`, runs it and returns the result. Running code does not have access to local scope. `filename` is optional. -Example of using `js.runInThisContext` and `eval` to run the same code: +Example of using `vm.runInThisContext` and `eval` to run the same code: var localVar = 123, usingscript, evaled, - js = require('javascript'); + vm = require('javascript'); - usingscript = js.runInThisContext('localVar = 1;', - 'myfile.js'); + usingscript = vm.runInThisContext('localVar = 1;', + 'myfile.vm'); console.log('localVar: ' + localVar + ', usingscript: ' + usingscript); evaled = eval('localVar = 1;'); @@ -29,16 +29,16 @@ Example of using `js.runInThisContext` and `eval` to run the same code: // localVar: 123, usingscript: 1 // localVar: 1, evaled: 1 -`js.runInThisContext` does not have access to the local scope, so `localVar` is unchanged. +`vm.runInThisContext` does not have access to the local scope, so `localVar` is unchanged. `eval` does have access to the local scope, so `localVar` is changed. -In case of syntax error in `code`, `js.runInThisContext` emits the syntax error to stderr +In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error to stderr and throws.an exception. -### js.runInNewContext(code, [sandbox], [filename]) +### vm.runInNewContext(code, [sandbox], [filename]) -`js.runInNewContext` compiles `code` to run in `sandbox` as if it were loaded from `filename`, +`vm.runInNewContext` compiles `code` to run in `sandbox` as if it were loaded from `filename`, then runs it and returns the result. Running code does not have access to local scope and the object `sandbox` will be used as the global object for `code`. `sandbox` and `filename` are optional. @@ -47,29 +47,29 @@ Example: compile and execute code that increments a global variable and sets a n These globals are contained in the sandbox. var util = require('util'), - js = require('javascript'), + vm = require('javascript'), sandbox = { animal: 'cat', count: 2 }; - js.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.js'); + vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm'); console.log(util.inspect(sandbox)); // { animal: 'cat', count: 3, name: 'kitty' } Note that running untrusted code is a tricky business requiring great care. To prevent accidental -global variable leakage, `js.runInNewContext` is quite useful, but safely running untrusted code +global variable leakage, `vm.runInNewContext` is quite useful, but safely running untrusted code requires a separate process. -In case of syntax error in `code`, `js.runInThisContext` emits the syntax error to stderr +In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error to stderr and throws an exception. -### js.createScript(code, [filename]) +### vm.createScript(code, [filename]) `createScript` compiles `code` as if it were loaded from `filename`, -but does not run it. Instead, it returns a `js.Script` object representing this compiled code. +but does not run it. Instead, it returns a `vm.Script` object representing this compiled code. This script can be run later many times using methods below. The returned script is not bound to any global object. It is bound before each run, just for that run. `filename` is optional. @@ -80,18 +80,18 @@ and throws an exception. ### script.runInThisContext() -Similar to `js.runInThisContext` but a method of a precompiled `Script` object. +Similar to `vm.runInThisContext` but a method of a precompiled `Script` object. `script.runInThisContext` runs the code of `script` and returns the result. Running code does not have access to local scope, but does have access to the `global` object (v8: in actual context). Example of using `script.runInThisContext` to compile code once and run it multiple times: - var js = require('javascript'); + var vm = require('javascript'); globalVar = 0; - var script = js.createScript('globalVar += 1', 'myfile.js'); + var script = vm.createScript('globalVar += 1', 'myfile.vm'); for (var i = 0; i < 1000 ; i += 1) { script.runInThisContext(); @@ -104,7 +104,7 @@ Example of using `script.runInThisContext` to compile code once and run it multi ### script.runInNewContext([sandbox]) -Similar to `js.runInNewContext` a method of a precompiled `Script` object. +Similar to `vm.runInNewContext` a method of a precompiled `Script` object. `script.runInNewContext` runs the code of `script` with `sandbox` as the global object and returns the result. Running code does not have access to local scope. `sandbox` is optional. @@ -112,14 +112,13 @@ Example: compile code that increments a global variable and sets one, then execu These globals are contained in the sandbox. var util = require('util'), - js = require('javascript'), + vm = require('javascript'), sandbox = { animal: 'cat', count: 2 }; - var script = js.createScript( - 'count += 1; name = "kitty"', 'myfile.js'); + var script = vm.createScript('count += 1; name = "kitty"', 'myfile.vm'); for (var i = 0; i < 10 ; i += 1) { script.runInNewContext(sandbox); diff --git a/lib/javascript.js b/lib/vm.js similarity index 100% rename from lib/javascript.js rename to lib/vm.js diff --git a/test/simple/test-querystring.js b/test/simple/test-querystring.js index 27fe388edc..ba3262c1dc 100644 --- a/test/simple/test-querystring.js +++ b/test/simple/test-querystring.js @@ -55,7 +55,7 @@ var qsWeirdObjects = [ ]; } -var Script = require('javascript').Script; +var Script = require('vm').Script; var foreignObject = Script.runInContext('({"foo": ["bar", "baz"]})', Script.createContext()); var qsNoMungeTestCases = [ diff --git a/test/simple/test-script-context.js b/test/simple/test-script-context.js index 610ceaf165..a2622d03ea 100644 --- a/test/simple/test-script-context.js +++ b/test/simple/test-script-context.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; var script = new Script('"passed";'); common.debug('run in a new empty context'); diff --git a/test/simple/test-script-new.js b/test/simple/test-script-new.js index ff5ae3ce02..117b62afc8 100644 --- a/test/simple/test-script-new.js +++ b/test/simple/test-script-new.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; common.debug('run a string'); var script = new Script('"passed";'); common.debug('script created'); diff --git a/test/simple/test-script-static-context.js b/test/simple/test-script-static-context.js index a8c106e4f3..388a995b6b 100644 --- a/test/simple/test-script-static-context.js +++ b/test/simple/test-script-static-context.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; common.debug('run in a new empty context'); var context = Script.createContext(); diff --git a/test/simple/test-script-static-new.js b/test/simple/test-script-static-new.js index a8f12090a2..81453e82b5 100644 --- a/test/simple/test-script-static-new.js +++ b/test/simple/test-script-static-new.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; common.debug('run a string'); var result = Script.runInNewContext('"passed";'); diff --git a/test/simple/test-script-static-this.js b/test/simple/test-script-static-this.js index cd48b709e6..c9809ff14c 100644 --- a/test/simple/test-script-static-this.js +++ b/test/simple/test-script-static-this.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; common.debug('run a string'); var result = Script.runInThisContext('"passed";'); diff --git a/test/simple/test-script-this.js b/test/simple/test-script-this.js index 2e071a912d..b8eeb97e69 100644 --- a/test/simple/test-script-this.js +++ b/test/simple/test-script-this.js @@ -1,7 +1,7 @@ common = require("../common"); assert = common.assert -var Script = require('javascript').Script; +var Script = require('vm').Script; common.debug('run a string'); var script = new Script('"passed";');