Precompiling IronPython Code for a SDL-SDK Project?

Apr 3, 2009 at 4:57 PM
I know within standard IronPython I can run clr.CompileModules("foo.dll", "") to compile code down to a prebuilt assembly.

Is there a way to get portions of my IronPython code compiled down to a DLL that will run within Silverlight?  I'm assuming I can't use the standard ipy.exe clr.CompileModules() as i'd need my code linked/referenced against the Silverlight runtimes rather than the .Net 3.5, etc.

Seems to me that it should be doable but I'm at a loss as to how to do it properly.
Apr 3, 2009 at 5:13 PM
Edited Apr 3, 2009 at 6:21 PM

Good question. I haven’t tried this myself, but it does seem possible. Here are some issues that can get in the way:

1. It has to be run from Silverlight since you need references to SL assemblies, not the desktop (as you said). CompileModules doesn’t have an easy way of decoupling this.

2. I’m not sure if CompileModules uses a feature of Reflection.Emit to save a DLL to the filesystem. If so, then that would need to be changed to go through the DLR’s PlatformAdaptationLayer, so it could be changed in SL to write to isolated storage.

3. Once the assembly is in IsolatedStorage, it can be loaded directly from there, or (in Silverlight 3 only) saved to your machine with the SaveFileDialog.

Feel free to investigate this, and if #2 is an issue we can try to fix it in the next release of IronPython.


Apr 3, 2009 at 5:47 PM
Didn't even think to look at the IronPython source - for some reason I thought the clr module was built-in to .Net itself.

The IronPython CompileModules method explicitly takes a string for the assemblyName; this string is passed to Microsoft.Scripting.Runtime.ScriptCode.SaveToAssembly() as a string.

Ultimately that assemblyName is passed as a string to Microsoft.Scripting.Generation.AssemblyGen() , along with a directoryname split out from the original assemblyName.

If I recall correctly, the IsolatedStorage isn't accesable via normal open() operations and uses it's own FileStreams, so I'm not sure how to get the system to write to IsolatedStorage instead.

Apr 3, 2009 at 6:21 PM
My initial point was to verify whether IronPython is using Reflection.Emit to save the assembly, and it is, through AssemblyBuilder.Save. There is no way to CLR-wide redirect file-system actions, so unfortunately, we can't change where that API writes the DLL to. However (I'm not too familiar with Ref.Emit's API), if AssemblyBuilder has a way to get a byte[] for the DLL, then we could write the DLL to Isolated Storage, or just show the file save dialog.
Jun 16, 2009 at 5:54 PM

I've started working on this the last couple of days.

I'm not certain yet if I'll need to produce an IronPython patch or I can make my stuff operate independently....

I haven't checked any code in yet, but I did setup a google code project for the work:

I'm aiming to create at the least to start with either a linkable DLL that you can invoke from a running sdl-sdk app to compile the code in the XAP, or a standalone Silverlight App that can convert an uploaded XAP to dll.

I am also looking into implementing a command line compilation with a --nostdlib option similar to Boo's which will allow linking specifically against the Silverlight / SDL-SDK assemblies.

Jun 16, 2009 at 9:52 PM

Followup questions as I dig through...

What would the necessary context be for loading a Silverlight IronPython app living in an Assembly from the SDL-SDK?  Looking through Source, it appears the SDL-SDK looks specifically for a default filename (e.g. when it loads up and finds it's Entry Point.

Would we still need an file which then loads the shiny new assembly, or is there a way to build a full assembly that the SDL-SDK can load fully that I'm not aware of?

Jun 17, 2009 at 10:11 AM


I'm also looking for some way to compile .py files for use under Silverlight...

-- Thibaut

Jun 17, 2009 at 9:09 PM

There’s been enough “ask” for this feature that I’ll look into this more. It’s most likely possible, but I haven’t personally tried it. Someone else was looking into this too, but I’m not sure how far he got … regardless I’ll get back to you.

Jun 17, 2009 at 9:13 PM

Working on it as we speak.... Hoping I can make something work soonish!

Jun 30, 2009 at 2:25 PM

Can someone tell me if using clr.CompileModules and using the SDL-SDK I can get a fully statically compiled executable.. This can include all the iron* dll's but it must be non relocatable code that where all labels in the symbol table are resolved?