Skip to content
Open
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
29 changes: 0 additions & 29 deletions test/other/test_export_global_address.c

This file was deleted.

77 changes: 77 additions & 0 deletions test/other/test_export_global_address.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <emscripten.h>
#include <assert.h>
#include <stdio.h>

#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;
}
8 changes: 6 additions & 2 deletions test/other/test_export_global_address.out
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading