Search |
||
DLR Expression Tree like in JavaPosted by forax on July 31, 2009 at 3:33 PM PDT
Two months ago, I was discussing with a friend about
dynamic languages and JSR 292 when he mentions
DLR Expression Trees.
At that time, I had a vague memory,
mostly because I was under the effect of a
persisting jet lag, when I attend the talk of Mads Torgersen
about it at last year JVM Summit.
An Expression Tree
The main idea is to be able to create an AST and to
transform it into runnable code at runtime.
MethodType methodType = MethodType.make(int.class, int.class, int.class);
ExprBuilder builder = new ExprBuilder(methodType);
Var a = builder.parameter(0);
Var b = builder.parameter(1);
builder.
load(a).
load(b).
add();
MethodHandle mh = builder.createMethodHandle(Main.class);
System.out.println(mh.
Also notice that createMethodhandle takes a class as argument because the anonymous method will be able to access to all fields, methods, etc even if they are declared private (thanks to the anonymous class loader) like a classical member of this class.
If you want to play with it, the code is
available here
and works with current
jdk7 binaries (b67).
/usr/jdk/jdk1.7.0/bin/java -cp classes:lib/asm-all-3.2.jar -XX:+EnableInvokeDynamic Main
Cheers, »
Related Topics >>
Open JDK Comments
Comments are listed in date ascending order (oldest first)
Submitted by aberrant on Mon, 2009-08-03 09:36.
Wait.. did you just implement a simple closure/lambda using invokedynamic? Or is this more of a mixin/method injection example? I don't quite understand all of it yet but it looks interesting.
Submitted by forax on Wed, 2009-08-05 12:14.
Better later than never...
@aberrant, it's not closure by example you can't capture a field, so it's more a lambda generator using method handle (not invokedynamic). You're right that it allows a kind of method (or function) injection.
@4bugzilla, you are right, I use ASM. It's as clean as DLR expression tree implementation which heavily relies on namespace System.Reflection.Emit. I agree that bytecode manipulation is not standard, there is no package java.reflection.emit in jdk7 but it's fairly usual, in jdk7, reflection use bytecode generation to speed up method invocation, implement java.lang.reflect.Proxy or to speed up XSLT transformation. Also Hibernate or Toplink use bytecode manipulation.
At last, in this example, the bytecode generator is fairly trivial.
Rémi
Submitted by 4bugzilla on Tue, 2009-08-04 08:58.
As far as I can see you are using asm bytecode manipulation library to create some bytecode on the fly. Please correct me if I am wrong.
Tha is why it looks to me like this is not very "clean" and "true" implementation of Expression Tree using jdk 7 features. For me it is some sort of "simulation" which requires non standard and non-trivial hacks like bytecode manipulation.
The main idea is to be able
Submitted by ugghome on Tue, 2009-09-01 01:43.
The main idea is to be able to create an AST and to transform it into runnable code at runtime. ed hardy,
|
||
|
|