Skip to content

Commit 67e6925

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

20 files changed

+100
-121
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.STANDALONE_WASM or (shared.Settings.MINIMAL_RUNTIME and not shared.Settings.USE_PTHREADS):
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
204204
/*** ASM_MODULE_EXPORTS ***/
205205
#endif
206206
wasmTable = asm['__indirect_function_table'];
207+
#if ASSERTIONS
208+
assert(wasmTable);
209+
#endif
210+
211+
#if !USE_PTHREADS
212+
wasmMemory = asm['memory'];
213+
#if ASSERTIONS
214+
assert(wasmMemory);
215+
assert(wasmMemory.buffer.byteLength === {{{ INITIAL_MEMORY }}});
216+
#endif
217+
updateGlobalBufferAndViews(wasmMemory.buffer);
218+
#endif
219+
220+
#if MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE
221+
#if ASSERTIONS
222+
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';
223+
#endif
224+
HEAPU8.set(new Uint8Array(Module['mem']), {{{ GLOBAL_BASE }}});
225+
#endif
207226

208227
initRuntime(asm);
209228
#if USE_PTHREADS && PTHREAD_POOL_SIZE

src/preamble_minimal.js

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -55,52 +55,13 @@ Module['wasm'] = base64Decode('{{{ getQuoted("WASM_BINARY_DATA") }}}');
5555
#include "runtime_functions.js"
5656
#include "runtime_strings.js"
5757

58-
#if USE_PTHREADS
59-
if (!ENVIRONMENT_IS_PTHREAD) {
60-
#endif
61-
62-
#if ALLOW_MEMORY_GROWTH && MAXIMUM_MEMORY != -1
63-
var wasmMaximumMemory = {{{ MAXIMUM_MEMORY >>> 16 }}};
64-
#else
65-
var wasmMaximumMemory = {{{ INITIAL_MEMORY >>> 16}}};
66-
#endif
67-
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-
78-
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
87-
88-
#if ASSERTIONS
89-
#if USE_PTHREADS
90-
if (!ENVIRONMENT_IS_PTHREAD) {
91-
#endif
92-
assert(buffer.byteLength === {{{ INITIAL_MEMORY }}});
93-
#if USE_PTHREADS
94-
}
95-
#endif
96-
#endif // ASSERTIONS
97-
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.
10258
var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64;
59+
var wasmMemory, buffer, wasmTable;
60+
10361
function updateGlobalBufferAndViews(b) {
62+
#if ASSERTIONS && USE_PTHREADS
63+
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');
64+
#endif
10465
buffer = b;
10566
HEAP8 = new Int8Array(b);
10667
HEAP16 = new Int16Array(b);
@@ -111,33 +72,33 @@ function updateGlobalBufferAndViews(b) {
11172
HEAPF32 = new Float32Array(b);
11273
HEAPF64 = new Float64Array(b);
11374
}
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
12775

128-
#if USE_PTHREADS && ((MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE) || USES_DYNAMIC_ALLOC)
76+
#if USE_PTHREADS
12977
if (!ENVIRONMENT_IS_PTHREAD) {
78+
#if ALLOW_MEMORY_GROWTH && MAXIMUM_MEMORY != -1
79+
var wasmMaximumMemory = {{{ MAXIMUM_MEMORY >>> 16 }}};
80+
#else
81+
var wasmMaximumMemory = {{{ INITIAL_MEMORY >>> 16}}};
13082
#endif
131-
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 }}});
83+
wasmMemory = new WebAssembly.Memory({
84+
'initial': {{{ INITIAL_MEMORY >>> 16 }}}
85+
#if USE_PTHREADS || !ALLOW_MEMORY_GROWTH || MAXIMUM_MEMORY != -1
86+
, 'maximum': wasmMaximumMemory
13787
#endif
88+
, 'shared': true
89+
});
90+
}
13891

139-
#if USE_PTHREADS && ((MEM_INIT_METHOD == 1 && !MEM_INIT_IN_WASM && !SINGLE_FILE) || USES_DYNAMIC_ALLOC)
92+
#if MODULARIZE
93+
if (ENVIRONMENT_IS_WORKER) {
94+
updateGlobalBufferAndViews({{{EXPORT_NAME}}}.buffer);
95+
} else {
96+
updateGlobalBufferAndViews(wasmMemory.buffer);
14097
}
98+
#else
99+
updateGlobalBufferAndViews(wasmMemory.buffer);
100+
#endif
101+
141102
#endif
142103

143104
#include "runtime_stack_check.js"

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;

src/shell_minimal.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ var _scriptDir = (typeof document !== 'undefined' && document.currentScript) ? d
140140
// coincide.
141141
var ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_PTHREAD = typeof importScripts === 'function';
142142

143-
#if MODULARIZE
144-
if (ENVIRONMENT_IS_WORKER) {
145-
var buffer = {{{EXPORT_NAME}}}.buffer;
146-
}
147-
#endif
148-
149143
var currentScriptUrl = typeof _scriptDir !== 'undefined' ? _scriptDir : ((typeof document !== 'undefined' && document.currentScript) ? document.currentScript.src : undefined);
150144
#endif // USE_PTHREADS
151145

system/lib/pthread/library_pthread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,15 @@ EMSCRIPTEN_RESULT emscripten_wait_for_call_i(
440440
return res;
441441
}
442442

443-
static pthread_t main_browser_thread_id_ = 0;
443+
static pthread_t main_browser_thread_id_;
444444

445445
void EMSCRIPTEN_KEEPALIVE emscripten_register_main_browser_thread_id(
446446
pthread_t main_browser_thread_id) {
447447
main_browser_thread_id_ = main_browser_thread_id;
448448
}
449449

450450
pthread_t EMSCRIPTEN_KEEPALIVE emscripten_main_browser_thread_id() {
451+
assert(main_browser_thread_id_);
451452
return main_browser_thread_id_;
452453
}
453454

system/lib/pthread/library_pthread_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ int pthread_key_delete(pthread_key_t key) {
319319
return 0;
320320
}
321321

322-
void* pthread_gett specific(pthread_key_t key) {
322+
void* pthread_getspecific(pthread_key_t key) {
323323
if (key == 0)
324324
return NULL;
325325
uintptr_t* tls = (uintptr_t*)key;
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": 4927,
5+
"a.js.gz": 2352,
6+
"a.wasm": 10895,
7+
"a.wasm.gz": 6927,
8+
"total": 16385,
9+
"total_gz": 9656
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": 21310,
5+
"a.js.gz": 8280,
66
"a.mem": 3171,
77
"a.mem.gz": 2715,
8-
"total": 25056,
9-
"total_gz": 11364
8+
"total": 25069,
9+
"total_gz": 11381
1010
}

0 commit comments

Comments
 (0)