Here's a bit of new about invokedynamic:  I just pushed my updated  
code (about 10KLOC) to an OpenJDK integration area:
   http://hg.openjdk.java.net/jdk7/tl/jdk/file/tip/src/share/classes/java/dyn/

It's much more functional than before.  See demo below for a small  
example.  Or look at the combinator definitions (e.g., FromGeneric) in  
the JDK code.

I'm 2/3 done pulling that version back into mlvm patches, on top of  
bsd-port.

Over time, the official JDK bits get integrated first into Sun's  
weekly builds, and then they (I hope) will flow to the BSD-port repo,  
allowing the MLVM patch set to shrink.  This may make private  
"bleeding edge" builds easier to cope with.

Best wishes,
-- John


import java.dyn.*;
import static java.dyn.MethodHandles.*;
import static java.dyn.MethodType.*;

public class FidgetDemo {
     public static void main(String... av) {
         if (av.length == 0)  av = new String[]{ "fred", "buster",  
"ricky" };
         System.out.println("Fidgety self-modifying call site...");
         for (int i = 0; i < 6; i++) {
             System.out.println("--- loop #"+i);
             for (String a : av) {
                 String result = InvokeDynamic.<String>fidget(a);
                 System.out.println(result);
             }
         }
     }

     private static String itch(String x) { return x+" can't get  
comfortable"; }
     private static String fuss(String x) { return x+" is feeling  
moody"; }
     private static String bore(String x) { return x+" needs a change  
of scenery"; }
     private static final String[] NAMES = { "itch", "fuss", "bore" };
     private static MethodHandle which(int i) {
         return lookup().findStatic(FidgetDemo.class, NAMES[i %  
NAMES.length],
                     make(String.class, String.class));
     }

     // It is easy to build hybrid closure-like method handles out of  
classes.
     private static class Guard extends JavaMethodHandle {
         MethodHandle target;  // wrapped target
         CSite        site;    // included site
         // constructor folds a sneaky site reference into a direct  
target
         Guard(CSite site, MethodHandle target) {
             super(INVOKE);
             this.target = target;
             this.site = site;
         }

         String invoke(String x) {
             if ((++site.count & 3) == 0)
                 site.setTarget(new Guard(site, which(site.count)));
             return target.<String>invoke(x);
         }
         private static final MethodHandle INVOKE =
             lookup().findVirtual(Guard.class, "invoke",
                 make(String.class, String.class));

         public String toString() { return "Guard:"+target.toString(); }
     }

     // Use a local call site subclass.  (These are optional but fun.)
     private static class CSite extends CallSite {
         int count;
         public CSite(Class caller, String name, MethodType type) {
             super(caller, name, type);
             //?? super(String.class, "gloat", make(void.class));
             System.out.println("[link] new call site: "+this);
             setTarget(new Guard(this, which(0)));
         }
         // this is just for the noise value:
         public void setTarget(MethodHandle t) {
             System.out.println("[link] set target to "+t);
             super.setTarget(t);
         }
     }

     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("linkDynamic"); }
     private static CallSite linkDynamic(Class caller, String name,  
MethodType type) {
         assert((Object)name == "fidget" &&
                 type == make(String.class, String.class));
         return new CSite(caller, name, type);
     }
}

/* resulting output
--------
Fidgety self-modifying call site...
--- loop #0
[link] new call site:  
CallSite#14491894[fidget(java.lang.String)java.lang.String => null]
[link] set target to Guard:itch(java.lang.String)java.lang.String
fred can't get comfortable
buster can't get comfortable
ricky can't get comfortable
--- loop #1
[link] set target to Guard:fuss(java.lang.String)java.lang.String
fred can't get comfortable
buster is feeling moody
ricky is feeling moody
--- loop #2
fred is feeling moody
[link] set target to Guard:bore(java.lang.String)java.lang.String
buster is feeling moody
ricky needs a change of scenery
--- loop #3
fred needs a change of scenery
buster needs a change of scenery
[link] set target to Guard:itch(java.lang.String)java.lang.String
ricky needs a change of scenery
--- loop #4
fred can't get comfortable
buster can't get comfortable
ricky can't get comfortable
--- loop #5
[link] set target to Guard:fuss(java.lang.String)java.lang.String
fred can't get comfortable
buster is feeling moody
ricky is feeling moody
--------
  */


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to jvm-languages@googlegroups.com
To unsubscribe from this group, send email to 
jvm-languages+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to