Monday, March 09, 2009

JSEXT

I've been playing with a javascript library, which works as both a command line, and server-side execution Environment. Its called JSEXT and is a rather interesting beast. plus you can call your server side functions from script that executes in the browser, and JSEXT will generate the boilerplate needed for the AJAX call automatically.

For one thing it provides an incredible amount of auto loading magic, which sees it find not only functions in other javascript files but also in C header files. Any C library will effectively be usable form javascript. That got me thinking Cairo is a C library

Getting the helloworld example from the Cairo FAQ going was reletivly simple. But JSEXT provides something better, an OO wrapper for the Cairo library. Granted I did have to check the source out from SVN and compile it to get them, and then sort out a couple of minor bugs in the wrapper.

Anyway the following code is a slight modification of the basic example. This script is intended to be accessed from a web browser as title.jsx?"Insert your text here" and yes it works perfectly fine if you set the src attribute of an img tag.




function(str){
this.responseHeaders.contentType = "image/png";

var surf = new JSEXT1.Cairo.Surface.image("ARGB32", 800, 80);
var cr = surf.context();

with(cr){
selectFontFace("serif", "normal", "bold");
fontSize = 32;
source = [0, 0, 1];
moveTo(10, 50);
showText(str);
}
surf.writeToPNG(stdout);

}


Yes this is a file containing one anonymous function. This is one of the more curious features of JSEXT. You can give it a directory tree and it will walk it and build a set of Javascript objects based on what it finds. This does requie one method per file, which is somewhat unusual. But seems to work alright in practice. It is also uses lazy loading so only the methods you actually use get loaded into the runtime.

No comments: