-
Notifications
You must be signed in to change notification settings - Fork 285
Support GOOS=js GOARCH=wasm generated code #432
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
Comments
PS the reason we don't currently add an API for non-exported functions as the spec hints you should avail only exported ones. There's probably a decent security reason for not breaking that seal. OTOH, the only callers who could access a non-exported function (outside the module) would be host functions. We could avail the api and say "use at your own risk" or add a flag on the runtime that disables anything from ever returning from |
Maybe wait for golang/go#25612 and golang/go#42372? I think the major blocker is that it doesn't support WASI and doesn't have option to export functions. One of the comments arround calling functions is: "If I call hello_world from the WebAssembly host, on which goroutine will it run?" The go compiler also requires alot of imported functions (which is defined on https://github.com/golang/go/blob/3a19102de3435a3823126e74cf42c67037ff1890/misc/wasm/wasm_exec.js#L208-L454), the code doesn't run without those functions and it's passing the stack-pointer to the function. Golang can import functions using the magical assembly instruction |
@inkeliz thanks for doing the research. there's no current reason to rush before the issues you mentioned! |
We could add a special-cased option to CompileConfig which can understand Go's naming convention and make exports as if it was done correctly in the first place. Ex taking an expression like this: "github.com_wuhuizuo_go_wasm_go_provider_native.Fibonacci" and making an export "Fibonacci". |
It will also require some custom module, to replace |
https://github.com/prep/wasmexec this lib closes this issue maybe? |
Hey there! I'm the author of ☝️. I'm still tinkering with that package, but it is basically working with the small tests I've been running locally. As per @codefromthecrypt's original question: That isn't working right now, but I've already got it working in a local branch. For those who are interested, what happens under the hood is:
The I'm struggling a bit to make it into a nice API for the user. It's currently just a That said, my plan is to create a faux-waPC API for |
I wanted to chime in as things develop. Firstly, I really appreciate the hints @inkeliz gave including the "no compatibility" promise from go. History has told me that blocking on any specific Go issue to complete can be tantamount to never doing anything. Since folks here and in competing runtimes are doing things, we have stake in this, and also it makes no sense for the go wasm runtime to not support go ;) This doesn't remove the issues like compatibility risk, though we are starting from a later version of Go anyway. For example, our minimum is 1.17, so we don't need to think about past incompat, just knowing it can happen again. When we opened this issue, it wasn't that long ago, but also we are much more mature for the age. We can figure out how to do this without a lot of liability. In both looking at go impl, as well its wiki I've noticed the following things: Exporting of functions is different than working with go generated wasm in generalYou can today use "run" to kick off wasm similar to a WASI command with "_start'. Tracking of exports is a separate topic and can progress independently. The go function imports should be two modules, but it is packed into one "go".We have the ability to rename imports now, so we can actually create two implementation modules for the "go" module imported by go-generated wasm. ex.
|
I've just pushed some changes to @codefromthecrypt as for porting things over, I'd be happy to help where I can. I think there is value in keeping |
@prep sounds good. FYI to test portability of the sys interfaces needed (ex nanotime etc), I started porting the exec_wasm.js yesterday. I'll keep going on this and once done compile a list of suggestions and/or PR to your repo. You can ping me on yours when you like also. |
@cherrymui @aclements I think most of the ecosystem are stumped about the purposed of the "go" "debug" function import when targeting |
On a somewhat related topic, the current implementation requires us to implement the runtime.getRandomData import while at the same time needing I wonder if it's too cheeky to do something like this in //go:linkname getRandomData runtime.getRandomData
func getRandomData(r []byte) To funnel those requests through the same import. I'm unsure if this works, considering |
FYI the host side implemented here needn't do any goroutine safely, and really should highly discourage anyone from trying to do that. It may seem odd because Go makes some of the _js files use mutexes. For example, you might think because function wrappers are implemented with locks, then so should the host side. However, it is misleading because the atomic primitives which would implement the mutexes are left a TODO. This means there's no safety provided in practice, and function wrappers if allocated concurrently would have unpredictable results. In summary, go 1.19 won't be able to do concurrency in its wasm, so there's no point cluttering the code with anything that fakes as if it could. If this every changes, we can adjust after that. |
#621 should solve this |
I was looking at @wuhuizuo's test which uses this source and compiles it like
GOOS=js GOARCH=wasm go build -o wasm.wasm
https://github.com/wuhuizuo/go-wasm-go/blob/main/provider/wasm-go/main.go
The resulting wasm isn't exporting those functions. "run" "resume" "getsp" and "mem" are externalized.
You can identify, for example "Fibonacci" if you use the custom name section and get "github.com_wuhuizuo_go_wasm_go_provider_native.Fibonacci". However, again that function is neither the short name, nor is it exported. There's probably some approach to getting the function prefix somewhere
If we want to support this tech we'd need to:
I'm not sure how many use wasm produced by go vs tinygo, but raising it as I noticed there's no way to lookup these functions and use them, yet. We probably need some 👍 and people to say why/how they are using this, so that we don't displace time on things only used for demo projects.
The text was updated successfully, but these errors were encountered: