Build: Accessing the DLR engine in Silverlight 2

Note: This code has been updated using Silverlight 2 RTM

With Silverlight 2 a new set of components have been introduced into the equation, the most important one is the CLR Execution engine, that support in this case the dynamic language runtime (DLR) that we are going to host in this post. The following chart shows the items that have been changed since version 1.0

 

The interesting area regarding the DLR is the interoperability options between the silverlight components and scripted languages. In the release 2.0 we are including support for Iron Python, Ruby and Managed JavaScript. These languages are currently offered through codeplex here. The support for VBScript has been replaced by the future version of Visual Basic, called VBX, but it won’t be supported until the language is released. This shows one of the other beauties of the DLR, the ability to add new languages on the fly. These languages can be used in two directions, script to silverlight and silverlight to script.

This post will focus on hosting the scripting engine in your Silverlight application adding dynamic code to the execution, allowing the inclusion of instances into the scripted code. The first thing that we need to do is including the namespaces:

using Microsoft.Scripting;

using Microsoft.Scripting.Hosting;
using Microsoft.JScript.Runtime;

With these namespaces we have access to the DLR different engines, in this scenario we are going to use the JavaScript engine.

ScriptRuntime MyRuntime = JScript.CreateRuntime();

With the instance in place now we can retrieve the script engine as follows:

ScriptEngine JSEngine = MyRuntime.GetEngine("js");

In this case, my option was managed JavaScript. This object now focuses on the language per se, exploring the class will fascinate you, as you have access to the core execution of a full program, being able to compile code, define scope and execute it. Now the internals of the execution needs to be explored, I have mentioned the scope; you create a new scope using the following command:

IScriptScope Scope = JSEngine.CreateScope();

Each execution scope represent the environment where your code will run, it allows you to set variables and their values. We can add an object instance that the scripted code will run (similar to executing COM components in the past but now linked to your silverlight object). Let’s suppose that I expose a method that calls a WCF method:

IRemoteInterface TransparentProxy = MyFactory.CreateChannel();
Scope.SetVariable("WCFProxy", TransparentProxy);

Now that we have the scope, we can start defining our code. In order to use the code we should use the ScriptSource object.

ScriptSource Code = JSEngine.CreateScriptSourceFromString(“WCFProxy.MyMethod();”);

The script engine allows you to load external files as well, just explore the options that it offers. The ScriptSource object allows you to play with the code, like getting individual lines. All this information can be dynamically changed, giving you the flexibility that you need. Now, the final step, we can compile the code to make sure that is correct and execute it (note that you can execute it directly and will be compiled on the fly).

source.Compile() ; // Optional
Code.Execute(Scope);

You can shutdown the engine when is not needed using the Shutdown() command. You can see the power of injecting code into your application on the fly and the simplicity of the model. Explore the DLR!