Ok,
I’m trying to do something interesting with the DLR (I emailed John
before finding this forum… sorry, John). My project is here →
http://www.codeplex.com/dlrscript
What I’m doing is hosting the DLR within a Silverlight 2.0 beta 1 app.
My app scans the html dom for Script tags that target DLR languages
effectively adding a new client scripting language to the browser. I’m
embedding a couple objects as global vars into the script environment.
One of these objects provides a mechanism for creating and event handler
which is used in conjunction with my other object (document which
provides getElementById). The app itself is written in C#, and my test
page works perfectly for DLR Python (aka IronPython) and DLR JScript
(AKA managed JavaScript). For DLR Ruby (AKA IronRuby), the globals are
working, and the wiring up of the event works. The problem has to do
with how I’m calling the Ruby function when the event fires (it fires a
C# anonymous function which calls the Ruby function).
The way I’m calling the function is via an instance of the Ruby
ScriptEngine’s Execute function using a single statement (source
kind)… the code looks something like this:
// This is actually a function
String method = “someFunction”; /// this is actually a param
String codeID = “rby_command”; // this is also a param
SourceCodeKind kind = SourceCodeKind.SingleStatement; //
param
// SourceCodeKind is actually a variable… first time through this is
// “Statements”
// This is the actual function beginning
Exception result = null;
ErrorSink sink = new ErrorSink();
try
{
CompilerOptions copts =
engine.GetDefaultCompilerOptions();
SourceUnit scu =
engine.CreateScriptSourceFromString(text, scriptid + “_” +
this.shortName, kind);
CompilerContext cc = new CompilerContext(scu, copts,
sink);
ICompiledCode icc = engine.Compile(scu, copts, sink);
if (sink.AnyError) return new Exception("Syntax Error :"
-
sink.ToString());
if (scope == null) // This gets created the first
iteration through
{
scope = engine.Runtime.CreateScope();
}
engine.Execute(scope, scu);
return null;
}
catch (Exception ex)
{
result = ex;
}
return result;
I would expect that as long as I keep the same scope then the
environment should be able to locate the local variable/function I’m
trying to call. BTW, I’ve tried interactive, and expression, as well.
None of them work. So what am I missing? My objects are definitely
working.
BTW, the code in the latest released project has changed. Ruby doesn’t
work there (it is working perfectly except for this one item), so if I
need to I will upload a zip of the project here.
Also, thank you Michael of Michael.net… you’re logo DSL was very
helpful for getting me this far with Ruby.
Jay Kimble
http://www.theruntime.com/blogs/jaykimble