rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 09 Jan 2014 08:21:26 +0100
branchNbHtml4J
changeset 1421 b8e33a00bfab
parent 835 24096bd2b38a
child 1476 dbfabad86e5c
permissions -rw-r--r--
Copy the array before passing it to JavaScript when using net.java.html.js.JavaScriptBody
jaroslav@560
     1
/**
jaroslav@560
     2
 * Back 2 Browser Bytecode Translator
jaroslav@560
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@560
     4
 *
jaroslav@560
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@560
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@560
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@560
     8
 *
jaroslav@560
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@560
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@560
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@560
    12
 * GNU General Public License for more details.
jaroslav@560
    13
 *
jaroslav@560
    14
 * You should have received a copy of the GNU General Public License
jaroslav@560
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@560
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@560
    17
 */
jaroslav@560
    18
package org.apidesign.bck2brwsr.emul.lang;
jaroslav@560
    19
jaroslav@694
    20
import java.lang.reflect.Method;
jaroslav@568
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1421
    22
import org.apidesign.bck2brwsr.core.JavaScriptOnly;
jaroslav@568
    23
jaroslav@560
    24
/**
jaroslav@560
    25
 *
jaroslav@560
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@560
    27
 */
jaroslav@560
    28
public class System {
jaroslav@560
    29
    private System() {
jaroslav@560
    30
    }
jaroslav@560
    31
jaroslav@568
    32
    @JavaScriptBody(args = { "value", "srcBegin", "dst", "dstBegin", "count" }, body = 
jaroslav@568
    33
        "if (srcBegin < dstBegin) {\n" +
jaroslav@568
    34
        "    while (count-- > 0) {\n" +
jaroslav@568
    35
        "        dst[dstBegin + count] = value[srcBegin + count];\n" +
jaroslav@568
    36
        "    }\n" +
jaroslav@568
    37
        "} else {\n" +
jaroslav@568
    38
        "    while (count-- > 0) {\n" +
jaroslav@568
    39
        "        dst[dstBegin++] = value[srcBegin++];\n" +
jaroslav@568
    40
        "    }\n" +
jaroslav@568
    41
        "}"
jaroslav@568
    42
    )
jaroslav@694
    43
    public static void arraycopy(Object src, int srcBegin, Object dst, int dstBegin, int count) {
jaroslav@694
    44
        try {
jaroslav@694
    45
            Class<?> system = Class.forName("java.lang.System");
jaroslav@694
    46
            Method m = system.getMethod("arraycopy", Object.class, int.class, Object.class, int.class, int.class);
jaroslav@694
    47
            m.invoke(null, src, srcBegin, dst, dstBegin, count);
jaroslav@694
    48
        } catch (Exception ex) {
jaroslav@694
    49
            throw new IllegalStateException(ex);
jaroslav@694
    50
        }
jaroslav@694
    51
    }
jaroslav@595
    52
jaroslav@595
    53
    @JavaScriptBody(args = { "arr", "expectedSize" }, body = 
jaroslav@595
    54
        "while (expectedSize-- > arr.length) { arr.push(0); }; return arr;"
jaroslav@595
    55
    )
jaroslav@595
    56
    public static native byte[] expandArray(byte[] arr, int expectedSize);
jaroslav@599
    57
jaroslav@835
    58
    @JavaScriptBody(args = { "arr", "expectedSize" }, body = 
jaroslav@835
    59
        "while (expectedSize-- > arr.length) { arr.push(0); }; return arr;"
jaroslav@835
    60
    )
jaroslav@835
    61
    public static native char[] expandArray(char[] arr, int expectedSize);
jaroslav@835
    62
jaroslav@704
    63
    @JavaScriptBody(args = {}, body = "return new Date().getTime();")
lubomir@755
    64
    private static native double currentTimeMillisDouble();
lubomir@755
    65
lubomir@755
    66
    public static long currentTimeMillis() {
lubomir@755
    67
        return (long) currentTimeMillisDouble();
lubomir@755
    68
    }
lubomir@755
    69
jaroslav@636
    70
    public static long nanoTime() {
jaroslav@668
    71
        return 1000000L * currentTimeMillis();
jaroslav@636
    72
    }
jaroslav@604
    73
    @JavaScriptBody(args = { "obj" }, body="return vm.java_lang_Object(false).hashCode__I.call(obj);")
jaroslav@604
    74
    public static native int identityHashCode(Object obj);
jaroslav@1421
    75
    
jaroslav@1421
    76
    @JavaScriptOnly(name = "toJS", value = "function(v) {\n" + 
jaroslav@1421
    77
        "  if (v === null) return null;\n" +
jaroslav@1421
    78
        "  if (Object.prototype.toString.call(v) === '[object Array]') {\n" +
jaroslav@1421
    79
        "    return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n" +
jaroslav@1421
    80
        "  }\n" +
jaroslav@1421
    81
        "  return v.valueOf();\n" +
jaroslav@1421
    82
        "}\n"
jaroslav@1421
    83
    )
jaroslav@1421
    84
    public static native int toJS();
jaroslav@1421
    85
    
jaroslav@1421
    86
    private static Object convArray(Object o) {
jaroslav@1421
    87
        if (o instanceof Object[]) {
jaroslav@1421
    88
            Object[] arr = (Object[]) o;
jaroslav@1421
    89
            final int l = arr.length;
jaroslav@1421
    90
            Object[] ret = new Object[l];
jaroslav@1421
    91
            for (int i = 0; i < l; i++) {
jaroslav@1421
    92
                ret[i] = convArray(arr[i]);
jaroslav@1421
    93
            }
jaroslav@1421
    94
            return ret;
jaroslav@1421
    95
        }
jaroslav@1421
    96
        return o;
jaroslav@1421
    97
    }
jaroslav@560
    98
}