Skip to content
Jing Lu edited this page May 23, 2013 · 22 revisions

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();

Using object in script

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' };

Use object in .Net program

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:

ObjectValue

Add method into object in .Net program

To add method into object in .Net program, see NativeFunctionObject.

Using custom .Net type instead of ObjectValue

To use your own .Net type instead of ObjectValue you need make your class inheriting from ObjectValue.

class MyObject : ObjectValue
{
}

Create instance in .Net program

Add instance into ScriptRunningMachine:

srm["obj"] = new MyObject();

Create instance in script

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:

  1. Use default constructor - Import a type into script with default constructor by using srm.ImportType method:

     srm.ImportType(typeof(MyObject));
    
  2. 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);

See Also