File size: 1,979 Bytes
00df61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * @license
 * Copyright 2019 The Emscripten Authors
 * SPDX-License-Identifier: MIT
 */

// Create the wasm memory. (Note: this only applies if IMPORTED_MEMORY is defined)
#if !IMPORTED_MEMORY
#error "this file should not be included when IMPORTED_MEMORY is set"
#endif

// check for full engine support (use string 'subarray' to avoid closure compiler confusion)

function initMemory() {
#if WASM_ESM_INTEGRATION && PTHREADS
  if (ENVIRONMENT_IS_PTHREAD) {
    wasmMemory = globalThis.wasmMemory;
    assert(wasmMemory);
    updateMemoryViews();
  }
#endif

  {{{ runIfWorkerThread('return') }}}

#if expectToReceiveOnModule('wasmMemory')
  if (Module['wasmMemory']) {
    wasmMemory = Module['wasmMemory'];
  } else
#endif
  {
    var INITIAL_MEMORY = {{{ makeModuleReceiveExpr('INITIAL_MEMORY', INITIAL_MEMORY) }}};

#if ASSERTIONS
    assert(INITIAL_MEMORY >= {{{ STACK_SIZE }}}, `INITIAL_MEMORY should be larger than STACK_SIZE, was ${INITIAL_MEMORY}! (STACK_SIZE={{{ STACK_SIZE }}})`);
#endif
    /** @suppress {checkTypes} */
#if MINIMAL_RUNTIME && WASM_WORKERS
    wasmMemory = Module['mem'] || new WebAssembly.Memory({
#else
    wasmMemory = new WebAssembly.Memory({
#endif
      'initial': {{{ toIndexType(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
#if ALLOW_MEMORY_GROWTH
      // In theory we should not need to emit the maximum if we want "unlimited"
      // or 4GB of memory, but VMs error on that atm, see
      // https://github.com/emscripten-core/emscripten/issues/14130
      // And in the pthreads case we definitely need to emit a maximum. So
      // always emit one.
      'maximum': {{{ toIndexType(MAXIMUM_MEMORY / WASM_PAGE_SIZE) }}},
#else
      'maximum': {{{ toIndexType(`INITIAL_MEMORY / ${WASM_PAGE_SIZE}`) }}},
#endif // ALLOW_MEMORY_GROWTH
#if SHARED_MEMORY
      'shared': true,
#endif
#if MEMORY64 == 1
      'address': 'i64',
#endif
    });
  }

  updateMemoryViews();
}

#if WASM_ESM_INTEGRATION || MINIMAL_RUNTIME
initMemory();
#endif