Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/runtime_init_memory.js
4128 views
1
/**
2
* @license
3
* Copyright 2019 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
// Create the wasm memory. (Note: this only applies if IMPORTED_MEMORY is defined)
8
#if !IMPORTED_MEMORY
9
{{{ throw "this file should not be be included when IMPORTED_MEMORY is set"; }}}
10
#endif
11
12
// check for full engine support (use string 'subarray' to avoid closure compiler confusion)
13
14
function initMemory() {
15
#if WASM_ESM_INTEGRATION && PTHREADS
16
if (ENVIRONMENT_IS_PTHREAD) {
17
wasmMemory = globalThis.wasmMemory;
18
assert(wasmMemory);
19
updateMemoryViews();
20
}
21
#endif
22
23
{{{ runIfWorkerThread('return') }}}
24
25
#if expectToReceiveOnModule('wasmMemory')
26
if (Module['wasmMemory']) {
27
wasmMemory = Module['wasmMemory'];
28
} else
29
#endif
30
{
31
var INITIAL_MEMORY = {{{ makeModuleReceiveExpr('INITIAL_MEMORY', INITIAL_MEMORY) }}};
32
33
#if ASSERTIONS
34
assert(INITIAL_MEMORY >= {{{STACK_SIZE}}}, 'INITIAL_MEMORY should be larger than STACK_SIZE, was ' + INITIAL_MEMORY + '! (STACK_SIZE=' + {{{STACK_SIZE}}} + ')');
35
#endif
36
/** @suppress {checkTypes} */
37
#if MINIMAL_RUNTIME && WASM_WORKERS
38
wasmMemory = Module['mem'] || new WebAssembly.Memory({
39
#else
40
wasmMemory = new WebAssembly.Memory({
41
#endif
42
'initial': {{{ toIndexType(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
43
#if ALLOW_MEMORY_GROWTH
44
// In theory we should not need to emit the maximum if we want "unlimited"
45
// or 4GB of memory, but VMs error on that atm, see
46
// https://github.com/emscripten-core/emscripten/issues/14130
47
// And in the pthreads case we definitely need to emit a maximum. So
48
// always emit one.
49
'maximum': {{{ toIndexType(MAXIMUM_MEMORY / WASM_PAGE_SIZE) }}},
50
#else
51
'maximum': {{{ toIndexType(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
52
#endif // ALLOW_MEMORY_GROWTH
53
#if SHARED_MEMORY
54
'shared': true,
55
#endif
56
#if MEMORY64 == 1
57
'address': 'i64',
58
#endif
59
});
60
}
61
62
updateMemoryViews();
63
}
64
65
#if WASM_ESM_INTEGRATION || MINIMAL_RUNTIME
66
initMemory();
67
#endif
68
69