Skip to content

Commit 6465935

Browse files
committed
Export data addresses from the Wasm module just like other symbols.
This removes the special handling of exported immutable globals which simplifies the code in a few different ways. For programs that export data addresses (this is relatively rare) this means codesize reduction for the generated JS (since it no longer contains the constant values) and the codesize increase for the Wasm binary (since it now contains extra exports). The main reason for this is consistency with dynamic linking (where data exports are always needed) and a reduction in complexity. Fixes: #25556
1 parent 0cd0fab commit 6465935

24 files changed

+158
-206
lines changed

src/lib/libcore.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,16 +1632,11 @@ addToLibrary({
16321632
dynCalls[name.substr(8)] = exportedSymbol;
16331633
}
16341634
#endif
1635-
// Globals are currently statically enumerated into the output JS.
1636-
// TODO: If the number of Globals grows large, consider giving them a
1637-
// similar DECLARE_ASM_MODULE_EXPORTS = 0 treatment.
1638-
if (typeof exportedSymbol.value === 'undefined') {
16391635
#if MINIMAL_RUNTIME
1640-
globalThis[name] = exportedSymbol;
1636+
globalThis[name] = exportedSymbol;
16411637
#else
1642-
globalThis[name] = Module[name] = exportedSymbol;
1638+
globalThis[name] = Module[name] = exportedSymbol;
16431639
#endif
1644-
}
16451640
}
16461641
exportAliases(wasmExports);
16471642
},

