emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Enable a runtime function without exporting it to Module

Open mzoliker opened this issue 9 months ago • 5 comments

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

mzoliker avatar May 09 '24 14:05 mzoliker

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?

sbc100 avatar May 09 '24 14:05 sbc100

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.

mzoliker avatar May 09 '24 15:05 mzoliker

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?)

sbc100 avatar May 09 '24 15:05 sbc100

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!

mzoliker avatar May 09 '24 15:05 mzoliker

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")

sbc100 avatar May 09 '24 16:05 sbc100