Thursday, November 08, 2007

Dynamic code generation for images using Rhino and JavaScript

Chris Double describes an interesting way for dynamic code generation based on (Smalltalk like) images.

In his example he implemented an image class in Javascript and two simple assemblers (one for x86, one for ARM). He uses the Rhino JavaScript engine (provided in js.jar) to run the scripts and write the image to disk. He then uses a simple C program to call a (now native) function in the image.

Actually his generation step hard codes the image generation depending on the specific assembler. But it should be easy to create a more general bytecode like assembler for image generation which compiles to the selected architecture.

Really interesting to use JavaScript/Rhino for that...

If you want to play with his files you have to manually download them since the link to the TAR does not work:

js.jar
x86assembler.js
generate.js
image.js
armassembler.js
run.c

Just call Rhino using: java -jar js.jar generate.js to create the image.

No comments: