mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
JS: fix unwrapping JS objects implemented in Java
This commit is contained in:
parent
1c292f3fbe
commit
9757213379
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user