1 # 2 # Copyright (C) 2015 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 .class public LTrivialHelloWorld; 17 .super Ljava/lang/Object; 18 19 .method public constructor <init>()V 20 .registers 1 21 invoke-direct {p0}, Ljava/lang/Object;-><init>()V 22 return-void 23 .end method 24 25 .method public static run()V 26 .registers 8 27 # Trivial 0-arg hello world 28 create-lambda v0, LTrivialHelloWorld;->doHelloWorld(J)V 29 # TODO: create-lambda should not write to both v0 and v1 30 invoke-lambda v0, {} 31 32 # Slightly more interesting 4-arg hello world 33 create-lambda v2, doHelloWorldArgs(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V 34 # TODO: create-lambda should not write to both v2 and v3 35 const-string v4, "A" 36 const-string v5, "B" 37 const-string v6, "C" 38 const-string v7, "D" 39 invoke-lambda v2, {v4, v5, v6, v7} 40 41 invoke-static {}, LTrivialHelloWorld;->testFailures()V 42 43 return-void 44 .end method 45 46 #TODO: should use a closure type instead of jlong. 47 .method public static doHelloWorld(J)V 48 .registers 5 # 1 wide parameters, 3 locals 49 50 const-string v0, "Hello world! (0-args, no closure)" 51 52 sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream; 53 invoke-virtual {v1, v0}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V 54 55 return-void 56 .end method 57 58 #TODO: should use a closure type instead of jlong. 59 .method public static doHelloWorldArgs(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V 60 .registers 9 # 1 wide parameter, 4 narrow parameters, 3 locals 61 62 const-string v0, " Hello world! (4-args, no closure)" 63 sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream; 64 65 invoke-virtual {v1, p2}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V 66 invoke-virtual {v1, p3}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V 67 invoke-virtual {v1, p4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V 68 invoke-virtual {v1, p5}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V 69 70 invoke-virtual {v1, v0}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V 71 72 return-void 73 .end method 74 75 # Test exceptions are thrown as expected when used opcodes incorrectly 76 .method private static testFailures()V 77 .registers 4 # 0 parameters, 4 locals 78 79 const v0, 0 # v0 = null 80 const v1, 0 # v1 = null 81 :start 82 invoke-lambda v0, {} # invoking a null lambda shall raise an NPE 83 :end 84 return-void 85 86 :handler 87 const-string v2, "Caught NPE" 88 sget-object v3, Ljava/lang/System;->out:Ljava/io/PrintStream; 89 invoke-virtual {v3, v2}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V 90 91 return-void 92 93 .catch Ljava/lang/NullPointerException; {:start .. :end} :handler 94 .end method 95