Skip to content

Commit 40252f5

Browse files
authored
Allow HEAPX symbols in EXPORT_RUNTIME_METHODS (#21439)
- Factor out updateMemoryViews. - Enable HEAPXX exports to be specified in EXPORT_RUNTIME_METHODS. - Add all HEAP symbosl to EXPORT_RUNTIME_METHODS unless in STRICT mode. Replaces #21407
1 parent 7dbbbaa commit 40252f5

27 files changed

+102
-71
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ See docs/process.md for more on how version tagging works.
2121
3.1.55 (in development)
2222
-----------------------
2323
- Update sdl2-mixer port from 2.6.0 to 2.8.0
24+
- In `STRICT` mode the `HEAPXX` symbols (such as `HEAP8` and `HEAP32`) are now
25+
only exported on demand. This means that they must be added to
26+
`EXPORTED_RUNTIME_METHODS` for them to appear on the `Module` object. For
27+
now, this only effects users of `STRICT` mode. (#21439)
2428

2529
3.1.54 - 02/15/24
2630
-----------------

src/modules.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ function exportRuntime() {
349349
// being exported. how we show the message depends on whether it's
350350
// a function (almost all of them) or a number.
351351
function maybeExport(name) {
352+
// HEAP objects are exported separately in updateMemoryViews
353+
if (name.startsWith('HEAP')) {
354+
return;
355+
}
352356
// if requested to be exported, export it
353357
if (EXPORTED_RUNTIME_METHODS_SET.has(name)) {
354358
let exported = name;
@@ -385,6 +389,13 @@ function exportRuntime() {
385389
'abort',
386390
'wasmMemory',
387391
'wasmExports',
392+
'HEAPF32',
393+
'HEAPF64',
394+
'HEAP_DATA_VIEW',
395+
'HEAP8', 'HEAPU8',
396+
'HEAP16', 'HEAPU16',
397+
'HEAP32', 'HEAPU32',
398+
'HEAP64', 'HEAPU64',
388399
];
389400

390401
// These are actually native wasm functions these days but we allow exporting

src/preamble.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,7 @@ var HEAP,
138138
var HEAP_DATA_VIEW;
139139
#endif
140140

141-
function updateMemoryViews() {
142-
var b = wasmMemory.buffer;
143-
#if SUPPORT_BIG_ENDIAN
144-
Module['HEAP_DATA_VIEW'] = HEAP_DATA_VIEW = new DataView(b);
145-
#endif
146-
Module['HEAP8'] = HEAP8 = new Int8Array(b);
147-
Module['HEAP16'] = HEAP16 = new Int16Array(b);
148-
Module['HEAPU8'] = HEAPU8 = new Uint8Array(b);
149-
Module['HEAPU16'] = HEAPU16 = new Uint16Array(b);
150-
Module['HEAP32'] = HEAP32 = new Int32Array(b);
151-
Module['HEAPU32'] = HEAPU32 = new Uint32Array(b);
152-
Module['HEAPF32'] = HEAPF32 = new Float32Array(b);
153-
Module['HEAPF64'] = HEAPF64 = new Float64Array(b);
154-
#if WASM_BIGINT
155-
Module['HEAP64'] = HEAP64 = new BigInt64Array(b);
156-
Module['HEAPU64'] = HEAPU64 = new BigUint64Array(b);
157-
#endif
158-
}
141+
#include "runtime_shared.js"
159142

160143
#if ASSERTIONS
161144
assert(!Module['STACK_SIZE'], 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')

src/preamble_minimal.js

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
{{{
8-
// Helper function to export a symbol on the module object
9-
// if requested.
10-
globalThis.maybeExport = (x) => MODULARIZE && EXPORT_ALL ? `Module['${x}'] = ` : '';
11-
// Export to the AudioWorkletGlobalScope the needed variables to access
12-
// the heap. AudioWorkletGlobalScope is unable to access global JS vars
13-
// in the compiled main JS file.
14-
globalThis.maybeExportIfAudioWorklet = (x) => (MODULARIZE && EXPORT_ALL) || AUDIO_WORKLET ? `Module['${x}'] = ` : '';
15-
null;
16-
}}}
17-
187
#if SAFE_HEAP
198
#include "runtime_safe_heap.js"
209
#endif
@@ -65,27 +54,7 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
6554
#endif
6655
wasmMemory;
6756

68-
function updateMemoryViews() {
69-
var b = wasmMemory.buffer;
70-
#if ASSERTIONS && SHARED_MEMORY
71-
assert(b instanceof SharedArrayBuffer, 'requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag');
72-
#endif
73-
#if SUPPORT_BIG_ENDIAN
74-
{{{ maybeExport('HEAP_DATA_VIEW') }}} HEAP_DATA_VIEW = new DataView(b);
75-
#endif
76-
{{{ maybeExport('HEAP8') }}} HEAP8 = new Int8Array(b);
77-
{{{ maybeExport('HEAP16') }}} HEAP16 = new Int16Array(b);
78-
{{{ maybeExport('HEAPU8') }}} HEAPU8 = new Uint8Array(b);
79-
{{{ maybeExport('HEAPU16') }}} HEAPU16 = new Uint16Array(b);
80-
{{{ maybeExport('HEAP32') }}} HEAP32 = new Int32Array(b);
81-
{{{ maybeExportIfAudioWorklet('HEAPU32') }}} HEAPU32 = new Uint32Array(b);
82-
{{{ maybeExportIfAudioWorklet('HEAPF32') }}} HEAPF32 = new Float32Array(b);
83-
{{{ maybeExport('HEAPF64') }}} HEAPF64 = new Float64Array(b);
84-
#if WASM_BIGINT
85-
{{{ maybeExport('HEAP64') }}} HEAP64 = new BigInt64Array(b);
86-
{{{ maybeExport('HEAPU64') }}} HEAPU64 = new BigUint64Array(b);
87-
#endif
88-
}
57+
#include "runtime_shared.js"
8958

9059
#if IMPORTED_MEMORY
9160
#if PTHREADS
@@ -116,6 +85,10 @@ else {
11685
#endif // MODULARIZE
11786
#endif // PTHREADS
11887

88+
#if ASSERTIONS && SHARED_MEMORY
89+
assert(wasmMemory.buffer instanceof SharedArrayBuffer, 'requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag');
90+
#endif
91+
11992
updateMemoryViews();
12093
#endif // IMPORTED_MEMORY
12194

src/runtime_shared.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright 2024 The Emscripten Authors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
{{{
8+
// Helper function to export a heap symbol on the module object,
9+
// if requested.
10+
globalThis.maybeExportHeap = (x) => {
11+
// For now, we export all heap object when not building with MINIMAL_RUNTIME
12+
let shouldExport = !MINIMAL_RUNTIME && !STRICT;
13+
if (!shouldExport) {
14+
if (MODULARIZE && EXPORT_ALL) {
15+
shouldExport = true;
16+
} else if (AUDIO_WORKLET && (x == 'HEAP32' || x == 'HEAPU32')) {
17+
// Export to the AudioWorkletGlobalScope the needed variables to access
18+
// the heap. AudioWorkletGlobalScope is unable to access global JS vars
19+
// in the compiled main JS file.
20+
shouldExport = true;
21+
} else if (EXPORTED_RUNTIME_METHODS.includes(x)) {
22+
shouldExport = true;
23+
}
24+
}
25+
26+
return shouldExport ? `Module['${x}'] = ` : '';
27+
};
28+
null;
29+
}}}
30+
31+
function updateMemoryViews() {
32+
var b = wasmMemory.buffer;
33+
#if SUPPORT_BIG_ENDIAN
34+
{{{ maybeExport('HEAP_DATA_VIEW') }}} HEAP_DATA_VIEW = new DataView(b);
35+
#endif
36+
{{{ maybeExportHeap('HEAP8') }}}HEAP8 = new Int8Array(b);
37+
{{{ maybeExportHeap('HEAP16') }}}HEAP16 = new Int16Array(b);
38+
{{{ maybeExportHeap('HEAPU8') }}}HEAPU8 = new Uint8Array(b);
39+
{{{ maybeExportHeap('HEAPU16') }}}HEAPU16 = new Uint16Array(b);
40+
{{{ maybeExportHeap('HEAP32') }}}HEAP32 = new Int32Array(b);
41+
{{{ maybeExportHeap('HEAPU32') }}}HEAPU32 = new Uint32Array(b);
42+
{{{ maybeExportHeap('HEAPF32') }}}HEAPF32 = new Float32Array(b);
43+
{{{ maybeExportHeap('HEAPF64') }}}HEAPF64 = new Float64Array(b);
44+
#if WASM_BIGINT
45+
{{{ maybeExportHeap('HEAP64') }}}HEAP64 = new BigInt64Array(b);
46+
{{{ maybeExportHeap('HEAPU64') }}}HEAPU64 = new BigUint64Array(b);
47+
#endif
48+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1725
1+
1685
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3635
1+
3541
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6845
1+
6858
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18753
1+
18751
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1827
1+
1796
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4419
1+
4328
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1660
1+
1627
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3362
1+
3287
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1618
1+
1591
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3312
1+
3237
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1618
1+
1591
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3312
1+
3237
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1610
1+
1583
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3297
1+
3222
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1618
1+
1591
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3312
1+
3237
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1618
1+
1591
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3312
1+
3237
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58034
1+
58097
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31565
1+
31628
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
56997
1+
57049

tools/link.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,18 @@ def phase_linker_setup(options, state, newargs):
880880
else:
881881
default_setting('INCOMING_MODULE_JS_API', [])
882882

883+
if not settings.MINIMAL_RUNTIME and not settings.STRICT:
884+
# Export the HEAP object by default, when not running in STRICT mode
885+
settings.EXPORTED_RUNTIME_METHODS.extend([
886+
'HEAPF32',
887+
'HEAPF64',
888+
'HEAP_DATA_VIEW',
889+
'HEAP8', 'HEAPU8',
890+
'HEAP16', 'HEAPU16',
891+
'HEAP32', 'HEAPU32',
892+
'HEAP64', 'HEAPU64',
893+
])
894+
883895
# Default to TEXTDECODER=2 (always use TextDecoder to decode UTF-8 strings)
884896
# in -Oz builds, since custom decoder for UTF-8 takes up space.
885897
# In pthreads enabled builds, TEXTDECODER==2 may not work, see

0 commit comments

Comments
 (0)