Log In

Matt Briggs

"Not all code needs to be a factory, some of it can just be origami." - _why, the lucky stiff

Hosted Scripting in .Net 4

.net
by Matt Briggs on 01/05/10

So this is just the coolest thing evar.

One of the prime motivations for Microsoft doing its IronRuby and IronPython work is as embedable languages in existing CLR applications. When I read that I was sort of underwhelmed, expecting some really hairy communication mechanisms like com interop. Boy, I was wrong.

The general idea is each dynamic language implements a ScriptEngine, which is used by the DLR hosting framework. Because it is interface based, you can work with any DLR language exactly the same way. This means that by going this route, you get support for ruby, python, scheme, etc. Which is hela-cool.

From the hosting application point of view, what you need is a ScriptRuntime (which takes a ScriptEngine). The runtime will then be able to give you a ScriptScope object, which is basically the bride that you use to communicate with the hosted script. You can register assemblies or objects with the scope, and those values become accessable from the hosted script. By the same tolken, the hosted script can register values, that can then be read from the hosting application.

Something like this

var csharp_var = "I am set from C# land";
var ruby = "csharp_land = :ruby_ftw.to_s"; // my odd ruby example

var runtime = new ScriptRuntime("rb");
var scope = runtime.CreateScope();

scope.SetVariable("csharp_land", csharp_var) 
// expose the c# variable to hosted ruby

Console.WriteLine("Before script execution: " + csharp_var); 
// Before script execution: I am set from C# land
runtime.Execute(ruby, scope);
Console.WriteLine("After script execution: " + csharp_var); 
// After script execution: ruby_ftw

Freakin awesome.

Want to expose a library? runtime.LoadAssembly(<Assembly>). Want to get a variable from the script? scope.GetVariable(<name>). You can even get methods defined in the script in to C#, and they will be defined as Actions or Funcs, allowing you to do callbacks.

The thing that I like about this isn't so much that its possible, it is that you can implement it in about 3 lines of extremely straight forward C#. Even things like UI scripting for automated tests becomes trivial, you just expose the base object on each form and you are pretty much done; full access to the code at runtime.

I really didn't expect to be excited about this, but it really may be the highlight session for me from PDC09. If you are interested in this, here is the vid

my code blog.

what I am reading

Sidebar_clean_code

the people I follow

  • 24 ways
  • ABtests.com - Learn. Share. Improve your conversions today.
  • Ajaxian » Front Page
  • Alex Young
  • BEST IN CLASS
  • briancarper.net (λ)
  • Carbonica Blog Feed
  • Catalog Living
  • Clients From Hell
  • Clojure/core Blog
  • code is code
  • Coding Horror
  • CSS-Tricks
  • Daily Vim: Text Editor Tips, Tricks, Tutorials, and HOWTOs
  • David Chelimsky
  • dean.edwards.name/weblog
  • DHTML Kitchen News
  • disclojure: all things clojure
  • Edge Rails.info
  • End of Line
  • English - AkitaOnRails.com
  • Err the Blog
  • Evil Monkey Labs
  • Extra Cheese
  • Extra Cheese
  • For A Beautiful Web
  • Francis Hwang's site
  • Free Ruby and Rails Screencasts
  • Giles Bowkett
  • Hacker News
  • has_many :bugs, :through => :rails
  • Higgins for President
  • HTML5 Doctor
  • Information Is Beautiful
  • It's an all-you-can-leet buffet !
  • Jay Fields' Thoughts
  • JGUIMONT>COM
  • John Barnette
  • John Resig
  • K. Scott Allen
  • Katz Got Your Tongue?
  • Kirby's Dreamland
  • Kotaku
  • Kotka
  • Lambda the Ultimate - Programming Languages Weblog
  • Lazycoder
  • Loud Thinking by David Heinemeier Hansson
  • LukeW | Writings on Digital Product Strategy and Design
  • mir.aculo.us
  • MongoTips by John Nunemaker
  • Moonbase
  • No Strings Attached
  • Nuby on Rails
  • Official jQuery Blog
  • ones zeros majors and minors
  • opensoul.org by Brandon Keepers
  • Painfully Obvious
  • Painfully Obvious
  • Particletree
  • Paul Irish
  • Perfection kills
  • Plataforma Tecnologia Blog » English
  • Rails on PostgreSQL :
  • Railscasts
  • RedFlagDeals.com - Latest Deals
  • Relaselog | RLSLOG.net
  • remy sharp's b:log
  • Riding Rails - home
  • RightJS News
  • rmurphey
  • Room 101
  • Rubinius Blog
  • Ruby Best Practices
  • Ruby Inside
  • Ruby Quicktips
  • Ruby treats women as objects
  • RubyFlow
  • Signal vs. Noise
  • Slash7 with Amy Hoy - Home
  • Smashing Magazine Feed
  • Snail in a Turtleneck
  • Software Craftsmanship – Katas
  • St. on IT
  • Stevey's Blog Rants
  • Technomancy
  • Tender Lovemaking
  • Test Obsessed
  • Zed Shaw
  • The CSS Ninja
  • The GitHub Blog
  • The MongoDB NoSQL Database Blog
  • The Napkin ~ A Blog By Highgroove Studios
  • The UX Booth
  • The Word of Notch
  • the { buckblogs :here } - Home
  • Thoughts From Eric
  • Uncle Bob's Blog
  • VIM Tips Blog
  • Virtuous Code
  • Web Designer Wall - Design Trends and Tutorials
  • Wow! eBook - Great ebook, great site!
  • #<Mongoid::Criteria:0xba4fdfc>
profile for Matt Briggs at Stack Overflow
Feed
atom 1.0

mattcode.net stack

Rightjs
Rails
Mongo
Dropbox