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

Why does emscripten.Instantiate and InstantiateForModule differ drastically? #1781

Closed
DejeroDaniel opened this issue Oct 14, 2023 · 5 comments

Comments

@DejeroDaniel
Copy link

One imports all the host functions needed, and the other barely imports anything. The documentation point you towards Instantiate, which won't work for any C++ module requiring exception support, for example.

Why don't they both set the same emscription host functions?

@ncruces
Copy link
Collaborator

ncruces commented Oct 15, 2023

Maybe @jerbob92 (the creator of wazero-emscripten-embind and our resident emscripten expert) can help?

@jerbob92
Copy link
Contributor

jerbob92 commented Oct 15, 2023

That's because Emscripten dynamically registers host functions as needed, which are called invoke_xxx, where xxx is the function signature. Wazero needs to know which dynamically registered host functions are imported by the module so that it can generate the function as well (https://github.com/tetratelabs/wazero/blob/main/internal/emscripten/emscripten.go#L43). If Wazero wouldn't do this, it would need to register any combination possible, which is not feasible.

I suspect Instantiate is left in to not break external code, but it's probably not very usable in most cases, so I would always go withInstantiateForModule.

Please note that real C++ exceptions is not supported by Wazero yet, I have been building that here:
https://github.com/jerbob92/wazero/blob/feature/implement-emscripten-cpp-exceptions/internal/emscripten/exceptions.go

And it does work, but I don't like the implementation so I'm still waiting for a response on #1654 to make a PR.

@Danlock
Copy link
Contributor

Danlock commented Oct 15, 2023

OK. Should I make a PR to change the documentation to use InstantiateForModule (since it sounds like Instantiate should be deprecated)?

@Danlock
Copy link
Contributor

Danlock commented Oct 15, 2023

Also as an aside, I'm interested in using wazero to run Tesseract, compiled to WASM via Emscripten.
The Tesseract WASM emscripten imports are minimal enough that I'd figure I could try to implement them myself. (env._emscripten_fs_load_embedded_files and env.system)

Do you think this project is feasible with the current support of emscripten in wazero?

@jerbob92
Copy link
Contributor

OK. Should I make a PR to change the documentation to use InstantiateForModule (since it sounds like Instantiate should be deprecated)?

Yeah, sounds good!

Also as an aside, I'm interested in using wazero to run Tesseract, compiled to WASM via Emscripten. The Tesseract WASM emscripten imports are minimal enough that I'd figure I could try to implement them myself. (env._emscripten_fs_load_embedded_files and env.system)

Do you think this project is feasible with the current support of emscripten in wazero?

I don't know what these imports are, but generally you would need to recompile for a standalone WASM file, then you will have way less emscripten specific imports. I use this branch to compile my stuff so that it uses WASI for things like filesystem operations: emscripten-core/emscripten#18285

So it is feasible, but you might need to do some special steps to get there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants