[IronPython] How to run ScriptCode from ScriptCode.LoadFromAssembly?

Yongming Wang wangym at gmail.com
Tue Sep 30 01:33:13 CEST 2008


I want to save the ScriptCode to assembly, load and run it next time.
I tried 3 ways, but all failed.

1. Here is my code
---------------------------------------------------

        public void RunScript(string script)
        {
            try
            {
                if (!System.IO.File.Exists(@"E:\Source
\IronPython-2.0B5\Src\PyDemo\bin\Debug\demo.dll"))
                {
                    _source =
_engine.CreateScriptSourceFromString(script,
Microsoft.Scripting.SourceCodeKind.Statements);
                    _binary = _source.Compile();
                    ScriptCode code =
HostingHelpers.GetScriptCode(_binary);
                    List<ScriptCode> codes = new List<ScriptCode>();
                    codes.Add(code);
                    ScriptCode.SaveToAssembly("demo.dll",
codes.ToArray());
                    _binary.Execute(_scope);
                }
                else
                {
                    ScriptCode[] codes =
ScriptCode.LoadFromAssembly(HostingHelpers.GetDomainManager(_runtime),
                        System.Reflection.Assembly.LoadFile(@"E:\Source
\IronPython-2.0B5\Src\PyDemo\bin\Debug\demo.dll"));
                    if (codes != null && codes.Length >= 1)
                    {
                        OptimizedScriptCode optimizedCode =
(OptimizedScriptCode)codes[0];
                        Scope scope = optimizedCode.CreateScope();
                        this._mainForm.setScope(scope); //
SetObjectName "Button1" etc.
                        optimizedCode.Run(scope);
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
        }
----------------------------------------------------------------------------
-----------------------------
As you can see, ScriptCode.SaveToAssembly was execute succesfully.
And ScriptCode.LoadFromAssembly successfully returned.
But when I got exception when I run it.
I got Microsoft.Scripting.Runtime.UnboundNameException
{"name 'Button1' is not defined"}
But I have set the object name to the scope. I don't know why.

2. If I use another ScriptScope _scope, e.g.
-------------------------------------
  OptimizedScriptCode optimizedCode = (OptimizedScriptCode)codes[0];
  optimizedCode.Run(HostingHelpers.GetScope(_scope));
--------------------------------------------------
I got System.NullReferenceException because code is null in the
following function of OptimizedScriptCode.cs
---------------------------------------------------
        protected override object InvokeTarget(LambdaExpression code,
Scope scope) {
            if (scope == _optimizedScope) {
                return _optimizedTarget(scope, LanguageContext);
            }

            // new scope, compile unoptimized code and use that.
            if (_unoptimizedTarget == null) {
                // TODO: fix generated DLR ASTs - languages should
remove their usage
                // of GlobalVariables and then this can go away.
                code = new GlobalLookupRewriter().RewriteLambda(code);

                _unoptimizedTarget =
code.Compile<DlrMainCallTarget>(SourceUnit.EmitDebugSymbols);
            }


            return _unoptimizedTarget(scope,
LanguageContext);
        }
-------------------------------------------------------

3. If I  modified the InvokeTarget function to
 if (scope == _optimizedScope || code == null)
I got System.InvalidCastException.
Cann't convert Microsoft.Scripting.Runtime.SymbolDictionary to
Microsoft.Scripting.Runtime.GlobalsDictionary.
I don't know why this convertion wass needed.

Could anyone give a help?
Thanks.
Yongming




More information about the Ironpython-users mailing list