-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WASM: strange behavior when multi-value is used #2512
Comments
While multi-value returns are supported by WASM, they are not supported by the WASM C ABI. They are represented as a struct. As for the parameter, what is happening there is that the WASM C ABI passes the return value indirectly. In other words, the underlying function type is effectively As for the References: |
Thank you, that supplies some important pieces to this puzzle. Since this function is being exported to the browser, it needs to be callable by Javascript. It sounds like an important next step in research would be to understand what exactly is expected to be passed by Javascript to the function. (It would be the equivalent of 'a pointer to a struct containing places for two integers', but I'm not sure off-hand how to represent that in Javascript. It might be as simple as an Int32Array.) That being said, the function doesn't look like it actually does the intended work of returning two integers. How is that getting lost? |
It does return 2 values through the pointer, but it does so with a single store. If you take the i32 values and concatenate them ( |
Oh thank you, I missed that! Now that we have a little more information, I was able to get this working fine. For posterity: If you have a Go function like this:
And we use these settings for
TinyGo will compile that to the equivalent WASM of the following WAT function:
Basically the function is looking for an address in its linear memory in which to put the function result. It is expecting the embedder to know three things:
And if you want to use those values from the embedding Javascript, all you need to do is access the embedded linear memory. (Made accessible elsewhere in the WAT/WASM via The way to call The way to call
Result from the console should be Update 1/13/21: Added a few comments to clarify code |
This seems like the sort of thing that ought to be documented somewhere obvious for future reference. |
The way that Go code looks and the way it is represented in WebAssembly are two very different things. They happen to match in many cases, but in many other cases they don't. For example, Go (and TinyGo) support We could document this. If we do that, this would be the most appropriate place: https://tinygo.org/docs/concepts/compiler-internals/calling-convention/ |
dev branch
Go supports the use of multiple results. So does WebAssembly.
If I write a simple Go function that returns multiple values:
Ideally that would result in the WASM equivalent of this WAT:
Instead the following is generated:
Basically it seems to:
My guess is that TinyGo does not currently support multiple results, but the documentation implies to me that it is supported. (It says that 'all basic types and all regular control flow' is supported. I think of multiple function results as a basic type.)
I turned on debug information, and the following resulted:
The text was updated successfully, but these errors were encountered: