Return mutliple values from a wasm function

1.2k views Asked by At

I have a Golang TCP server running locally and connected to a wasm module. Currently, I can return one value from a wasm function. Through this blog, I noticed that there should be possible to return multiple values rather than one. However, in my Go TCP server, I do not get any response from the wasm function.

This is how I did it for returning one value, and it works fine:

// Rust code for wasm modules
#[no_mangle]
pub extern "C" fn echo(ptr: *mut u8, length: usize) -> *mut u8 {
    ...
    let newptr = get_ptr(); 
    newptr
}

In the Go server:

// Go code to connect to wasm modules and get access to the returned value
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
linker := wasmtime.NewLinker(store)
...
newPtr, err := server.funcs["echo"].Call(server.ptr, int32(len(recivedBytes)))
check(err)
ptr := newPtr.(int32)

The following is how I try to return multiple values from the wasm function:

// Rust code for wasm modules
#[no_mangle]
pub extern "C" fn echo(ptr: *mut u8, length: usize) -> (*mut u8, i32) {
    ...
    let newptr = get_ptr(); 
    let newlength = get_length();
    (newptr, newlength)
}

In the Go server I have added the following line of code:

    wasmtime.NewConfig().SetWasmMultiValue(true)

And the rest is the same as before:

/* Go code to connect to wasm modules and get access to the returned value */
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
linker := wasmtime.NewLinker(store)
...
result, err := server.funcs["echo"].Call(server.ptr, int32(len(recivedBytes)))
check(err)
res := result.([]wasmtime.Val)

I compile the wasm modules using the cargo wasi build command, and it compiles fine. But when sending a request to the Go server, which talks to the wasm modules, I get an error that tells got 2 expected 3 arguments. I do not understand what should the third argument be.

0

There are 0 answers