boot/src/test/java/org/netbeans/html/boot/impl/JsMethods.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 15 Jan 2016 13:05:42 +0100
changeset 1043 b189d001b9bd
parent 1041 36165f49f598
child 1051 d0e6c8f97dc3
permissions -rw-r--r--
#257579: Erase the parameter types before computing the signature
jaroslav@123
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@123
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@123
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@123
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@123
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.impl;
jaroslav@123
    44
jtulach@1043
    45
import java.util.Map;
jaroslav@123
    46
import net.java.html.js.JavaScriptBody;
jaroslav@163
    47
import net.java.html.js.JavaScriptResource;
jaroslav@123
    48
jaroslav@123
    49
jaroslav@123
    50
/**
jaroslav@123
    51
 *
jtulach@655
    52
 * @author Jaroslav Tulach
jaroslav@123
    53
 */
jaroslav@163
    54
@JavaScriptResource("jsmethods.js")
jaroslav@123
    55
public class JsMethods {
jtulach@1041
    56
    private java.lang.Object value;
jaroslav@196
    57
    
jaroslav@123
    58
    @JavaScriptBody(args = {}, body = "return 42;")
jtulach@1041
    59
    public static java.lang.Object fortyTwo() {
jaroslav@123
    60
        return -42;
jaroslav@123
    61
    }
jaroslav@123
    62
    
jaroslav@123
    63
    @JavaScriptBody(args = {"x", "y" }, body = "return x + y;")
jaroslav@123
    64
    public static native int plus(int x, int y);
jaroslav@123
    65
    
jaroslav@123
    66
    @JavaScriptBody(args = {"x"}, body = "return x;")
jaroslav@123
    67
    public static native int plus(int x);
jaroslav@123
    68
    
jaroslav@123
    69
    @JavaScriptBody(args = {}, body = "return this;")
jtulach@1041
    70
    public static native java.lang.Object staticThis();
jaroslav@123
    71
    
jaroslav@123
    72
    @JavaScriptBody(args = {}, body = "return this;")
jtulach@1041
    73
    public native java.lang.Object getThis();
jaroslav@123
    74
    @JavaScriptBody(args = {"x"}, body = "return x;")
jaroslav@123
    75
    public native int plusInst(int x);
jaroslav@153
    76
    
jaroslav@153
    77
    @JavaScriptBody(args = {}, body = "return true;")
jaroslav@153
    78
    public static boolean truth() {
jaroslav@153
    79
        return false;
jaroslav@153
    80
    }
jaroslav@160
    81
    
jaroslav@191
    82
    @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()();")
jaroslav@160
    83
    public static native void callback(Runnable r);
jaroslav@161
    84
    
jaroslav@161
    85
    @JavaScriptBody(args = { "at", "arr" }, javacall = true, body =
jaroslav@161
    86
          "var a = 0;\n"
jaroslav@161
    87
        + "for (var i = 0; i < arr.length; i++) {\n"
jaroslav@362
    88
        + "  a = at.@org.netbeans.html.boot.impl.Arithm::sumTwo(II)(a, arr[i]);\n"
jaroslav@161
    89
        + "}\n"
jaroslav@161
    90
        + "return a;"
jaroslav@161
    91
    )
jaroslav@161
    92
    private static native int sumArr(Arithm at, int... arr);
jaroslav@161
    93
    
jaroslav@161
    94
    public static int sumArr(int... arr) {
jaroslav@161
    95
        return sumArr(new Arithm(), arr);
jaroslav@161
    96
    }
jaroslav@163
    97
    
jaroslav@163
    98
    @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);")
jaroslav@163
    99
    public static native int useExternalMul(int x, int y);
jaroslav@171
   100
    
jaroslav@362
   101
    @JavaScriptBody(args = { "m" }, javacall = true, body = "return m.@org.netbeans.html.boot.impl.JsMethods::getThis()();")
jaroslav@171
   102
    public static native JsMethods returnYourSelf(JsMethods m);
jaroslav@191
   103
    
jaroslav@362
   104
    @JavaScriptBody(args = { "x", "y" }, javacall = true, body = "return @org.netbeans.html.boot.impl.JsMethods::useExternalMul(II)(x, y);")
jaroslav@191
   105
    public static native int staticCallback(int x, int y);
jaroslav@191
   106
jaroslav@191
   107
    @JavaScriptBody(args = { "v" }, javacall = true, body = "return @java.lang.Integer::parseInt(Ljava/lang/String;)(v);")
jaroslav@191
   108
    public static native int parseInt(String v);
jaroslav@192
   109
    
jtulach@933
   110
    @JavaScriptBody(args = { "v" }, body = "return v.toString();")
jtulach@933
   111
    public static native String fromEnum(Enm v);
jtulach@933
   112
    
jaroslav@439
   113
    @JavaScriptBody(args = "arr", body = "return arr;")
jtulach@1041
   114
    public static native java.lang.Object[] arr(java.lang.Object[] arr);
jaroslav@439
   115
    
jaroslav@192
   116
    @JavaScriptBody(args = { "useA", "useB", "a", "b" }, body = "var l = 0;"
jaroslav@192
   117
        + "if (useA) l += a;\n"
jaroslav@192
   118
        + "if (useB) l += b;\n"
jaroslav@192
   119
        + "return l;\n"
jaroslav@192
   120
    )
jaroslav@192
   121
    public static native long chooseLong(boolean useA, boolean useB, long a, long b);
jaroslav@196
   122
    
jtulach@1041
   123
    protected void onError(java.lang.Object o) throws Exception {
jaroslav@196
   124
        value = o;
jaroslav@196
   125
    }
jaroslav@196
   126
    
jtulach@1041
   127
    java.lang.Object getError() {
jaroslav@196
   128
        return value;
jaroslav@196
   129
    }
jaroslav@196
   130
    
jaroslav@196
   131
    @JavaScriptBody(args = { "err" }, javacall = true, body = 
jaroslav@362
   132
        "this.@org.netbeans.html.boot.impl.JsMethods::onError(Ljava/lang/Object;)(err);"
jaroslav@362
   133
      + "return this.@org.netbeans.html.boot.impl.JsMethods::getError()();"
jaroslav@196
   134
    )
jtulach@1041
   135
    public native java.lang.Object recordError(java.lang.Object err);
jaroslav@451
   136
    
jaroslav@451
   137
    @JavaScriptBody(args = { "x", "y" }, body = "return x + y;")
jaroslav@451
   138
    public static int plusOrMul(int x, int y) {
jaroslav@451
   139
        return x * y;
jaroslav@451
   140
    }
jtulach@900
   141
    
jtulach@900
   142
    @JavaScriptBody(args = { "x" }, keepAlive = false, body = "throw 'Do not call me!'")
jtulach@1041
   143
    public static native int checkAllowGC(java.lang.Object x);
jtulach@1043
   144
jtulach@1043
   145
    @JavaScriptBody(args = { "map", "value" }, javacall = true, body =
jtulach@1043
   146
       "map.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)('key',value);"
jtulach@1043
   147
    )
jtulach@1043
   148
    public static native void callParamTypes(Map<String,Integer> map, int value);
jtulach@933
   149
    
jtulach@933
   150
    enum Enm {
jtulach@933
   151
        A, B;
jtulach@933
   152
    }
jaroslav@123
   153
}
jtulach@1043
   154