-
Notifications
You must be signed in to change notification settings - Fork 35
NativeFunctionObject
Jing Lu edited this page May 14, 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 - run-time context
- owner - owner of this function (this keyword)
- args - argument list (object[])
An example to make ReoScript can start a windows process up with given exec-file path:
ScriptRunningMachine srm = new ScriptRunningMachine();
See more details about 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:
exec('notepad.exe');
The Windows Notepad should be started up.
ObjectValue obj = new ObjectValue();
Learn more about ObjectValue.
obj["sayHello"] = new NativeFunctionObject("sayHello", (ctx, owner, args) =>
{
Console.WriteLine("Hello World!");
return null;
});
Add object into ScriptRunningMachine:
srm["obj"] = obj;
obj.sayHello();
The following text will be printed out:
Hello World!