Skip to content
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

Have one wasi initialization method supporting _start, _initialize, or neither #44995

Closed
anuraaga opened this issue Oct 14, 2022 · 2 comments
Closed
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@anuraaga
Copy link

anuraaga commented Oct 14, 2022

What is the problem this feature will solve?

Currently, there are two methods in wasi.js for initializaing, start(instance) and initialize(instance). The former requires the wasm guest to have a _start method and no _initialize, the latter will use _initialize if present and will fail if _start is present.

The wasi library is initializing the host side that is imported by a wasm guest. Because the guest may be compiled in any language targeting wasm, there actually isn't much control by the host on what initialization method gets included in the final binary. This means that for a host supporting general wasm binaries, there is this required boilerplate for initialization

if (exports['_start']) {
  wasi.start(instance);
} else {
  wasi.initialize(instance);
}

I think it would be more user friendly with just one way to initialize. Notably, this is to prevent the bug of calling neither method - wasi will not function if it is not initialized to wire up Memory from the guest. Anecdotally, my first experience with this API was using binaries without _start or _initialize, so I didn't call either, missing the fact that wasi.initialize is appropriate and required to be called in this case for Memory.

What is the feature you are proposing to solve the problem?

Remove wasi.start() and only have wasi.initialize(). The behavior would be

  • Wire up Memory
  • If both _start and _initialize are defined, throw an error
  • If _start is defined, call it
  • If _initialize is defined, call it

Notably, if neither is defined, it will still wire up Memory and function correctly. I'm not aware of any potential ambiguous case from having only this one entry point.

If the approach seems reasonable, happy to send a PR for it.

What alternatives have you considered?

No response

@anuraaga anuraaga added the feature request Issues that request new features to be added to Node.js. label Oct 14, 2022
@devsnek
Copy link
Member

devsnek commented Oct 14, 2022

This is currently following how WASI has been specified: https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md. Note there are some issue links at the bottom of that document regarding this, but for now that's how it is.

@anuraaga
Copy link
Author

Thanks for the link @devsnek, I had missed the point about the exit code in _start which is another difference between the two. Unfortunately compilers like TinyGo and maybe others do use _start even for reactors, but perhaps then the boiler plate is the only way to handle this. Hopefully the spec will make this simpler in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

2 participants