Skip to content

Exception when passing a callback as parameter #59

@kamphaus

Description

@kamphaus

Describe the bug

Trying to call a WASM method passing a callback parameter fails with a RuntimeException.

Steps to reproduce

The following Rust code builds the WASM code passed to Instance:

#[no_mangle]
pub extern fn greet(print: extern fn(u8)) {
    print(42)
}

The following Java code fails when trying to call the greet method:

Instance instance = new Instance(wasm);
Consumer<Object> print = (Object o) -> System.out.println("Printed: " + ((Integer) o));
instance.exports.getFunction("greet").apply(print);
instance.close();

Actual behavior

When run, this fails with the following exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.RuntimeException: Failed to convert the argument 0nth of `greet` into a WebAssembly value.
	at org.wasmer.Instance.nativeCallExportedFunction(Native Method)
	at org.wasmer.Exports.lambda$null$0(Exports.java:85)
	at org.example.wasmhost.WasmHostApplication.main(WasmHostApplication.java:45)
	... 8 more

Additional context

My Java version is:

$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)

Metadata

Metadata

Assignees

Labels

❓ questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions