Welcome to MSDN Blogs Sign in | Join | Help

JavaScript = Portable Object Code

A few days ago I submitted a short paper to a small conference on modular programming languages and systems describing a small experiment that I did in my series of "Cool Things You Can Do With JavaScript".

I was just about leaving my office before heading to .ch, and before finally closing my Web browser and locking my PC I entered the name of said conference into Google. Perhaps I shouldn't say that. I should be using MSN Search, but unfortunately "goo" + autocomplete is just too convenient. Maybe MSN should think of something that autocompletes fast, too. Anyway, I found the conference, and - gosh! - that the submission date was just 3 weeks away. [Profanity], I said, because I wanted to submit something to this conference for ages, but now 3 weeks seemed a rather [more profanity] short time. OK, I said, calming myself, there's this idea that you (=me, as I was talking to myself) had been contemplating a while ago: A compiler that would compile a certain simple programming language into JavaScript! This would enable you to "do" Ajax in this, or by extension, any language! With browsers slowly becoming runtime environments for Web applications, this made perfect sense. Also, that should be simple enough to hack and write about in 3 weeks, I thought.

I knew that you can create script on-the-fly and run it through eval, but I had yet to see anyone transforming one scripting language using script into another (hence using JavaScript as a portable "object code"), and all within the browser and without installing anything. To cut a long tale short, the conclusion of which you already know (if you don't: re-read the first sentence of this post), it worked, and after 3 weeks of tinkering and scribbling, I had done it. Most of the time was obviously spent on the compiler proper (I chose Oberon, a simple yet powerful language that you can write a to-JavaScript-compiler for in about one and a half weeks). The following functions are the glue to make it work:

function findScriptType(scp,typ)
{
var code=[];
for(var i=0;i<scp.length;++i){
if(scp[i].type.toLowerCase()==typ){
code.push(scp[i].text)
}
}
return code
}

function addScript(par,code)
{
var scp=document.createElement("script");
scp.text=code;
par.appendChild(scp)//this will also execute the code
}

function compileAll(typ,comp)
{
var scp=document.getElementsByTagName("script");
if(scp.length>0){
var par=scp[0].parentNode;
var code=findScriptType(scp,typ);
for(var i=0;i<code.length;++i){
addScript(par,comp(code[i]))
}
}
}

function myCompiler(code)
{
return code //or some transformation thereof
}

compileAll("text/x-mylang",myCompiler);

The meat is in the myCompiler function that does the transformation (not shown here). To see what was required to compile Oberon into JavaScript I have to refer you to my paper (if it is accepted, or alternatively an MSR Technical Report that I posted recently). Compiling one scripting language on-the-fly into another is not the fastest way of doing stuff, but as a proof of concept it works a treat.

Published Monday, April 24, 2006 3:26 PM by sompost
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Friday, June 02, 2006 9:48 AM by Ralph Sommerer's Budget Wisdom

# Target JavaScript: GWT, Script# and Oberon Script

The paper on Oberon Script that I submitted to the Joint Modular Languages Conference (JMLC 2006), and...

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker