rScript now supports Visual Basic and runtime instancing
I made a lot of progress today with the rScripting engine, much more progress than I had anticipated actually.
The Script engine now instances objects into a wrapped class that provides methods for invoking the scripts methods and accessing their properties. The instancing occurs by using a new ScriptFactory class; the factory will instance the script into the wrapper class which has been called a ScriptObject.
Aside form adding runtime support for scripts, I also fully implemented the .NET Visual Basic 3.5 compiler, providing support for users who want to write scripts via the VB language. I’m currently using the C# 4.0 language compiler with the engine, and wasn’t sure what the latest version of the VB compiler was. I looked around on MSDN and found references to 3.5 which sounds about right, so that’s the currently supported version in the engine.
I also wrote several example scripts for developers to see how the engine can be used. The example implements the compiler and compiles the scripts without any issues.
I also wrote two example compiler classes that are included. At the moment they just wrap the C# and VB compilers in them, but the idea behind it is to demonstrate how you can provide scripting support to your own language compiler of choice (provided it’s .NET) due to rScript providing a way for you to wrap your custom compiler in a Compiler class that can be used by the Compiler Engine.
The following example demonstrates how to implement the rScripting engine into your existing .NET projects. The example instances the engine, defines the compiler to use, then compiles the scripts. Note that the VB example needs to change the ScriptExtension property due to the engine defaulting to .cs
//Instance the rScripting compiler. CompileEngine compiler = new CompileEngine(); //Set the compiler to use the built in C# compiler. compiler.Compiler = "C#"; //Compile the the Examples TestScript.cs file //Compiler will check the root directory since a
//different source repository was not supplied. Boolean compiledSuccess = compiler.Compile();
Implementing a Visual Basic version of the scripting engine is just as easy. The example is shown in C# but the engine can be wrote in Visual Basic. Once I get VB re-installed I will demonstrate the engine using VB itself rather than showing C# code using a VB script engine.
//Instance the rScripting compiler. CompileEngine compiler = new CompileEngine(); //Set the compiler to use the built in C# compiler. compiler.Compiler = "VB"; compiler.ScriptExtension = ".vb"; //Compile the the Examples TestScript.cs file //Compiler will check the root directory since a different
//source repository was not supplied. Boolean compiledSuccess = compiler.Compile();
Next I will demonstrate how to access a compiled script, instance it and invoke a method. In this example, the TestScript contains a method called ‘CreateFile’
//Instance the rScript Script Factory. This is used to
//generate Script Objects that provide //runtime access to the scripts,
//their methods and their properties. ScriptFactory factory = new ScriptFactory(compiler.CompiledAssembly); //Creates a Instance of the TestScript, wraps it in a
//ScriptObject Type that provides //helpers methods for accessing properties and invoking methods. ScriptObject obj = factory.GetScript("TestScript"); //Invoke the CreateFile method found inside
//out TestScript.cs file. obj.InvokeMethod("CreateFile");
Lastly the C# TestScript file and the VB TestScript files
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestNamespace { public class TestScript { public void CreateFile() { System.IO.File.Create(System.IO.Path.Combine(
Environment.CurrentDirectory, "TestCSharp.txt")); } } }
Public Class TestScript
Public Sub CreateFile()
System.IO.File.Create(System.IO.Path.Combine(
System.Environment.CurrentDirectory, "TestVB.txt"))
End Sub
End Class
With how I wrote rScript, you can add scripting support to any of your .NET applications regardless of language. If you want to use IronRuby or IronPython, implement a wrapper class using ICompiler and you can easily implement scripts for your app with the ability to quickly access their methods and properties.
I’m planning on adding Ruby/Python support at a later date, but for now I’m going to setup a Codeplex site and release rScripting 1.0 with just C# and VB support. I will also provide a complete set of documentation on the site for how to use the engine in your applications. Hopefully I can get this all setup and published by the end of the week.