Skip to content

How to extend function for script

Jing Lu edited this page May 14, 2013 · 1 revision

There is a .Net class NativeFunctionObject available to describe an interface of function which defined in .Net program but called in script. Once it be created and added into script, it will be available for script executing.

Since there is a global object invisibly existed in script context, all variables or objects running in script are managed in the global object. And ScriptRunningMachine provides the methods to access or modify the global object. (Learn more about ScriptRunningMachine)

Create function in .Net program

Create the ScriptRunningMachine as below:

ScriptRunningMachine srm = new ScriptRunningMachine();

Create and add extended function:

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

When call this function in script as below:

hello();

The "Hello World!" will be printed out in console:

Hello World!

See more about NativeFunctionObject.