Spidermonkey in PECL


Today I finally released spidermonkey on PECL, you can find it here: http://pecl.php.net/package/spidermonkey/

The whole thing should work fine, I’m already working on several optimisations thanks to jorendorff from Mozilla on IRC. The next thing is to provide better error report, compilation/decompilation of scripts and more default functions for a bunch of things like Iterators and the like.

The last thing I added is a bunch of function applied to streams, here is an exemple:

/* create context */
$ctx = new JSContext();
/* provide fopen/fclose functions */
$ctx->registerFunction('fopen');
$ctx->registerFunction('fclose');
/* for output */
$ctx->registerFunction('printf');

Then in Javascript:

/* open file */
fd = fopen('somefile.txt', 'r')
while (line = fd.getline()) {
  printf("%s\n", line)
}
fclose(fd)

Or

/* open file */
fd = fopen('somefile.txt', 'w')
/* here fd.SEEK_SET is the default value and is optional */
fd.seek(1024, fd.SEEK_SET)
fd.write("some text here :D")
fclose(fd)

, , , , ,

  1. #1 by Jonathan on February 24, 2009 - 8:31 pm

    Congrats !

  2. #2 by Christophe on February 25, 2009 - 9:16 am

    Is there any plan to use this server-side Javascript syntax in the browser ? Will you process on the server the script tags marked with runat=”server” ?

  3. #3 by Christophe Robin on February 25, 2009 - 10:11 am

    No there is no plan like this yet, what I’m providing is a tool allowing you to run Javascript – There are a lot of ways to parse those kind of tags so I think it’s better for the user to implement it himself using either a template system, an output filter or whatever you want. That way you’ll get a lot more control.

  4. #4 by Christophe on February 25, 2009 - 10:20 am

    Makes sense, thanks.

  5. #5 by Oliver on February 26, 2009 - 12:31 pm

    Congrats. Hopefully have an excuse to use your extension soon.

  6. #6 by Christophe Robin on February 26, 2009 - 11:07 pm

    Tomorrow I’ll release version 0.1.1 which should remove a bug on NULL PHP values and makes several optimisations coming with revision 47 on the SVN

  7. #7 by Dima on May 8, 2009 - 10:23 am

    Hi, I tried to install this extension, but I found the problem:

    yums:/# pecl search spidermonkey
    Retrieving data…0%MATCHED PACKAGES, CHANNEL PECL.PHP.NET:
    =======================================
    PACKAGE STABLE/(LATEST) LOCAL
    spidermonkey 0.1.2 (beta) JavaScript engine for PHP
    yums:/# pecl install spidermonkey
    Failed to download pecl/spidermonkey within preferred state “stable”, latest release is version 0.1.2, stability “beta”, use “channel://pecl.php.net/spidermonkey-0.1.2″ to install
    Cannot initialize ‘channel://pecl.php.net/spidermonkey’, invalid or missing package file
    Package “channel://pecl.php.net/spidermonkey” is not valid
    install failed
    yums:/# pecl channel-add channel://pecl.php.net/spidermonkey-0.1.2
    channel-add: Cannot open “channel://pecl.php.net/spidermonkey-0.1.2″ (Cannot download nonjavascript:document.tcommentform.submit();-http URL “channel://pecl.php.net/spidermonkey-0.1.2″)
    yums:/#

  8. #8 by Christophe Robin on May 8, 2009 - 11:59 am

    I’d suggest you to build the latest SVN revision, just check how to do at the end of: http://www.bombstrike.org/2009/02/bringing-javascript-to-the-server/
    Also, make sure you have: PHP 5.3 ( beta or RC ), spidermonkey 1.7+

  9. #9 by martin on May 21, 2009 - 12:45 am

    This looks very promising and is just what I am looking for for a project. Unfortunately getting it to run proves to be the most excruciating task. I’ve finally got the latest php snapshot running but this disables CLI and subsequently pear and pecl. Anyway, I managed to restore that functionality but when i try to install the package i run into this error:

    checking whether to enable spidermonkey support… yes, shared
    configure: error: jsapi.h not found. Please reinstall libjs.
    ERROR: `/etc/php5/temp/spidermonkey/configure’ failed

    i have no idea where to get “jslib”. Part of what package-system or package is that? What I could find is either the old mozilla “jslib” or the rather new “jslibs” project. What do I have to do to get past this point (on to the next fatal error?)

    thank you very much for the great extension and I’d be too happy actually using it!

  10. #10 by Christophe Robin on May 21, 2009 - 3:14 pm

    Which distribution are you using ? Depending on the distro, there should be package named libjs or spidermonkey, if not you’ll need to get this file: http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz and build/install it on your system manually.

  11. #11 by martin on May 21, 2009 - 7:36 pm

    Thanks for the reply! I am using Ubuntu 8.10. I installed spidermonkey from the repository. There also are several packages that begin with libjs but most of them appear to be just clientside js frameworks or joystick drivers.

    I just tried that mozilla link but it refused to compile.

    The install still fails at the same position. My js-cli (that came with spidermonkey i presume) version is JavaScript-C 1.7.0 2007-10-03.

    Is there may a static binary i could use to test?

  12. #12 by John on June 6, 2009 - 5:51 pm

    Great work on this extension. Very useful to have, although we can’t use it because of the need for PHP 5.3 beta/RC. Do you know why that’s required and if there’s any way we can make this work with 5.2.x?

  13. #13 by saaadel on August 11, 2009 - 9:41 am

    Simple trick with this module code and ….
    http://saaadel.com/phpinfo.php

    spidermonkey 0.1.2 work at PHP Version 5.2.6-3ubuntu4.1

  14. #14 by wojta on September 3, 2009 - 11:47 am

    I have added this bug with PHP compiled as Apache2 module:
    http://pecl.php.net/bugs/bug.php?id=16641

  15. #15 by Ionut G. Stan on December 9, 2009 - 11:59 pm

    Hi Christophe,

    Great work on this extension! I have a couple of feature requests (to come) and a bug report (unfortunately):

    http://pecl.php.net/bugs/bug.php?id=16967

    Alas, my C skills are almost null, otherwise I’d have tried to help you on this. Maybe if you could give me at least some pointers on where to look for solving this…

(will not be published)