Current status and API of spidermonkey


Spidermonkey had many features and changes done this week, so let’s look at what it can actually do, and what it can’t.

Features

  1. Execute Javascript ( well, of course it does :D )
  2. Register PHP functions to use in Javascript
  3. Assign PHP values in Javascript
  4. Register Classes in Javascript, allowing the user to instanciate them.

Just with those functionalities, you can already do a great deal of things with JS, and extend it really easily.

Conversion

When working with each others, Javascript convert is types to Zval ( PHP native type ) and vice-versa. Most types are successfully converted, but there are some exceptions:

  • Javascript arrays are converted to stdClass due to the fact that they are stored as Objects by Spidermonkey.
  • Javascript closures are not yet supported, but because PHP 5.3 support closures too, it should be done soon.
  • Javascript regexp are not supported either, because there are no equivalents on the PHP side. In the end they’ll be converted to string.

Streams resources will soon have a prototype added by default with functions like read(), getline(), write(), etc…
Objects extending the Iterator abstract will also have a forEach function defined.

API

Here is the API:

class JSContext {
    /* evaluate javascript source code
     *
     * @param string $script             A string containing the javascript source code
     * @return mixed                     The last value in the global scope is returned to PHP
     */

    public mixed  evaluateScript(string $script);

    /* register a function for use in javascript
     *
     * @param callback $callback         A valid callback that will be called
     * @param string   $name             The name under which the function will appear in Javascript,
     *                                   mandatory for closures and recommanded for array($obj, 'function')
     */

    public void   registerFunction(callback $callback [, string $name]):

    /* register a class for use in javascript so that it can be instancied using "new"
     *
     * @param callback $class_name       A valid class to export
     * @param string   $exported_name    The name under which the class will appear
     */

    public void   registerClass(string $class_name [, string $exported_name])

    /* register a variable for use in javascript
     *
     * @param string   $name             The name of the variable in Javascript
     * @param mixed    $value            Value for the variable, objects  and resources
     *                                   are passed by reference
     */

    public void   assign(string $name, mixed $value)

    /* those function allow you to play with the version the engine is running
     * it's not totally compatible and most versions will not be loaded by
     * the engine, you can still have a try with the constants defined above */

    public mixed  setVersion(long $version)
    public long   getVersion()
    public string getVersionString(long $version)
}

This is the current status at revision 38. Next revisions will be aimed at bug correction and error management. If you have any ideas for features, feel free to comments this post.

, , ,

  1. #1 by Calvin Liu on August 3, 2009 - 8:37 am

    A small mistake of the comment block of method ‘assign’ in the API box:
    /* register a function for use in javascript…

    It should be ‘…a variable for use…’. :-)

(will not be published)