Skip to content

Commit 1023fa7

Browse files
committed
Have wasm module export its memory in MINIMAL_RUNTIME mode.
See #12315
1 parent a45e3dd commit 1023fa7

17 files changed

+75
-96
lines changed

emcc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,9 @@ def include_and_export(name):
17391739
# requires JS legalization
17401740
shared.Settings.LEGALIZE_JS_FFI = 0
17411741

1742+
if shared.Settings.MINIMAL_RUNTIME or shared.Settings.STANDALONE_WASM:
1743+
shared.Settings.MEMORY_DEFINED_IN_WASM = 1
1744+
17421745
if shared.Settings.WASM_BIGINT:
17431746
shared.Settings.LEGALIZE_JS_FFI = 0
17441747

emscripten.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,7 @@ def create_em_js(forwarded_json, metadata):
536536

537537

538538
def add_standard_wasm_imports(send_items_map):
539-
# Normally we import these into the wasm (so that JS could use them even
540-
# before the wasm loads), while in standalone mode we do not depend
541-
# on JS to create them, but create them in the wasm and export them.
542-
if not shared.Settings.STANDALONE_WASM:
539+
if not shared.Settings.MEMORY_DEFINED_IN_WASM:
543540
memory_import = 'wasmMemory'
544541
if shared.Settings.MODULARIZE and shared.Settings.USE_PTHREADS:
545542
# Pthreads assign wasmMemory in their worker startup. In MODULARIZE mode, they cannot assign inside the

