Skip to content

Commit

Permalink
wasm gc: support JS functions with varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Oct 4, 2024
1 parent a5212fb commit 1d47146
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,16 @@ TeaVM.wasm = function() {
} else {
return 0;
}
}
},
apply: (instance, method, args) => {
if (instance === null) {
let fn = getGlobalName(method);
return fn(...args);
} else {
return instance[method](...args);
}
},
concatArray: (a, b) => a.concat(b)
};
for (let name of ["wrapByte", "wrapShort", "wrapChar", "wrapInt", "wrapFloat", "wrapDouble", "unwrapByte",
"unwrapShort", "unwrapChar", "unwrapInt", "unwrapFloat", "unwrapDouble"]) {
Expand All @@ -308,6 +317,7 @@ TeaVM.wasm = function() {
imports.teavmJso["callMethod" + i] = (instance, method, ...args) =>
instance !== null ? instance[method](...args) : getGlobalName(method)(...args);
imports.teavmJso["construct" + i] = (constructor, ...args) => new constructor(...args);
imports.teavmJso["arrayOf" + i] = (...args) => args
}
}

Expand Down
15 changes: 15 additions & 0 deletions jso/impl/src/main/java/org/teavm/jso/impl/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private JS() {

@InjectedBy(JSNativeInjector.class)
@NoSideEffects
@Import(name = "concatArray", module = "teavmJso")
public static native JSObject concatArray(JSObject a, JSObject b);

@InjectedBy(JSNativeInjector.class)
Expand Down Expand Up @@ -562,76 +563,90 @@ public static native JSObject invoke(JSObject instance, JSObject method, JSObjec
JSObject l, JSObject m);

@InjectedBy(JSNativeInjector.class)
@Import(name = "apply", module = "teavmJso")
public static native JSObject apply(JSObject instance, JSObject method, JSArray<JSObject> v);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf1", module = "teavmJso")
public static native JSObject arrayOf(JSObject a);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf2", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf3", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf4", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf5", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf6", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf7", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf8", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@Import(name = "arrayOf9", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf10", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i, JSObject j);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf11", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i, JSObject j, JSObject k);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf12", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i, JSObject j, JSObject k, JSObject l);

@InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class)
@NoSideEffects
@Import(name = "arrayOf13", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i, JSObject j, JSObject k, JSObject l, JSObject m);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,8 @@ private boolean processMethod(MethodReader method, String name, CallLocation cal
var arg = invoke.getArguments().get(i);
var byRef = byRefParams[i];
if (vararg && i == invoke.getArguments().size() - 1
&& typeHelper.isSupportedByRefType(method.parameterType(i))) {
&& typeHelper.isSupportedByRefType(method.parameterType(i))
&& !wasmGC) {
byRef = true;
}
arg = marshaller.wrapArgument(callLocation, arg,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/java/org/teavm/jso/test/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@RunWith(TeaVMTestRunner.class)
@SkipJVM
@OnlyPlatform(TestPlatform.JAVASCRIPT)
@OnlyPlatform({TestPlatform.JAVASCRIPT, TestPlatform.WEBASSEMBLY_GC})
@EachTestCompiledSeparately
public class CallTest {
@Test
Expand Down

0 comments on commit 1d47146

Please sign in to comment.