Compartilhar via


4 different ways to execute IronPython code using the DLR Hosting API

The Hosting API offers 4 different sets of methods to execute
a script. These methods provide hosts - managed application in C#, VB and other .NET languages -  with powerful, convenient and versatile
ways to execute scripts from the DLR languages like IronPython, IronRuby etc. ( Though the title mentions just IronPython, this post applies to others like IronRuby, ToyScript etc as well)

I have put together a short summary of these methods and the
types that define them. The descriptions found here are shortened versions of the descriptions found in the Hosting
API spec
. Please refer to the same for more specific details about the type and
the method.

The types that define these method families are ScriptRuntime, ScriptScope, ScriptSource and CompiledCode.

ScriptRuntime

This class is the starting point for hosting. ScriptRuntime
represents global script state.  This
includes referenced assemblies, a "global object"
(ScriptRuntime.Globals), "published" scopes (scopes bound to a name
on Globals), available language engines, etc.

public ScriptScope ExecuteFile(string path);

This method executes the source identified in the path
argument and returns the new ScriptScope in which the source executed.
ExecuteFile determines the language engine to use from the path's extension and
the ScriptRuntime's configuration, comparing extensions case-insensitively.

The path is an absolute pathname or a filename that
naturally resolves with standard .NET BCL file open calls.

ScriptScope

This class represents a namespace essentially.  Hosts can bind variable names in
ScriptScopes, fetch variable values, etc. 
Hosts can execute code within scopes for distinct name bindings.

        public object Execute(string
code);

        public
T Execute<T>(string code);

These methods return objects
that result from executing code in the string argument.

 

ScriptSource

ScriptSource represents source code and offers a variety of
ways to execute or compile the source. 
You can get ScriptSources from factory methods on ScriptEngine, and
ScriptSources are tied to the engine that created them.

        public object Execute(ScriptScope
scope);

        public
ObjectHandle ExecuteAndWrap(ScriptScope
scope);

        public
T Execute<T>(ScriptScope scope);

        public int ExecuteProgram();

These methods execute the source code and return the result.  All but one method
executes the code in the specified ScriptScope.

CompiledCode

CompiledCode represents code that has been compiled to execute
repeatedly without having to compile it each time, and it represents the
default ScriptScope the code runs in. You can also execute the code in any
ScriptScope if you need it to execute in a clean scope each time, or you want
to accumulate side effects from the code in another scope.

        public object Execute() { }

        public object Execute(ScriptScope
scope) { }

        public
ObjectHandle ExecuteAndWrap() { }

        public
ObjectHandle ExecuteAndWrap(ScriptScope
scope) { }

        public T
Execute<T>() { }

        public
T Execute<T>(ScriptScope scope) { }

 

Unlike the ScriptSource’s execute methods, these do not
compile the code again.

A few of these methods can be found in the samples from the earlier blog posts. For more details and documentation about these methods and
types please refer to the Hosting API spec.

Comments

  • Anonymous
    July 28, 2008
    The comment has been removed