Browse Source

Fix docs for 'javascript' module

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
19ed02b286
  1. 63
      doc/api/script.markdown

63
doc/api/script.markdown

@ -1,24 +1,24 @@
## Script ## Executing JavaScript
`Script` class compiles and runs JavaScript code. You can access this class with: You can access this module with:
var Script = require('javascript').Script; var js = require('javascript');
New JavaScript code can be compiled and run immediately or compiled, saved, and run later. New JavaScript code can be compiled and run immediately or compiled, saved, and run later.
### Script.runInThisContext(code, [filename]) ### js.runInThisContext(code, [filename])
Similar to `process.compile`. `Script.runInThisContext` compiles `code` as if it were loaded from `filename`, `js.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. runs it and returns the result. Running code does not have access to local scope. `filename` is optional.
Example of using `Script.runInThisContext` and `eval` to run the same code: Example of using `js.runInThisContext` and `eval` to run the same code:
var localVar = 123, var localVar = 123,
usingscript, evaled, usingscript, evaled,
Script = require('javascript').Script; js = require('javascript');
usingscript = Script.runInThisContext('localVar = 1;', usingscript = js.runInThisContext('localVar = 1;',
'myfile.js'); 'myfile.js');
console.log('localVar: ' + localVar + ', usingscript: ' + console.log('localVar: ' + localVar + ', usingscript: ' +
usingscript); usingscript);
@ -29,16 +29,16 @@ Example of using `Script.runInThisContext` and `eval` to run the same code:
// localVar: 123, usingscript: 1 // localVar: 123, usingscript: 1
// localVar: 1, evaled: 1 // localVar: 1, evaled: 1
`Script.runInThisContext` does not have access to the local scope, so `localVar` is unchanged. `js.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. `eval` does have access to the local scope, so `localVar` is changed.
In case of syntax error in `code`, `Script.runInThisContext` emits the syntax error to stderr In case of syntax error in `code`, `js.runInThisContext` emits the syntax error to stderr
and throws.an exception. and throws.an exception.
### Script.runInNewContext(code, [sandbox], [filename]) ### js.runInNewContext(code, [sandbox], [filename])
`Script.runInNewContext` compiles `code` to run in `sandbox` as if it were loaded from `filename`, `js.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 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`. the object `sandbox` will be used as the global object for `code`.
`sandbox` and `filename` are optional. `sandbox` and `filename` are optional.
@ -47,56 +47,54 @@ Example: compile and execute code that increments a global variable and sets a n
These globals are contained in the sandbox. These globals are contained in the sandbox.
var util = require('util'), var util = require('util'),
Script = require('javascript').Script, js = require('javascript'),
sandbox = { sandbox = {
animal: 'cat', animal: 'cat',
count: 2 count: 2
}; };
Script.runInNewContext( js.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.js');
'count += 1; name = "kitty"', sandbox, 'myfile.js');
console.log(util.inspect(sandbox)); console.log(util.inspect(sandbox));
// { animal: 'cat', count: 3, name: 'kitty' } // { animal: 'cat', count: 3, name: 'kitty' }
Note that running untrusted code is a tricky business requiring great care. To prevent accidental Note that running untrusted code is a tricky business requiring great care. To prevent accidental
global variable leakage, `Script.runInNewContext` is quite useful, but safely running untrusted code global variable leakage, `js.runInNewContext` is quite useful, but safely running untrusted code
requires a separate process. requires a separate process.
In case of syntax error in `code`, `Script.runInThisContext` emits the syntax error to stderr In case of syntax error in `code`, `js.runInThisContext` emits the syntax error to stderr
and throws an exception. and throws an exception.
### new Script(code, [filename]) ### js.createScript(code, [filename])
`new Script` compiles `code` as if it were loaded from `filename`, `createScript` compiles `code` as if it were loaded from `filename`,
but does not run it. Instead, it returns a `Script` object representing this compiled code. but does not run it. Instead, it returns a `js.Script` object representing this compiled code.
This script can be run later many times using methods below. This script can be run later many times using methods below.
The returned script is not bound to any global object. The returned script is not bound to any global object.
It is bound before each run, just for that run. `filename` is optional. It is bound before each run, just for that run. `filename` is optional.
In case of syntax error in `code`, `new Script` emits the syntax error to stderr In case of syntax error in `code`, `createScript` prints the syntax error to stderr
and throws an exception. and throws an exception.
### script.runInThisContext() ### script.runInThisContext()
Similar to `Script.runInThisContext` (note capital 'S'), but now being a method of a precompiled Script object. Similar to `js.runInThisContext` but a method of a precompiled `Script` object.
`script.runInThisContext` runs the code of `script` and returns the result. `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 Running code does not have access to local scope, but does have access to the `global` object
(v8: in actual context). (v8: in actual context).
Example of using `script.runInThisContext` to compile code once and run it multiple times: Example of using `script.runInThisContext` to compile code once and run it multiple times:
var Script = require('javascript').Script, var js = require('javascript');
scriptObj, i;
globalVar = 0; globalVar = 0;
scriptObj = new Script('globalVar += 1', 'myfile.js'); var script = js.createScript('globalVar += 1', 'myfile.js');
for (i = 0; i < 1000 ; i += 1) { for (var i = 0; i < 1000 ; i += 1) {
scriptObj.runInThisContext(); script.runInThisContext();
} }
console.log(globalVar); console.log(globalVar);
@ -106,7 +104,7 @@ Example of using `script.runInThisContext` to compile code once and run it multi
### script.runInNewContext([sandbox]) ### script.runInNewContext([sandbox])
Similar to `Script.runInNewContext` (note capital 'S'), but now being a method of a precompiled Script object. Similar to `js.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. `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. Running code does not have access to local scope. `sandbox` is optional.
@ -114,18 +112,17 @@ Example: compile code that increments a global variable and sets one, then execu
These globals are contained in the sandbox. These globals are contained in the sandbox.
var util = require('util'), var util = require('util'),
Script = require('javascript').Script, js = require('javascript'),
scriptObj, i,
sandbox = { sandbox = {
animal: 'cat', animal: 'cat',
count: 2 count: 2
}; };
scriptObj = new Script( var script = js.createScript(
'count += 1; name = "kitty"', 'myfile.js'); 'count += 1; name = "kitty"', 'myfile.js');
for (i = 0; i < 10 ; i += 1) { for (var i = 0; i < 10 ; i += 1) {
scriptObj.runInNewContext(sandbox); script.runInNewContext(sandbox);
} }
console.log(util.inspect(sandbox)); console.log(util.inspect(sandbox));

Loading…
Cancel
Save