0
I Use This!
Inactive
Analyzed 4 days ago. based on code collected 4 days ago.

Project Summary

Python-SpidermonkeyThis Python module allows for the implementation of Javascript? classes, objects and functions in Python, as well as the evaluation and calling of Javascript scripts and functions. It borrows heavily from Claes Jacobssen's Javascript Perl module, which in turn is based on Mozilla's PerlConnect Perl binding.

This code was originally written by John J. Lee in 2003. After being unmaintained for a number of years, it was subsequently picked up by Atul Varma in 2008.

TutorialThe first thing you'll want to do is create a Runtime instance, which encapsulates a JSRuntime object from Spidermonkey. From the JSAPI User Guide:

A JSRuntime, or runtime, is the space in which the Javascript variables, objects, scripts, and contexts used by your application are allocated. Every JSContext and every object in an application lives within a JSRuntime. They cannot travel to other runtimes or be shared across runtimes. Most applications only need one runtime.

Creating the Runtime instance is straightforward:

>>> from spidermonkey import Runtime
>>> rt = Runtime()You'll then want to use the Runtime to create a Contextinstance, which encapsulates a JSContext object from Spidermonkey. From the JSAPI User Guide:

A JSContext, or context, is like a little machine that can do many things involving Javascript code and objects. It can compile and execute scripts, get and set object properties, call Javascript functions, convert Javascript data from one type to another, create objects, and so on.

In Firefox, for instance, a different context is used for each webpage you view. A separate context is even created for each physical browser window, because much of Firefox's functionality is actually written in Javascript. Contexts can have their own security policies associated with them, and objects can be shared between multiple contexts.

Creating a context in Python-Spidermonkey is done like so:

>>> cx = rt.new_context()Now that you've got a context, you can do lots of things, like evaluating arbitrary Javascript expressions and using their results in Python code:

>>> cx.eval_script("1 + 2") + 3
6We can create classes in Python and access them in Javascript, too:

>>> class Foo:
... def hello(self):
... print "Hello, Javascript world!"
>>> cx.bind_class(Foo, bind_constructor=True)
>>> cx.eval_script("var f = new Foo(); f.hello();")
Hello, Javascript world!We can also get back objects from Javascript and use them:

>>> f = cx.eval_script("f;")
>>> f.hello()
Hello, Javascript world!LimitationsThe module currently has a number of features that still need to be implemented. For instance, it's not yet possible to call a function defined in Javascript:

>>> cx.eval_script("function foo(x) { return x + 1; }; foo;")
{'prototype': {}}Errors in Javascript code also don't produce particularly helpful tracebacks:

>>> cx.eval_script("3 + undefinedVariable")
Traceback (most recent call last):
...
JSError: can't evaluate Javascript scriptInstallationNote that at present, installation has only been tested on OS X and 64-bit Ubuntu Linux; support for Windows is forthcoming.

At present, you'll need a C compiler on your system to install this extension, as well as the Pyrex package.

Check out the Python-Spidermonkey module from the SVN repository. From the root of your checkout, run: python setup.py buildDon't worry about the compiler warnings. Then, with appropriate permissions, run:
python setup.py installTestingThe module has a test suite. Just run:

python test.pyNote that one of the tests currently prints out a Javascript error message. This isn't a test failure (though it is a bug).

AcknowledgementsThanks to Brendan Eich for help with several Spidermonkey issues (and for all his Mozilla work), and to Erwin on the freenode #c IRC channel for gdb tips.

Tags

javascript python

In a Nutshell, python-spidermonkey...

GNU General Public License v2.0 or later
Permitted

Commercial Use

Modify

Distribute

Place Warranty

Forbidden

Sub-License

Hold Liable

Required

Distribute Original

Disclose Source

Include Copyright

State Changes

Include License

These details are provided for information only. No information here is legal advice and should not be used as such.

All Licenses

This Project has No vulnerabilities Reported Against it

Did You Know...

  • ...
    Black Duck offers a free trial so you can discover if there are open source vulnerabilities in your code
  • ...
    anyone with an Open Hub account can update a project's tags
  • ...
    in 2016, 47% of companies did not have formal process in place to track OS code
  • ...
    you can subscribe to e-mail newsletters to receive update from the Open Hub blog

Languages

Languages?height=75&width=75
C
95%
3 Other
5%

30 Day Summary

Jan 20 2019 — Feb 19 2019

12 Month Summary

Feb 19 2018 — Feb 19 2019

Ratings

Be the first to rate this project
Click to add your rating
   Spinner
Review this Project!