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 { } else {
return 0; 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", for (let name of ["wrapByte", "wrapShort", "wrapChar", "wrapInt", "wrapFloat", "wrapDouble", "unwrapByte",
"unwrapShort", "unwrapChar", "unwrapInt", "unwrapFloat", "unwrapDouble"]) { "unwrapShort", "unwrapChar", "unwrapInt", "unwrapFloat", "unwrapDouble"]) {
@ -308,6 +317,7 @@ TeaVM.wasm = function() {
imports.teavmJso["callMethod" + i] = (instance, method, ...args) => imports.teavmJso["callMethod" + i] = (instance, method, ...args) =>
instance !== null ? instance[method](...args) : getGlobalName(method)(...args); instance !== null ? instance[method](...args) : getGlobalName(method)(...args);
imports.teavmJso["construct" + i] = (constructor, ...args) => new constructor(...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) @InjectedBy(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "concatArray", module = "teavmJso")
public static native JSObject concatArray(JSObject a, JSObject b); public static native JSObject concatArray(JSObject a, JSObject b);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@ -562,76 +563,90 @@ public final class JS {
JSObject l, JSObject m); JSObject l, JSObject m);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@Import(name = "apply", module = "teavmJso")
public static native JSObject apply(JSObject instance, JSObject method, JSArray<JSObject> v); public static native JSObject apply(JSObject instance, JSObject method, JSArray<JSObject> v);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf1", module = "teavmJso")
public static native JSObject arrayOf(JSObject a); public static native JSObject arrayOf(JSObject a);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf2", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b); public static native JSObject arrayOf(JSObject a, JSObject b);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf3", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c); public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf4", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d); public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf5", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e); public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf6", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f); public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf7", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g); JSObject g);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf8", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h); JSObject g, JSObject h);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(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, public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f,
JSObject g, JSObject h, JSObject i); JSObject g, JSObject h, JSObject i);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf10", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, 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 g, JSObject h, JSObject i, JSObject j);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf11", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, 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 g, JSObject h, JSObject i, JSObject j, JSObject k);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf12", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, 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 g, JSObject h, JSObject i, JSObject j, JSObject k, JSObject l);
@InjectedBy(JSNativeInjector.class) @InjectedBy(JSNativeInjector.class)
@PluggableDependency(JSNativeInjector.class) @PluggableDependency(JSNativeInjector.class)
@NoSideEffects @NoSideEffects
@Import(name = "arrayOf13", module = "teavmJso")
public static native JSObject arrayOf(JSObject a, JSObject b, JSObject c, JSObject d, JSObject e, JSObject f, 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); 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 arg = invoke.getArguments().get(i);
var byRef = byRefParams[i]; var byRef = byRefParams[i];
if (vararg && i == invoke.getArguments().size() - 1 if (vararg && i == invoke.getArguments().size() - 1
&& typeHelper.isSupportedByRefType(method.parameterType(i))) { && typeHelper.isSupportedByRefType(method.parameterType(i))
&& !wasmGC) {
byRef = true; byRef = true;
} }
arg = marshaller.wrapArgument(callLocation, arg, arg = marshaller.wrapArgument(callLocation, arg,

View File

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