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

Enable a runtime function without exporting it to Module #21920

Open
mzoliker opened this issue May 9, 2024 · 5 comments
Open

Enable a runtime function without exporting it to Module #21920

mzoliker opened this issue May 9, 2024 · 5 comments

Comments

@mzoliker
Copy link

mzoliker commented May 9, 2024

Hi! This one should be easy but I have not found an elegant way to solve it. Using emscripten v3.1.59.
I have a JS function which is called from C, this JS function uses the stringToNewUTF8() library function to return a string to the C program.
This JS function is added via —pre-js (also tried with —post-js).
Basically, I want the stringToNewUTF8 function to be defined, but not exported to Module.
Using -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 make things work but now I get stringToNewUTF8 defined on Module, which is not useful.

What is the best way to do this?

Please let me know if you need additional information on this use case.

Thanks a lot for your help!

Kind regards,
Maurice

@sbc100
Copy link
Collaborator

sbc100 commented May 9, 2024

The short answer is that because stringToNewUTF8 is actually a JS library function you can use -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$stringToNewUTF8. (You might need to escape the $ if you are typing that in a shell).

The long answer is that if your JS function is called from C it should probably part of the --js-library file in which case you can add a __deps attribute to that function that contains ($stringToNewUTF8). In general its not possible to call JS functions that are defined in --pre-js or --post-js directly from C code.. how is it that you are doing that? Are you building with -sERROR_ON_DEFINED_SYMBOLS=0 perhaps?

@mzoliker
Copy link
Author

mzoliker commented May 9, 2024

Thanks a lot, it works as I expected now :-). I had already tried -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE but it didn’t work probably because of the missing $, which by the way had to be escaped this way \$$stringToNewUTF8 because I am using make.

@sbc100
Copy link
Collaborator

sbc100 commented May 9, 2024

How are you calling a function from --post-js from your native code? In general, native code shouldn't be able to call those symbols. (Are you building with -sERROR_ON_DEFINED_SYMBOLS=0 perhaps?)

@mzoliker
Copy link
Author

mzoliker commented May 9, 2024

And to answer your question, no I am not building with -sERROR_ON_DEFINED_SYMBOLS=0.

In —pre-js (or —post-js), I am just adding to Module the functions that I want to execute from C. And running them from C with EM_ASM({ return Module.functionName(); }).

This is not ideal though, so I am interested in investigating your solution, which would be a lot cleaner.

Do you have a link to the documentation of this __deps attribute? Thanks a lot!

@sbc100
Copy link
Collaborator

sbc100 commented May 9, 2024

Oh .. I see, you not actually calling the functions from C, you calling them from inline JS inside of C. In that case two things.

(1) You can probably just skip the Module prefix and define and call functionName directly.

(2) You can embed your dependencies directly in the C source code using EM_JS_DEPS(deps, "$stringToNewUTF8")

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