ScriptRunningMachine (often called SRM) is the main class of ReoScript engine, which provides most entry functions such as running script, loading from resource or access the context variables.

Create ScriptRunningMachine

To create ScriptRunningMachine instance:
ScriptRunningMachine srm = new ScriptRunningMachine();

By default, ScriptRunningMachine will be created with standard feature set, it is designed as much as possible similar to the default ECMAScript functionality set, but sometimes the application might don't want a part of features available or visible from script, such as disabling the alert function, to change the default functionality set, create ScriptRunningMachine with a CoreFeatures enumerator specified:
ScriptRunningMachine srm = new ScriptRunningMachine(CoreFeatures.FullFeatures);

See CoreFeatures for more details.

Use ScriptRunningMachine to run script

ScriptRunningMachine provides two types of method to run script, the load and run:


void Load(string filepath)
void Load(Uri uri)
void Load(Stream stream)


object Run(string script)
object Run(FileInfo fileInfo)

Load method will run script without returning the execute result, and it ignores the errors during execution. Run method will return the execution result and throw exceptions if there is any errors happen.

Run script as string

srm.Run("alert('hello world!');");

Run script from file


Run script from internal resource of project

using (MemoryStream ms = new MemoryStream(Resources.script))

Calculate Expression

To evaluate an expression, use 'CalcExpression' method. (See also Language Specification about what is Expression)
object result = srm.CalcExpression("2 * (3 + 4)");

The result is:

Run script repeatedly

Compile the script once will improve execution speed. For example:
CompiledScript code = srm.Compile(script);
for (int i = 0; i < 10; i++) {

Check syntax errors before run

`srm.Compile` method returns an object named CompiledScript which contains an error list, where is the syntax errors collected during compiling. See Error Handling.

Data exchange between .NET program

Global Object

There is a default object existed in script context globally, the object 'script', that is similar to the object 'window' in JavaScript, for example:
a = 10;    // assume 'a' does not exist

It actually is same as
script.a = 10;     // in JavaScript, it is 'window.a'

Also as function declaration.
function start() {

It is same as:
script.start = function() {


Access property of 'script' object in .NET program

'script' object can be accessed totally from .NET program by using:
    srm[string key] = object;
    srm.SetGlobalVariable(string key, object object)
    srm.GetGlobalVariable(string key)
    srm.RemoveGlobalVariable(string key)

Add any variable or objects from .NET into script context

    srm["k"] = 10;

Add functions into script context

The NativeFunctionObject class implements a proxy between .NET and script context.
    srm["exec"] = new NativeFunctionObject("exec", (ctx, owner, args) =>
      if (args.Length <= 0) return null;

      string exeName = Convert.ToString(args[0]);

      return null;

Call from script:

The Windows Notepad will be started up.

Call script's function from .NET program

Function defined in script like:
function hello(arg) {
   alert('hello ' + arg + '!');

To call it from .NET program, use `srm.InvokeFunctionIfExisted` method:
srm.InvokeFunctionIfExisted(string functionName, params object[] args);

    srm.InvokeFunctionIfExisted("hello", "world");

If function defined in any object, which should be passed as argument:
    srm.InvokeFunctionIfExisted(object, "hello", "world");

Get execution result from script

ReoScript can operate any types in script, get a result from script need convert it into .NET types, ReoScript provides some methods help to convert the data types.
string ConvertToString(object obj);
bool GetBoolValue(object obj);
double GetDoubleValue(object obj);    
float GetFloatValue(object obj);
int GetIntValue(object obj);
long GetLongValue(object obj);

Following methods are used to get value from an array of arguments:
    int GetIntParam(object[] args, int index, int def)
    long GetLongParam(object[] args, int index, long def)
    float GetIntParam(object[] args, int index, float def)

Check ScriptRunningMachine status


IsRunning is a property of ScriptRunningMachine to indicate whether the script currently is on running. For example:
function check_run() {
   if (!request_quit) {
       setTimeout(check_run, 1000);

This script will keep check `requestquit` flag every one second. If `requestquit` is false, `setTimeout` will be invoked and an asynchronous-calling will be performed. This script will keeps running after `checkrun` be invoked until `requestquit` be set to true, so we need to know whether the script executing is finished or not.
bool running = srm.IsRunning;

If running is true, it means script is running now.


To force abort a script execution thread, use the `ForceStop` method:
if (srm.IsRunning)

See also Enhanced Async-Calling.

Standard I/O Interface

Standard I/O Interface for script

There are 4 built-in functions available for script:
function __stdin__() {}           // read a byte from Standard Input
function __stdout__(obj) {}       // write an object to Standard Output
function __stdinln__() {}         // read a string line from Standard Input
function __stdoutln__(line) {}    // write a string line to Standard Output

The `console` object wraps these functions: = function() { return __stdin__(); };
console.readline = function() { return __stdinln__(); };

console.write = function(t) { __stdout__(t); };
console.log = function(t) { __stdoutln__(t); };

Standard I/O Interface for .NET

There are two interfaces be used for Standard I/O Interface in .NET:
interface IStandardInputProvider;
interface IStandardOutputListener;

When data redirected by built-in standard i/o functions, the implementation of above interface will be invoked.

Only one IStandardInputProvider can be set to SRM using the following method:
srm.StandardInputProvider = new MyInputProvider();

One or more IStandardOutputListener could be added into SRM using the following method:
srm.AddStandardOutputListener(new MyOutputListener());

Setting ScriptRunningMachine


WorkMode is a property of ScriptRunningMachine to decide how the mode does ScriptRunningMachine works on. Like enable or disable DirectAccess feature. (See WorkMode)


WorkPath is a property of ScriptRunningMachine to indicate if there is another script file will be imported into current script context, where to find the script file. WorkPath is default path where ScriptRunningMachine started up and works in.


CoreFeatures is a property of ScriptRunningMachine to decide what features can be supported for script executing. (See CoreFeatures)

Last edited Jan 14, 2015 at 8:11 AM by unvell, version 6