rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotations.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 12 Jul 2013 16:06:38 +0200
changeset 1248 a3eb8b0dfb81
parent 1244 4b43ab1f72e8
permissions -rw-r--r--
Needs to mangle the parameters to convert / to _
jaroslav@1239
     1
/**
jaroslav@1239
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1239
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1239
     4
 *
jaroslav@1239
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1239
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1239
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1239
     8
 *
jaroslav@1239
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1239
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1239
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1239
    12
 * GNU General Public License for more details.
jaroslav@1239
    13
 *
jaroslav@1239
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1239
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1239
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1239
    17
 */
jaroslav@1239
    18
package org.apidesign.vm4brwsr;
jaroslav@1239
    19
jaroslav@1239
    20
import net.java.html.js.JavaScriptBody;
jaroslav@1239
    21
import net.java.html.js.JavaScriptResource;
jaroslav@1239
    22
jaroslav@1239
    23
/**
jaroslav@1239
    24
 *
jaroslav@1239
    25
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1239
    26
 */
jaroslav@1239
    27
@JavaScriptResource("htmlannotations.js")
jaroslav@1239
    28
public class HtmlAnnotations {
jaroslav@1248
    29
    private Object callback;
jaroslav@1248
    30
    
jaroslav@1248
    31
    
jaroslav@1239
    32
    @JavaScriptBody(args = {}, body = "return 42;")
jaroslav@1239
    33
    public static int fourtyTwo() {
jaroslav@1239
    34
        return -1;
jaroslav@1239
    35
    }
jaroslav@1239
    36
    
jaroslav@1239
    37
    @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);")
jaroslav@1239
    38
    public static native int useExternalMul(int x, int y);
jaroslav@1239
    39
    
jaroslav@1240
    40
    public static int callback() {
jaroslav@1240
    41
        final int[] arr = { 0 };
jaroslav@1240
    42
        callback(new Runnable() {
jaroslav@1240
    43
            @Override
jaroslav@1240
    44
            public void run() {
jaroslav@1240
    45
                arr[0]++;
jaroslav@1240
    46
            }
jaroslav@1240
    47
        });
jaroslav@1240
    48
        return arr[0];
jaroslav@1240
    49
    }
jaroslav@1240
    50
    
jaroslav@1240
    51
    @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()()")
jaroslav@1240
    52
    private static native void callback(Runnable r);
jaroslav@1243
    53
jaroslav@1243
    54
    @JavaScriptBody(args = {  }, javacall = true, body = "return @org.apidesign.vm4brwsr.HtmlAnnotations::callback()();")
jaroslav@1244
    55
    public static native int staticCallback();
jaroslav@1244
    56
    
jaroslav@1244
    57
    
jaroslav@1244
    58
    protected long chooseLong(boolean takeFirst, boolean takeSecond, long first, long second) {
jaroslav@1244
    59
        long l = 0;
jaroslav@1244
    60
        if (takeFirst) l += first;
jaroslav@1244
    61
        if (takeSecond) l += second;
jaroslav@1244
    62
        return l;
jaroslav@1244
    63
    }
jaroslav@1244
    64
    
jaroslav@1248
    65
    protected void onError(Object obj) throws Exception {
jaroslav@1248
    66
        callback = obj;
jaroslav@1248
    67
    }
jaroslav@1248
    68
    
jaroslav@1248
    69
    Object getError() {
jaroslav@1248
    70
        return callback;
jaroslav@1248
    71
    }
jaroslav@1248
    72
    
jaroslav@1244
    73
    public static Object create() {
jaroslav@1244
    74
        return new HtmlAnnotations();
jaroslav@1244
    75
    }
jaroslav@1244
    76
    @JavaScriptBody(args = { "impl", "a", "b" }, javacall = true, body = 
jaroslav@1244
    77
        "return impl.@org.apidesign.vm4brwsr.HtmlAnnotations::chooseLong(ZZJJ)(true, false, a, b);"
jaroslav@1244
    78
    )
jaroslav@1244
    79
    public static native long first(Object impl, long a, long b);
jaroslav@1248
    80
    
jaroslav@1248
    81
    @JavaScriptBody(args = { "impl", "d" }, javacall = true, body = 
jaroslav@1248
    82
        "impl.@org.apidesign.vm4brwsr.HtmlAnnotations::onError(Ljava/lang/Object;)(d);" +
jaroslav@1248
    83
        "return impl.@org.apidesign.vm4brwsr.HtmlAnnotations::getError()();"
jaroslav@1248
    84
    )
jaroslav@1248
    85
    public static native Double onError(Object impl, Double d);
jaroslav@1239
    86
}