json-tck/src/main/java/net/java/html/js/tests/Bodies.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 12 Dec 2014 11:22:40 +0100
branchgc
changeset 900 2ee22312e414
parent 872 1b8f1aed344a
child 909 4abb0f37d33e
permissions -rw-r--r--
Giving API users better control over GC aspects of their objects
jaroslav@424
     1
/**
jaroslav@424
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@424
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@424
     5
 *
jaroslav@424
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@424
     7
 * Other names may be trademarks of their respective owners.
jaroslav@424
     8
 *
jaroslav@424
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@424
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@424
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@424
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@424
    13
 * License. You can obtain a copy of the License at
jaroslav@424
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@424
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@424
    16
 * specific language governing permissions and limitations under the
jaroslav@424
    17
 * License.  When distributing the software, include this License Header
jaroslav@424
    18
 * Notice in each file and include the License file at
jaroslav@424
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@424
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@424
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@424
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@424
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@424
    24
 * your own identifying information:
jaroslav@424
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@424
    26
 *
jaroslav@424
    27
 * Contributor(s):
jaroslav@424
    28
 *
jaroslav@424
    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@424
    31
 *
jaroslav@424
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@424
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@424
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@424
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@424
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@424
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@424
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@424
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@424
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@424
    41
 * made subject to such option by the copyright holder.
jaroslav@424
    42
 */
jaroslav@424
    43
package net.java.html.js.tests;
jaroslav@424
    44
jaroslav@424
    45
import java.util.concurrent.Callable;
jaroslav@424
    46
import net.java.html.js.JavaScriptBody;
jaroslav@424
    47
jaroslav@424
    48
/**
jaroslav@424
    49
 *
jtulach@790
    50
 * @author Jaroslav Tulach
jaroslav@424
    51
 */
jaroslav@424
    52
