rt/emul/compact/src/main/java/java/lang/System.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 06:13:59 +0200
changeset 1946 bafdddd2a0cf
parent 1711 35157f2e7f4d
permissions -rw-r--r--
System.exit terminates associated launcher
jaroslav@1260
     1
/**
jaroslav@1260
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1260
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1260
     4
 *
jaroslav@1260
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1260
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1260
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1260
     8
 *
jaroslav@1260
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1260
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1260
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1260
    12
 * GNU General Public License for more details.
jaroslav@1260
    13
 *
jaroslav@1260
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1260
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1260
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1260
    17
 */
jaroslav@1260
    18
package java.lang;
jaroslav@1260
    19
jaroslav@1416
    20
import java.io.BufferedOutputStream;
jaroslav@1322
    21
import java.io.ByteArrayInputStream;
jaroslav@1416
    22
import java.io.IOException;
jaroslav@1322
    23
import java.io.InputStream;
jaroslav@1416
    24
import java.io.OutputStream;
jaroslav@1322
    25
import java.io.PrintStream;
jaroslav@1410
    26
import java.util.Properties;
jaroslav@1279
    27
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1279
    28
jaroslav@1260
    29
/** Poor man's re-implementation of most important System methods.
jaroslav@1260
    30
 *
jaroslav@1260
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1260
    32
 */
jaroslav@1260
    33
public class System {
jaroslav@1260
    34
    private System() {
jaroslav@1260
    35
    }
jaroslav@1260
    36
    
jaroslav@1260
    37
    public static void arraycopy(Object value, int srcBegin, Object dst, int dstBegin, int count) {
jaroslav@1260
    38
        org.apidesign.bck2brwsr.emul.lang.System.arraycopy(value, srcBegin, dst, dstBegin, count);
jaroslav@1260
    39
    }
jaroslav@1260
    40
    
jaroslav@1260
    41
    public static long currentTimeMillis() {
jaroslav@1260
    42
        return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
jaroslav@1260
    43
    }
jaroslav@1260
    44
    
jaroslav@1711
    45
    public static long nanoTime() {
jaroslav@1711
    46
        return org.apidesign.bck2brwsr.emul.lang.System.nanoTime();
jaroslav@1711
    47
        
jaroslav@1711
    48
    }
jaroslav@1711
    49
    
jaroslav@1260
    50
    public static int identityHashCode(Object obj) {
jtulach@1483
    51
        return Class.defaultHashCode(obj);
jaroslav@1260
    52
    }
jaroslav@1260
    53
jaroslav@1260
    54
    public static String getProperty(String name) {
jaroslav@1328
    55
        if ("os.name".equals(name)) {
jaroslav@1328
    56
            return userAgent();
jaroslav@1328
    57
        }
jaroslav@1260
    58
        return null;
jaroslav@1260
    59
    }
jaroslav@1260
    60
    
jaroslav@1362
    61
    @JavaScriptBody(args = {}, body = "return (typeof navigator !== 'undefined') ? navigator.userAgent : 'unknown';")
jaroslav@1328
    62
    private static native String userAgent();
jaroslav@1328
    63
    
jaroslav@1260
    64
    public static String getProperty(String key, String def) {
jaroslav@1260
    65
        return def;
jaroslav@1260
    66
    }
jaroslav@1260
    67
    
jaroslav@1410
    68
    public static Properties getProperties() {
jaroslav@1410
    69
        throw new SecurityException();
jaroslav@1410
    70
    }
jaroslav@1410
    71
    
jaroslav@1410
    72
    public static void setProperties(Properties p) {
jaroslav@1410
    73
        throw new SecurityException();
jaroslav@1410
    74
    }
jaroslav@1410
    75
    
jaroslav@1260
    76
    /**
jaroslav@1260
    77
     * Returns the system-dependent line separator string.  It always
jaroslav@1260
    78
     * returns the same value - the initial value of the {@linkplain
jaroslav@1260
    79
     * #getProperty(String) system property} {@code line.separator}.
jaroslav@1260
    80
     *
jaroslav@1260
    81
     * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
jaroslav@1260
    82
     * Windows systems it returns {@code "\r\n"}.
jaroslav@1260
    83
     */
jaroslav@1260
    84
    public static String lineSeparator() {
jaroslav@1260
    85
        return "\n";
jaroslav@1260
    86
    }
jaroslav@1279
    87
jaroslav@1946
    88
    @JavaScriptBody(args = { "exitCode" }, body = ""
jaroslav@1946
    89
        + "var xhttp = new XMLHttpRequest();\n"
jaroslav@1946
    90
        + "xhttp.open('GET', '/?exit=' + exitCode, true);\n"
jaroslav@1946
    91
        + "xhttp.send();\n"
jaroslav@1946
    92
        + "window.close();\n"
jaroslav@1946
    93
    )
jaroslav@1279
    94
    public static void exit(int exitCode) {
jaroslav@1279
    95
    }
jaroslav@1322
    96
    
jaroslav@1322
    97
    public final static InputStream in;
jaroslav@1322
    98
jaroslav@1322
    99
    public final static PrintStream out;
jaroslav@1322
   100
jaroslav@1322
   101
    public final static PrintStream err;
jaroslav@1322
   102
    
jaroslav@1416
   103
    public static void setOut(PrintStream out) {
jaroslav@1416
   104
        throw new SecurityException();
jaroslav@1416
   105
    }
jaroslav@1416
   106
jaroslav@1416
   107
    public static void setIn(InputStream in) {
jaroslav@1416
   108
        throw new SecurityException();
jaroslav@1416
   109
    }
jaroslav@1416
   110
jaroslav@1416
   111
    public static void setErr(PrintStream err) {
jaroslav@1416
   112
        throw new SecurityException();
jaroslav@1416
   113
    }
jaroslav@1416
   114
    
jaroslav@1322
   115
    static {
jaroslav@1322
   116
        in = new ByteArrayInputStream(new byte[0]);
jaroslav@1416
   117
        out = new PrintStream(new BufferedOutputStream(new SystemStream("log")));
jaroslav@1416
   118
        err = new PrintStream(new BufferedOutputStream(new SystemStream("warn")));
jaroslav@1322
   119
    }
jaroslav@1416
   120
jaroslav@1416
   121
    private static final class SystemStream extends OutputStream {
jaroslav@1416
   122
        private final String method;
jaroslav@1416
   123
jaroslav@1416
   124
        public SystemStream(String method) {
jaroslav@1416
   125
            this.method = method;
jaroslav@1416
   126
        }
jaroslav@1416
   127
jaroslav@1416
   128
        @Override
jaroslav@1416
   129
        public void write(byte b[], int off, int len) throws IOException {
jaroslav@1416
   130
            write(method, new String(b, off, len, "UTF-8"));
jaroslav@1416
   131
        }
jaroslav@1416
   132
jaroslav@1416
   133
        @JavaScriptBody(args = { "method", "b" }, body = "if (typeof console !== 'undefined') console[method](b.toString());")
jaroslav@1416
   134
        private static native void write(String method, String b);
jaroslav@1416
   135
jaroslav@1416
   136
        @Override
jaroslav@1416
   137
        public void write(int b) throws IOException {
jaroslav@1416
   138
            write(new byte[] { (byte)b });
jaroslav@1416
   139
        }
jaroslav@1416
   140
    } // end of SystemStream
jaroslav@1260
   141
}