Home | History | Annotate | Download | only in smali
      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 LCaptureVariables;
     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     # Test boolean capture
     28     const v2, 1           # v2 = true
     29     capture-variable v2, "Z"
     30     create-lambda v0, LCaptureVariables;->printCapturedVariable_Z(J)V
     31     # TODO: create-lambda should not write to both v0 and v1
     32     invoke-lambda v0, {}
     33 
     34     # Test byte capture
     35     const v2, 82       # v2 = 82, 'R'
     36     capture-variable v2, "B"
     37     create-lambda v0, LCaptureVariables;->printCapturedVariable_B(J)V
     38     # TODO: create-lambda should not write to both v0 and v1
     39     invoke-lambda v0, {}
     40 
     41     # Test char capture
     42     const v2, 0x2202       # v2 = 0x2202, ''
     43     capture-variable v2, "C"
     44     create-lambda v0, LCaptureVariables;->printCapturedVariable_C(J)V
     45     # TODO: create-lambda should not write to both v0 and v1
     46     invoke-lambda v0, {}
     47 
     48     # Test short capture
     49     const v2, 1000 # v2 = 1000
     50     capture-variable v2, "S"
     51     create-lambda v0, LCaptureVariables;->printCapturedVariable_S(J)V
     52     # TODO: create-lambda should not write to both v0 and v1
     53     invoke-lambda v0, {}
     54 
     55     # Test int capture
     56     const v2, 12345678
     57     capture-variable v2, "I"
     58     create-lambda v0, LCaptureVariables;->printCapturedVariable_I(J)V
     59     # TODO: create-lambda should not write to both v0 and v1
     60     invoke-lambda v0, {}
     61 
     62     # Test long capture
     63     const-wide v2, 0x0badf00dc0ffeeL # v2 = 3287471278325742
     64     capture-variable v2, "J"
     65     create-lambda v0, LCaptureVariables;->printCapturedVariable_J(J)V
     66     # TODO: create-lambda should not write to both v0 and v1
     67     invoke-lambda v0, {}
     68 
     69     # Test float capture
     70     const v2, infinityf
     71     capture-variable v2, "F"
     72     create-lambda v0, LCaptureVariables;->printCapturedVariable_F(J)V
     73     # TODO: create-lambda should not write to both v0 and v1
     74     invoke-lambda v0, {}
     75 
     76     # Test double capture
     77     const-wide v2, -infinity
     78     capture-variable v2, "D"
     79     create-lambda v0, LCaptureVariables;->printCapturedVariable_D(J)V
     80     # TODO: create-lambda should not write to both v0 and v1
     81     invoke-lambda v0, {}
     82 
     83     #TODO: capture objects and lambdas once we have support for it
     84 
     85     # Test capturing multiple variables
     86     invoke-static {}, LCaptureVariables;->testMultipleCaptures()V
     87 
     88     # Test failures
     89     invoke-static {}, LCaptureVariables;->testFailures()V
     90 
     91     return-void
     92 .end method
     93 
     94 #TODO: should use a closure type instead of a long
     95 .method public static printCapturedVariable_Z(J)V
     96     .registers 5 # 1 wide parameter, 3 locals
     97 
     98     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'Z'): value is "
     99 
    100     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    101     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    102 
    103     liberate-variable v2, p0, "Z"
    104     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(Z)V
    105 
    106     return-void
    107 .end method
    108 
    109 #TODO: should use a closure type instead of a long
    110 .method public static printCapturedVariable_B(J)V
    111     .registers 5 # 1 wide parameter, 3 locals
    112 
    113     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'B'): value is "
    114 
    115     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    116     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    117 
    118     liberate-variable v2, p0, "B"
    119     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(C)V  # no println(B), use char instead.
    120 
    121     return-void
    122 .end method
    123 
    124 #TODO: should use a closure type instead of a long
    125 .method public static printCapturedVariable_C(J)V
    126     .registers 5 # 1 wide parameter, 3 locals
    127 
    128     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'C'): value is "
    129 
    130     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    131     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    132 
    133     liberate-variable v2, p0, "C"
    134     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(C)V
    135 
    136     return-void
    137 .end method
    138 
    139 #TODO: should use a closure type instead of a long
    140 .method public static printCapturedVariable_S(J)V
    141     .registers 5 # 1 wide parameter, 3 locals
    142 
    143     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'S'): value is "
    144 
    145     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    146     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    147 
    148     liberate-variable v2, p0, "S"
    149     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(I)V  # no println(S), use int instead
    150 
    151     return-void
    152 .end method
    153 
    154 #TODO: should use a closure type instead of a long
    155 .method public static printCapturedVariable_I(J)V
    156     .registers 5 # 1 wide parameter, 3 locals
    157 
    158     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'I'): value is "
    159 
    160     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    161     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    162 
    163     liberate-variable v2, p0, "I"
    164     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(I)V
    165 
    166     return-void
    167 .end method
    168 
    169 #TODO: should use a closure type instead of a long
    170 .method public static printCapturedVariable_J(J)V
    171     .registers 6 # 1 wide parameter, 4 locals
    172 
    173     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'J'): value is "
    174 
    175     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    176     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    177 
    178     liberate-variable v2, p0, "J"
    179     invoke-virtual {v1, v2, v3}, Ljava/io/PrintStream;->println(J)V
    180 
    181     return-void
    182 .end method
    183 
    184 #TODO: should use a closure type instead of a long
    185 .method public static printCapturedVariable_F(J)V
    186     .registers 5 # 1 parameter, 4 locals
    187 
    188     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'F'): value is "
    189 
    190     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    191     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    192 
    193     liberate-variable v2, p0, "F"
    194     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(F)V
    195 
    196     return-void
    197 .end method
    198 
    199 #TODO: should use a closure type instead of a long
    200 .method public static printCapturedVariable_D(J)V
    201     .registers 6 # 1 wide parameter, 4 locals
    202 
    203     const-string v0, "(CaptureVariables) (0-args, 1 captured variable 'D'): value is "
    204 
    205     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    206     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    207 
    208     liberate-variable v2, p0, "D"
    209     invoke-virtual {v1, v2, v3}, Ljava/io/PrintStream;->println(D)V
    210 
    211     return-void
    212 .end method
    213 
    214 # Test capturing more than one variable.
    215 .method private static testMultipleCaptures()V
    216     .registers 4 # 0 parameters, 4 locals
    217 
    218     const v2, 1           # v2 = true
    219     capture-variable v2, "Z"
    220 
    221     const v2, 82       # v2 = 82, 'R'
    222     capture-variable v2, "B"
    223 
    224     const v2, 0x2202       # v2 = 0x2202, ''
    225     capture-variable v2, "C"
    226 
    227     const v2, 1000 # v2 = 1000
    228     capture-variable v2, "S"
    229 
    230     const v2, 12345678
    231     capture-variable v2, "I"
    232 
    233     const-wide v2, 0x0badf00dc0ffeeL # v2 = 3287471278325742
    234     capture-variable v2, "J"
    235 
    236     const v2, infinityf
    237     capture-variable v2, "F"
    238 
    239     const-wide v2, -infinity
    240     capture-variable v2, "D"
    241 
    242     create-lambda v0, LCaptureVariables;->printCapturedVariable_ZBCSIJFD(J)V
    243     # TODO: create-lambda should not write to both v0 and v1
    244     invoke-lambda v0, {}
    245 
    246 .end method
    247 
    248 #TODO: should use a closure type instead of a long
    249 .method public static printCapturedVariable_ZBCSIJFD(J)V
    250     .registers 7 # 1 wide parameter, 5 locals
    251 
    252     const-string v0, "(CaptureVariables) (0-args, 8 captured variable 'ZBCSIJFD'): value is "
    253     const-string v4, ","
    254 
    255     sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
    256     invoke-virtual {v1, v0}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    257 
    258     liberate-variable v2, p0, "Z"
    259     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(Z)V
    260     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    261 
    262     liberate-variable v2, p0, "B"
    263     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(C)V
    264     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    265 
    266     liberate-variable v2, p0, "C"
    267     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(C)V
    268     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    269 
    270     liberate-variable v2, p0, "S"
    271     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(I)V
    272     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    273 
    274     liberate-variable v2, p0, "I"
    275     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(I)V
    276     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    277 
    278     liberate-variable v2, p0, "J"
    279     invoke-virtual {v1, v2, v3}, Ljava/io/PrintStream;->print(J)V
    280     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    281 
    282     liberate-variable v2, p0, "F"
    283     invoke-virtual {v1, v2}, Ljava/io/PrintStream;->print(F)V
    284     invoke-virtual {v1, v4}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    285 
    286     liberate-variable v2, p0, "D"
    287     invoke-virtual {v1, v2, v3}, Ljava/io/PrintStream;->println(D)V
    288 
    289     return-void
    290 .end method
    291 
    292 # Test exceptions are thrown as expected when used opcodes incorrectly
    293 .method private static testFailures()V
    294     .registers 4 # 0 parameters, 4 locals
    295 
    296     const v0, 0  # v0 = null
    297     const v1, 0  # v1 = null
    298 :start
    299     liberate-variable v0, v2, "Z" # invoking a null lambda shall raise an NPE
    300 :end
    301     return-void
    302 
    303 :handler
    304     const-string v2, "(CaptureVariables) Caught NPE"
    305     sget-object v3, Ljava/lang/System;->out:Ljava/io/PrintStream;
    306     invoke-virtual {v3, v2}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
    307 
    308     return-void
    309 
    310     .catch Ljava/lang/NullPointerException; {:start .. :end} :handler
    311 .end method
    312