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

Dino Viehland dinov at microsoft.com
Tue Sep 30 02:01:34 CEST 2008


My guess is you're hitting a bug I already have a fix for.  What's probably happening is we're burning the SymbolID into the compiled code and then when you run it we have the value inappropriately associated with the wrong string.  This should be fixed in the latest sources which were just pushed up today.

Also in RC1 ScriptRuntime.LoadAssembly will effectively do the load for you and then a simple "import mymodulename" should work although maybe you don't want to do compile & run the import.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Yongming Wang
Sent: Monday, September 29, 2008 4:33 PM
To: users at lists.ironpython.com
Subject: [IronPython] How to run ScriptCode from ScriptCode.LoadFromAssembly?

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

_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list