diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapper.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapper.java index b9f6ca151..778e5b16e 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapper.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapper.java @@ -165,14 +165,14 @@ public final class JSWrapper { if (o == null) { return null; } - return ((JSWrapper) o).js; + return o instanceof JSWrapper ? ((JSWrapper) o).js : directJavaToJs(o); } public static JSObject maybeUnwrap(Object o) { if (o == null) { return null; } - return isJava(o) ? unwrap(o) : directJavaToJs(o); + return o instanceof JSWrapper ? unwrap(o) : directJavaToJs(o); } public static JSObject javaToJs(Object o) { diff --git a/tests/src/test/java/org/teavm/jso/test/JSWrapperTest.java b/tests/src/test/java/org/teavm/jso/test/JSWrapperTest.java index 51fc2aa7b..1d3bd268f 100644 --- a/tests/src/test/java/org/teavm/jso/test/JSWrapperTest.java +++ b/tests/src/test/java/org/teavm/jso/test/JSWrapperTest.java @@ -292,6 +292,25 @@ public class JSWrapperTest { assertEquals(JSNumber.valueOf(23), getProperty(o, "foo")); } + @Test + public void createArray() { + var array = new J[] { + new JImpl(23), + new JImpl(42) + }; + assertEquals(23, array[0].foo()); + assertEquals(42, callFoo(array[1])); + assertEquals("23,42", concatFoo(array)); + } + + @Test + public void createArrayAndReturnToJS() { + assertEquals("23,42", concatFoo(() -> new J[] { + new JImpl(23), + new JImpl(42) + })); + } + private void callSetProperty(Object instance, Object o) { setProperty(instance, "foo", o); } @@ -341,4 +360,35 @@ public class JSWrapperTest { interface ReturningObject extends JSObject { Object get(); } + + @JSBody(params = "o", script = "return o.foo();") + private static native int callFoo(JSObject o); + + @JSBody(params = "array", script = "return array[0].foo() + ',' + array[1].foo(); ") + private static native String concatFoo(J[] array); + + @JSBody(params = "supplier", script = "let array = supplier.get(); " + + "return array[0].foo() + ',' + array[1].foo();") + private static native String concatFoo(JArraySupplier supplier); + + interface J extends JSObject { + int foo(); + } + + class JImpl implements J { + private int value; + + JImpl(int value) { + this.value = value; + } + + @Override + public int foo() { + return value; + } + } + + interface JArraySupplier extends JSObject { + J[] get(); + } }