Home > Developing Using CodeFluent Entities, Exploring the CodeFluent Runtime > Exploring the CodeFluent Runtime: Reusing a Script Engine (like JScript or VBScript) with .NET

Exploring the CodeFluent Runtime: Reusing a Script Engine (like JScript or VBScript) with .NET

Following our “Exploring the CodeFluent Runtime“ post series, I thought I’d share with you another cool class available in the CodeFluent Runtime which is the ScriptEngine class.

The ScriptEngine class is a which wraps Windows Scripting Engines so you can easily and quickly reuse them with .NET code. The most useful engine is of course JScript which is Microsoft’s JavaScript implementation. JScript is installed on every Windows and is the exact same engine that is used by Internet Explorer. VBScript is also shipped out-of-the-box with every Windows installation and our ScriptEngine class indeed supports it. It should (this has not tested though) also support other scripting engines like ActivePython, ActivePerl, etc.

Here are some simple JavaScript examples that demonstrate how to leverage these engines:

Console.WriteLine(ScriptEngine.Eval("jscript", "1+2+3"));
// will output 6 (of Int32 type)

Console.WriteLine(ScriptEngine.Eval("jscript", "1.2"));
// will output 1.2 (of Double type)

Console.WriteLine(ScriptEngine.Eval("jscript", "1+2+3+'hello'"));
// will output 6hello (of String type)

You can also use external objects with these engines. Here I declare a Customer class (Note the ComVisible attribute set to true, this is mandatory as Script Engines are COM objects):

public class Customer
    public string Name { get; set; }

And I reuse it directly in JavaScript using my ScriptEngine class:

            Customer customer = new Customer();
            customer.Name = "Joe";
            Console.WriteLine(ScriptEngine.Eval("jscript", "MyObj.Name", new KeyValuePair<string, object>("MyObj", customer)));
            // will output Joe (of String type)

You can also pre-parse a script text and run it when you want. This is better in terms of performance since the script will only be parsed once. Here is how you can do it, with a JavaScript method declared in the script source:

            ScriptEngine engine = new ScriptEngine("jscript");
            ParsedScript preParsed = engine.Parse("function MyMethod(arg1, arg2){return arg1*2 + arg2.Name}");
            Console.WriteLine(preParsed.CallMethod("MyMethod", 1, customer));
            // will output 2Joe (of String type)

If you want to externalize business rules (for example tax computation in a LOB app), or if you want to ensure your application supports higher level programming capabilities (like if you’re writing a game that needs some parts to be scriptable), this is a cool way to do it. Just provide an object model (like the Customer class in my example) as a programming facade to your users and they will be capable of programming your app. No need to understand complex Lex/Yacc/BNF syntax, to redefine the JavaScript syntax. Just reference the CodeFluent Runtime and here you go.

The CodeFluent Runtime is available in two flavors:


The R&D Team.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s