OdeToCode IC Logo

Can I Replace JavaScript (and everything else) With Volta?

Friday, December 7, 2007

In my last post, I looked at using managed code in Silverlight as a replacement for JavaScript and offered the following code as an example:

HtmlDocument document = HtmlPage.Document;
HtmlElement select = document.GetElementByID("mySelect");

for (int i = 0; i < 10; i++)
{
    
HtmlElement option = document.CreateElement("option");
    option.SetAttribute(
"value", "foo" + i.ToString());
    option.SetAttribute(
"innerHTML", "foo" + i.ToString());
    select.AppendChild(option);
}

In my opinion, the managed code API offers a sub-optimal experience relative to the plain and simple JavaScript that would implement the same functionality. To be fair, Silverlight's mission isn't to replace JavaScript, HTML, web browsers, and motherly love (as yet, I guess), but I wanted to explore the idea.

In an ironic twist, Microsoft's Live Labs released a preview of Volta this week with the following key messages:

  • Volta automates certain low-level aspects of distributing applications across multiple tiers, allowing programmers to devote their creative energy to the distinguishing features of their applications.
  • Via declarative tier splitting, Volta lets developers postpone irreversible design decisions until the last responsible moment, making it faster and cheaper to change the architecture to accommodate evolving needs.
  • Through MSIL rewriting, Volta follows developer's declarations to turn a single-tiered application into a multi-tiered application, generating boilerplate code for communication and serialization.

The first two bullet points make me uncomfortable. "Declarative tier splitting" sounds a lot like a movie I saw during the COM+ days. In that movie a hero by the name of "Location Transparency" was killed by 10 million chatty microbes from the Andromeda galaxy. It was a slow death, and a sad ending.

Two things sparked my interest in Volta, however. Seeing Erik Meijer at the helm of Volta was one, and MSIL rewriting was the second. There are MSIL re-writing tools available today (see the profiling API and Mono.Cecil as examples), but mainstream MSIL re-writing can bring many benefits to the CLR. One only has to look into Java land where bytecode modifications yield interesting possibilities for aspect oriented programming, inversion of control scenarios, application optimizations, and more.

The result was that I downloaded the new bits and gave Volta a spin. Volta can run client applications in the browser, so I continued the "how to kill JavaScript" bit by trying my C# code using the Volta programming model:

Select mySelect = Document.GetById<Select>("mySelect");

for (int i = 0; i < 10; i++)
{
    mySelect.Add(
new Option() { Value = "foo " + i.ToString(),
                                Text =
"foo "  + i.ToString()
                               });
}

... much closer to the model I was looking for, and it works in IE and FireFox, too.

Wait! What about the Document.GetByID method? Is that a static method on a static class just waiting to plunge a knife into the heart of a unit test? No – it turns out Document is a property on the Volta Page class that implements an IJavaScriptObject interface (hooray!). You can almost taste the testability, except that Document is a read-only property (boo!). I hope that the MSIL re-writing in Volta will give us the ability to inject mock Documents for testing.

It will be interesting to see how project Volta progresses.