Mon, 02 Jul 2018 16:43:07 +0100
Merge
sundar@1524 | 1 | /* |
sundar@1524 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
sundar@1524 | 3 | * |
sundar@1524 | 4 | * Redistribution and use in source and binary forms, with or without |
sundar@1524 | 5 | * modification, are permitted provided that the following conditions |
sundar@1524 | 6 | * are met: |
sundar@1524 | 7 | * |
sundar@1524 | 8 | * - Redistributions of source code must retain the above copyright |
sundar@1524 | 9 | * notice, this list of conditions and the following disclaimer. |
sundar@1524 | 10 | * |
sundar@1524 | 11 | * - Redistributions in binary form must reproduce the above copyright |
sundar@1524 | 12 | * notice, this list of conditions and the following disclaimer in the |
sundar@1524 | 13 | * documentation and/or other materials provided with the distribution. |
sundar@1524 | 14 | * |
sundar@1524 | 15 | * - Neither the name of Oracle nor the names of its |
sundar@1524 | 16 | * contributors may be used to endorse or promote products derived |
sundar@1524 | 17 | * from this software without specific prior written permission. |
sundar@1524 | 18 | * |
sundar@1524 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
sundar@1524 | 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
sundar@1524 | 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
sundar@1524 | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
sundar@1524 | 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
sundar@1524 | 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
sundar@1524 | 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
sundar@1524 | 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
sundar@1524 | 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
sundar@1524 | 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sundar@1524 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sundar@1524 | 30 | */ |
sundar@1524 | 31 | |
sundar@1524 | 32 | // Simple sample to demonstrate openjdk asmtools assembler with |
sundar@1524 | 33 | // nashorn dynalink linker in a invokedynamic instruction. |
sundar@1524 | 34 | // |
sundar@1524 | 35 | // To assemble this file, use the following command: |
sundar@1524 | 36 | // |
sundar@1524 | 37 | // java -cp <asmtools.jar> org.openjdk.asmtools.Main jasm Main.asm |
sundar@1524 | 38 | // |
sundar@1524 | 39 | // See also: https://wiki.openjdk.java.net/display/CodeTools/asmtools |
sundar@1524 | 40 | // |
sundar@1524 | 41 | // NOTE: Uses nashorn internals and so *may* break with later nashorn! |
sundar@1524 | 42 | |
sundar@1524 | 43 | super public class Main |
sundar@1524 | 44 | version 52:0 |
sundar@1524 | 45 | { |
sundar@1524 | 46 | |
sundar@1524 | 47 | |
sundar@1524 | 48 | public Method "<init>":"()V" |
sundar@1524 | 49 | stack 1 locals 1 |
sundar@1524 | 50 | { |
sundar@1524 | 51 | aload_0; |
sundar@1524 | 52 | invokespecial Method java/lang/Object."<init>":"()V"; |
sundar@1524 | 53 | return; |
sundar@1524 | 54 | } |
sundar@1524 | 55 | |
sundar@1524 | 56 | public static Method main:"([Ljava/lang/String;)V" |
sundar@1524 | 57 | stack 2 locals 2 |
sundar@1524 | 58 | { |
sundar@1524 | 59 | // List l = new ArrayList(); |
sundar@1524 | 60 | new class java/util/ArrayList; |
sundar@1524 | 61 | dup; |
sundar@1524 | 62 | invokespecial Method java/util/ArrayList."<init>":"()V"; |
sundar@1524 | 63 | astore_1; |
sundar@1524 | 64 | aload_1; |
sundar@1524 | 65 | |
sundar@1524 | 66 | // l.add("hello"); |
sundar@1524 | 67 | ldc String "hello"; |
sundar@1524 | 68 | invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2; |
sundar@1524 | 69 | pop; |
sundar@1524 | 70 | |
sundar@1524 | 71 | // l.add("world"); |
sundar@1524 | 72 | aload_1; |
sundar@1524 | 73 | ldc String "world"; |
sundar@1524 | 74 | invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2; |
sundar@1524 | 75 | pop; |
sundar@1524 | 76 | |
sundar@1524 | 77 | // printLength(l); |
sundar@1524 | 78 | aload_1; |
sundar@1524 | 79 | invokestatic Method printLength:"(Ljava/lang/Object;)V"; |
sundar@1524 | 80 | |
sundar@1524 | 81 | // printLength(args); // args is argument of main method |
sundar@1524 | 82 | aload_0; |
sundar@1524 | 83 | invokestatic Method printLength:"(Ljava/lang/Object;)V"; |
sundar@1524 | 84 | return; |
sundar@1524 | 85 | } |
sundar@1524 | 86 | |
sundar@1524 | 87 | private static Method printLength:"(Ljava/lang/Object;)V" |
sundar@1524 | 88 | stack 2 locals 1 |
sundar@1524 | 89 | { |
sundar@1524 | 90 | getstatic Field java/lang/System.out:"Ljava/io/PrintStream;"; |
sundar@1524 | 91 | aload_0; |
sundar@1524 | 92 | |
sundar@1524 | 93 | // Using nashorn embedded dynalink linker with the following invokedynamic |
sundar@1524 | 94 | // 'length' property on a bean - arrays, lists supported |
sundar@1524 | 95 | |
sundar@1524 | 96 | 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; |
sundar@1524 | 97 | |
sundar@1524 | 98 | // print 'length' value |
sundar@1524 | 99 | invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V"; |
sundar@1524 | 100 | return; |
sundar@1524 | 101 | } |
sundar@1524 | 102 | |
sundar@1524 | 103 | } // end Class Main |