Skip to content

Integrate with .net program

Jing Lu edited this page Jun 18, 2013 · 82 revisions

ReoScript provides the ability to execute script in your application. It usually could be used in the following cases:

  • Application with script execution

    Software with Macro or Script execution is required to be available for end-user. (like VBA in Excel)

  • Script to be ran under a native library that is provided by your Application

    You have a library written in .NET program, and you want to your end-user can use them by writing script.

  • Console Script Execution

    Some batch process program, Daily build, File publish or something else. You are able to make extensions for ReoScript in .NET program, and writing and running the script in console.

Setup ReoScript

  1. Download ReoScript binary or build source file. Add the following DLLs into reference list of your project.

     Antlr3.Runtime.dll
     Unvell.ReoScript.dll
    
  2. Import the following namespace

     using Unvell.ReoScript;
    
  3. Create ScriptRunningMachine and keep the instance

     ScriptRunningMachine srm = new ScriptRunningMachine();
    
  4. Run script from different source

    • Run script in text

        srm.Run("console.log('hello world');");
      
    • Run script from internal resource

        using(MemoryStream ms = new MemoryStream(Resources.script)) {
            srm.Load(ms);
        }
      
    • Run script from specified file

        srm.Load("C:\\scripts\\main.rs");
      
    • Run script from specified stream (could be a network, or unzipped stream etc.)

        srm.Load(new NetworkStream(...));
      

    See ScriptRunningMachine.

Practices to integrate ReoScript in your application

There are two practices to integrate ReoScript in your application.

  1. Wrapper Objects, Properties and Methods (Manual Mode)
  2. DirectAccess (Automatic Mode)

Wrapper Objects, Properties and Methods (Manual Mode)

To connect the two worlds between script and .NET, the wrapper objects, properties and methods may be necessary to be written.

              +-----------------------------------------------------+
              |                       Script                        |
              |    +-------------------------------------------+    |
              +--- |  Wrapper objects, properties and methods  | ---+        <--- You may have to do
              |    +-------------------------------------------+    |
              |                  .NET Application                   |
              +-----------------------------------------------------+

I called this as 'Manual Mode' because anything you want to provide for script are controllable precisely. Your end-user can only use the functions what you want to be used. This mode is recommended if you are planning to support script execution is available for your end-user.

To write wrapper objects, properties and methods, you may use the following classes in .NET program.

DirectAccess (Automatic Mode)

All the objects, types, properties and methods which requested to use in script are all mapping to .NET Runtime automatically by ReoScript engine. In this Automatic Mode, you basically nothing to do.

              +-----------------------------------------------------+
              |                       Script                        |
              |    +-------------------------------------------+    |
              +--- |     .NET Reflection / ReoScript Engine    | ---+        <--- You don't have to do
              |    +-------------------------------------------+    |
              |                  .NET Application                   |
              +-----------------------------------------------------+

Please refer to DirectAccess.

My Suggestion

Although ReoScript provides DirectAccess mechanism to access .NET object directly, it is recommended that writing wrapper objects, properties and methods if you are planning to make script execution to be available for your end-user.

For example, assuming there is a .NET object with one method and property:

public class Application
{
    public void Start() { ... }
    public string Name { get; set; }
}

With DirectAccess, you would be able to call 'Start' method from script directly:

var app = new Application();
app.start();

Or access its property like:

app.name = 'stuff';

But if you reformed your .NET program, like renaming method, the script will be unavailable. For usability it is recommended that writing wrapper method for your application and script.

  1. Create wrapper object - create a class inheriting from ObjectValue, keep an instance of original object. And add methods that you want to provide for script.

     public class ApplicationObject : ObjectValue 
     {
         public Application Application { get; set; }
    
         public ApplicationObject() {
             // keep original instance
             this.Application = new Application();
    
             // add wrapper method
             this["start"] = new NativeFunctionObject("start", (ctx, owner, args)=> {
                 this.Application.Start();
             });
         }
     }
    

    About function extension please see NativeFunctionObject.

  2. Import this wrapper object type into script context

     // prepare srm
     ScriptRunningMachine srm = new ScriptRunningMachine();
    
     // import .Net type into srm
     srm.ImportType(typeof(ApplicationObject), "Application");
    

Then the Application class will be available to script.

var app = new Application();
app.start();