Scripting Server

JsScript - Reference Documentation

Class implementing support for compiling and running script fragments

Index of Methods

Constructor

Prototype

JsScript(String filename)

JsScript(String filename, Number lineno, String source)

Description

Compile script.

Arguments

Type Name Description
String filename Name of file containing the script
Number lineno Starting line number (1 = first line)
String source The source code

Exceptions

Name Value Description
GPError GPError.ARGUMENTS_MISSING Too few arguments in call
GPError GPError.INVALID_ARGUMENTS Too many arguments in call
GPError GPError.INVALID_TYPE Type of argument is invalid for call

Example


var scriptsource = 'globalvar = 123; var localvar = 456;function f() { return 1};';

var s = new JsScript("sample", 1, scriptsource);


newDynamicScope()

Prototype

void newDynamicScope(String constructor)

Description

Create a new dynamic scope using the given constructor function.

A dynamic scope inherits all predefined top level functions and properties, but is otherwise a clean top level scope. Use this to create a runtime environment to execute scripts.

Arguments

Type Name Description
String constructor The name of the constructor function for the scope

Return

Object A new dynamic scope derived from the top level scope

Exceptions

Name Value Description
GPError GPError.ARGUMENTS_MISSING Too few arguments in call
GPError GPError.INVALID_ARGUMENTS Too many arguments in call
GPError GPError.INVALID_TYPE Type of argument is invalid for call

Example


var scope = JsScript.newDynamicScope("Object");

execute()

Prototype

Object execute(Object scope)

Object execute(Object scope, String cwd)

Description

Execute script within the defined scope object.

The caller may specify a current working directory (cwd) which is used when executing the script. If not specified, then the current working directory of the calling script is used.

Arguments

Type Name Description
Object scope The top level scope object for script execution.
Object cwd The current working directory to use.

Return

Object The result of evaluating the script, i.e. the last assignment or function return in the code.

Exceptions

Name Value Description
GPError GPError.ARGUMENTS_MISSING Too few arguments in call
GPError GPError.INVALID_ARGUMENTS Too many arguments in call
GPError GPError.INVALID_TYPE Type of argument is invalid for call

Example


s.execute(scope);

assert(typeof(globalvar) == "undefined");
assert(typeof(scope.globalvar) != "undefined");

var thisdir = GPSystem.mapFilename("", GPSystem.CWD);
var subdir = GPSystem.mapFilename("scriptdir", GPSystem.CWD);

var s = new JsScript(subdir + "/test.js");

// Execute script with current directory of this script
// The script will set the global variable cwd
s.execute(this);
assert(thisdir == cwd);

// Execute script with subdirectory relative to this script
s.execute(this, subdir);
assert(subdir == cwd);