From f620f9513edadfefcda5d120d89b892e4ea50099 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 10 Sep 2015 12:11:37 +0300 Subject: [PATCH] Working on TCK for JSO --- pom.xml | 5 + teavm-classlib/pom.xml | 5 + .../main/java/org/teavm/jso/plugin/JS.java | 20 +-- .../teavm/jso/plugin/JSNativeGenerator.java | 10 ++ teavm-jso/pom.xml | 14 ++ .../org/teavm/jso/test/ConversionTest.java | 130 ++++++++++++++++++ .../java/org/teavm/jso/test/JSBodyTest.java | 51 +++++++ .../platform/plugin/PlatformGenerator.java | 1 + teavm-tests/pom.xml | 6 + 9 files changed, 226 insertions(+), 16 deletions(-) create mode 100644 teavm-jso/src/test/java/org/teavm/jso/test/ConversionTest.java create mode 100644 teavm-jso/src/test/java/org/teavm/jso/test/JSBodyTest.java diff --git a/pom.xml b/pom.xml index 3649cf62c..a0f575d81 100644 --- a/pom.xml +++ b/pom.xml @@ -254,6 +254,11 @@ maven-bundle-plugin 2.5.3 + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index faee0528e..95ff26998 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -54,6 +54,11 @@ teavm-jso ${project.version} + + org.teavm + teavm-jso-impl + ${project.version} + com.google.code.gson gson diff --git a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JS.java b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JS.java index 690385ba9..bb3244524 100644 --- a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JS.java +++ b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JS.java @@ -16,7 +16,6 @@ package org.teavm.jso.plugin; import java.lang.reflect.Array; -import java.util.Iterator; import java.util.function.Function; import org.teavm.dependency.PluggableDependency; import org.teavm.javascript.spi.GeneratedBy; @@ -60,6 +59,9 @@ final class JS { @InjectedBy(JSNativeGenerator.class) public static native byte unwrapByte(JSObject value); + @InjectedBy(JSNativeGenerator.class) + public static native char unwrapCharacter(JSObject value); + @InjectedBy(JSNativeGenerator.class) public static native short unwrapShort(JSObject value); @@ -430,21 +432,7 @@ final class JS { public static native JSObject instantiate(JSObject instance, JSObject constructor, JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, JSObject g, JSObject h); - public static Iterable iterate(final JSArrayReader array) { - return () -> new Iterator() { - int index; - @Override public boolean hasNext() { - return index < array.getLength(); - } - @Override public T next() { - return array.get(index++); - } - @Override public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - + @InjectedBy(JSNativeGenerator.class) @JSBody(params = { "instance", "index" }, script = "return instance[index];") public static native JSObject get(JSObject instance, JSObject index); diff --git a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JSNativeGenerator.java b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JSNativeGenerator.java index 9406bfb08..44d64c0a0 100644 --- a/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JSNativeGenerator.java +++ b/teavm-jso-impl/src/main/java/org/teavm/jso/plugin/JSNativeGenerator.java @@ -32,6 +32,7 @@ import org.teavm.model.CallLocation; import org.teavm.model.ClassReader; import org.teavm.model.MethodReader; import org.teavm.model.MethodReference; +import org.teavm.model.ValueType; /** * @@ -123,6 +124,10 @@ public class JSNativeGenerator implements Injector, DependencyPlugin, Generator writer.append("$rt_ustr("); context.writeExpr(context.getArgument(0)); writer.append(")"); + } else if (methodRef.getDescriptor().parameterType(0) == ValueType.BOOLEAN) { + writer.append("(!!("); + context.writeExpr(context.getArgument(0)); + writer.append("))"); } else { context.writeExpr(context.getArgument(0)); } @@ -135,6 +140,11 @@ public class JSNativeGenerator implements Injector, DependencyPlugin, Generator context.writeExpr(context.getArgument(0)); writer.append(")"); break; + case "unwrapBoolean": + writer.append("("); + context.writeExpr(context.getArgument(0)); + writer.ws().append("?").ws().append("1").ws().append(":").ws().append("0").append(")"); + break; default: if (methodRef.getName().startsWith("unwrap")) { context.writeExpr(context.getArgument(0)); diff --git a/teavm-jso/pom.xml b/teavm-jso/pom.xml index c3dcfce2b..e301cfd8c 100644 --- a/teavm-jso/pom.xml +++ b/teavm-jso/pom.xml @@ -37,6 +37,20 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + tck + + + + org.apache.maven.plugins maven-checkstyle-plugin diff --git a/teavm-jso/src/test/java/org/teavm/jso/test/ConversionTest.java b/teavm-jso/src/test/java/org/teavm/jso/test/ConversionTest.java new file mode 100644 index 000000000..784df0a4d --- /dev/null +++ b/teavm-jso/src/test/java/org/teavm/jso/test/ConversionTest.java @@ -0,0 +1,130 @@ +/* + * 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.test; + +import static org.junit.Assert.*; +import org.junit.Test; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public class ConversionTest { + @Test + public void convertsPrimitivesToJavaScript() { + assertEquals("true:2:3:64:4:5.5:6.5:foo", combinePrimitives(true, (byte) 2, (short) 3, + '@', 4, 5.5F, 6.5, "foo")); + } + + @Test + public void convertsPrimitivesToJava() { + Primitives map = getPrimitives(); + assertTrue(map.getA()); + assertEquals(2, map.getB()); + assertEquals(3, map.getC()); + assertEquals('@', map.getD()); + assertEquals(4, map.getE()); + assertEquals(5.5, map.getF(), 0.01); + assertEquals(6.5, map.getG(), 0.01); + assertEquals("foo", map.getH()); + } + + @Test + public void convertsArraysToJava() { + PrimitiveArrays arrays = getPrimitiveArrays(); + + boolean[] booleanArray = arrays.getA(); + assertEquals(4, booleanArray.length); + assertTrue(booleanArray[0]); + assertFalse(booleanArray[1]); + + assertArrayEquals(new byte[] { 2 }, arrays.getB()); + assertArrayEquals(new short[] { 3 }, arrays.getC()); + assertArrayEquals(new char[] { '@' }, arrays.getD()); + assertArrayEquals(new int[] { 4 }, arrays.getE()); + assertArrayEquals(new float[] { 5.5F }, arrays.getF(), 0.01F); + assertArrayEquals(new double[] { 6.5 }, arrays.getG(), 0.01); + assertArrayEquals(new String[] { "foo" }, arrays.getH()); + } + + @JSBody(params = { "a", "b", "c", "d", "e", "f", "g", "h" }, script = "" + + "return '' + a + ':' + b + ':' + c + ':' + d + ':' + e + ':' + f.toFixed(1) + ':'" + + "+ g.toFixed(1) + ':' + h;") + private static native String combinePrimitives(boolean a, byte b, short c, char d, int e, float f, double g, + String h); + + @JSBody(params = {}, script = "return { a : true, b : 2, c : 3, d : 64, e : 4, f : 5.5, g : 6.5, h : 'foo' };") + private static native Primitives getPrimitives(); + + @JSBody(params = {}, script = "return { a : [true], b : [2], c : [3], d : [64], e : [4], f : [5.5], " + + "g : [6.5], h : ['foo'] };") + private static native PrimitiveArrays getPrimitiveArrays(); + + interface Primitives extends JSObject { + @JSProperty + boolean getA(); + + @JSProperty + byte getB(); + + @JSProperty + short getC(); + + @JSProperty + char getD(); + + @JSProperty + int getE(); + + @JSProperty + float getF(); + + @JSProperty + double getG(); + + @JSProperty + String getH(); + } + + interface PrimitiveArrays extends JSObject { + @JSProperty + boolean[] getA(); + + @JSProperty + byte[] getB(); + + @JSProperty + short[] getC(); + + @JSProperty + char[] getD(); + + @JSProperty + int[] getE(); + + @JSProperty + float[] getF(); + + @JSProperty + double[] getG(); + + @JSProperty + String[] getH(); + } +} diff --git a/teavm-jso/src/test/java/org/teavm/jso/test/JSBodyTest.java b/teavm-jso/src/test/java/org/teavm/jso/test/JSBodyTest.java new file mode 100644 index 000000000..34d7f393f --- /dev/null +++ b/teavm-jso/src/test/java/org/teavm/jso/test/JSBodyTest.java @@ -0,0 +1,51 @@ +/* + * 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.test; + +import static org.junit.Assert.*; +import org.junit.Test; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public class JSBodyTest { + @Test + public void staticWorks() { + assertEquals(12, add(5, 7)); + } + + @Test + public void memberWorks() { + assertEquals(12, convert(convert(5).add(convert(7)))); + } + + @JSBody(params = { "a", "b" }, script = "return a + b;") + private static native int add(int a, int b); + + @JSBody(params = "n", script = "return n;") + private static native Num convert(int n); + + @JSBody(params = "n", script = "return n;") + private static native int convert(Num n); + + static abstract class Num implements JSObject { + @JSBody(params = "other", script = "return this + other;") + public final native Num add(Num other); + } +} diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index ecfea811f..f9bfd3cdf 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -70,6 +70,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin case "classFromResource": case "objectFromResource": case "marshall": + case "getPlatformObject": context.writeExpr(context.getArgument(0)); break; } diff --git a/teavm-tests/pom.xml b/teavm-tests/pom.xml index 45bb9a28b..93a29ffef 100644 --- a/teavm-tests/pom.xml +++ b/teavm-tests/pom.xml @@ -48,6 +48,12 @@ teavm-jso ${project.version} + + org.teavm + teavm-jso + ${project.version} + tck + junit junit