-
Get a monthly update on best practices for delivering successful software.
Take a step back and squint and you'll see that Ajax, with it's ability to pass structured data back and forth between client and server, can be used to implement just about any architecture or abstraction that involves communication between execution environments. The only things getting in the way are the platform and performance limitations of the browser. Even with this limitation, we've already seen implementations from the obvious RPC (DWR, JSON-RPC, etc.) all the way to X11 (XML11) and various client engines and protocols in between (TibCo GI, ZK, Echo2, JackBe, etc.). If you haven't realized this already, there's more than one way to do things with Ajax.
Now along comes Clorox, a different kind of abstraction on top of Ajax. Developed as an MIT class project (see the paper here), Clorox hides all of the asynchronous request business behind simple Javascript data structure access.
In place of the asynchronous, RPC-based abstraction furnished by AJAX, Clorox provides the illusion of synchronously-accessed data structures shared between the web browser and web server, which is to say, it provides a shared memory abstraction. These data structures look exactly like ordinary JavaScript objects on the client side, allowing programmers to focus on what they do best (writing compelling web applications) without worrying about data locality, message reordering, callback functions, or data prefetching. Additionally, to free programmers from concerns over locking, Clorox allows multiple operations on these data structures to be grouped into atomic actions.
Clorox is a client-side technology. You have to write the backend code -- returning JSON objects with a TTL that controls caching -- yourself. Clorox consists of the Javascript runtime (which incorporates MochiKit) and a compiler (based on Rhino) that transforms application Javascript to Javascript. Why this extra step of compilation?
The Clorox Compiler compiles Clorox JavaScript files (.cjs files) into plain JavaScript. Didn't we just say that programmers write their applications in ordinary JavaScript, you ask? Yes, they do. However, under the hood, Clorox does have to make asynchronous AJAX calls to fetch data. The Clorox Compiler takes in JavaScript code that makes synchronous accesses to data structures and transforms it into JavaScript code that accesses the data structures using continuation-passing style programming. (This means that every time a data structure is accessed, we supply a method that indicates what to do after fetching the appropriate element). Essentially, this involves breaking up the code into a collection of callback functions. At runtime, these CPS accesses will generate the appropriate sequence of RPCs. In any case, installing the compiler is very easy. Just download it from the Clorox web site. It's packaged as a single jar file, so there's nothing else to do. It's written using Java 1.5, so you'll need a recent version of the Java Runtime to use it.
So you write code like this:
var cache = new ClientCache(new SimpleAJAXTransport("/path/to/your/script.pl"));
var cs_array = new SmartArray("basicArray", new NoPrefetchPolicy(), cache);
function printNumbers() {
for (var i = 0; i < 100; ++i) {
doWork(i);
}
}
function doWork(i) {
document.getElementById("outputDiv").innerHTML += cs_array.access(i) + " ";
}
and the compiler turns it into some ugly stuff with callbacks that does all the Ajax stuff for you. Clorox also allows you to configure whether it prefetches values or not for improved performance. You can see the framework in action in a mapping demo and a search suggestion demo.
Right now Clorox is in Alpha and has only been tested with Firefox 1.5. The documentation is brief but serviceable but more code examples are needed to provide a good framework to start your own application. Future developments will include automatic cache tuning. As this toolkit evolves, it could become the foundation for other, higher levels of abstraction. ORM for Ajax anyone?
Topics: Ajax Frameworks, Code Generation
So, I missed two Javascript code generators, found the new home of a third, and found one more Ajax OS beastie. First the code generators:
(js (defun foobar (a b) (return (+ a b))))
and the Javascript:
function foobar(a, b) {
return a + b;
}
The compiler extracts CIL from a .net assembly. It filters out the classes which are marked with the ScriptAttribute. It selects the target language and emits the source.
I've already blogged about this tool once before, but it does belong in any listing of Javascript code generators, so in it goes.
Next, on the heels of our entry on Atomic OS, we have another cute little command line shell toy in Ajax: JS/UIX Terminal.
It's cute to be able to play invaders in the browser, for about 5 minutes. Under the covers, it purports to be an OS written in Javascript, implemented on top of the browser:
JS/UIX is an UN*X-like OS for standard web-browsers, written entirely in JavaScript (no plug-ins used). It comprises a virtual machine, shell, virtual file-system, process-management, and brings its own terminal with screen- and keyboard-mapping.
Cute as well, and probably an interesting learning experience. Not sure if the OS on top of the Browser is where the future of Ajax is. Abstracting services down to that level seems like overkill.
Topics: Ajax Examples, Code Generation, Javascript
Javascript code generation seems to be here to stay, with GWT taking roots and Ruby providing it's RJS Templates. It seems like it's time for a roundup of the little critters.

I'd only feel comfortable using RJS and GWT at this point, since RJS doesn't really "hide" the Javascript all that much, and GWT provides the hosted mode. If you are debugging a complex, generated hunk of Javascript, you'd better be able to either make sense of the code (RJS) or go back to the source language (Java in GWT). I'm surprised the other tools don't think to provide some sort of high-level language debugging capability. I guess too few developers these days remember what it was like to debug assembly language generated from a compiler.
There's also the ParenScript LISP->Javascript compiler, but it, alas, seems to have gone the way of all single developer open source projects. Last, there is this article from Microsoft Research about the implementation of an Oberon to Javascript compiler. An interesting how-to of how such a thing can be built. Sorry, just the article, no download.
Topics: Ajax Development, C#, Code Generation, GWT, Javascript