rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotations.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1923 4185cdeeee7e
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vmtest.impl;
    19 
    20 import java.util.concurrent.Callable;
    21 import net.java.html.js.JavaScriptBody;
    22 import net.java.html.js.JavaScriptResource;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 @JavaScriptResource("htmlannotations.js")
    29 public class HtmlAnnotations {
    30     private Object callback;
    31     
    32     
    33     @JavaScriptBody(args = {}, body = "return 42;")
    34     public static int fourtyTwo() {
    35         return -1;
    36     }
    37     
    38     @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);")
    39     public static native int useExternalMul(int x, int y);
    40     
    41     public static int callback() {
    42         final int[] arr = { 0 };
    43         callback(new Runnable() {
    44             @Override
    45             public void run() {
    46                 arr[0]++;
    47             }
    48         });
    49         return arr[0];
    50     }
    51     
    52     @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()()")
    53     private static native void callback(Runnable r);
    54 
    55     @JavaScriptBody(args = {  }, javacall = true, body = "return @org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::callback()();")
    56     public static native int staticCallback();
    57 
    58     @JavaScriptBody(args = {  }, wait4js = false, body = "/* do nothing */")
    59     public static native void empty();
    60     
    61     
    62     protected long chooseLong(boolean takeFirst, boolean takeSecond, long first, long second) {
    63         long l = 0;
    64         if (takeFirst) l += first;
    65         if (takeSecond) l += second;
    66         return l;
    67     }
    68     
    69     protected void onError(Object obj) throws Exception {
    70         callback = obj;
    71     }
    72     
    73     Object getError() {
    74         return callback;
    75     }
    76     
    77     public static Object create() {
    78         return new HtmlAnnotations();
    79     }
    80     @JavaScriptBody(args = { "impl", "a", "b" }, javacall = true, body = 
    81         "return impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::chooseLong(ZZJJ)(true, false, a, b);"
    82     )
    83     public static native long first(Object impl, long a, long b);
    84     
    85     @JavaScriptBody(args = { "impl", "d" }, javacall = true, body = 
    86         "impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::onError(Ljava/lang/Object;)(d);" +
    87         "return impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::getError()();"
    88     )
    89     public static native Double onError(Object impl, Double d);
    90 
    91     @JavaScriptBody(args = {  }, body = "return new Date(2016, 4, 1);")
    92     public static native Object april2016();
    93 
    94     @JavaScriptBody(args = { "date" }, body = "return date.getFullYear()")
    95     public static native int year(Object date);
    96 
    97     @JavaScriptBody(args = { "call" }, javacall = true, body = ""
    98         + "var b = call.@java.util.concurrent.Callable::call()();\n"
    99         + "return b ? 'yes' : 'no';\n"
   100     )
   101     public static native String yesNo(Callable<Boolean> call);
   102 
   103     @JavaScriptBody(args = { "arr", "val" }, body =
   104         "return arr[0] === val;"
   105     )
   106     public static native boolean compareArr(Object[] arr, Object val);
   107 }