Skip to content

How to use ReoScript to run JavaScript

Jing edited this page Jul 25, 2019 · 42 revisions

Run JavaScript in .NET Application

Download ReoScript libraries or build from source code. The following two DLLs must be added as project reference.

  • Antlr3.Runtime.dll
  • Unvell.ReoScript.dll

And import this namespace:

using Unvell.ReoScript;

Create an instance of class ScriptRunningMachine, it managements the instances and memory during load/run scripts. (Learn more about ScriptRunningMachine)

ScriptRunningMachine srm = new ScriptRunningMachine();

Call Run method to run script statements.

srm.Run("alert('hello world!');");

After execute above, a message box with text 'hello world' will be displayed.

Difference between JavaScript and ReoScript

There is a brief of difference between JavaScript and ReoScript:

  1. GlobalObject named _window_ in JavaScript, but named _script_ in ReoScript.

     window.hello = function() { };              // JavaScript
     script.hello = function() { };              // ReoScript
    

    See about GlobalObject.

  2. Iteration for...in works on Array and returns element itself.

     var arr = [1, 5, 'ok', false];
     for (e in arr) {
             console.log(e);
     }
    

    JavaScript returns index, but ReoScript returns element itself as below:

     JavaScript:             ReoScript:
     0                       1
     1                       5
     2                       ok
     3                       false
    

    See about Enumerator.

  3. setTimeout and setInterval are supported to pass arguments.

    It is possible to pass arguments when calling setTimeout or setInterval, for example:

     function run(obj) {
         console.log('Object is ' + obj.name);
     }
    
     setTimeout(run, 1000, {name: 'obj1'});
    

    See about Enhanced Async-Calling.

  4. Argument list of a function is called arguments in JavaScript but called __args__ in ReoScript.

     JavaScript:                                 ReoScript:
    
     function hello() {                          function hello() {
         console.log(arguments.length);              console.log(__args__.length);
     }                                           }
    
  5. Regular Expressions currently is not supported. However, you can make an extension such as extended native function to support .NET Regular Expressions for script. (See How to extend function for script)

  6. with keyword is not supported in ReoScript.

  7. ReoScript provides several own features, see Language Feature.

Integrate ReoScript into your .NET Application

See How to integrate ReoScript into your .NET Application.

Merge script files

In HTML and JavaScript we could use <script> tag to import another script file into current page. It is also possible to implement the same effect in ReoScript by using import keyword.

Script File: common.rs

var MAX_PATH = 256;                           // global variable for all script files

function get_extension(filepath) {
   var index = filepath.lastIndexOf('.');
   return filePath.substr(index + 1);
}

Script File: main.rs

import "common.rs";

var ext = get_extension('sample.txt');
console.log('extension is ' + ext);

The result is:

extension is txt

Run ReoScript in Windows Console

You may also run a script from Windows Console by using ReoScript Runner. (See ReoScript Runner)