rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/InvokeDynamic.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Aug 2014 10:37:23 +0200
branchjdk8
changeset 1658 3e5087bd13c1
parent 1654 da24a2411ee7
child 1659 d279ddd06652
permissions -rw-r--r--
Managed to call the bootstrap method. Just with wrong arguments.
jaroslav@1648
     1
/**
jaroslav@1648
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1648
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1648
     4
 *
jaroslav@1648
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1648
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1648
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1648
     8
 *
jaroslav@1648
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1648
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1648
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1648
    12
 * GNU General Public License for more details.
jaroslav@1648
    13
 *
jaroslav@1648
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1648
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1648
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1648
    17
 */
jaroslav@1654
    18
package org.apidesign.bck2brwsr.vm8;
jaroslav@1648
    19
jaroslav@1648
    20
import java.lang.invoke.CallSite;
jaroslav@1648
    21
import java.lang.invoke.ConstantCallSite;
jaroslav@1648
    22
import java.lang.invoke.MethodHandles;
jaroslav@1648
    23
import java.lang.invoke.MethodType;
jaroslav@1658
    24
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1648
    25
jaroslav@1648
    26
public class InvokeDynamic {
jaroslav@1648
    27
jaroslav@1648
    28
    public static String dynamicSay() {
jaroslav@1648
    29
        return TEST_dynamic_boot1(new InvokeDynamic());
jaroslav@1648
    30
    }
jaroslav@1648
    31
jaroslav@1648
    32
    private static String TEST_dynamic_boot1(InvokeDynamic instance) {
jaroslav@1648
    33
        throw new IllegalStateException("Can't touch this");
jaroslav@1648
    34
    }
jaroslav@1648
    35
jaroslav@1648
    36
    public static CallSite boot1(MethodHandles.Lookup lookup, String name, MethodType type) {
jaroslav@1658
    37
        assertReal("1st parameter lookup", lookup);
jaroslav@1658
    38
        assertReal("2nd parameter name", name);
jaroslav@1658
    39
        assertReal("3rd parameter type", type);
jaroslav@1648
    40
        try {
jaroslav@1648
    41
            return new ConstantCallSite(lookup.findVirtual(InvokeDynamic.class, "instance_sayHello", MethodType.methodType(String.class)));
jaroslav@1648
    42
        } catch (NoSuchMethodException | IllegalAccessException e) {
jaroslav@1648
    43
            throw new IllegalStateException(e);
jaroslav@1648
    44
        }
jaroslav@1648
    45
    }
jaroslav@1648
    46
jaroslav@1658
    47
    @JavaScriptBody(args = { "msg", "value" }, body = 
jaroslav@1658
    48
        "if (!value) throw msg + ' value: ' + value;"
jaroslav@1658
    49
    )
jaroslav@1658
    50
    private static void assertReal(String msg, Object value) {
jaroslav@1658
    51
        assert value != null : msg;
jaroslav@1658
    52
        System.err.println(msg + " value: " + value);
jaroslav@1658
    53
    }
jaroslav@1658
    54
    
jaroslav@1648
    55
    public String instance_sayHello() {
jaroslav@1648
    56
        return "Hello from Dynamic!";
jaroslav@1648
    57
    }
jaroslav@1648
    58
}