final class Bodies {
jaroslav@424
    53
    @JavaScriptBody(args = { "a", "b" }, body = "return a + b;")
jaroslav@424
    54
    public static native int sum(int a, int b);
jtulach@848
    55
jtulach@848
    56
    @JavaScriptBody(args = { "a", "b" }, javacall = true, body = 
jtulach@848
    57
        "return @net.java.html.js.tests.Bodies::sum(II)(a, b);"
jtulach@848
    58
    )
jtulach@848
    59
    public static native int sumJS(int a, int b);
jaroslav@424
    60
    
jaroslav@424
    61
    @JavaScriptBody(args = {"r"}, javacall = true, body = "r.@java.lang.Runnable::run()();")
jaroslav@424
    62
    static native void callback(Runnable r);
jaroslav@424
    63
jtulach@900
    64
    @JavaScriptBody(args = {"r"}, wait4js = false, keepAlive = false, javacall = true, body = "r.@java.lang.Runnable::run()();")
jaroslav@577
    65
    static native void asyncCallback(Runnable r);
jtulach@848
    66
    
jaroslav@424
    67
    @JavaScriptBody(args = {"c"}, javacall = true, body = "return c.@java.util.concurrent.Callable::call()();")
jaroslav@430
    68
    static native Object callback(Callable<? extends Object> c);
jaroslav@430
    69
jaroslav@430
    70
    @JavaScriptBody(args = {"c", "v"}, javacall = true, body = "var arr = c.@java.util.concurrent.Callable::call()(); arr.push(v); return arr;")
jaroslav@430
    71
    static native Object callbackAndPush(Callable<String[]> c, String v);
jaroslav@424
    72
    
jaroslav@424
    73
    @JavaScriptBody(args = { "v" }, body = "return v;")
jaroslav@424
    74
    public static native Object id(Object v);
jaroslav@424
    75
    
jaroslav@424
    76
    @JavaScriptBody(args = { "v" }, body = "return { 'x' : v };")
jaroslav@424
    77
    public static native Object instance(int v);
jaroslav@424
    78
    
jaroslav@424
    79
    @JavaScriptBody(args = "o", body = "o.x++;")
jaroslav@424
    80
    public static native void incrementX(Object o);
jaroslav@424
    81
jtulach@848
    82
    @JavaScriptBody(args = "o", wait4js = true, body = "o.x++;")
jtulach@848
    83
    static native void incrementXAsync(Object o);
jtulach@848
    84
jaroslav@424
    85
    @JavaScriptBody(args = "o", body = "return o.x;")
jtulach@872
    86
    public static native int readIntX(Object o);
jtulach@872
    87
    
jtulach@872
    88
    @JavaScriptBody(args = "o", body = "return o.x;")
jtulach@872
    89
    public static native Object readX(Object o);
jtulach@872
    90
    
jtulach@900
    91
    @JavaScriptBody(args = { "o", "x" }, keepAlive = false, body = "o.x = x;")
jtulach@872
    92
    public static native Object setX(Object o, Object x);
jaroslav@424
    93
jtulach@900
    94
    @JavaScriptBody(args = { "c" }, keepAlive = false, javacall = true, body = 
jaroslav@424
    95
        "return c.@net.java.html.js.tests.Sum::sum(II)(40, 2);"
jaroslav@424
    96
    )
jaroslav@424
    97
    public static native int sumIndirect(Sum c);
jaroslav@424
    98
    
jaroslav@424
    99
    @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
jaroslav@424
   100
    public static native Object select(Object[] arr, int index);
jaroslav@424
   101
jaroslav@424
   102
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@424
   103
    public static native int length(Object[] arr);
jaroslav@429
   104
    
jaroslav@436
   105
    @JavaScriptBody(args = { "o", "vo" }, body = "if (vo) o = o.valueOf(); return typeof o;")
jaroslav@436
   106
    public static native String typeof(Object o, boolean useValueOf);
jaroslav@436
   107
jaroslav@436
   108
    @JavaScriptBody(args = { "b" }, body = "return typeof b;")
jaroslav@436
   109
    public static native String typeof(boolean b);
jaroslav@424
   110
jaroslav@429
   111
    @JavaScriptBody(args = { "o" }, body = "return Array.isArray(o);")
jaroslav@429
   112
    public static native boolean isArray(Object o);
jaroslav@429
   113
jaroslav@429
   114
    @JavaScriptBody(args = { "arr", "i", "value" }, body = "arr[i] = value; return arr[i];")
jaroslav@429
   115
    public static native String modify(String[] arr, int i, String value);
jaroslav@424
   116
    
jaroslav@424
   117
    @JavaScriptBody(args = {}, body = "return true;")
jaroslav@424
   118
    public static native boolean truth();
jaroslav@446
   119
    
jaroslav@446
   120
    @JavaScriptBody(args = { "s" }, javacall = true, body = 
jaroslav@446
   121
        "return s.@net.java.html.js.tests.Sum::sum([Ljava/lang/Object;)([1, 2, 3]);"
jaroslav@446
   122
    )
jaroslav@446
   123
    public static native int sumArr(Sum s);
jaroslav@579
   124
    
jaroslav@579
   125
    @JavaScriptBody(args = {}, javacall = true, body = 
jaroslav@579
   126
        "return @net.java.html.js.tests.Bodies::fourtyTwo()();"
jaroslav@579
   127
    )
jaroslav@579
   128
    public static native int staticCallback();
jaroslav@579
   129
    
jaroslav@579
   130
    static int fourtyTwo() {
jaroslav@579
   131
        return 42;
jaroslav@579
   132
    }
jtulach@710
   133
    
jtulach@710
   134
    @JavaScriptBody(args = { "arr" }, body = 
jtulach@710
   135
        "var sum = 0;\n" +
jtulach@710
   136
        "for (var i = 0; i < arr.length; i++) {\n" +
jtulach@710
   137
        "  sum += arr[i];\n" +
jtulach@710
   138
        "}\n" +
jtulach@710
   139
        "return sum;\n"
jtulach@710
   140
    )
jtulach@710
   141
    public static native double sumVector(double[] arr);
jtulach@710
   142
    
jtulach@710
   143
    @JavaScriptBody(args = { "arr" }, body = 
jtulach@710
   144
        "var sum = 0;\n" +
jtulach@710
   145
        "for (var i = 0; i < arr.length; i++) {\n" +
jtulach@710
   146
        "  for (var j = 0; j < arr[i].length; j++) {\n" +
jtulach@710
   147
        "    sum += arr[i][j];\n" +
jtulach@710
   148
        "  }\n" +
jtulach@710
   149
        "}\n" +
jtulach@710
   150
        "return sum;\n"
jtulach@710
   151
    )
jtulach@710
   152
    public static native double sumMatrix(double[][] arr);
jtulach@848
   153
jtulach@848
   154
    static void incCounter(int howMuch, final Object js) {
jtulach@848
   155
        for (int i = 0; i < howMuch; i++) {
jtulach@848
   156
            asyncCallback(new Runnable() {
jtulach@848
   157
                @Override
jtulach@848
   158
                public void run() {
jtulach@848
   159
                    incrementXAsync(js);
jtulach@848
   160
                }
jtulach@848
   161
            });
jtulach@848
   162
        }
jtulach@848
   163
    }
jtulach@848
   164
    
jtulach@848
   165
    @JavaScriptBody(args = {}, javacall = true, body = 
jtulach@848
   166
        "var v = { x : 0 };\n" +
jtulach@848
   167
        "@net.java.html.js.tests.Bodies::incCounter(ILjava/lang/Object;)(42, v);\n" +
jtulach@848
   168
        "return v.x;\n"
jtulach@848
   169
    )
jtulach@848
   170
    static native int incAsync();
jtulach@859
   171
    
jaroslav@857
   172
    @JavaScriptBody(args = { "arr" }, body = 
jaroslav@857
   173
        "var ret = [];\n" +
jaroslav@857
   174
        "for (var i in arr) {\n" +
jaroslav@857
   175
        "  ret.push(arr[i]);\n" +
jaroslav@857
   176
        "}\n" +
jaroslav@857
   177
        "return ret;\n"
jaroslav@857
   178
    )
jaroslav@857
   179
    static native Object[] forIn(Object[] in);
jaroslav@860
   180
jtulach@867
   181
    @JavaScriptBody(args = { "max" }, body = 
jtulach@867
   182
        "var arr = [];\n"
jtulach@867
   183
      + "for (var i = 0; i < max; i++) {\n"
jtulach@867
   184
      + "  arr.push(i);\n"
jtulach@867
   185
      + "}\n"
jtulach@867
   186
      + "return arr.length;"
jtulach@867
   187
    )
jtulach@867
   188
    static native int gc(double max);
jtulach@867
   189
jtulach@859
   190
    @JavaScriptBody(args = {}, javacall = true, body = 
jtulach@859
   191
        "return @net.java.html.js.tests.Bodies::problematicString()();"
jtulach@859
   192
    )
jtulach@859
   193
    public static native String problematicCallback();
jtulach@859
   194
    
jtulach@859
   195
    static String problematicString() {
jtulach@859
   196
        return "{\n" +
jtulach@859
   197
"    MyViewModel: {\n" +
jtulach@859
   198
"//      ViewModel: JavaViewModel,\n" +
jtulach@859
   199
"\n" +
jtulach@859
   200
"    }          \n" +
jtulach@859
   201
"}";
jtulach@859
   202
    }
jaroslav@424
   203
}