diff --git a/test/other/test_export_global_address.c b/test/other/test_export_global_address.c deleted file mode 100644 index e815a74bda74a..0000000000000 --- a/test/other/test_export_global_address.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include - -#ifdef USE_KEEPALIVE -EMSCRIPTEN_KEEPALIVE -#endif -int g_foo = 4; - -EM_JS(int*, get_foo_from_js, (void), { - assert(_g_foo !== undefined, "g_foo not exported to JS"); -#if __wasm64__ - return BigInt(_g_foo); -#else - return _g_foo; -#endif -}); - -int main() { - printf("get_foo_from_js: %d\n", *get_foo_from_js()); - printf("g_foo: %d\n", g_foo); - if (get_foo_from_js() != &g_foo) { - printf("addresses failed to match\n"); - printf("js: %p\n", get_foo_from_js()); - printf("native: %p\n", &g_foo); - return 1; - } - return 0; -} diff --git a/test/other/test_export_global_address.cpp b/test/other/test_export_global_address.cpp new file mode 100644 index 0000000000000..fd4348b8b6e17 --- /dev/null +++ b/test/other/test_export_global_address.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +#ifdef USE_KEEPALIVE +#define KEEPALIVE EMSCRIPTEN_KEEPALIVE +#else +#define KEEPALIVE +#endif + +extern "C" { +KEEPALIVE int g_var = 4; + +KEEPALIVE int g_func(int x) { + return x + g_var; +} +} + +namespace ns { +KEEPALIVE int ns_var = 42; +} + +KEEPALIVE int cpp_func(int x) { + return x * 2; +} + +EM_JS(int*, get_var_from_js, (void), { + assert(_g_var !== undefined, "g_var not exported to JS"); +#if __wasm64__ + return BigInt(_g_var); +#else + return _g_var; +#endif +}); + +EM_JS(int, call_func_from_js, (int arg), { + assert(_g_func !== undefined, "g_func not exported to JS"); + return _g_func(arg); +}); + +EM_JS(int*, get_ns_var_from_js, (void), { + assert(__ZN2ns6ns_varE !== undefined, "ns::ns_var not exported to JS"); +#if __wasm64__ + return BigInt(__ZN2ns6ns_varE); +#else + return __ZN2ns6ns_varE; +#endif +}); + +EM_JS(int, call_cpp_func_from_js, (int arg), { + assert(__Z8cpp_funci !== undefined, "cpp_func not exported to JS"); + return __Z8cpp_funci(arg); +}); + +int main() { + printf("get_var_from_js: %d\n", *get_var_from_js()); + printf("g_var: %d\n", g_var); + if (get_var_from_js() != &g_var) { + printf("addresses failed to match\n"); + printf("js: %p\n", get_var_from_js()); + printf("native: %p\n", &g_var); + return 1; + } + printf("call_func_from_js: %d\n", call_func_from_js(10)); + + printf("get_ns_var_from_js: %d\n", *get_ns_var_from_js()); + printf("ns_var: %d\n", ns::ns_var); + if (get_ns_var_from_js() != &ns::ns_var) { + printf("ns_var addresses failed to match\n"); + printf("js: %p\n", get_ns_var_from_js()); + printf("native: %p\n", &ns::ns_var); + return 1; + } + printf("call_cpp_func_from_js: %d\n", call_cpp_func_from_js(5)); + + return 0; +} diff --git a/test/other/test_export_global_address.out b/test/other/test_export_global_address.out index b6338edf0388e..c99f3e3ec4076 100644 --- a/test/other/test_export_global_address.out +++ b/test/other/test_export_global_address.out @@ -1,2 +1,6 @@ -get_foo_from_js: 4 -g_foo: 4 +get_var_from_js: 4 +g_var: 4 +call_func_from_js: 14 +get_ns_var_from_js: 42 +ns_var: 42 +call_cpp_func_from_js: 10 diff --git a/test/test_other.py b/test/test_other.py index ac25238850111..d1a48e3ab74c0 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -12350,11 +12350,11 @@ def test_assembly_preprocessed(self): @parameterized({ '': (['-DUSE_KEEPALIVE'],), 'minimal': (['-DUSE_KEEPALIVE', '-sMINIMAL_RUNTIME'],), - 'command_line': (['-sEXPORTED_FUNCTIONS=_g_foo,_main'],), - 'himem': (['-sEXPORTED_FUNCTIONS=_g_foo,_main', '-sGLOBAL_BASE=2gb', '-sINITIAL_MEMORY=3gb'],), + 'command_line': (['-sEXPORTED_FUNCTIONS=_g_var,_g_func,__ZN2ns6ns_varE,__Z8cpp_funci,_main'],), + 'himem': (['-sEXPORTED_FUNCTIONS=_g_var,_g_func,__ZN2ns6ns_varE,__Z8cpp_funci,_main', '-sGLOBAL_BASE=2gb', '-sINITIAL_MEMORY=3gb'],), }) def test_export_global_address(self, args): - self.do_other_test('test_export_global_address.c', cflags=args) + self.do_other_test('test_export_global_address.cpp', cflags=args) def test_linker_version(self): out = self.run_process([EMCC, '-Wl,--version'], stdout=PIPE).stdout