ObjectValue is a class to implement the base type of object in ReoScript. ObjectValue has a key/value paired property list.

When an object created in script, the ScriptRunningMachine creates an ObjectValue instance in C#.

    var obj = new Object();    // using Object constructor
    var obj = {};              // using JSON literal

    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 using standard JavaScript/ECMAScript object literal syntax:
    var apple = { name: 'apple', color: 'red' };

Use object in .NET program

You may create objects in .NET program, all the objects created in .NET 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:

The result is:

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:

2. Use customized constructor - The customized constructor function can be defined in .Net program using TypedNativeFunctionObject class.

        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

Last edited Jul 11, 2013 at 12:39 AM by unvell, version 3