|
39 | 39 | // |
40 | 40 | // __attribute__((import_name("foo"))) int foo(int x, int y); |
41 | 41 | // |
| 42 | +// __attribute__((used)) static void* __em_js_ref_foo = &foo; |
| 43 | +// |
42 | 44 | // __attribute__((used, visibility("default"))) |
43 | 45 | // char __em_js__foo[] = "(int x, int y)<::>{ return 2 * x + y; }"; |
44 | 46 | // |
|
50 | 52 | // We use <::> to separate the arguments from the function body because it isn't |
51 | 53 | // valid anywhere in a C function declaration. |
52 | 54 |
|
| 55 | +// The __em_js_ref_foo pointer simply exists in order to force a reference to |
| 56 | +// `foo` to exist in the object file, even if there are no other local uses. |
| 57 | +// This means the linker will always use the import_name attribute for this |
| 58 | +// function even if it is not locally used. |
| 59 | + |
53 | 60 | // Generated __em_js__-prefixed symbols are read by binaryen, and the string |
54 | 61 | // data is extracted into the Emscripten metadata dictionary under the |
55 | 62 | // "emJsFuncs" key. emJsFuncs itself is a dictionary where the keys are function |
|
59 | 66 | // emJsFuncs metadata is read in emscripten.py's create_em_js, which creates an |
60 | 67 | // array of JS function strings to be included in the JS output. |
61 | 68 |
|
62 | | -#define _EM_JS(ret, c_name, js_name, params, code) \ |
63 | | - _EM_JS_CPP_BEGIN \ |
64 | | - ret c_name params EM_IMPORT(js_name); \ |
65 | | - EMSCRIPTEN_KEEPALIVE \ |
66 | | - __attribute__((section("em_js"), aligned(1))) char __em_js__##js_name[] = \ |
67 | | - #params "<::>" code; \ |
| 69 | +#define _EM_JS(ret, c_name, js_name, params, code) \ |
| 70 | + _EM_JS_CPP_BEGIN \ |
| 71 | + ret c_name params EM_IMPORT(js_name); \ |
| 72 | + __attribute__((used)) static void* __em_js_ref_##c_name = &c_name; \ |
| 73 | + EMSCRIPTEN_KEEPALIVE \ |
| 74 | + __attribute__((section("em_js"), aligned(1))) char __em_js__##js_name[] = \ |
| 75 | + #params "<::>" code; \ |
68 | 76 | _EM_JS_CPP_END |
69 | 77 |
|
70 | 78 | #define EM_JS(ret, name, params, ...) _EM_JS(ret, name, name, params, #__VA_ARGS__) |
|
0 commit comments