Embedded IronRuby and IronPython in Silverlight with Multiple Source Files

by rsutton 16. November 2009 10:17

I have yet to find a simple clear example of using multiple files when embedding IronPython or IronRuby in Silverlight, so I decided to share an example I have been working on.

NOTE: During testing have found this does not work with 0.91 or 0.92 of the DLR.  I guess there is a bug with imports and it should be fixed any day now.  Go here and click all releases to get 0.90 of the IronRuby and IronPython dlls.

Here is the code in my MainPage.xaml.cs:

    public delegate string pyFunc(string s);
    public delegate IronRuby.Builtins.MutableString rbFunc(string s);
 
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            pythonblock.Text = DoPython();
            rubyblock.Text = DoRuby();
        }
 
        private string DoPython()
        {
            var setup = Python.CreateRuntimeSetup(null);
            setup.HostType = typeof(BrowserScriptHost);
 
            var runtime = new ScriptRuntime(setup);
            var engine = Python.GetEngine(runtime);
            var scope = engine.CreateScope();
 
            var mscope = engine.CreateScope();
            var module = engine.CreateScriptSourceFromFile("module1.py");
            module.Execute(mscope);
 
            runtime.Globals.SetVariable("module1", mscope);
 
            var source = engine.CreateScriptSourceFromFile("main.py");
            source.Execute(scope);
 
            var func = scope.GetVariable<pyFunc>("hello");
 
            return func("world");
        }
 
        private string DoRuby()
        {
            var setup = new ScriptRuntimeSetup();
            setup.HostType = typeof(BrowserScriptHost);
            setup.AddRubySetup();
 
            var runtime = new ScriptRuntime(setup);
            var engine = Ruby.GetEngine(runtime);
            var scope = engine.CreateScope();
 
            var mscope = engine.CreateScope();
            var module = engine.CreateScriptSourceFromFile("module1.rb");
            module.Execute(mscope);
 
            runtime.Globals.SetVariable("module1", mscope);
 
            var source = engine.CreateScriptSourceFromFile("main.rb");
            source.Execute(scope);
 
            var func = scope.GetVariable<rbFunc>("hello");
 
            return func("world");
        }
    }

Here are the source files:

main.py:

import module1
 
def hello(s):
    return module1.f1('python hello ' + s)
 

module1.py:

def f1(s):
    return 'python f1: ' + s

main.rb:

require "module1"
 
def hello(s)
    f1('ruby hello ' + s)
end
 

module1.rb:

def f1(s)
    'ruby f1: ' + s
end

 

I hope this is helpful and if anyone has any suggestions for improvement please let me know.

Tags:

Steps for using Git with SVN (Update2)

by rsutton 6. November 2009 04:18

Well I think I may have just become a Git convert today.  Yesterday I began playing with Git and quickly fell in love with how branching works.  I’ve known for a long time that switching to Git at work from SVN would not be reasonable, so I didn’t give it much thought.  Well yesterday I realized that Git can be used quite well with SVN, so here I’ve listed the commands that have made working with Git and SVN quite pleasant.  First I must mention that the best install for Windows is msysgit.  I’ve never been a fan of cygwin, so this gets around using it.

Here is what I do to initially setup the repository.

git svn init svn://myserver/trunk/MyCode
git svn fetch –rHEAD
UPDATE: If you do –rX with X being a previous revision number you will get all the history from that point forward
git svn rebase

To update to the latest just do

git svn rebase

To commit changes to SVN do the following

git svn dcommit

Now here comes the cool part.  When you are working with branches you can create a branch just using normal Git, then when you are ready pull in the changes into your master branch and when you commit to svn it will have a complete log of your changes.  Check this out.

git branch testing
git checkout testing

…makes some changes…

git commit –a –m ‘Testing 1’

…make some changes…

git commit –a –m ‘Testing 2’

Switch back to the master branch

git checkout master

Merge in the changes. 

git pull . testing (or git merge testing)

Send them off to SVN

git svn dcommit

TADAA!!!

Capture

Now this is cool ;)

This and this were very helpful in getting this working.  Thanks!

UPDATE: A couple of additional notes that are helpful.

When updating your code and there are uncommitted modifications.

git stash
git svn rebase
git stash apply
git stash clear

To revert (as they say in the SVN world)

git checkout filename

Tags:

Development

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

Profile

Member of the Church of Jesus Christ of Latter-Day Saints, Developer and IT Professional