Saturday, April 30, 2011

RPython to Javascript


RPythonic 0.3.1 now integrates with the fantastic Emscripten project to allow conversion of RPython into Javascript. PyPy itself had an experimental backend for Javascript, but this was recently removed because no one was maintaining it. Emscripten is more useful in any case because it will allow for the translation of C libraries used within RPython/RFFI. The PyPy backend would have been limited to only translating RPython code.

Translation begins with passing the RPython entry-point to rpy.compile(). Compile will then take care of translating to C, passing to Emmaken and LLVM, disassemble the LLVM binary, optimize with Emscripten's remove-dead-functions, pass that to emscripten.py to generate the javascript, and finally optimize the javascript with Closure. The result is 6,000 lines of javascript, most of which is RPython's reference counting garbage collector.

To make RPythonic easy to install several things are now included:

  • prebuilt d8 (google v8 javascript interpreter)

  • Emscripten configured for Ubuntu 11.04

  • Closure

  • PyCparser

  • CppHeaderParser

Monday, April 18, 2011

RPython modules for CPython




It is now possible to use RPythonic to write CPython extension modules in RPython, RPythonic will work as the front-end that uses the PyPy translation toolchain to generate C code, compile it with GCC, package it in a cache directory with generated ctypes wrappers, and finally replace the decorated functions in-place in CPython.

RPythonic 0.2.8



RPython CPython Module API



import os, sys
sys.path.append('..')
import rpythonic
rpythonic.set_cache( '../cache' )
rpythonic.set_pypy_root( '../../pypy' )
################################
rpy = rpythonic.RPython()
@rpy.bind() # declare arg types is optional if,
def add( a=1, b=1000 ): # keyword defaults are given
return a+b
@rpy.bind(a=float, b=float)
def sub( a, b ):
return a-b
rpy.cache('test1') # only compiles if cache is dirty
########### now functions are using compiled version ###########
print add( 99, 88 )

Module Compiling and Caching