Skip to content

NativeFunctionObject

Jing Lu edited this page May 13, 2013 · 12 revisions

NativeFunctionObject is a class provided by ReoScript to define an interface of function which created in .Net programs but called in script. This is typically used to extend features for ReoScript, make it more powerful and more native ability to do something.

Constructor of NativeFunctionObject:

new NativeFunctionObject(string name, Func<ctx, owner, args> body)

where

  • name - name of function
  • body - native function body
    • ctx - runtime context
    • owner - function owner
    • args - arguments

Add function in .Net program

An example to make ReoScript can start a windows process up with given exec-file path:

ScriptRunningMachine srm = new ScriptRunningMachine();

srm.SetGlobalVariable("exec", new NativeFunctionObject("exec", 
  (ctx, owner, args) =>
  {
    if (args.Length < 0) return null;

    string exeName = Convert.ToString(args[0]);
    System.Diagnostics.Process.Start(exeName);

    return null;
  })
);

Then call this function in script:

srm.Run(" exec('notepad.exe'); ");

The Windows Notepad should be started up.

Add method into an object in .Net program

Create object in .Net program

ObjectValue obj = new ObjectValue();

Add Method

obj["sayHello"] = new NativeFunctionObject("sayHello", (ctx, owner, args) =>
{
  Console.WriteLine("Hello World!");
  return null;
});

Add Object into script

Add object into ScriptRunningMachine:

srm["obj"] = obj;

Using Method in script

obj.sayHello();

The following text will be printed out:

Hello World!