Skip to content


Embedding JavaScript in Python

Reading some posts about embedding languages/runtimes in applications on Planet GNOME reminded me I still had to announce some really quick and incomplete code blob I created some days after last GUADEC edition (which was insanely cool, thanks guys).

It takes WebKit‘s JavaScriptCore and allows you to embed it in some Python program, so you, as a Python developer, can allow consumers to write plugins using JavaScript. Don’t ask me whether it’s useful, maybe it’s not, but anyway.

There’s one catch: currently there is no support to expose custom Python objects to the JavaScript runtime: you’re able to use JavaScript objects and functions etc. from within Python, but not the other way around. I started working on this, but the JSCore API lacked some stuff to be able to implement this cleanly (or I missed a part of it, that’s possible as well), maybe it has changed by now… There is transparent translation of JavaScript base types: unicode strings, booleans, null (which becomes None in Python), undefined (which becomes jscore.UNDEFINED) and floats.

I did not work on the code for quite a long time because of too much real-job-work, maybe it no longer compiles, sorry… Anyway, it’s available in git here, patches welcome etc. I guess this is the best sample code around. It’s using Cython for compilation (never tried with Pyrex, although this might work as well). If anyone can use it, great, if not, too bad, I did learn Cython doing this ;-)

Posted in Development, Linux.

Tagged with , , , .


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. James says

    Howdy, this sounds mighty similar to what I did for “python-rhodes” not long ago:

    http://fagonfoss.com/blog/2007/06/rhodes-01-preview-released/

    To get the code:
    bzr clone http://beta.unstated.net/~james/code/projects/rhodes/rhodes-ng/

  2. James says

    Oh, I forgot to add – you can export python objects into JS and return JS objects back to the python virtual machine – tricky much :)

  3. hacerus says

    this is exactly what i am looking for!!! thanks!!

  4. Vikki says

    Hi Nicolas,

    I tried using your API JScore at :
    http://git.nicolast.be/?p=python-jscore.git;a=commit;h=2801be031c47ff92d6e3c595b403292a7074e080

    I tried to build the dll using my own setup and the source code files found at Webkit.

    but whenever I am trying to call any function from a test file it says “‘module’ object has no attribute ‘GlobalContext’ ”

    I found out that whenever you are doing this : self.ctx = JSGlobalContextCreate(NULL)

    it starts throwing the above error for all classes.

    Could you please urgently help me to either explain how to solve the above issue or help me in
    correctly installing using your setup.py.

    When I am runnning your Setup.py it says Unable to run pkg-config, is it installed?

    Please reply urgently, I need to work and finish some deadlines using this API at work.



Some HTML is OK

or, reply to this post via trackback.