X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=doc%2Fapi%2Fvm.markdown;h=39940ce21a073fe8b8661bd446c35b604af3a81b;hb=4cc6180fc56d432e9433ab15ab5ccc820826c341;hp=8ad7e72d4fe76d36d5f97231c552528d921beb12;hpb=61ccaf9a974bedf54622a1d6ad6b6ad00f95f5a5;p=platform%2Fupstream%2Fnodejs.git diff --git a/doc/api/vm.markdown b/doc/api/vm.markdown index 8ad7e72..39940ce 100644 --- a/doc/api/vm.markdown +++ b/doc/api/vm.markdown @@ -1,176 +1,127 @@ # Executing JavaScript - Stability: 3 - Stable + Stability: 2 - Stable You can access this module with: - var vm = require('vm'); + const vm = require('vm'); JavaScript code can be compiled and run immediately or compiled, saved, and run later. -## vm.runInThisContext(code, [options]) - -`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running -code does not have access to local scope, but does have access to the current -`global` object. - -Example of using `vm.runInThisContext` and `eval` to run the same code: - - var localVar = 'initial value'; - - var vmResult = vm.runInThisContext('localVar = "vm";'); - console.log('vmResult: ', vmResult); - console.log('localVar: ', localVar); +## Class: Script - var evalResult = eval('localVar = "eval";'); - console.log('evalResult: ', evalResult); - console.log('localVar: ', localVar); +A class for holding precompiled scripts, and running them in specific sandboxes. - // vmResult: 'vm', localVar: 'initial value' - // evalResult: 'eval', localVar: 'eval' +### new vm.Script(code, options) -`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. +Creating a new `Script` compiles `code` but does not run it. Instead, the +created `vm.Script` object represents 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. -In this way `vm.runInThisContext` is much like an [indirect `eval` call][1], -e.g. `(0,eval)('code')`. However, it also has the following additional options: +The options when creating a script are: - `filename`: allows you to control the filename that shows up in any stack - traces produced. + traces produced from this script. +- `lineOffset`: allows you to add an offset to the line number that is + displayed in stack traces +- `columnOffset`: allows you to add an offset to the column number that is + displayed in stack traces - `displayErrors`: whether or not to print any errors to stderr, with the line of code that caused them highlighted, before throwing an exception. - Will capture both syntax errors from compiling `code` and runtime errors - thrown by executing the compiled code. Defaults to `true`. + Applies only to syntax errors compiling the code; errors while running the + code are controlled by the options to the script's methods. - `timeout`: a number of milliseconds to execute `code` before terminating - execution. If execution is terminated, an `Error` will be thrown. - -[1]: http://es5.github.io/#x10.4.2 - - -## vm.createContext([sandbox]) - -If given a `sandbox` object, will "contextify" that sandbox so that it can be -used in calls to `vm.runInContext` or `script.runInContext`. Inside scripts run -as such, `sandbox` will be the global object, retaining all its existing -properties but also having the built-in objects and functions any standard -[global object][2] has. Outside of scripts run by the vm module, `sandbox` will -be unchanged. - -If not given a sandbox object, returns a new, empty contextified sandbox object -you can use. - -This function is useful for creating a sandbox that can be used to run multiple -scripts, e.g. if you were emulating a web browser it could be used to create a -single sandbox representing a window's global object, then run all `