src/lib/libdylink.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ var LibraryDylink = {
249249
var newValue;
250250
if (typeof value == 'function') {
251251
newValue = {{{ to64('addFunction(value)') }}};
252-
} else if (typeof value == {{{ POINTER_JS_TYPE }}}) {
252+
} else if (typeof value.value == {{{ POINTER_JS_TYPE }}}) {
253253
newValue = value;
254254
} else {
255255
// The GOT can only contain addresses (i.e data addresses or function
@@ -306,7 +306,7 @@ var LibraryDylink = {
306306
// Detect immuable wasm global exports. These represent data addresses
307307
// which are relative to `memoryBase`
308308
if (isImmutableGlobal(value)) {
309-
return value.value + {{{ to64('memoryBase') }}};
309+
return new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}'}, value.value + {{{ to64('memoryBase') }}});
310310
}
311311

312312
// Return unmodified value (no relocation required).
@@ -354,10 +354,8 @@ var LibraryDylink = {
354354
#endif
355355
} else if (typeof value == 'number') {
356356
entry.value = {{{ to64('value') }}};
357-
#if MEMORY64
358-
} else if (typeof value == 'bigint') {
357+
} else if (typeof value.value == {{{ POINTER_JS_TYPE }}}) {
359358
entry.value = value;
360-
#endif
361359
} else {
362360
throw new Error(`bad export type for '${symName}': ${typeof value} (${value})`);
363361
}
@@ -422,6 +420,8 @@ var LibraryDylink = {
422420
// Keep __heap_base stack aligned.
423421
var end = ret + alignMemory(size, {{{ STACK_ALIGN }}});
424422
#if ASSERTIONS
423+
//dbg(ret);
424+
//dbg(HEAP8.length);
425425
assert(end <= HEAP8.length, 'failure to getMemory - memory growth etc. is not supported there, call malloc/sbrk directly or increase INITIAL_MEMORY');
426426
#endif
427427
___heap_base = end;
@@ -859,10 +859,15 @@ var LibraryDylink = {
859859

860860
// Add any EM_ASM function that exist in the side module
861861
if ('__start_em_asm' in moduleExports) {
862-
var start = moduleExports['__start_em_asm'];
863-
var stop = moduleExports['__stop_em_asm'];
862+
var start = moduleExports['__start_em_asm'].value;
863+
var stop = moduleExports['__stop_em_asm'].value;
864+
#if CAN_ADDRESS_2GB
865+
start >>>= 0;
866+
stop >>>= 0;
867+
#else
864868
{{{ from64('start') }}}
865869
{{{ from64('stop') }}}
870+
#endif
866871
while (start < stop) {
867872
var jsString = UTF8ToString(start);
868873
addEmAsm(start, jsString);
@@ -892,7 +897,7 @@ var LibraryDylink = {
892897

893898
for (var name in moduleExports) {
894899
if (name.startsWith('__em_js__')) {
895-
var start = moduleExports[name]
900+
var start = moduleExports[name].value
896901
var jsString = UTF8ToString({{{ from64Expr('start') }}});
897902
// EM_JS strings are stored in the data section in the form
898903
// SIG<::>BODY.

src/preamble.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,13 +719,10 @@ function getWasmImports() {
719719
wasmExports = instance.exports;
720720

721721
#if MAIN_MODULE
722-
// No relocation needed here.. but calling this just so that updateGOT is
723-
// called.
724722
#if RELOCATABLE
725-
var origExports = wasmExports = relocateExports(wasmExports, {{{ GLOBAL_BASE }}});
726-
#else
727-
var origExports = wasmExports = relocateExports(wasmExports);
723+
wasmExports = relocateExports(wasmExports, {{{ GLOBAL_BASE }}});
728724
#endif
725+
var origExports = wasmExports;
729726
#endif
730727

731728
#if ASYNCIFY

src/settings_internal.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
// underscore.
1717
var WASM_EXPORTS = [];
1818

19-
// Similar to above but only includes the data symbols (address exports).
20-
var DATA_EXPORTS = [];
21-
2219
// An array of all symbols exported from all the side modules specified on the
2320
// command line.
2421
// These are raw symbol names and are not mangled to include the leading

test/codesize/test_codesize_hello_dylink.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 26546,
3-
"a.out.js.gz": 11349,
2+
"a.out.js": 26669,
3+
"a.out.js.gz": 11374,
44
"a.out.nodebug.wasm": 17761,
55
"a.out.nodebug.wasm.gz": 9003,
6-
"total": 44307,
7-
"total_gz": 20352,
6+
"total": 44430,
7+
"total_gz": 20377,
88
"sent": [
99
"__syscall_stat64",
1010
"emscripten_resize_heap",

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 245299,
2+
"a.out.js": 245418,
33
"a.out.nodebug.wasm": 574162,
4-
"total": 819461,
4+
"total": 819580,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/codesize/test_codesize_minimal_64.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
2-
"a.out.js": 2595,
3-
"a.out.js.gz": 1243,
4-
"a.out.nodebug.wasm": 62,
5-
"a.out.nodebug.wasm.gz": 76,
6-
"total": 2657,
7-
"total_gz": 1319,
2+
"a.out.js": 2604,
3+
"a.out.js.gz": 1247,
4+
"a.out.nodebug.wasm": 75,
5+
"a.out.nodebug.wasm.gz": 88,
6+
"total": 2679,
7+
"total_gz": 1335,
88
"sent": [],
99
"imports": [],
1010
"exports": [
1111
"a (memory)",
1212
"b (__wasm_call_ctors)",
13-
"c (add)"
13+
"c (add)",
14+
"d (global_val)"
1415
],
1516
"funcs": [
1617
"$__wasm_call_ctors",

test/codesize/test_codesize_minimal_O0.expected.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ var __emscripten_stack_restore = makeInvalidEarlyAccess('__emscripten_stack_rest
13231323
var __emscripten_stack_alloc = makeInvalidEarlyAccess('__emscripten_stack_alloc');
13241324
var _emscripten_stack_get_current = makeInvalidEarlyAccess('_emscripten_stack_get_current');
13251325
var memory = makeInvalidEarlyAccess('memory');
1326+
var _global_val = Module['_global_val'] = makeInvalidEarlyAccess('_global_val');
13261327
var __indirect_function_table = makeInvalidEarlyAccess('__indirect_function_table');
13271328
var wasmMemory = makeInvalidEarlyAccess('wasmMemory');
13281329

@@ -1347,12 +1348,12 @@ function assignWasmExports(wasmExports) {
13471348
_emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'];
13481349
assert(typeof wasmExports['memory'] != 'undefined', 'missing Wasm export: memory');
13491350
memory = wasmMemory = wasmExports['memory'];
1351+
assert(typeof wasmExports['global_val'] != 'undefined', 'missing Wasm export: global_val');
1352+
_global_val = Module['_global_val'] = wasmExports['global_val'].value;
13501353
assert(typeof wasmExports['__indirect_function_table'] != 'undefined', 'missing Wasm export: __indirect_function_table');
13511354
__indirect_function_table = wasmExports['__indirect_function_table'];
13521355
}
13531356

1354-
var _global_val = Module['_global_val'] = 65536;
1355-
13561357
var wasmImports = {
13571358

13581359
};

test/codesize/test_codesize_minimal_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 19364,
3-
"a.out.js.gz": 6998,
2+
"a.out.js": 19489,
3+
"a.out.js.gz": 7016,
44
"a.out.nodebug.wasm": 1136,
55
"a.out.nodebug.wasm.gz": 659,
6-
"total": 20500,
7-
"total_gz": 7657,
6+
"total": 20625,
7+
"total_gz": 7675,
88
"sent": [],
99
"imports": [],
1010
"exports": [

test/codesize/test_codesize_minimal_O1.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 3049,
3-
"a.out.js.gz": 1301,
2+
"a.out.js": 3061,
3+
"a.out.js.gz": 1300,
44
"a.out.nodebug.wasm": 449,
55
"a.out.nodebug.wasm.gz": 337,
6-
"total": 3498,
7-
"total_gz": 1638,
6+
"total": 3510,
7+
"total_gz": 1637,
88
"sent": [],
99
"imports": [],
1010
"exports": [

0 commit comments

Comments
 (0)