Spicing Up Embedded JavaScript


One thing that I absolutely adore is the sheer embeddability of JavaScript. It’s a (comparatively) simple language that is fiercely flexible. I tend to liken JavaScript to water – alone it’s painfully simple but it can take the form of its container – and mixing it with anything enhances its flavor.

Since JavaScript, alone, is so dumb (we’ve become spoiled by browsers which provide DOM, setTimeout/setInterval, and even a global object reference – none of which are necessarily required my an ECMAScript implementation) we must rely upon the spice of ‘larger’ languages to help it gain some taste.

We can start by taking a look at some of the most popular languages that are available today: Python, Perl, PHP, Ruby, and Java — all of which have, at least, one embeddable JavaScript interpreter.

One thing is the same in all the interpreter implementations, as well, they all have the ability to introduce (at least) simple objects into interpreter (from the parent language) and extract values again. Sometimes this may be as simple as executing a JavaScript function and getting its return value (which is often translated from its internal JavaScript form back into the native language).

There’s a couple points upon which I like to evaluate the embeddability of a JavaScript interpreter, specifically:

  1. Object Translation: If objects/values are passed to/from the interpreter to/from the native language – is that translation handled automatically?
  2. Simplicity: How hard is it to get up-and-running? (Is extra compilation required or is it written using native language code?)
  3. Bleed-through: Can JavaScript communicate to the base language or is it a one-way-street?

The first point is the easiest one to find compatibility with – virtually all embeddable JavaScript interpreters do some form of object translation. Some do it better (like JE and Rhino) but it generally shouldn’t be a problem for simple scripts.

PHP

» PHPJS

A flexible, pure-PHP, JavaScript interpreter that even accepts compiling to an intermediary bytecode. There is no bleed-through but good object translation.

  1. include "js.php";
  2.  
  3. # Introduce a new JavaScript function, named print,
  4. # which calls a PHP function
  5. function show($msg) {
  6.   echo "$msg\n";
  7. }
  8.  
  9. $jsi_vars["print"] = "show";
  10.  
  11. # Add in a new pure-JavaScript function which calls
  12. # our previously-introduced print function
  13. jsc::compile("function alert(msg){ print('Alert:' + msg); }");
  14.  
  15. # Prints out 'Alert: text'
  16. js_exec("alert('text');");

» J2P5: Pure-PHP ECMAScript Engine

Probably the simplest of the available interpreters – there doesn’t appear to be a clear way of communicating from JavaScript-to-PHP (or vice versa). Although, it is implemented in pure-PHP which allows for some nice cross-platform compatibility.

  1. include "js/js.php";
  2.  
  3. $script = <<<EOD
  4.   print("Hello from JavaScript");
  5. EOD;
  6.  
  7. js::run($script);

Perl

» JavaScript::SpiderMonkey

An embedding of Mozilla’s Spidermonkey JavaScript engine into Perl. Since it uses Spidermonkey it’ll require that extra source code and compilation. Object translation is good but there’s no bleed-through (JavaScript calling native Perl functionality that isn’t explicitly defined).

  1. use JavaScript::SpiderMonkey;
  2.  
  3. # Instantiate the interpreter
  4. my $j = JavaScript::SpiderMonkey->new();
  5.  
  6. # Introduce a new JavaScript function, named print,
  7. # which calls a Perl function
  8. $j->function_set("print", sub { print @_, "\n" } );
  9.  
  10. # Add in a new pure-JavaScript function which calls
  11. # our previously-introduced print function
  12. $j->eval("function alert(msg){ print('Alert:' + msg); }");
  13.  
  14. # Prints out 'Alert: text'
  15. $j->eval("alert('text');");

» JE: Pure-Perl ECMAScript Engine

A pure-Perl JavaScript engine – shows incredible promise (probably my favorite new engine in addition to Rhino). Has tremendous potential (including the ability to serialize the runtime environment!).

  1. use JE;
  2.  
  3. # Instantiate the interpreter
  4. my $j = new JE;
  5.  
  6. # Introduce a new JavaScript function, named print,
  7. # which calls a Perl function
  8. $j->new_function(print => sub { print @_, "\n" } );
  9.  
  10. # Add in a new pure-JavaScript function which calls
  11. # our previously-introduced print function
  12. $j->eval("function alert(msg){ print('Alert:' + msg); }");
  13.  
  14. # Prints out 'Alert: text'
  15. $j->method(alert => "text");
  16.  
  17. # So does this
  18. $j->eval("alert('text');");
  19.  
  20. # Introduce the ability to do remote requests
  21. use Net::FTP;
  22.  
  23. $j->bind_class( package => "Net::FTP", name => "FTP" );
  24.  
  25. # Execute native Perl functionality (FTP module)
  26. # directly from JavaScript
  27. $j->eval(<<'--end--');
  28.   var connect = new FTP("ftp.mozilla.org");
  29.   connect.get("index.html");
  30. --end--

