Tuesday, September 16th, 2008
Jeene: Automatic partial evaluation for JavaScript, in JavaScript
Jeene is a new open source project by Karl Krukow, which aims to create a partial evaluator for JavaScript.
A partial evaluator (or program specializer) is a program which takes two inputs: another program and an environment mapping variables to values; it outputs a specialized (i.e., more efficient) version of the input program with respect to the environment. One can think of a partial evaluator as a mix between an interpreter and a compiler: it interprets the static parts of the program and emits code for the dynamic parts.
- // This code actually works ;-)
- Function.prototype.specialize = net.higherorder.jeene.Jeene.make();
- var mk_tag = function(tag,clz,cont) {
- return "< "+tag+" class='"+clz+"'>"+cont+"";
- };
- var mk_div_green = mk_tag.specialize({tag:'div', clz: 'green'});
- mk_div_green("Pratt rocks!");
- //result: <div class='green'>Pratt rocks!</div>
- mk_div_green.toSource ? mk_div_green.toSource() : mk_div_green.toString();
- //result:
- //(function (cont) {return ("<div class='green'>" + cont) + "</div>";})
This last line, shows that the output function is much more efficient than what is created by general JavaScript curriers which have been seen before. These functions merely wait evaluating the function until all parameters are supplied; instead, a partial evaluator will create specialized function taking advantage of the information given.





Line six should probably be:
return “”+cont+””;
otherwise the final output doesn’t make sense.
Yes, the code in the blog posting is accurate, though. I think it is the result of forgetting to escape ‘<‘.