From 3dd76c9355b35639adb60dc40543a8e89ccc4a6d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 19 Aug 2016 22:22:07 +0300 Subject: [PATCH] Fix bugs in WASM target --- .../teavm/classlib/java/lang/TBoolean.java | 6 +-- .../org/teavm/classlib/java/lang/TByte.java | 6 +-- .../org/teavm/classlib/java/lang/TClass.java | 30 -------------- .../org/teavm/classlib/java/lang/TDouble.java | 6 +-- .../org/teavm/classlib/java/lang/TFloat.java | 6 +-- .../org/teavm/classlib/java/lang/TLong.java | 6 +-- .../org/teavm/classlib/java/lang/TShort.java | 6 +-- .../org/teavm/classlib/java/util/TArrays.java | 15 ++++--- .../java/org/teavm/runtime/Allocator.java | 2 +- core/src/main/java/org/teavm/vm/TeaVM.java | 2 +- .../src/main/java/org/teavm/wasm/Example.java | 4 +- .../main/java/org/teavm/wasm/WasmRuntime.java | 25 ++++++------ .../java/org/teavm/platform/Platform.java | 4 -- .../teavm/platform/PlatformPrimitives.java | 39 ------------------- 14 files changed, 33 insertions(+), 124 deletions(-) delete mode 100644 platform/src/main/java/org/teavm/platform/PlatformPrimitives.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java index 9ba748289..20eaf08de 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java @@ -17,14 +17,10 @@ package org.teavm.classlib.java.lang; import org.teavm.classlib.java.io.TSerializable; -/** - * - * @author Alexey Andreev - */ public class TBoolean extends TObject implements TSerializable, TComparable { public static final TBoolean TRUE = new TBoolean(true); public static final TBoolean FALSE = new TBoolean(false); - public static final TClass TYPE = TClass.booleanClass(); + public static final Class TYPE = boolean.class; private boolean value; public TBoolean(boolean value) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TByte.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TByte.java index cde654290..379f86dd3 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TByte.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TByte.java @@ -15,14 +15,10 @@ */ package org.teavm.classlib.java.lang; -/** - * - * @author Alexey Andreev - */ public class TByte extends TNumber implements TComparable { public static final byte MIN_VALUE = -128; public static final byte MAX_VALUE = 127; - public static final TClass TYPE = TClass.byteClass(); + public static final Class TYPE = byte.class; public static final int SIZE = 8; private byte value; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index e5f248449..d83bf3a96 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -108,36 +108,6 @@ public class TClass extends TObject implements TAnnotatedElement { return getClass(platformClass.getMetadata().getArrayItem()); } - @SuppressWarnings("unchecked") - static TClass booleanClass() { - return (TClass) getClass(Platform.getPrimitives().getBooleanClass()); - } - - @SuppressWarnings("unchecked") - static TClass byteClass() { - return (TClass) getClass(Platform.getPrimitives().getByteClass()); - } - - @SuppressWarnings("unchecked") - static TClass shortClass() { - return (TClass) getClass(Platform.getPrimitives().getShortClass()); - } - - @SuppressWarnings("unchecked") - static TClass longClass() { - return (TClass) getClass(Platform.getPrimitives().getLongClass()); - } - - @SuppressWarnings("unchecked") - static TClass floatClass() { - return (TClass) getClass(Platform.getPrimitives().getFloatClass()); - } - - @SuppressWarnings("unchecked") - static TClass doubleClass() { - return (TClass) getClass(Platform.getPrimitives().getDoubleClass()); - } - public boolean desiredAssertionStatus() { return true; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index 1bed48e08..d7ea47e69 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -17,10 +17,6 @@ package org.teavm.classlib.java.lang; import org.teavm.jso.JSBody; -/** - * - * @author Alexey Andreev - */ public class TDouble extends TNumber implements TComparable { public static final double POSITIVE_INFINITY = 1 / 0.0; public static final double NEGATIVE_INFINITY = -POSITIVE_INFINITY; @@ -31,7 +27,7 @@ public class TDouble extends TNumber implements TComparable { public static final int MAX_EXPONENT = 1023; public static final int MIN_EXPONENT = -1022; public static final int SIZE = 64; - public static final TClass TYPE = TClass.doubleClass(); + public static final Class TYPE = double.class; private double value; public TDouble(double value) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java index c16e38eec..17330d7f9 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java @@ -17,10 +17,6 @@ package org.teavm.classlib.java.lang; import org.teavm.jso.JSBody; -/** - * - * @author Alexey Andreev - */ public class TFloat extends TNumber implements TComparable { public static final float POSITIVE_INFINITY = 1 / 0.0f; public static final float NEGATIVE_INFINITY = -POSITIVE_INFINITY; @@ -31,7 +27,7 @@ public class TFloat extends TNumber implements TComparable { public static final int MAX_EXPONENT = 127; public static final int MIN_EXPONENT = -126; public static final int SIZE = 32; - public static final TClass TYPE = TClass.floatClass(); + public static final Class TYPE = float.class; private float value; public TFloat(float value) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index d105ca31f..c9bc22278 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -17,14 +17,10 @@ package org.teavm.classlib.java.lang; import org.teavm.javascript.spi.GeneratedBy; -/** - * - * @author Alexey Andreev - */ public class TLong extends TNumber implements TComparable { public static final long MIN_VALUE = -0x8000000000000000L; public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL; - public static final TClass TYPE = TClass.longClass(); + public static final Class TYPE = long.class; public static final int SIZE = 64; private long value; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java index bbbf5e80a..867537047 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java @@ -15,14 +15,10 @@ */ package org.teavm.classlib.java.lang; -/** - * - * @author Alexey Andreev - */ public class TShort extends TNumber implements TComparable { public static final short MIN_VALUE = -32768; public static final short MAX_VALUE = 32767; - public static final TClass TYPE = TClass.shortClass(); + public static final Class TYPE = short.class; public static final int SIZE = 16; private short value; diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java index a4582893c..c28b6c3d9 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java @@ -17,13 +17,18 @@ package org.teavm.classlib.java.util; import java.lang.reflect.Array; import java.util.Objects; -import org.teavm.classlib.java.lang.*; +import org.teavm.classlib.java.lang.TClass; +import org.teavm.classlib.java.lang.TComparable; +import org.teavm.classlib.java.lang.TDouble; +import org.teavm.classlib.java.lang.TFloat; +import org.teavm.classlib.java.lang.TIllegalArgumentException; +import org.teavm.classlib.java.lang.TInteger; +import org.teavm.classlib.java.lang.TMath; +import org.teavm.classlib.java.lang.TObject; +import org.teavm.classlib.java.lang.TString; +import org.teavm.classlib.java.lang.TStringBuilder; import org.teavm.classlib.java.lang.reflect.TArray; -/** - * - * @author Alexey Andreev - */ public class TArrays extends TObject { public static char[] copyOf(char[] array, int length) { char[] result = new char[length]; diff --git a/core/src/main/java/org/teavm/runtime/Allocator.java b/core/src/main/java/org/teavm/runtime/Allocator.java index 2a208c173..c35057346 100644 --- a/core/src/main/java/org/teavm/runtime/Allocator.java +++ b/core/src/main/java/org/teavm/runtime/Allocator.java @@ -36,7 +36,7 @@ public final class Allocator { public static Address allocateArray(RuntimeClass tag, int size) { Address result = address; - int sizeInBytes = tag.size * 4 + Structure.sizeOf(RuntimeArray.class); + int sizeInBytes = tag.itemType.size * size + Structure.sizeOf(RuntimeArray.class); address = result.add(sizeInBytes); fillZero(result, sizeInBytes); diff --git a/core/src/main/java/org/teavm/vm/TeaVM.java b/core/src/main/java/org/teavm/vm/TeaVM.java index 69b1f28da..6a0b09500 100644 --- a/core/src/main/java/org/teavm/vm/TeaVM.java +++ b/core/src/main/java/org/teavm/vm/TeaVM.java @@ -362,7 +362,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { return; } - inline(classSet); + //inline(classSet); if (wasCancelled()) { return; } diff --git a/core/src/main/java/org/teavm/wasm/Example.java b/core/src/main/java/org/teavm/wasm/Example.java index 7e5b9ba1d..54480a1ab 100644 --- a/core/src/main/java/org/teavm/wasm/Example.java +++ b/core/src/main/java/org/teavm/wasm/Example.java @@ -34,7 +34,7 @@ public final class Example { println(String.valueOf(a)); } - println("A(2) + A(3) = " + new A(2).getValue() + new A(3).getValue()); + println("A(2) + A(3) = " + (new A(2).getValue() + new A(3).getValue())); for (int i = 0; i < 4; ++i) { println("instance(" + i + ") = " + instance(i).foo()); @@ -43,7 +43,7 @@ public final class Example { Base[] array = { new Derived1(), new Derived2() }; println("array.length = " + array.length); for (Base elem : array) { - println("array[i]" + elem.foo()); + println("array[i] = " + elem.foo()); } println("Derived2 instanceof Base = " + (new Derived2() instanceof Base)); diff --git a/core/src/main/java/org/teavm/wasm/WasmRuntime.java b/core/src/main/java/org/teavm/wasm/WasmRuntime.java index a5e7f5655..de954c511 100644 --- a/core/src/main/java/org/teavm/wasm/WasmRuntime.java +++ b/core/src/main/java/org/teavm/wasm/WasmRuntime.java @@ -147,22 +147,22 @@ public final class WasmRuntime { } private static int getInt(Address data) { - byte a = data.getByte(); - byte b = data.add(1).getByte(); - byte c = data.add(2).getByte(); - byte d = data.add(3).getByte(); + int a = data.getByte() & 0xFF; + int b = data.add(1).getByte() & 0xFF; + int c = data.add(2).getByte() & 0xFF; + int d = data.add(3).getByte() & 0xFF; return (a << 24) | (b << 16) | (c << 8) | d; } private static long getLong(Address data) { - long a = data.getByte(); - long b = data.add(1).getByte(); - long c = data.add(2).getByte(); - long d = data.add(3).getByte(); - long e = data.add(4).getByte(); - long f = data.add(5).getByte(); - long g = data.add(6).getByte(); - long h = data.add(7).getByte(); + long a = data.getByte() & 0xFF; + long b = data.add(1).getByte() & 0xFF; + long c = data.add(2).getByte() & 0xFF; + long d = data.add(3).getByte() & 0xFF; + long e = data.add(4).getByte() & 0xFF; + long f = data.add(5).getByte() & 0xFF; + long g = data.add(6).getByte() & 0xFF; + long h = data.add(7).getByte() & 0xFF; return (a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h; } @@ -184,6 +184,7 @@ public final class WasmRuntime { value = index.getByte(); index = index.add(1); result |= (value & 0x7F) << shift; + shift += 7; } while ((value & 0x80) != 0); offset = index; return result; diff --git a/platform/src/main/java/org/teavm/platform/Platform.java b/platform/src/main/java/org/teavm/platform/Platform.java index 7cfa01ffc..6a3fcefba 100644 --- a/platform/src/main/java/org/teavm/platform/Platform.java +++ b/platform/src/main/java/org/teavm/platform/Platform.java @@ -66,10 +66,6 @@ public final class Platform { @PluggableDependency(PlatformGenerator.class) public static native Class asJavaClass(PlatformObject obj); - public static PlatformPrimitives getPrimitives() { - return (PlatformPrimitives) Window.current(); - } - public static PlatformConsole getConsole() { return (PlatformConsole) Window.current(); } diff --git a/platform/src/main/java/org/teavm/platform/PlatformPrimitives.java b/platform/src/main/java/org/teavm/platform/PlatformPrimitives.java deleted file mode 100644 index e51c0ceb9..000000000 --- a/platform/src/main/java/org/teavm/platform/PlatformPrimitives.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2015 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.platform; - -import org.teavm.jso.JSMethod; -import org.teavm.jso.JSObject; - -public interface PlatformPrimitives extends JSObject { - @JSMethod("$rt_booleancls") - PlatformClass getBooleanClass(); - - @JSMethod("$rt_bytecls") - PlatformClass getByteClass(); - - @JSMethod("$rt_shortcls") - PlatformClass getShortClass(); - - @JSMethod("$rt_longcls") - PlatformClass getLongClass(); - - @JSMethod("$rt_floatcls") - PlatformClass getFloatClass(); - - @JSMethod("$rt_doublecls") - PlatformClass getDoubleClass(); -}