Skip to content

Commit d45b395

Browse files
authored
Extract common memory declarations. NFC (emscripten-core#24386)
I noticed that regular preamble had types for Closure for all the memory arrays, whereas preamble_minimal didn't but probably should. OTOH, preamble also had unused `HEAP` variable, which preamble_minimal correctly didn't. Those vars + runtime{Initialized,Exited} seem easy enough to extract into runtime_shared.js so that they stay consistent and in one place.
1 parent 0e1522f commit d45b395

31 files changed

+216
-227
lines changed

src/preamble.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ if (typeof WebAssembly != 'object') {
4141

4242
// Wasm globals
4343

44-
#if !WASM_ESM_INTEGRATION || IMPORTED_MEMORY
45-
var wasmMemory;
46-
#endif
47-
4844
#if SHARED_MEMORY
4945
// For sending to workers.
5046
var wasmModule;
@@ -99,44 +95,6 @@ function _free() {
9995
#endif // free
10096
#endif // ASSERTIONS
10197

102-
// Memory management
103-
104-
var HEAP,
105-
/** @type {!Int8Array} */
106-
HEAP8,
107-
/** @type {!Uint8Array} */
108-
HEAPU8,
109-
/** @type {!Int16Array} */
110-
HEAP16,
111-
/** @type {!Uint16Array} */
112-
HEAPU16,
113-
/** @type {!Int32Array} */
114-
HEAP32,
115-
/** @type {!Uint32Array} */
116-
HEAPU32,
117-
/** @type {!Float32Array} */
118-
HEAPF32,
119-
#if WASM_BIGINT
120-
/* BigInt64Array type is not correctly defined in closure
121-
/** not-@type {!BigInt64Array} */
122-
HEAP64,
123-
/* BigUint64Array type is not correctly defined in closure
124-
/** not-t@type {!BigUint64Array} */
125-
HEAPU64,
126-
#endif
127-
/** @type {!Float64Array} */
128-
HEAPF64;
129-
130-
#if SUPPORT_BIG_ENDIAN
131-
var HEAP_DATA_VIEW;
132-
#endif
133-
134-
var runtimeInitialized = false;
135-
136-
#if EXIT_RUNTIME
137-
var runtimeExited = false;
138-
#endif
139-
14098
/**
14199
* Indicates whether filename is delivered via file protocol (as opposed to http/https)
142100
* @noinline
@@ -150,11 +108,6 @@ assert(typeof Int32Array != 'undefined' && typeof Float64Array !== 'undefined' &
150108
'JS engine does not provide full typed array support');
151109
#endif
152110

153-
#if IMPORTED_MEMORY
154-
// In non-standalone/normal mode, we create the memory here.
155-
#include "runtime_init_memory.js"
156-
#endif // !IMPORTED_MEMORY && ASSERTIONS
157-
158111
#if RELOCATABLE
159112
var __RELOC_FUNCS__ = [];
160113
#endif

src/preamble_minimal.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,9 @@ var tempI64;
2525
#if WASM2JS && WASM != 2
2626
// WASM == 2 includes wasm2js.js separately.
2727
#include "wasm2js.js"
28+
#if !WASM2JS
29+
}
2830
#endif
29-
30-
var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
31-
#if WASM_BIGINT
32-
HEAP64, HEAPU64,
33-
#endif
34-
#if SUPPORT_BIG_ENDIAN
35-
HEAP_DATA_VIEW,
36-
#endif
37-
wasmMemory;
38-
39-
#if ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE
40-
var runtimeInitialized = false;
41-
#endif
42-
43-
#if EXIT_RUNTIME
44-
var runtimeExited = false;
4531
#endif
4632

4733
#include "runtime_shared.js"
48-
49-
#if IMPORTED_MEMORY
50-
#include "runtime_init_memory.js"
51-
initMemory();
52-
#endif

src/runtime_init_memory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ function initMemory() {
5454
updateMemoryViews();
5555
}
5656

57-
#if WASM_ESM_INTEGRATION
57+
#if WASM_ESM_INTEGRATION || MINIMAL_RUNTIME
5858
initMemory();
5959
#endif

src/runtime_shared.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "runtime_stack_check.js"
88
#include "runtime_exceptions.js"
99
#include "runtime_debug.js"
10-
#include "memoryprofiler.js"
1110

1211
#if SAFE_HEAP
1312
#include "runtime_safe_heap.js"
@@ -65,6 +64,53 @@ var wasmOffsetConverter;
6564
#include "wasm_offset_converter.js"
6665
#endif
6766

67+
// Memory management
68+
69+
#if !WASM_ESM_INTEGRATION || IMPORTED_MEMORY
70+
var wasmMemory;
71+
#endif
72+
73+
var
74+
/** @type {!Int8Array} */
75+
HEAP8,
76+
/** @type {!Uint8Array} */
77+
HEAPU8,
78+
/** @type {!Int16Array} */
79+
HEAP16,
80+
/** @type {!Uint16Array} */
81+
HEAPU16,
82+
/** @type {!Int32Array} */
83+
HEAP32,
84+
/** @type {!Uint32Array} */
85+
HEAPU32,
86+
/** @type {!Float32Array} */
87+
HEAPF32,
88+
/** @type {!Float64Array} */
89+
HEAPF64;
90+
91+
#if WASM_BIGINT
92+
// BigInt64Array type is not correctly defined in closure
93+
var
94+
/** not-@type {!BigInt64Array} */
95+
HEAP64,
96+
/* BigUint64Array type is not correctly defined in closure
97+
/** not-@type {!BigUint64Array} */
98+
HEAPU64;
99+
#endif
100+
101+
#if SUPPORT_BIG_ENDIAN
102+
/** @type {!DataView} */
103+
var HEAP_DATA_VIEW;
104+
#endif
105+
106+
#if !MINIMAL_RUNTIME || ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE
107+
var runtimeInitialized = false;
108+
#endif
109+
110+
#if EXIT_RUNTIME
111+
var runtimeExited = false;
112+
#endif
113+
68114
{{{
69115
// Helper function to export a heap symbol on the module object,
70116
// if requested.
@@ -116,3 +162,10 @@ if (ENVIRONMENT_IS_NODE) {
116162
global.performance ??= require('perf_hooks').performance;
117163
}
118164
#endif
165+
166+
#if IMPORTED_MEMORY
167+
// In non-standalone/normal mode, we create the memory here.
168+
#include "runtime_init_memory.js"
169+
#endif // !IMPORTED_MEMORY && ASSERTIONS
170+
171+
#include "memoryprofiler.js"

0 commit comments

Comments
 (0)