Tuesday, September 16th, 2008

Jeene: Automatic partial evaluation for JavaScript, in JavaScript

Category: 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.

javascript

  1. // This code actually works ;-)
  2. Function.prototype.specialize = net.higherorder.jeene.Jeene.make();
  3.  
  4. var mk_tag = function(tag,clz,cont) {
  5.   return "< "+tag+" class='"+clz+"'>"+cont+"";
  6. };
  7.  
  8. var mk_div_green = mk_tag.specialize({tag:'div', clz: 'green'});
  9.  
  10. mk_div_green("Pratt rocks!");
  11. //result: <div class='green'>Pratt rocks!</div>
  12.  
  13. mk_div_green.toSource ? mk_div_green.toSource() : mk_div_green.toString();
  14. //result:
  15. //(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.

Posted by Dion Almaer at 5:29 am
2 Comments

++---
2.8 rating from 13 votes

2 Comments »

Comments feed TrackBack URI

Line six should probably be:
return “”+cont+””;
otherwise the final output doesn’t make sense.

Comment by Jabapyth — September 16, 2008

Yes, the code in the blog posting is accurate, though. I think it is the result of forgetting to escape ‘<‘.

Comment by krukow — September 16, 2008

Leave a comment

You must be logged in to post a comment.