Skip to content

Fix data address exports under -sMAIN_MODULE=1 #23020

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

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4435,6 +4435,27 @@ def test_dylink_global_var(self):
int x = 123;
''', expected=['extern is 123.\n'], force_c=True)

@needs_dylink
def test_dylink_global_var_export(self):
self.do_run(r'''
#include <assert.h>
#include <stdio.h>
#include <emscripten.h>
#include <emscripten/em_asm.h>

EMSCRIPTEN_KEEPALIVE int my_number = 123456;

int main(void) {
void* js_address = EM_ASM_PTR({
console.log("JS:_my_number:", _my_number, HEAP32[_my_number/4]);
return _my_number;
});
printf("C: my_number: %ld %d\n", (long)&my_number, my_number);
assert(js_address == &my_number);
return 0;
}
''', emcc_args=['-sMAIN_MODULE'], force_c=True)

@with_dylink_reversed
def test_dylink_global_var_modded(self):
self.dylink_test(main=r'''
Expand Down
4 changes: 3 additions & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat

if base_metadata:
function_exports = base_metadata.function_exports
global_exports = base_metadata.global_exports
# We want the real values from the final metadata but we only want to
# include names from the base_metadata. See phase_link() in link.py.
global_exports = {k: v for k, v in metadata.global_exports.items() if k in base_metadata.global_exports}
else:
function_exports = metadata.function_exports
global_exports = metadata.global_exports
Expand Down