Monday, June 8, 2009

Da Vinci Machine(MLVM) Ready for Hacking

Da Vinci Machine/MLVM, as said in the website , is a multi-language renaissance for the JVM, which is under the umbrella of JSR 292. Some dynamic languages on JVM like JRuby(and Groovy?) have played with those cool MLVM functionalities some times ago in some limited scope. Because the MLVM need to be built from the source, and because on some platforms OpenJDK could not be built easily.
In recent days, the OpenJDK/JDK milestone 3 released, and the MLVM is one of the coolest features targeted in the milestone 3. So, I think it is the time to try how the MLVM tastes in the midst of my busy schedule:)
I download the JDK binary snapshot b59 for my windows. Install and re-setup my path. Then, copy below helloworld codes(shameless got from the repo of MLVM, thanks for John Rose, leader of MLVM/JSR292):

import java.dyn.*;
public class Hello {
public static void main(String... av) {
if (av.length == 0) av = new String[] { "world" };
greeter(av[0] + " (from a statically linked call site)");
for (String whom : av) {
greeter.invoke(whom); // strongly typed direct call
// previous line generates invokevirtual MethodHandle.invoke(String)void
Object x = whom;
InvokeDynamic.hail(x); // weakly typed invokedynamic
// previous line generates invokedynamic MethodHandle.invoke(Dynamic)Dynamic
static void greeter(String x) { System.out.println("Hello, "+x); }
// intentionally pun between the method and its reified handle:
static MethodHandle greeter
= MethodHandles.lookup().findStatic(Hello.class, "greeter",
MethodType.make(void.class, String.class));
// Set up a class-local bootstrap method.
static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
assert(type.parameterCount() == 1 && (Object)name == "hail"); // in lieu of MOP
System.out.println("set target to adapt "+greeter);
MethodHandle target = MethodHandles.convertArguments(greeter, type);
CallSite site = new CallSite(caller, name, type);
return site;


Oh, Error! Yes., that seem "a bug in the 1.7 compiler " discussed here.
try again,

>>javac -source 1.7


>>java -XX:+EnableInvokeDynamic Hello
Hello, world (from a statically linked call site)
Hello, world
set target to adapt greeter(java.lang.String)void
Exception in thread "main" java.lang.UnsupportedOperationException: NYI
at sun.dyn.FromGeneric.buildAdapterFromBytecodes(
at sun.dyn.FromGeneric.(
at sun.dyn.FromGeneric.of(
at sun.dyn.FromGeneric.make(
at sun.dyn.MethodHandleImpl.convertArguments(
at java.dyn.MethodHandles.convertArguments(
at Hello.bootstrapDynamic(
at sun.dyn.CallSiteImpl.makeSite(
at Hello.main(

Cong too! Although the weakly typed invokedynamic entry seem throw the UnsupportedOperationException. (although it is odd why the helloworld example can not pass the test^_^)
It is super-cool! I guess some concepts such as MethodHandle will affect the implementation of the proposal of "closure" in Java.

One natural question is, how do our Eclipse get ready to work with MLVM?

Can ECJ emit the invokedynamic bytecode?
Can Equinox(OSGI) benifit from from the new dynamic behavior?

Come on!

No comments: