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

Help compiling and executing functions #23350

Open
NaitorStudios opened this issue Jan 9, 2025 · 5 comments
Open

Help compiling and executing functions #23350

NaitorStudios opened this issue Jan 9, 2025 · 5 comments

Comments

@NaitorStudios
Copy link

Hi, I've been trying to compile a cpp into wasm, in this case using SINGLE_FILE option.
It generates a JS file that loads the WASM from a BASE64 string, which is what I want.
Since my project can only have a single JS file, I've copied and pasted the Emscripten js code at the top of my main js file.
But I have no clue on how to call the C/C++ functions from it, of if I'm even getting the code to execute at all...

Can you guys please help me with a basic example?
I can't find anything on the subject on the internet, information about it is very escarce...

@sbc100
Copy link
Collaborator

sbc100 commented Jan 9, 2025

There are docs on the various different ways you can interact with your module here: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html.

The most simple ways is to simple call your exported functions directly via e.g. Module._myfunc(..). However, there are several higher level ways to interact.

@NaitorStudios
Copy link
Author

NaitorStudios commented Jan 11, 2025

I got it working, but I've noticed something weird about it...
The first exported function appears to call the wasm function directly, while the other functions calls the function from glue code, which calls the wasm function...
I've worked around this by wrapping the first function to a JS var using cwrap, but this is far from optimal...
Why does this happen? Why can't it all call either the function directly from wasm or from js?
I feel like I would need to add a dummy first function to get them all working the same way...

Edit: Seems like it's not the first function, but one specifically, for no apparent reason...

image

image

And here's if I wrap it with cwrap:
image

@NaitorStudios
Copy link
Author

NaitorStudios commented Jan 11, 2025

In code, after formatting it, I noticed the indentation:
image

Does that mean anything?
I think it might just be something from the beautifier...

@sbc100
Copy link
Collaborator

sbc100 commented Jan 13, 2025

This is because the wasm module is instantiated asynchronously. So at that point where the code above is run the exports (in your case Module['asm'] are not yet available. We create these little wrapper functions so that if you happen to take a reference to them before the module is ready that function will work once that module is ready.

However you need to be carefull not to try to call any of these function until the module is actually ready. See https://emscripten.org/docs/api_reference/module.html#Module.onRuntimeInitialized.

You can also opt out of async compilation in some cases if you prefer: https://emscripten.org/docs/tools_reference/settings_reference.html#wasm-async-compilation

BTW, f you want to ready the generated output you can can link without optimizations or add --minify=0 to the optimized build.

(BTW, when filing bugs like this its easier for us if you can copy and paste the text into github rather than attaching screenshots)

@sbc100
Copy link
Collaborator

sbc100 commented Jan 13, 2025

Also, what version of emscripten are you using? Module['asm'] was removed quite a long time ago I think..

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

2 participants