-
Notifications
You must be signed in to change notification settings - Fork 35
ObjectValue
ObjectValue is a class to implement the type of object in ReoScript. ObjectValue has a property list with key/value pairs.
When an object created in script, the ScriptRunningMachine will create an ObjectValue instance in C#.
ReoScript:
var obj = new Object(); // using Object constructor
var obj = {}; // using JSON literal
C#:
ObjectValue obj = new ObjectValue();
var apple = new Object();
Set or get property value:
apple.color = 'red'; // set
var a = apple.color; // get
Or create object with standard JavaScript/ECMAScript object literal syntax:
var apple = { name: 'apple', color: 'red' };
You may create objects in .Net program, the objects are also available to script.
ScriptRunningMachine srm = new ScriptRunningMachine();
ObjectValue obj = srm.CreateNewObject();
obj["name"] = "apple";
obj["color"] = "red";
srm["apple"] = obj;
Run script to output property value:
alert(apple.color);
The result is:
To add method into object in .Net program, see NativeFunctionObject.
To use your own .Net type instead of ObjectValue you need make your class inheriting from ObjectValue.
class MyObject : ObjectValue
{
}
Add instance into ScriptRunningMachine:
srm["obj"] = new MyObject();
To allow script to create an instance of custom .Net type object, you need firstly add a constructor function into script, there is two ways to add constructor function for your custom type:
-
Use default constructor - Import a type into script with default constructor by using
srm.ImportType
method:srm.ImportType(typeof(MyObject));
-
Use customized constructor - The customized constructor function can be defined in .Net program using TypedNativeFunctionObject class.
Usage:
new TypedNativeFunctionObject(type, constructor_name, function_body, prototype_builder);
For example:
srm["MyObject"] = new TypedNativeFunctionObject(typeof(MyObject), "MyObject", (ctx, owner, args) => { // code in here executed by normal function calling like 'MyClass()' MyObject obj = owner as MyObject; return obj; }, (proto) => { // code in here executed by prototype of this constructor to be created first time proto["method_in_prototype"] = new NativeFunctionObject(...); });
Once the constructor be added into script, you may create the instance of custom type in script as below:
var obj = new MyObject();
debug.assert(typeof obj == 'object');
debug.assert(obj instanceof MyObject);