Working on TCK for JSO

This commit is contained in:
Alexey Andreev 2015-09-10 12:11:37 +03:00
parent e1f6bfdaeb
commit f620f9513e
9 changed files with 226 additions and 16 deletions

View File

@ -254,6 +254,11 @@
<artifactId>maven-bundle-plugin</artifactId> <artifactId>maven-bundle-plugin</artifactId>
<version>2.5.3</version> <version>2.5.3</version>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
</build> </build>

View File

@ -54,6 +54,11 @@
<artifactId>teavm-jso</artifactId> <artifactId>teavm-jso</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso-impl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>

View File

@ -16,7 +16,6 @@
package org.teavm.jso.plugin; package org.teavm.jso.plugin;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.function.Function; import java.util.function.Function;
import org.teavm.dependency.PluggableDependency; import org.teavm.dependency.PluggableDependency;
import org.teavm.javascript.spi.GeneratedBy; import org.teavm.javascript.spi.GeneratedBy;
@ -60,6 +59,9 @@ final class JS {
@InjectedBy(JSNativeGenerator.class) @InjectedBy(JSNativeGenerator.class)
public static native byte unwrapByte(JSObject value); public static native byte unwrapByte(JSObject value);
@InjectedBy(JSNativeGenerator.class)
public static native char unwrapCharacter(JSObject value);
@InjectedBy(JSNativeGenerator.class) @InjectedBy(JSNativeGenerator.class)
public static native short unwrapShort(JSObject value); 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, 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); JSObject c, JSObject d, JSObject e, JSObject f, JSObject g, JSObject h);
public static <T extends JSObject> Iterable<T> iterate(final JSArrayReader<T> array) { @InjectedBy(JSNativeGenerator.class)
return () -> new Iterator<T>() {
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();
}
};
}
@JSBody(params = { "instance", "index" }, script = "return instance[index];") @JSBody(params = { "instance", "index" }, script = "return instance[index];")
public static native JSObject get(JSObject instance, JSObject index); public static native JSObject get(JSObject instance, JSObject index);

View File

@ -32,6 +32,7 @@ import org.teavm.model.CallLocation;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.MethodReader; import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference; 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("); writer.append("$rt_ustr(");
context.writeExpr(context.getArgument(0)); context.writeExpr(context.getArgument(0));
writer.append(")"); writer.append(")");
} else if (methodRef.getDescriptor().parameterType(0) == ValueType.BOOLEAN) {
writer.append("(!!(");
context.writeExpr(context.getArgument(0));
writer.append("))");
} else { } else {
context.writeExpr(context.getArgument(0)); context.writeExpr(context.getArgument(0));
} }
@ -135,6 +140,11 @@ public class JSNativeGenerator implements Injector, DependencyPlugin, Generator
context.writeExpr(context.getArgument(0)); context.writeExpr(context.getArgument(0));
writer.append(")"); writer.append(")");
break; break;
case "unwrapBoolean":
writer.append("(");
context.writeExpr(context.getArgument(0));
writer.ws().append("?").ws().append("1").ws().append(":").ws().append("0").append(")");
break;
default: default:
if (methodRef.getName().startsWith("unwrap")) { if (methodRef.getName().startsWith("unwrap")) {
context.writeExpr(context.getArgument(0)); context.writeExpr(context.getArgument(0));

View File

@ -37,6 +37,20 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<classifier>tck</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -70,6 +70,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
case "classFromResource": case "classFromResource":
case "objectFromResource": case "objectFromResource":
case "marshall": case "marshall":
case "getPlatformObject":
context.writeExpr(context.getArgument(0)); context.writeExpr(context.getArgument(0));
break; break;
} }

View File

@ -48,6 +48,12 @@
<artifactId>teavm-jso</artifactId> <artifactId>teavm-jso</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>${project.version}</version>
<classifier>tck</classifier>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>