Note the extreme bleed-through that you can achieve with JE (in addition to excellent object translation).

Java

» Rhino

Probably one of the most famous embeddable JavaScript implementations. Implemented in pure Java has excellent object translation and perfect bleed-through. The bleeding can even be controlled explicitly at runtime (to create a more confined environment).

In the following code (using in some of the jQuery build system) two functions are defined which utilize a number of native Java packages – all without ever writing a single line of Java code.

  1. importPackage(java.io);
  2.  
  3. function writeFile( file, stream ) {
  4.   var buffer = new PrintWriter( new FileWriter( file ) );
  5.   buffer.print( stream );
  6.   buffer.close();
  7. }
  8.  
  9. function read( file ) {
  10.   var f = new File(file);
  11.   var reader = new BufferedReader(new FileReader(f));
  12.   var line = null;
  13.   var buffer = new java.lang.StringBuffer(f.length());
  14.   while( (line = reader.readLine()) != null) {
  15.     buffer.append(line);
  16.     buffer.append("\n");
  17.   }
  18.   return buffer.toString();
  19. }

Python

» Python-Spidermonkey

Previously this was one of the most depressing embedding efforts (and, really, the only one available for Python) but it has since had some new life blown into it – which is much appreciated. It supports decent object translation and bleed-through (including the ability to call native classes, much like in JE).

  1. from spidermonkey import Runtime
  2.  
  3. j = RunTime().new_context()
  4.  
  5. # Introduce a new JavaScript function, named print,
  6. # which calls a Python function
  7. j.bind_callable("print", lambda msg: print(msg));
  8.  
  9. # Add in a new pure-JavaScript function which calls
  10. # our previously-introduced print function
  11. j.eval_script("function alert(msg){ print('Alert:' + msg); }");
  12.  
  13. # Prints out 'Alert: text'
  14. j.eval_script("alert('text');");

Ruby

While there was a previous effort to embed Spidermonkey in Ruby it is largely defunct now, superseded by the new (strangely named) Johnson project.

» Johnson

This is a, relatively, new project that’s working to completely overhaul how communication is currently done in-between Ruby and JavaScript (using Spidermonkey to power the engine). There is complete bleed-through: Ruby can dig in and manipulate JavaScript objects at run-time and JavaScript can call back and access native Ruby functionality. Additionally they’re examining the ability to provide native browser environments using the env.js script that I created last year. While the cross-platform ease-of-use isn’t there yet the project absolutely has a ton of potential.

  1. require "johnson"
  2.  
  3. j = Johnson::Runtime.new
  4.  
  5. # Introduce a new JavaScript function, named print,
  6. # which calls a Ruby function
  7. j.print = lambda {|msg| puts msg};
  8.  
  9. # Add in a new pure-JavaScript function which calls
  10. # our previously-introduced print function
  11. j.evaluate("function alert(msg){ print('Alert:' + msg); }");
  12.  
  13. # Prints out 'Alert: text'
  14. j.evaluate("alert('text');");

Update: Another example of Johnson in action from John Barnette:

  1. require "johnson"
  2.  
  3. class Monkey
  4.   def eat
  5.     puts "Nom, nom, nom, bananas!"
  6.   end
  7. end
  8.  
  9. j = Johnson::Runtime.new
  10.  
  11. # toss a monkey into the runtime
  12. m = Monkey.new
  13. j[:m] = m
  14.  
  15. # add a JSland function to our monkey...
  16. j.evaluate("m.chow = function() { this.eat() }")
  17.  
  18. # ...and now it's available as an instance method on our native Ruby object!
  19. m.chow

There’s a ton of potential in all of these projects – providing interesting features for blurring the lines in-between JavaScript and different host languages. For your next project it should be pretty easy to find a solution to embedding JavaScript as your quick-and-dirty scripting language of choice.

Posted: June 15th, 2008


If you particularly enjoy my work, I appreciate donations given with Gittip.

14 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.