-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
syscall/js: Make syscall/js optional when compiling wasm? #27766
Comments
I have investigated creating a custom GOOS in the past, but it fails with that attached gnarly linker error. Guidance on it would be helpful, as implementing this would unblock the use of Go for my server side webassembly things. |
Loosely related to #25612 and github.com/go-interpreter/wagon |
Little to add except that I also have use cases for Go-to-wasm that don't involve a browser, and I'd be happy to chat about them to anyone interested in the issue. |
If you don't have Today, I would implement the backend API of |
@Xe @peterbourgon Probably best for you to jump in here. 😄 |
@neelance for now I have been using https://github.com/CommonWA/cwa-spec as an imaginary system ABI. What calls would we need to add to this for Go? The time call is one of them. |
Is there any effort to get https://github.com/CommonWA/cwa-spec adopted by https://github.com/WebAssembly, maybe as an official spec? |
CC @losfair |
Essentially I want to punt on this question. I want to compile Go down to a WebAssembly module, exporting a set of functions as a sort of public API, and binding to or declaring (somehow) a set of import functions, to consume other modules. I want to load and run this module in a server-side environment, with no relation to JS. CommonWA is not of much interest to me (us) in this context. |
Would you want to call the functions concurrently or only one at a time? |
@neelance I wouldn't take anything off the table in terms of functionality. If you're looking for some kind of examples, I can try to dig something up; let me know. edit: To be clear, I would want to be able to answer each of those questions differently, in different modules. |
@peterbourgon in this case using CommonWA (note: I am not married to CommonWA, it's just not something made by me and looks like it could easily be the basis we could build off of as a community to make it better for everyone) would us all just be standardizing on the set of things the binaries the Go compiler emits would be depending on. These things would then be implemented by the JS side of things too. |
Please do, they'd likely help. 😄 |
I have a similar use case to @peterbourgon in mind. Wasm on the server creates the opportunity for new types of features, such as user developed, server executed extensions to products. I'd love to use Go for these sorts of things! |
Sorry, but I still don't see why |
What can non-JavaScript hosts do to stub it out or "break" all of its functionality? |
@Xe I don't get your question. |
@neelance What is the minimal implementation required for every function to have to return an error-like response? |
@Xe That of course depends on the function. I would just start with an empty stub of each function which logs its execution and then add the implementation step by step. |
Yes, can you please provide these empty stubs? I can't figure them out on my own apparently. |
I don't even know which WebAssembly host you are trying to use. |
The import functions that you need to stub are the ones you find in Line 199 in 7a6ccec
|
For all of these functions: https://github.com/golang/go/blob/master/src/syscall/js/js_js.s
These are things that I must know to implement these calls and have not been able to reverse-engineer on my own. |
It's the normal Go calling convention. All arguments and return values live on the stack. You can find the function signatures in https://github.com/golang/go/blob/master/src/html/template/js.go and in the wasm_exec.js I linked above. You can also find the stack offsets in the wasm_exec.js if you don't want to calculate them on your own. A quick Google search got me this: https://science.raphael.poss.name/go-calling-convention-x86-64.html |
C has a different calling convention than Go, so you won't be able to use simple C function signatures. |
I realize the calling convention is different, I would translate that by hand. I want to just know what i should return for the values such that it will get error-like responses, consider The stack layout in relative ordinal bytes is something like this:
There are no arguments to this function and there is one int64 return that signifies an integer number. The zero return in this function is the value zero, as such: func runtime.nanotime() int64 {
return 0
} The idea is that this kind of basic documentation about what the ABI expects (and what values are zero-like) will provide people who want to use Go in WebAssembly on the server side guidance on how to stub out functionality that they don't want to support (for example, the very reasonable case of being not a browser and not having a javascript VM involved, so Javascript interaction is not needed and thus removable). Can you please help? |
@Xe Sorry, I don't have that right now. The |
Then if it shouldn't return 0 all the time, what is the valid thing to do? |
Fulfill the interface good enough that it supports the actions that you want to support or that it at least does not crash on initialization. Specifics depend on the case. |
This still holds. I don't see how I can give you more information without implementing it myself. There is no specification that you can fulfill in one go. You'll have to start with some concrete example that you want to make pass and then we can figure out together what is necessary to do so. |
I've written a proof of concept since so many people were asking for it: https://github.com/neelance/go_js_wasm_exec |
Thanks heaps @neelance. 😄 |
Exploring options for supporting different types of IO in neelance/go_js_wasm_exec#1. |
There is initial work being done on a CloudABI target at NuxiNL/go. This is the system ABI the CraneStation/wasmtime runtime plans to use. |
This is probably relevant as well: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ |
Awesome! This is exactly what I was waiting for. I'll look into it soon. |
Does this mean we'll have a |
@neelance It was suggested to me that anyone working on wasi for Go might want to join #wasi on the Mozilla IRC—just passing that info along. |
#31105 sounds related too. 😄 |
I believe for execution Go doesn't need any environment support. Even random seed and time can be hardcoded and it is fine for a lot of use cases. Forcing every host to provide a dummy implementation of WASI or syscall/js sounds very artificial. BTW when Rust and C compile to wasi they don't have any required imports. Ideally I would love to have the compiler options to not include some Go standard library components at all. For example I would prefer that in some cases the code that references any file or socket IO wouldn't even compile. |
If there is no environment, then your Go code can calculate something, but then only discard the result.
Neither Rust nor C has a runtime/goroutines. You can directly call a Rust/C function from your WebAssembly host. This is not easily possible with Go's semantics. |
TinyGo does not have this issue as they do not assume a JS host environment. |
This issue is titled "make syscall/js optional" while #31105 is instead to assume wasi. For wasi, #31105 (comment) describes a way to use Go with it. |
Not sure personally. I don't use Go for wasm any longer, so no longer have any err... skin in this issue. If other people do, I'll close or leave this open as seems appropriate. 😄 |
At present, when we compile to wasm
syscall/js
is automatically included.This forces the runtime to either be a browser (eg Firefox, Chrome, etc), or at least pretend to be one.
That's kind of non-optimal, as some of the wasm execution environments presently being developed aren't targeted at browser environments. eg:
In the WebAssembly Gophers Slack channel we have people asking about non-browser use cases fairly often. It seems likely that'll be a fairly standard use case, if it can be catered to as well.
How feasible would it be to have some way to suppress the default
syscall/js
inclusion, or otherwise make it optional?The text was updated successfully, but these errors were encountered: