[pypy-dev] The JVM backend and Jython

Philip Jenvey pjenvey at underboss.org
Wed Mar 30 21:59:39 CEST 2011

On Mar 30, 2011, at 12:18 AM, Antonio Cuni wrote:
>> I ask
>> because I would like to evaluate the generated Java parser as a
>> potential replacement for our current ANTLR based parser. It seems
>> like a nice baby step towards real collaboration since it seems like a
>> relatively easy place to start. Clearly it would need adjustments to
>> actually work for Jython - but I'd be able to look into that part. I
>> don't think I have the time to try to unbreak the translation
>> though...
> I have to warn you that at the moment, you cannot invoke any Java code from
> RPython.  Implementing it has been on my todo list for years now :-(, but I
> never managed to find the time and the motivation to do it.  However, for
> using the PyPy parser inside Jython it should be enough to do the other way
> around, i.e. call RPython code from Java, which should be possible.

FYI there's an interesting solution on how to call into arbitrary Java code from an invokedynamic enabled language via Atilla Szegedi's somewhat experimental Meta Object Protocol:


Basically on Java 7 invokedynamic a dynamic language invocation instruction is something along the lines of:

obj.someattr = someobj
invokedynamic "__setattr__"(Ljava/lang/String;Ljava/lang/Object;I)V;

Which might dispatch to a PyObject.__setattr__(String name, PyObject value) method (or easily something completely different in invokedynamic land).

The MOP ads another layer in between the invocation and the call site. So as the language implementor you'd use the MOP library to 'relink' your __setattr__ call site to the meta object protocol's more generic version (it only supports a few features but one of them is generic property access). Then you can do the invocation via the MOP, something like:

invokedynamic "dyn:setProp:someattr"(Ljava/lang/Object;I)V;

The point being that other JVM languages will eventually support the MOP protocol and then you'd get property access, invocation, etc. to those languages for free. More importantly, out of the box the MOP lib implements the protocol for plane old Java objects.

If you're already using invokedynamic the library seems simple to hook into and there's basically no call overhead added.

I'm not sure this would even be applicable to RPython as it's more static in nature. But it will certainly help in calling Java from regular Python.

Philip Jenvey

More information about the Pypy-dev mailing list