Change other languages behaviour from own Truffle language

Jaroslav Tulach jaroslav.tulach at oracle.com
Fri Apr 20 07:40:09 UTC 2018


Hello Lars,
this is interesting topic.

On pátek 20. dubna 2018 6:22:29 CEST Lars Schuetze wrote:
> We currently do research on role-oriented programming languages.
> Role-orientation is an enhancement of object-orientation such that objects
> can play roles. 

I assume [DCI](http://wiki.apidesign.org/wiki/DCI) is an example of such role 
orientation.

> By doing so, their type and behaviour (ie., method
> dispatch) is changed. For example, first a.m() dispatches to method m
> implemented in class A. But after playing the role r, a.m() will be
> dispatched to method f in R.

"after playing" or "while playing"? As far as I know in DCI the objects can be 
added to an "interaction" and while they are in the interaction they play the 
roles. Outside of (and "after") the interaction the objects behave normally.

> Also, there exist operators like in
> aspect-orientation that can change instances of classes in a crosscutting
> manner. Roles themselves define methods etc. just like normal classes.
> However, they cannot exist on their own. They always have to be played by
> objects. 
> Currently, there exist some implementations for these languages. For
> example, SCROLL (Scala Roles Language) [1] is implemented in Scala using
> the Dynamic trait and a handwritten dispatch implementation in userland to
> cope with the different memory model of roles.
 Eclipse ObjectTeams on the
> other hand is implemented in Java as an extension of JDT and also employs a
> load time compiler built with the ASM library. It is able to adapt existing
> and compiled applications with roles. It also has a handwritten runtime in
> userland. 

Java and Scala are statically compiled languages and the JVM doesn't make this 
kind of meta-programming completely easy. (Truffle) Dynamic languages may be 
more suited for this kind of manipulations by their natural virtue.

> My question would be if by using Truffle we would be able to implement such
> a behaviour that when calling a method a.m() in language A it will be
> dispatched to a method r.f() in our language? 

I believe it could be done with the decorator. All Truffle languages are 
supposed to work with [foreign objects](http://www.graalvm.org/truffle/javadoc/
com/oracle/truffle/api/interop/TruffleObject.html] - if you can wrap/decorate the 
object "a" from language A by your instance and pass it back, you will control 
everything including dispatch.

> I see that we could use
> Truffle to implement a language that can have that behaviour in its own but
> I don’t see if it is possible to change the behaviour of other languages
> from ours.

By passing your own instance of `TruffleObject` into a foreign language you 
basically take over all the [classical operations/messages](http://
www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/interop/Message.html) 
languages perform. Invocation, property access, etc. all gets routed through 
you and you can perform any magic you need.

I'd suggest you start with [MessageResolution](http://www.graalvm.org/truffle/
javadoc/com/oracle/truffle/api/interop/MessageResolution.html) sample. That 
should give you overview of the possibilities. Good luck.

-jt




More information about the graal-dev mailing list