view README @ 47:3f4982759e55

Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
author Atul Varma <varmaa@toolness.com>
date Mon, 06 Jul 2009 08:13:45 -0700
parents 04a6e9a67ae5
children 7401af070bb8
line wrap: on
line source

Pymonkey README
---------------

Pymonkey is a pure Python CAPI module to expose the Mozilla
SpiderMonkey engine to Python.

Rationale and Goals:

  * Python and JS are fairly complementary languages. Python provides
    tremendous developer productivity via its dynamic language
    features, its vibrant library of third-party client-side code, and
    its ease of readability. JavaScript, on the other hand, is widely
    known by a vast number of casual programmers due to its
    availability in web browsers; it was also envisioned from its
    conception to be executed as untrusted code, which gives it a lot
    of security features that Python has always lacked.

  * There's an increasing need for being able to run JS on the server
    side--particularly untrusted JS [1]. For instance, being able to
    duplicate business logic on the server and client (i.e., browser)
    would be very useful. There's Java-based solutions like Rhino out
    there, but nothing really mature is available for the Python
    world. Ideally, Pymonkey should enable a Python programmer to
    create a custom sandboxed environment for executing JS code
    without needing to write any C.

  * Pymonkey should have awesome Sphinx documentation with doctests
    and all the trappings of a model Python package. Not only should
    it be easy for Python programmers to learn how to use the module,
    but it should also be easy for them to learn more about how
    SpiderMonkey works by reading the docs and playing around with the
    code.

  * Pymonkey needs to have outstanding developer ergonomics. Full
    cross-language stack tracebacks should be available, for instance,
    and developers should be able to easily debug. Access to memory
    profiling facilities in JS-land is a must.

  * The module uses the Python CAPI: no SWIG, Pyrex, or other
    intermediaries. The obvious disadvantage here is that it means
    more C code, but the advantages are that 
    
      (A) contributors don't need to learn anything other than the
          Python and SpiderMonkey C APIs to contribute, and

      (B) it means one less dependency, which makes the build process
          easier.

    The module also doesn't use ctypes because using the SpiderMonkey
    C API requires fairly complex preprocessor macros defined in the
    engine's header files.

    Finally, Atul has never really made a straight Python CAPI module
    before, so he wanted to give it a try.

[1] https://wiki.mozilla.org/ServerJS

Building and Testing
--------------------

You can either build SpiderMonkey off the mozilla-central HG
repository, or off a mirror I made of its SpiderMonkey directory. The
latter can be obtained here:

  http://hg.toolness.com/spidermonkey/

Just HG clone that repository and read the instructions in the README
to build SpiderMonkey.

Then come back to the root of your pymonkey repository and run:

  python manage.py build --static --objdir=PATH_TO_OBJDIR

Where PATH_TO_OBJDIR is the path to your Mozilla/SpiderMonkey build's
objdir.

Note that at the moment, the build script is only tested on OS X.

Example Code
------------

Right now the only example code that exists is in the test suite at
test_pymonkey.py. Check it out and feel free to add more.

Challenges
----------

There's a number of challenges that need to be resolved before
pymonkey can be really usable. Here's some of them.

Garbage Collection

Python's garbage collection uses reference counting, whereas
SpiderMonkey's is mark-and-sweep. We'll likely run into situations
where there are cycles that exist between SpiderMonkey and Python
objects; this is actually quite similar to the relationship between
XPCOM and JavaScript in the Mozilla platform--XPCOM uses reference
counting too--so detecting such cycles will probably involve creating
something akin to XPCOM's cycle collector [1].

[1] https://developer.mozilla.org/en/Interfacing_with_the_XPCOM_cycle_collector