2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import net.java.html.js.JavaScriptBody;
21 import net.java.html.lib.Date;
22 import static net.java.html.lib.Exports.NaN;
23 import static net.java.html.lib.Exports.eval;
24 import static net.java.html.lib.Exports.isNaN;
25 import static net.java.html.lib.Exports.parseInt;
26 import net.java.html.lib.Objs;
29 public static int fourtyTwo() {
30 Number n = (Number) eval("6 * 7");
34 public static String hiProperty(String set) {
35 final Object obj = eval("var x = {}; x.x = 'Hi'; x");
37 throw new IllegalStateException("Some Value returned " + obj);
39 if (!(obj instanceof Objs)) {
40 throw new IllegalStateException("The result is Objs: " + obj.getClass());
46 return (String) js.$get("x");
49 public static double parse(String str) throws Exception {
53 public static double parse(String str, int radix) throws Exception {
54 return parseInt(str, radix);
57 public boolean checkNaN() throws Exception {
62 public double dateOperation() throws Exception {
63 Date eleven = new Date(2016, 1, 10);
64 return eleven.getFullYear() + eleven.getMonth();
67 public static double arrayForEach() throws Exception {
68 net.java.html.lib.Array<java.lang.Number> arr = new net.java.html.lib.Array<>();
69 assertNumber(arr.length.get(), 0.0, "Empty at first");
70 arr.push(1.1, 2.2, 3);
71 assertNumber(arr.$get(0), 1.1, "1.1 is on position 0");
72 assertNumber(arr.$get(1), 2.2, "2.2 is on position 1");
73 assertNumber(arr.$get(2), 3.0, "3 is on position 2");
74 final boolean[] called = { false };
75 final double[] sum = { 0.0 };
76 arr.forEach(new net.java.html.lib.Function.A1<java.lang.Number,Void>() {
78 public Void call(java.lang.Number p1) {
80 sum[0] += p1.doubleValue();
85 public Void call(Number p1, Object p2) {
90 public Void call(Number p1, Object p2, Object p3) {
95 public Void call(Number p1, Object p2, Object p3, Object p4) {
100 public Void call(Number p1, Object p2, Object p3, Object p4, Object p5) {
104 assertTrue(called[0], "forEach invoked");
105 assertEquals(sum[0], 6.3, 0.1, "Sum computed");
110 private static void assertEquals(double real, double expected, double delta, String msg) {
111 if (expected - delta < real && real < expected + delta) {
114 raise(msg + " exp: " + expected + " but was: " + real);
117 @JavaScriptBody(args = { "msg" }, body = "throw msg;")
118 private static native void raise(String msg);
120 private static void assertNumber(Object value, double expected, String msg) {
121 if (value instanceof Number) {
122 assertEquals(((Number)value).doubleValue(), expected, 0.1, msg);
125 raise("null is not a number");
128 raise("Not a number: " + value + " type: " + value.getClass());
132 private static void assertTrue(boolean b, String msg) {