Skip to content

Commit

Permalink
dynlib keeps exported functions alive in emscripten (#23469)
Browse files Browse the repository at this point in the history
ref https://forum.nim-lang.org/t/11338

ref https://github.com/beef331/wasm3/blob/master/src/wasm3/exporter.nim

ref emscripten-core/emscripten#6233

ref
https://github.com/emscripten-core/emscripten/blob/3.1.56/system/include/emscripten/em_macros.h#L10

`EMSCRIPTEN_KEEPALIVE` is a macro that is used to prevent unused
functions from being deadcode eliminated in emscripten, which is a
simple wrapper around `__attribute__((used))`. This PR follows suits and
expects dynlib is supposed to keep these functions alive. In the future,
`exportwasm` might be introduced.

After this PR, a function with `{.dynlib, exportc.}` can be reused from
other wasm programs reliably.
  • Loading branch information
ringabout committed Apr 2, 2024
1 parent 32fa7e2 commit a1602b0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/nimbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ __GNUC__
__TINYC__
__clang__
__AVR__
__EMSCRIPTEN__
*/


Expand Down Expand Up @@ -177,12 +178,13 @@ __AVR__
# define N_THISCALL_PTR(rettype, name) rettype (__thiscall *name)
# define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)

# ifdef __cplusplus
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
# ifdef __EMSCRIPTEN__
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport) __attribute__((used))
# define N_LIB_EXPORT_VAR __declspec(dllexport) __attribute__((used))
# else
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
# define N_LIB_EXPORT_VAR __declspec(dllexport)
# endif
# define N_LIB_EXPORT_VAR __declspec(dllexport)
# define N_LIB_IMPORT extern __declspec(dllimport)
#else
# define N_LIB_PRIVATE __attribute__((visibility("hidden")))
Expand Down Expand Up @@ -211,8 +213,13 @@ __AVR__
# define N_FASTCALL_PTR(rettype, name) rettype (*name)
# define N_SAFECALL_PTR(rettype, name) rettype (*name)
# endif
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
# define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
# ifdef __EMSCRIPTEN__
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default"), used))
# define N_LIB_EXPORT_VAR __attribute__((visibility("default"), used))
# else
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
# define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
# endif
# define N_LIB_IMPORT extern
#endif

Expand Down

0 comments on commit a1602b0

Please sign in to comment.