src/postamble_minimal.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
204204
/*** ASM_MODULE_EXPORTS ***/
205205
#endif
206206
wasmTable = asm['__indirect_function_table'];
207+
wasmMemory = asm['memory'];
208+
buffer = wasmMemory.buffer;
209+
210+
#if ASSERTIONS
211+
assert(wasmTable);
212+
assert(wasmMemory);
213+
assert(buffer.byteLength === {{{ INITIAL_MEMORY }}});
214+
#if USE_PTHREADS
215+
assert(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');
216+
#endif
217+
#endif
218+
219+
updateGlobalBufferAndViews(buffer);
220+
221+
#if MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE
222+
#if ASSERTIONS
223+
if (!Module['mem']) throw 'Must load memory initializer as an ArrayBuffer in to variable Module.mem before adding compiled output .js script to the DOM';
224+
#endif
225+
HEAPU8.set(new Uint8Array(Module['mem']), {{{ GLOBAL_BASE }}});
226+
#endif
207227

208228
initRuntime(asm);
209229
#if USE_PTHREADS && PTHREAD_POOL_SIZE

src/preamble_minimal.js

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,15 @@ var wasmMaximumMemory = {{{ MAXIMUM_MEMORY >>> 16 }}};
6565
var wasmMaximumMemory = {{{ INITIAL_MEMORY >>> 16}}};
6666
#endif
6767

68-
var wasmMemory = new WebAssembly.Memory({
69-
'initial': {{{ INITIAL_MEMORY >>> 16 }}}
70-
#if USE_PTHREADS || !ALLOW_MEMORY_GROWTH || MAXIMUM_MEMORY != -1
71-
, 'maximum': wasmMaximumMemory
72-
#endif
73-
#if USE_PTHREADS
74-
, 'shared': true
75-
#endif
76-
});
77-
68+
// These get assigned from the WebAssembly module exports
7869
var wasmTable;
79-
var buffer = wasmMemory.buffer;
80-
81-
#if USE_PTHREADS
82-
}
83-
#if ASSERTIONS
84-
assert(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');
85-
#endif
86-
#endif
70+
var wasmMemory;
71+
var buffer;
8772

88-
#if ASSERTIONS
89-
#if USE_PTHREADS
90-
if (!ENVIRONMENT_IS_PTHREAD) {
91-
#endif
92-
assert(buffer.byteLength === {{{ INITIAL_MEMORY }}});
9373
#if USE_PTHREADS
9474
}
9575
#endif
96-
#endif // ASSERTIONS
9776

98-
#if ALLOW_MEMORY_GROWTH
99-
// In ALLOW_MEMORY_GROWTH, we need to be able to re-initialize the
100-
// typed array buffer and heap views to the buffer whenever the heap
101-
// is resized.
10277
var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64;
10378
function updateGlobalBufferAndViews(b) {
10479
buffer = b;
@@ -111,31 +86,11 @@ function updateGlobalBufferAndViews(b) {
11186
HEAPF32 = new Float32Array(b);
11287
HEAPF64 = new Float64Array(b);
11388
}
114-
updateGlobalBufferAndViews(buffer);
115-
#else
116-
// In non-ALLOW_MEMORY_GROWTH scenario, we only need to initialize
117-
// the heap once, so optimize code size to do it statically here.
118-
var HEAP8 = new Int8Array(buffer);
119-
var HEAP16 = new Int16Array(buffer);
120-
var HEAP32 = new Int32Array(buffer);
121-
var HEAPU8 = new Uint8Array(buffer);
122-
var HEAPU16 = new Uint16Array(buffer);
123-
var HEAPU32 = new Uint32Array(buffer);
124-
var HEAPF32 = new Float32Array(buffer);
125-
var HEAPF64 = new Float64Array(buffer);
126-
#endif
12789

12890
#if USE_PTHREADS && ((MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE) || USES_DYNAMIC_ALLOC)
12991
if (!ENVIRONMENT_IS_PTHREAD) {
13092
#endif
13193

132-
#if MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE
133-
#if ASSERTIONS
134-
if (!Module['mem']) throw 'Must load memory initializer as an ArrayBuffer in to variable Module.mem before adding compiled output .js script to the DOM';
135-
#endif
136-
HEAPU8.set(new Uint8Array(Module['mem']), {{{ GLOBAL_BASE }}});
137-
#endif
138-
13994
#if USE_PTHREADS && ((MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE) || USES_DYNAMIC_ALLOC)
14095
}
14196
#endif

src/settings_internal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ var STRUCT_INFO = '';
187187
var MEMORYPROFILER = 0;
188188

189189
var GENERATE_SOURCE_MAP = 0;
190+
191+
// By default we set this to 0 meaning the wasm file does not defined its own
192+
// memory and instead the memory is defined in JavaScript.
193+
// This is set 1 in STANDALONE_WASM or MINIMAL_RUNTIME mode in which case
194+
// the wasm module exports its memory to JavaScript.
195+
var MEMORY_DEFINED_IN_WASM = 0;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 563,
33
"a.html.gz": 377,
4-
"a.js": 4957,
5-
"a.js.gz": 2373,
6-
"a.wasm": 10893,
7-
"a.wasm.gz": 6923,
8-
"total": 16413,
9-
"total_gz": 9673
4+
"a.js": 4935,
5+
"a.js.gz": 2357,
6+
"a.wasm": 10895,
7+
"a.wasm.gz": 6927,
8+
"total": 16393,
9+
"total_gz": 9661
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 588,
33
"a.html.gz": 386,
4-
"a.js": 21297,
5-
"a.js.gz": 8263,
4+
"a.js": 21318,
5+
"a.js.gz": 8287,
66
"a.mem": 3171,
77
"a.mem.gz": 2715,
8-
"total": 25056,
9-
"total_gz": 11364
8+
"total": 25077,
9+
"total_gz": 11388
1010
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 563,
33
"a.html.gz": 377,
4-
"a.js": 4444,
5-
"a.js.gz": 2199,
6-
"a.wasm": 10893,
7-
"a.wasm.gz": 6923,
8-
"total": 15900,
9-
"total_gz": 9499
4+
"a.js": 4418,
5+
"a.js.gz": 2180,
6+
"a.wasm": 10895,
7+
"a.wasm.gz": 6927,
8+
"total": 15876,
9+
"total_gz": 9484
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 588,
33
"a.html.gz": 386,
4-
"a.js": 20785,
5-
"a.js.gz": 8102,
4+
"a.js": 20802,
5+
"a.js.gz": 8112,
66
"a.mem": 3171,
77
"a.mem.gz": 2715,
8-
"total": 24544,
9-
"total_gz": 11203
8+
"total": 24561,
9+
"total_gz": 11213
1010
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 665,
33
"a.html.gz": 427,
4-
"a.js": 360,
5-
"a.js.gz": 281,
6-
"a.wasm": 102,
7-
"a.wasm.gz": 114,
8-
"total": 1127,
9-
"total_gz": 822
4+
"a.js": 330,
5+
"a.js.gz": 264,
6+
"a.wasm": 104,
7+
"a.wasm.gz": 112,
8+
"total": 1099,
9+
"total_gz": 803
1010
}

0 commit comments

Comments
 (0)