From ad57f957cbbf0bfb4827016c532d6fd4fa48264c Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sun, 25 Jan 2015 15:21:17 +0400 Subject: [PATCH] JSO bugfixes and improvements --- .../dom/webgl/WebGLRenderingContext.java | 22 +-- teavm-jso/src/main/java/org/teavm/jso/JS.java | 152 ++++++++++++++++-- .../java/org/teavm/jso/JSBooleanArray.java | 73 +++++++++ .../java/org/teavm/jso/JSDoubleArray.java | 77 +++++++++ ...unction.java => JSDoubleSortFunction.java} | 4 +- .../main/java/org/teavm/jso/JSFloatArray.java | 24 --- .../org/teavm/jso/JSFloatArrayReader.java | 83 ---------- .../src/main/java/org/teavm/jso/JSGlobal.java | 10 +- .../jso/plugin/JavascriptNativeProcessor.java | 31 +++- 9 files changed, 343 insertions(+), 133 deletions(-) create mode 100644 teavm-jso/src/main/java/org/teavm/jso/JSBooleanArray.java create mode 100644 teavm-jso/src/main/java/org/teavm/jso/JSDoubleArray.java rename teavm-jso/src/main/java/org/teavm/jso/{JSFloatSortFunction.java => JSDoubleSortFunction.java} (90%) delete mode 100644 teavm-jso/src/main/java/org/teavm/jso/JSFloatArray.java delete mode 100644 teavm-jso/src/main/java/org/teavm/jso/JSFloatArrayReader.java diff --git a/teavm-dom/src/main/java/org/teavm/dom/webgl/WebGLRenderingContext.java b/teavm-dom/src/main/java/org/teavm/dom/webgl/WebGLRenderingContext.java index 29da28f1c..23c0b25da 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/webgl/WebGLRenderingContext.java +++ b/teavm-dom/src/main/java/org/teavm/dom/webgl/WebGLRenderingContext.java @@ -652,7 +652,7 @@ public interface WebGLRenderingContext extends JSObject { void uniform1fv(WebGLUniformLocation location, Float32Array v); - void uniform1fv(WebGLUniformLocation location, JSFloatArrayReader v); + void uniform1fv(WebGLUniformLocation location, JSDoubleArrayReader v); void uniform1fv(WebGLUniformLocation location, float[] v); @@ -668,7 +668,7 @@ public interface WebGLRenderingContext extends JSObject { void uniform2fv(WebGLUniformLocation location, Float32Array v); - void uniform2fv(WebGLUniformLocation location, JSFloatArrayReader v); + void uniform2fv(WebGLUniformLocation location, JSDoubleArrayReader v); void uniform2fv(WebGLUniformLocation location, float[] v); @@ -684,7 +684,7 @@ public interface WebGLRenderingContext extends JSObject { void uniform3fv(WebGLUniformLocation location, Float32Array v); - void uniform3fv(WebGLUniformLocation location, JSFloatArrayReader v); + void uniform3fv(WebGLUniformLocation location, JSDoubleArrayReader v); void uniform3fv(WebGLUniformLocation location, float[] v); @@ -700,7 +700,7 @@ public interface WebGLRenderingContext extends JSObject { void uniform4fv(WebGLUniformLocation location, Float32Array v); - void uniform4fv(WebGLUniformLocation location, JSFloatArrayReader v); + void uniform4fv(WebGLUniformLocation location, JSDoubleArrayReader v); void uniform4fv(WebGLUniformLocation location, float[] v); @@ -714,19 +714,19 @@ public interface WebGLRenderingContext extends JSObject { void uniformMatrix2fv(WebGLUniformLocation location, boolean transpose, Float32Array value); - void uniformMatrix2fv(WebGLUniformLocation location, boolean transpose, JSFloatArrayReader value); + void uniformMatrix2fv(WebGLUniformLocation location, boolean transpose, JSDoubleArrayReader value); void uniformMatrix2fv(WebGLUniformLocation location, boolean transpose, float[] value); void uniformMatrix3fv(WebGLUniformLocation location, boolean transpose, Float32Array value); - void uniformMatrix3fv(WebGLUniformLocation location, boolean transpose, JSFloatArrayReader value); + void uniformMatrix3fv(WebGLUniformLocation location, boolean transpose, JSDoubleArrayReader value); void uniformMatrix3fv(WebGLUniformLocation location, boolean transpose, float[] value); void uniformMatrix4fv(WebGLUniformLocation location, boolean transpose, Float32Array value); - void uniformMatrix4fv(WebGLUniformLocation location, boolean transpose, JSFloatArrayReader value); + void uniformMatrix4fv(WebGLUniformLocation location, boolean transpose, JSDoubleArrayReader value); void uniformMatrix4fv(WebGLUniformLocation location, boolean transpose, float[] value); @@ -738,7 +738,7 @@ public interface WebGLRenderingContext extends JSObject { void vertexAttrib1fv(int indx, Float32Array values); - void vertexAttrib1fv(int indx, JSFloatArrayReader values); + void vertexAttrib1fv(int indx, JSDoubleArrayReader values); void vertexAttrib1fv(int indx, float[] values); @@ -746,7 +746,7 @@ public interface WebGLRenderingContext extends JSObject { void vertexAttrib2fv(int indx, Float32Array values); - void vertexAttrib2fv(int indx, JSFloatArrayReader values); + void vertexAttrib2fv(int indx, JSDoubleArrayReader values); void vertexAttrib2fv(int indx, float[] values); @@ -754,7 +754,7 @@ public interface WebGLRenderingContext extends JSObject { void vertexAttrib3fv(int indx, Float32Array values); - void vertexAttrib3fv(int indx, JSFloatArrayReader values); + void vertexAttrib3fv(int indx, JSDoubleArrayReader values); void vertexAttrib3fv(int indx, float[] values); @@ -762,7 +762,7 @@ public interface WebGLRenderingContext extends JSObject { void vertexAttrib4fv(int indx, Float32Array values); - void vertexAttrib4fv(int indx, JSFloatArrayReader values); + void vertexAttrib4fv(int indx, JSDoubleArrayReader values); void vertexAttrib4fv(int indx, float[] values); diff --git a/teavm-jso/src/main/java/org/teavm/jso/JS.java b/teavm-jso/src/main/java/org/teavm/jso/JS.java index d1096a78b..0481c80e2 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/JS.java +++ b/teavm-jso/src/main/java/org/teavm/jso/JS.java @@ -47,13 +47,25 @@ public final class JS { throw new AssertionError("Unexpected type"); } - public static native JSArray createArray(int size); + public static JSArray createArray(int size) { + return ((JSGlobal)JS.getGlobal()).newArray(size); + } - public static native JSIntArray createIntArray(int size); + public static JSIntArray createIntArray(int size) { + return ((JSGlobal)JS.getGlobal()).newIntArray(size); + } - public static native JSStringArray createStringArray(int size); + public static JSStringArray createStringArray(int size) { + return ((JSGlobal)JS.getGlobal()).newStringArray(size); + } - public static native JSFloatArray createFloatArray(int size); + public static JSBooleanArray createBooleanArray(int size) { + return ((JSGlobal)JS.getGlobal()).newBooleanArray(size); + } + + public static JSDoubleArray createDoubleArray(int size) { + return ((JSGlobal)JS.getGlobal()).newDoubleArray(size); + } @InjectedBy(JSNativeGenerator.class) public static native JSObject getTypeName(JSObject obj); @@ -113,6 +125,102 @@ public final class JS { return result; } + public static JSBooleanArray wrap(boolean[] array) { + JSBooleanArray result = createBooleanArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, array[i]); + } + return result; + } + + public static JSArray wrap(boolean[][] array) { + JSArray result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSArray> wrap(boolean[][][] array) { + JSArray> result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSIntArray wrap(byte[] array) { + JSIntArray result = createIntArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, array[i]); + } + return result; + } + + public static JSArray wrap(byte[][] array) { + JSArray result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSArray> wrap(byte[][][] array) { + JSArray> result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSIntArray wrap(short[] array) { + JSIntArray result = createIntArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, array[i]); + } + return result; + } + + public static JSArray wrap(short[][] array) { + JSArray result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSArray> wrap(short[][][] array) { + JSArray> result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSIntArray wrap(char[] array) { + JSIntArray result = createIntArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, array[i]); + } + return result; + } + + public static JSArray wrap(char[][] array) { + JSArray result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSArray> wrap(char[][][] array) { + JSArray> result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + public static JSIntArray wrap(int[] array) { JSIntArray result = createIntArray(array.length); for (int i = 0; i < array.length; ++i) { @@ -161,24 +269,48 @@ public final class JS { return result; } - public static JSFloatArray wrap(float[] array) { - JSFloatArray result = createFloatArray(array.length); + public static JSDoubleArray wrap(float[] array) { + JSDoubleArray result = createDoubleArray(array.length); for (int i = 0; i < array.length; ++i) { result.set(i, array[i]); } return result; } - public static JSArray wrap(float[][] array) { - JSArray result = createArray(array.length); + public static JSArray wrap(float[][] array) { + JSArray result = createArray(array.length); for (int i = 0; i < array.length; ++i) { result.set(i, wrap(array[i])); } return result; } - public static JSArray> wrap(float[][][] array) { - JSArray> result = createArray(array.length); + public static JSArray> wrap(float[][][] array) { + JSArray> result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSDoubleArray wrap(double[] array) { + JSDoubleArray result = createDoubleArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, array[i]); + } + return result; + } + + public static JSArray wrap(double[][] array) { + JSArray result = createArray(array.length); + for (int i = 0; i < array.length; ++i) { + result.set(i, wrap(array[i])); + } + return result; + } + + public static JSArray> wrap(double[][][] array) { + JSArray> result = createArray(array.length); for (int i = 0; i < array.length; ++i) { result.set(i, wrap(array[i])); } diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSBooleanArray.java b/teavm-jso/src/main/java/org/teavm/jso/JSBooleanArray.java new file mode 100644 index 000000000..1db51941e --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/JSBooleanArray.java @@ -0,0 +1,73 @@ +/* + * 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.jso; + +/** + * + * @author Alexey Andreev + */ +public interface JSBooleanArray extends JSBooleanArrayReader { + @JSIndexer + void set(int index, boolean value); + + int push(boolean a); + + int push(boolean a, boolean b); + + int push(boolean a, boolean b, boolean c); + + int push(boolean a, boolean b, boolean c, boolean d); + + boolean shift(); + + String join(String separator); + + String join(); + + JSBooleanArray concat(JSBooleanArrayReader a); + + JSBooleanArray concat(JSBooleanArray a, JSBooleanArray b); + + JSBooleanArray concat(JSBooleanArray a, JSBooleanArray b, JSBooleanArray c); + + JSBooleanArray concat(JSBooleanArray a, JSBooleanArray b, JSBooleanArray c, JSBooleanArray d); + + boolean pop(); + + int unshift(boolean a); + + int unshift(boolean a, boolean b); + + int unshift(boolean a, boolean b, boolean c); + + int unshift(boolean a, boolean b, boolean c, boolean d); + + JSBooleanArray slice(int start); + + JSBooleanArray slice(int start, int end); + + JSBooleanArray reverse(); + + JSBooleanArray splice(int start, int count); + + JSBooleanArray splice(int start, int count, boolean a); + + JSBooleanArray splice(int start, int count, boolean a, boolean b); + + JSBooleanArray splice(int start, int count, boolean a, boolean b, boolean c); + + JSBooleanArray splice(int start, int count, boolean a, boolean b, boolean c, boolean d); +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSDoubleArray.java b/teavm-jso/src/main/java/org/teavm/jso/JSDoubleArray.java new file mode 100644 index 000000000..9319be131 --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/JSDoubleArray.java @@ -0,0 +1,77 @@ +/* + * 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.jso; + +/** + * + * @author Alexey Andreev + */ +public interface JSDoubleArray extends JSDoubleArrayReader { + @JSIndexer + void set(int index, double value); + + int push(double a); + + int push(double a, double b); + + int push(double a, double b, double c); + + int push(double a, double b, double c, double d); + + double shift(); + + String join(String separator); + + String join(); + + JSDoubleArray concat(JSDoubleArrayReader a); + + JSDoubleArray concat(JSDoubleArray a, JSDoubleArray b); + + JSDoubleArray concat(JSDoubleArray a, JSDoubleArray b, JSDoubleArray c); + + JSDoubleArray concat(JSDoubleArray a, JSDoubleArray b, JSDoubleArray c, JSDoubleArray d); + + double pop(); + + int unshift(double a); + + int unshift(double a, double b); + + int unshift(double a, double b, double c); + + int unshift(double a, double b, double c, double d); + + JSDoubleArray slice(int start); + + JSDoubleArray slice(int start, int end); + + JSDoubleArray reverse(); + + JSDoubleArray sort(JSDoubleSortFunction function); + + JSDoubleArray sort(); + + JSDoubleArray splice(int start, int count); + + JSDoubleArray splice(int start, int count, double a); + + JSDoubleArray splice(int start, int count, double a, double b); + + JSDoubleArray splice(int start, int count, double a, double b, double c); + + JSDoubleArray splice(int start, int count, double a, double b, double c, double d); +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSFloatSortFunction.java b/teavm-jso/src/main/java/org/teavm/jso/JSDoubleSortFunction.java similarity index 90% rename from teavm-jso/src/main/java/org/teavm/jso/JSFloatSortFunction.java rename to teavm-jso/src/main/java/org/teavm/jso/JSDoubleSortFunction.java index a5a0a95ac..881f10eb2 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/JSFloatSortFunction.java +++ b/teavm-jso/src/main/java/org/teavm/jso/JSDoubleSortFunction.java @@ -20,6 +20,6 @@ package org.teavm.jso; * @author Alexey Andreev */ @JSFunctor -public interface JSFloatSortFunction { - int compare(float a, float b); +public interface JSDoubleSortFunction { + int compare(double a, double b); } diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSFloatArray.java b/teavm-jso/src/main/java/org/teavm/jso/JSFloatArray.java deleted file mode 100644 index d6f05133a..000000000 --- a/teavm-jso/src/main/java/org/teavm/jso/JSFloatArray.java +++ /dev/null @@ -1,24 +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.jso; - -/** - * - * @author Alexey Andreev - */ -public interface JSFloatArray extends JSFloatArrayReader { - -} diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSFloatArrayReader.java b/teavm-jso/src/main/java/org/teavm/jso/JSFloatArrayReader.java deleted file mode 100644 index da2aed665..000000000 --- a/teavm-jso/src/main/java/org/teavm/jso/JSFloatArrayReader.java +++ /dev/null @@ -1,83 +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.jso; - -/** - * - * @author Alexey Andreev - */ -public interface JSFloatArrayReader extends JSObject { - @JSProperty - int getLength(); - - @JSIndexer - float get(int index); - - @JSIndexer - void set(int index, float value); - - int push(float a); - - int push(float a, float b); - - int push(float a, float b, float c); - - int push(float a, float b, float c, float d); - - float shift(); - - String join(String separator); - - String join(); - - JSFloatArray concat(JSFloatArrayReader a); - - JSFloatArray concat(JSFloatArray a, JSFloatArray b); - - JSFloatArray concat(JSFloatArray a, JSFloatArray b, JSFloatArray c); - - JSFloatArray concat(JSFloatArray a, JSFloatArray b, JSFloatArray c, JSFloatArray d); - - float pop(); - - int unshift(float a); - - int unshift(float a, float b); - - int unshift(float a, float b, float c); - - int unshift(float a, float b, float c, float d); - - JSFloatArray slice(int start); - - JSFloatArray slice(int start, int end); - - JSFloatArray reverse(); - - JSFloatArray sort(JSFloatSortFunction function); - - JSFloatArray sort(); - - JSFloatArray splice(int start, int count); - - JSFloatArray splice(int start, int count, float a); - - JSFloatArray splice(int start, int count, float a, float b); - - JSFloatArray splice(int start, int count, float a, float b, float c); - - JSFloatArray splice(int start, int count, float a, float b, float c, float d); -} diff --git a/teavm-jso/src/main/java/org/teavm/jso/JSGlobal.java b/teavm-jso/src/main/java/org/teavm/jso/JSGlobal.java index fe29c8038..d97762451 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/JSGlobal.java +++ b/teavm-jso/src/main/java/org/teavm/jso/JSGlobal.java @@ -35,6 +35,12 @@ public interface JSGlobal extends JSObject { @JSConstructor("Array") JSStringArray newStringArray(int sz); + @JSConstructor("Array") + JSBooleanArray newBooleanArray(); + + @JSConstructor("Array") + JSBooleanArray newBooleanArray(int sz); + @JSConstructor("Array") JSIntArray newIntArray(); @@ -42,8 +48,8 @@ public interface JSGlobal extends JSObject { JSIntArray newIntArray(int sz); @JSConstructor("Array") - JSFloatArray newFloatArray(); + JSDoubleArray newDoubleArray(); @JSConstructor("Array") - JSFloatArray newFloatArray(int sz); + JSDoubleArray newDoubleArray(int sz); } diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java index 8bb924573..b2d44ab55 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java @@ -470,7 +470,7 @@ class JavascriptNativeProcessor { } Variable result = program.createVariable(); InvokeInstruction insn = new InvokeInstruction(); - insn.setMethod(new MethodReference(JS.class.getName(), "wrap", type, ValueType.parse(JSObject.class))); + insn.setMethod(new MethodReference(JS.class.getName(), "wrap", type, getWrapperType(type))); insn.getArguments().add(var); insn.setReceiver(result); insn.setType(InvocationType.SPECIAL); @@ -479,6 +479,35 @@ class JavascriptNativeProcessor { return result; } + private ValueType getWrapperType(ValueType type) { + if (type instanceof ValueType.Array) { + ValueType itemType = ((ValueType.Array)type).getItemType(); + if (itemType instanceof ValueType.Primitive) { + switch (((ValueType.Primitive)itemType).getKind()) { + case BOOLEAN: + return ValueType.parse(JSBooleanArray.class); + case BYTE: + case SHORT: + case INTEGER: + case CHARACTER: + return ValueType.parse(JSIntArray.class); + case FLOAT: + case DOUBLE: + return ValueType.parse(JSDoubleArray.class); + case LONG: + default: + return ValueType.parse(JSArray.class); + } + } else if (itemType.isObject("java.lang.String")) { + return ValueType.parse(JSStringArray.class); + } else { + return ValueType.parse(JSArray.class); + } + } else { + return ValueType.parse(JSObject.class); + } + } + private MethodReader getMethod(MethodReference ref) { ClassReader cls = classSource.get(ref.getClassName()); MethodReader method = cls.getMethod(ref.getDescriptor());