Use `__attribute__(export_name)` over `EMSCRIPTEN_KEEPALIVE` in `EM_JS` macro The `EMSCRIPTEN_KEEPALIVE` macro relies on `attribute((used))` which works slightly differently under emscripten (`wasm32-unknown-emscripten`) compared to other triples. This change avoids `EMSCRIPTEN_KEEPALIVE` here in favor of the `export_name` attribute which is more precise and correct although not (yet) suitable for us in `EMSCRIPTEN_KEEPALIVE` itself (We would need something like https://reviews.llvm.org/D76547 to make that work).
diff --git a/system/include/emscripten/em_js.h b/system/include/emscripten/em_js.h index ef7c502..f21a0c3 100644 --- a/system/include/emscripten/em_js.h +++ b/system/include/emscripten/em_js.h
@@ -37,7 +37,7 @@ // // __attribute__((import_name("foo"))) int foo(int x, int y); // -// __attribute__((used, visibility("default"))) +// __attribute__((export_name("__em_js__foo")), (section("em_js"))) // char __em_js__foo[] = "(int x, int y)<::>{ return 2 * x + y; }"; // // We pack the arguments and function body into a constant string so it's @@ -57,12 +57,12 @@ // emJsFuncs metadata is read in emscripten.py's create_em_js, which creates an // array of JS function strings to be included in the JS output. -#define _EM_JS(ret, c_name, js_name, params, code) \ - _EM_JS_CPP_BEGIN \ - ret c_name params EM_IMPORT(js_name); \ - EMSCRIPTEN_KEEPALIVE \ - __attribute__((section("em_js"), aligned(1))) char __em_js__##js_name[] = \ - #params "<::>" code; \ +#define _EM_JS(ret, c_name, js_name, params, code) \ + _EM_JS_CPP_BEGIN \ + ret c_name params EM_IMPORT(js_name); \ + __attribute__((export_name("__em_js__##js_name"), \ + section("em_js"), \ + aligned(1))) char __em_js__##js_name[] = #params "<::>" code; \ _EM_JS_CPP_END #define EM_JS(ret, name, params, ...) _EM_JS(ret, name, name, params, #__VA_ARGS__)