Search code examples
ioreturnreturn-valuewebassemblyreturn-type

What can WebAssembly return?


As I understand it, there are three ways for a WebAssembly instance to return values to the external caller:

  • returning a value directly as the result of a method call (is this only a single value of the basic datatypes {i32, i64, f32, f64}?)

  • calling an imported function with arguments, this function can store the arguments or access the memory while it is called and save the contents somewhere else

  • writing to a memory and have the external environment access it after execution

Are there more? What are the details?


Solution

  • Your assessment is roughly correct. Functions can either return a WebAssembly value (i32, i64, f32, f64) directly or return data indirectly via modifying elements in the global environment such as Memory, Table and Globals. If you are talking about returns data to a caller outside of the WebAssembly module than any such elemennts would themselves need to to be exported from he module. i.e. you can't use the WebAssembly memory to indirectly return data unless he memory itself is also exports.

    With the multi-value proposal you will also be able to directly return more than one value: https://github.com/WebAssembly/multi-value. Likewise the GC proposal will allow you to directly return complex structures. But neither of those is available today.