|
@ -1,4 +1,6 @@ |
|
|
## Executing JavaScript |
|
|
# Executing JavaScript |
|
|
|
|
|
|
|
|
|
|
|
<!--name=vm--> |
|
|
|
|
|
|
|
|
You can access this module with: |
|
|
You can access this module with: |
|
|
|
|
|
|
|
@ -7,7 +9,7 @@ You can access this module with: |
|
|
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. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### vm.runInThisContext(code, [filename]) |
|
|
## vm.runInThisContext(code, [filename]) |
|
|
|
|
|
|
|
|
`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running |
|
|
`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running |
|
|
code does not have access to local scope. `filename` is optional, it's used only |
|
|
code does not have access to local scope. `filename` is optional, it's used only |
|
@ -37,7 +39,7 @@ In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error |
|
|
and throws an exception. |
|
|
and throws an exception. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### vm.runInNewContext(code, [sandbox], [filename]) |
|
|
## vm.runInNewContext(code, [sandbox], [filename]) |
|
|
|
|
|
|
|
|
`vm.runInNewContext` compiles `code`, then runs it in `sandbox` and returns the |
|
|
`vm.runInNewContext` compiles `code`, then runs it in `sandbox` and returns the |
|
|
result. Running code does not have access to local scope. The object `sandbox` |
|
|
result. Running code does not have access to local scope. The object `sandbox` |
|
@ -66,7 +68,7 @@ requires a separate process. |
|
|
In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr |
|
|
In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr |
|
|
and throws an exception. |
|
|
and throws an exception. |
|
|
|
|
|
|
|
|
### vm.runInContext(code, context, [filename]) |
|
|
## vm.runInContext(code, context, [filename]) |
|
|
|
|
|
|
|
|
`vm.runInContext` compiles `code`, then runs it in `context` and returns the |
|
|
`vm.runInContext` compiles `code`, then runs it in `context` and returns the |
|
|
result. A (V8) context comprises a global object, together with a set of |
|
|
result. A (V8) context comprises a global object, together with a set of |
|
@ -100,14 +102,14 @@ requires a separate process. |
|
|
In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr |
|
|
In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr |
|
|
and throws an exception. |
|
|
and throws an exception. |
|
|
|
|
|
|
|
|
### vm.createContext([initSandbox]) |
|
|
## vm.createContext([initSandbox]) |
|
|
|
|
|
|
|
|
`vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent |
|
|
`vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent |
|
|
call to `vm.runInContext`. A (V8) context comprises a global object together with a set of |
|
|
call to `vm.runInContext`. A (V8) context comprises a global object together with a set of |
|
|
build-in objects and functions. The optional argument `initSandbox` will be shallow-copied |
|
|
build-in objects and functions. The optional argument `initSandbox` will be shallow-copied |
|
|
to seed the initial contents of the global object used by the context. |
|
|
to seed the initial contents of the global object used by the context. |
|
|
|
|
|
|
|
|
### vm.createScript(code, [filename]) |
|
|
## vm.createScript(code, [filename]) |
|
|
|
|
|
|
|
|
`createScript` compiles `code` but does not run it. Instead, it returns a |
|
|
`createScript` compiles `code` but does not run it. Instead, it returns a |
|
|
`vm.Script` object representing this compiled code. This script can be run |
|
|
`vm.Script` object representing this compiled code. This script can be run |
|
@ -119,6 +121,10 @@ In case of syntax error in `code`, `createScript` prints the syntax error to std |
|
|
and throws an exception. |
|
|
and throws an exception. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Class: Script |
|
|
|
|
|
|
|
|
|
|
|
A class for running scripts. Returned by vm.createScript. |
|
|
|
|
|
|
|
|
### script.runInThisContext() |
|
|
### script.runInThisContext() |
|
|
|
|
|
|
|
|
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object. |
|
|
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object. |
|
|