Hosting... executing function dynamically from C#

Ok,

I’m trying to do something interesting with the DLR (I emailed John
before finding this forum… sorry, John). My project is here -->

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

Could you simplify the example of what you’re trying to do?

Do you want to execute:
“def foo
end”

and then
“foo”

?

What is the source code (“text” variable)?

Tomas

Ack! I messed that up. Text is the script to run (I mislabelled it
above as methodName)… here’s a little more detail:

I have already executed a script that looks like this


def myFunction()

do a bunch of stuff

end

#call code to wire up a button click pushing the function name

When the button is clicked my anonymous C# code attempts to call the
function passed in (in this case, it would be “myFunction” which would
be the value of the Text variable in the function above).

What comes back is an error saying that the variable can’t be found.

Alright I found the problem and it appears to be a problem between the
chair and the keyboard… Sorry about that.

I fat fingered my function name, and as soon as I discovered that it
made sense that Iron Ruby was telling me that it couldn’t find that
function (because it couldn’t).

Jay