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

TypeNameDescription
StringfilenameName of file containing the script
NumberlinenoStarting line number (1 = first line)
StringsourceThe source code

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType 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

TypeNameDescription
StringconstructorThe name of the constructor function for the scope

Return

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType of argument is invalid for call

Example


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

execute()

Prototype

void execute(Object scope)

void 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

TypeNameDescription
ObjectscopeThe top level scope object for script execution.
ObjectcwdThe current working directory to use.

Return

Exceptions

NameValueDescription
GPErrorGPError.ARGUMENTS_MISSINGToo few arguments in call
GPErrorGPError.INVALID_ARGUMENTSToo many arguments in call
GPErrorGPError.INVALID_TYPEType 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);