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:

Object Translation: If objects/values are passed to/from the interpreter to/from the native language – is that translation handled automatically? Simplicity: How hard is it to get up-and-running? (Is extra compilation required or is it written using native language code?) 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.

include "js.php"; # Introduce a new JavaScript function, named print, # which calls a PHP function function show($msg) { echo "$msg

"; } $jsi_vars["print"] = "show"; # Add in a new pure-JavaScript function which calls # our previously-introduced print function jsc::compile("function alert(msg){ print('Alert:' + msg); }"); # Prints out 'Alert: text' 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.

include “js/js.php”;