boot/src/test/java/org/netbeans/html/boot/impl/JsMethods.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 boot/src/test/java/org/apidesign/html/boot/impl/JsMethods.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@123
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@123
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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
jaroslav@123
    45
import net.java.html.js.JavaScriptBody;
jaroslav@163
    46
import net.java.html.js.JavaScriptResource;
jaroslav@123
    47
jaroslav@123
    48
jaroslav@123
    49
/**
jaroslav@123
    50
 *
jaroslav@123
    51
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@123
    52
 */
jaroslav@163
    53
@JavaScriptResource("jsmethods.js")
jaroslav@123
    54
public class JsMethods {
jaroslav@196
    55
    private Object value;
jaroslav@196
    56
    
jaroslav@123
    57
    @JavaScriptBody(args = {}, body = "return 42;")
jaroslav@123
    58
    public static Object fortyTwo() {
jaroslav@123
    59
        return -42;
jaroslav@123
    60
    }
jaroslav@123
    61
    
jaroslav@123
    62
    @JavaScriptBody(args = {"x", "y" }, body = "return x + y;")
jaroslav@123
    63
    public static native int plus(int x, int y);
jaroslav@123
    64
    
jaroslav@123
    65
    @JavaScriptBody(args = {"x"}, body = "return x;")
jaroslav@123
    66
    public static native int plus(int x);
jaroslav@123
    67
    
jaroslav@123
    68
    @JavaScriptBody(args = {}, body = "return this;")
jaroslav@123
    69
    public static native Object staticThis();
jaroslav@123
    70
    
jaroslav@123
    71
    @JavaScriptBody(args = {}, body = "return this;")
jaroslav@123
    72
    public native Object getThis();
jaroslav@123
    73
    @JavaScriptBody(args = {"x"}, body = "return x;")
jaroslav@123
    74
    public native int plusInst(int x);
jaroslav@153
    75
    
jaroslav@153
    76
    @JavaScriptBody(args = {}, body = "return true;")
jaroslav@153
    77
    public static boolean truth() {
jaroslav@153
    78
        return false;
jaroslav@153
    79
    }
jaroslav@160
    80
    
jaroslav@191
    81
    @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()();")
jaroslav@160
    82
    public static native void callback(Runnable r);
jaroslav@161
    83
    
jaroslav@161
    84
    @JavaScriptBody(args = { "at", "arr" }, javacall = true, body =
jaroslav@161
    85
          "var a = 0;\n"
jaroslav@161
    86
        + "for (var i = 0; i < arr.length; i++) {\n"
jaroslav@362
    87
        + "  a = at.@org.netbeans.html.boot.impl.Arithm::sumTwo(II)(a, arr[i]);\n"
jaroslav@161
    88
        + "}\n"
jaroslav@161
    89
        + "return a;"
jaroslav@161
    90
    )
jaroslav@161
    91
    private static native int sumArr(Arithm at, int... arr);
jaroslav@161
    92
    
jaroslav@161
    93
    public static int sumArr(int... arr) {
jaroslav@161
    94
        return sumArr(new Arithm(), arr);
jaroslav@161
    95
    }
jaroslav@163
    96
    
jaroslav@163
    97
    @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);")
jaroslav@163
    98
    public static native int useExternalMul(int x, int y);
jaroslav@171
    99
    
jaroslav@362
   100
    @JavaScriptBody(args = { "m" }, javacall = true, body = "return m.@org.netbeans.html.boot.impl.JsMethods::getThis()();")
jaroslav@171
   101
    public static native JsMethods returnYourSelf(JsMethods m);
jaroslav@191
   102
    
jaroslav@362
   103
    @JavaScriptBody(args = { "x", "y" }, javacall = true, body = "return @org.netbeans.html.boot.impl.JsMethods::useExternalMul(II)(x, y);")
jaroslav@191
   104
    public static native int staticCallback(int x, int y);
jaroslav@191
   105
jaroslav@191
   106
    @JavaScriptBody(args = { "v" }, javacall = true, body = "return @java.lang.Integer::parseInt(Ljava/lang/String;)(v);")
jaroslav@191
   107
    public static native int parseInt(String v);
jaroslav@192
   108
    
jaroslav@192
   109
    @JavaScriptBody(args = { "useA", "useB", "a", "b" }, body = "var l = 0;"
jaroslav@192
   110
        + "if (useA) l += a;\n"
jaroslav@192
   111
        + "if (useB) l += b;\n"
jaroslav@192
   112
        + "return l;\n"
jaroslav@192
   113
    )
jaroslav@192
   114
    public static native long chooseLong(boolean useA, boolean useB, long a, long b);
jaroslav@196
   115
    
jaroslav@196
   116
    protected void onError(Object o) throws Exception {
jaroslav@196
   117
        value = o;
jaroslav@196
   118
    }
jaroslav@196
   119
    
jaroslav@196
   120
    Object getError() {
jaroslav@196
   121
        return value;
jaroslav@196
   122
    }
jaroslav@196
   123
    
jaroslav@196
   124
    @JavaScriptBody(args = { "err" }, javacall = true, body = 
jaroslav@362
   125
        "this.@org.netbeans.html.boot.impl.JsMethods::onError(Ljava/lang/Object;)(err);"
jaroslav@362
   126
      + "return this.@org.netbeans.html.boot.impl.JsMethods::getError()();"
jaroslav@196
   127
    )
jaroslav@196
   128
    public native Object recordError(Object err);
jaroslav@123
   129
}