8144186: Update Main.asm sample to use dynalink public API
authorsundar
Fri, 27 Nov 2015 18:28:16 +0530
changeset 34451 b7fec92235d8
parent 34450 a51b49cd704b
child 34452 c34f322b89b9
8144186: Update Main.asm sample to use dynalink public API Reviewed-by: hannesw, mhaupt
nashorn/samples/Main.asm
nashorn/samples/Main.class
--- a/nashorn/samples/Main.asm	Fri Nov 27 13:41:40 2015 +0530
+++ b/nashorn/samples/Main.asm	Fri Nov 27 18:28:16 2015 +0530
@@ -38,66 +38,128 @@
 //
 // See also: https://wiki.openjdk.java.net/display/CodeTools/asmtools
 //
-// NOTE: Uses nashorn internals and so *may* break with later nashorn!
+// NOTE: Uses dynalink API defined by JEP-276 (http://openjdk.java.net/jeps/276)
 
 super public class Main
 	version 52:0
 {
 
+private static final Field linker:"Ljdk/dynalink/DynamicLinker;";
 
+/*
+   static {
+        // create dynalink linker factory
+        final DynamicLinkerFactory factory = new DynamicLinkerFactory();
+
+        // create standard linker that can handle Java POJOs
+        linker = factory.createLinker();
+   }
+*/
+static Method "<clinit>":"()V"
+        stack 2 locals 1
+{
+    new     class jdk/dynalink/DynamicLinkerFactory;
+    dup;
+    invokespecial   Method jdk/dynalink/DynamicLinkerFactory."<init>":"()V";
+    astore_0;
+    aload_0;
+    invokevirtual   Method jdk/dynalink/DynamicLinkerFactory.createLinker:"()Ljdk/dynalink/DynamicLinker;";
+    putstatic       Field linker:"Ljdk/dynalink/DynamicLinker;";
+    return;
+}
+
+/*
+    // Bootstrap method used with invokedynamic methods
+
+    public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) {
+        // use dynalink linker to perform the actual linking
+        return linker.link(
+            new SimpleRelinkableCallSite(
+                new CallSiteDescriptor(
+                    MethodHandles.publicLookup(), 
+                    new NamedOperation(StandardOperation.GET_PROPERTY, name), type)
+            )
+        );
+    }
+
+ */
+public static Method bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
+        stack 10 locals 3
+{
+    getstatic       Field linker:"Ljdk/dynalink/DynamicLinker;";
+    new     class jdk/dynalink/support/SimpleRelinkableCallSite;
+    dup;
+    new     class jdk/dynalink/CallSiteDescriptor;
+    dup;
+    invokestatic    Method java/lang/invoke/MethodHandles.publicLookup:"()Ljava/lang/invoke/MethodHandles$Lookup;";
+    new     class jdk/dynalink/NamedOperation;
+    dup;
+    getstatic       Field jdk/dynalink/StandardOperation.GET_PROPERTY:"Ljdk/dynalink/StandardOperation;";
+    aload_1;
+    invokespecial   Method jdk/dynalink/NamedOperation."<init>":"(Ljdk/dynalink/Operation;Ljava/lang/Object;)V";
+    aload_2;
+    invokespecial   Method jdk/dynalink/CallSiteDescriptor."<init>":"(Ljava/lang/invoke/MethodHandles$Lookup;Ljdk/dynalink/Operation;Ljava/lang/invoke/MethodType;)V";
+    invokespecial   Method jdk/dynalink/support/SimpleRelinkableCallSite."<init>":"(Ljdk/dynalink/CallSiteDescriptor;)V";
+    invokevirtual   Method jdk/dynalink/DynamicLinker.link:"(Ljdk/dynalink/RelinkableCallSite;)Ljdk/dynalink/RelinkableCallSite;";
+    checkcast       class java/lang/invoke/CallSite;
+    areturn;
+}
+
+// default constructor that does nothing!
 public Method "<init>":"()V"
 	stack 1 locals 1
 {
-		aload_0;
-		invokespecial	Method java/lang/Object."<init>":"()V";
-		return;
+    aload_0;
+    invokespecial	Method java/lang/Object."<init>":"()V";
+    return;
 }
 
 public static Method main:"([Ljava/lang/String;)V"
 	stack 2 locals 2
 {
-                // List l = new ArrayList();
-		new	class java/util/ArrayList;
-		dup;
-		invokespecial	Method java/util/ArrayList."<init>":"()V";
-		astore_1;
-		aload_1;
+    // List l = new ArrayList();
+    new	class java/util/ArrayList;
+    dup;
+    invokespecial	Method java/util/ArrayList."<init>":"()V";
+    astore_1;
+    aload_1;
 
-                // l.add("hello");
-		ldc	String "hello";
-		invokeinterface	InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z",  2;
-		pop;
+    // l.add("hello");
+    ldc	String "hello";
+    invokeinterface	InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z",  2;
+    pop;
 
-                // l.add("world");
-		aload_1;
-		ldc	String "world";
-		invokeinterface	InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z",  2;
-		pop;
+    // l.add("world");
+    aload_1;
+    ldc	String "world";
+    invokeinterface	InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z",  2;
+    pop;
 
-                // printLength(l);
-		aload_1;
-		invokestatic	Method printLength:"(Ljava/lang/Object;)V";
+    // printLength(l);
+    aload_1;
+    invokestatic	Method printLength:"(Ljava/lang/Object;)V";
 
-                // printLength(args); // args is argument of main method
-		aload_0;
-		invokestatic	Method printLength:"(Ljava/lang/Object;)V";
-		return;
+    // printLength(args); // args is argument of main method
+    aload_0;
+    invokestatic	Method printLength:"(Ljava/lang/Object;)V";
+    return;
 }
 
 private static Method printLength:"(Ljava/lang/Object;)V"
 	stack 2 locals 1
 {
-		getstatic	Field java/lang/System.out:"Ljava/io/PrintStream;";
-		aload_0;
-
-                // Using nashorn embedded dynalink linker with the following invokedynamic
-                // 'length' property on a bean - arrays, lists supported
+    getstatic	Field java/lang/System.out:"Ljava/io/PrintStream;";
+    aload_0;
 
-                invokedynamic   InvokeDynamic REF_invokeStatic:jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;":"dyn:getProp|getElem|getMethod:length":"(Ljava/lang/Object;)Ljava/lang/Object;" int 0;
+    // Using bootstrap method in this class with the following invokedynamic
+    // which uses dynalink linker. Dynalink's bean linker handles Java beans.
+    // 'length' property on a bean - arrays, lists supported
 
-                // print 'length' value
-		invokevirtual	Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
-		return;
+    invokedynamic   InvokeDynamic REF_invokeStatic:Main.bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;":"length":"(Ljava/lang/Object;)Ljava/lang/Object;";
+
+    // print 'length' value
+    invokevirtual	Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
+    return;
 }
 
 } // end Class Main
Binary file nashorn/samples/Main.class has changed