"console=true": REPLs in Silverlight

18 Nov 2008

At Seattle CodeCamp v4.0 today, I talked a lot about REPLs, or "Read, Evaluate, Print Loops". They are the true "minimalist" view of programming; type a line, run a line. Most of them are no frills, only a terminal window and a blinking cursor, though they themselves could be really rich development environments. Also, REPLs are a vital tool for runtime-compilation languages, since the methods/variables/classes/etc are only *really* known to exist at runtime.

I started my talk with a Ruby REPL showing off how you can use IronRuby to explore .NET.

ruby-browser-repl

Ah, the beauty of two worlds colliding. You can use Ruby-idioms to explore the .NET types, starting with an A, B, or C, in the System namespace, just like they were Ruby classes to begin with. Call .NET methods, even with Ruby method naming conventions; IsInterface as is_interface.

The lurking awesomeness here is this REPL is running inside the browser! This is a HTML-based Silverlight application, which provides an IronRuby REPL. It accomplishes this by using the Dynamic Language Runtime (DLR) hosting APIs, but don't get hung up on the details of how that works, I'll explain later.

Monkey patching

This is nothing *new* in my world. DLRConsole has been around for a while now, and lets you write code in the browser to manipulate a Silverlight canvas. Both REPLs are self-contained, as in they only work against code manually loaded into them, like the previous screenshot shows with loading in the clock.rb and drag.rb files. This is fine for trying small things out, but becomes very cumbersome when trying to interact with a live application.

To interact with a live application seamlessly, the bare minimum would be to have a REPL available directly in the application itself. And that's what I've done:

silverlight-repl

If you build the latest bits, you can add a new initParam option called "console" to enable a REPL in any application:

<object ... >
  ...
  <param name="initParams" value="console=true" />
</object>

This will inject a HTML-based REPL into whatever web page your app is running on. The language of the REPL is set to whatever language your start script is. To make the console pretty, add this stylesheet to your page. Eventually this stylesheet will be included in the templates script/sl produces.

In the screenshot above, I did not type/click anything on the UI; the code I typed into the REPL set the search term and "clicked" the search button. This type of integration with your application could easily produce Waitr-like automation, for testing and whatnot. I'd really like to see Waitr ported to IronRuby. =

But there's plenty more to-do: this console is very simple.

  • It only supports Ruby currently; though simple Python statements will work (white space is a bitch =P).
  • It supports single and multi-line statements, but since the text input is implemented on top of a <input> tag, browser history gets in the way =P
  • History works, but never resets itself.
  • If you make a typo, and it's not clear that you've intended for the statement to be completed (like typing "class <enter>"), you'll have to force the console to run the command with <ctrl>+<enter>

Please, play around with this new "usefulness" and let me know what you think. I'd prefer you to leave comments on github, so everyone can participate in the discussion. The code for the REPL lives in Console.cs, so if you want to fix something on the list above, or see something else broken, fix it! If you find more bugs, please post them to Codeplex under the 0.x.0 release.

Hope this little feature makes your live a bit easier!

comments powered by Disqus