wasm gc: support JS functions with varargs

This commit is contained in:
Alexey Andreev 2024-10-04 10:02:06 +02:00
parent a5212fb9ca
commit 1d47146f43
4 changed files with 29 additions and 3 deletions

View File

@ -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"]) {
@ -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
}
}

View File

@ -39,6 +39,7 @@ public final class JS {
@InjectedBy(JSNativeInjector.class)
@NoSideEffects
@Import(name = "concatArray", module = "teavmJso")
public static native JSObject concatArray(JSObject a, JSObject b);
@InjectedBy(JSNativeInjector.class)
@ -562,76 +563,90 @@ public final class JS {
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);

View File

@ -1088,7 +1088,8 @@ class JSClassProcessor {
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,

View File

@ -31,7 +31,7 @@ import org.testng.annotations.Test;
@RunWith(TeaVMTestRunner.class)
@SkipJVM
@OnlyPlatform(TestPlatform.JAVASCRIPT)
@OnlyPlatform({TestPlatform.JAVASCRIPT, TestPlatform.WEBASSEMBLY_GC})
@EachTestCompiledSeparately
public class CallTest {
@Test