Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/runtime_common.js
4128 views
1
/**
2
* @license
3
* Copyright 2024 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
#include "runtime_stack_check.js"
8
#include "runtime_exceptions.js"
9
#include "runtime_debug.js"
10
11
#if SAFE_HEAP
12
#include "runtime_safe_heap.js"
13
#endif
14
15
#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH && !GROWABLE_ARRAYBUFFERS
16
// Support for growable heap + pthreads, where the buffer may change, so JS views
17
// must be updated.
18
function growMemViews() {
19
// `updateMemoryViews` updates all the views simultaneously, so it's enough to check any of them.
20
if (wasmMemory.buffer != HEAP8.buffer) {
21
updateMemoryViews();
22
}
23
}
24
#endif
25
26
#if USE_ASAN
27
#include "runtime_asan.js"
28
#endif
29
30
#if MODULARIZE
31
var readyPromiseResolve, readyPromiseReject;
32
#endif
33
34
#if (PTHREADS || WASM_WORKERS) && (ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION)
35
if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
36
// Create as web-worker-like an environment as we can.
37
var parentPort = worker_threads['parentPort'];
38
parentPort.on('message', (msg) => global.onmessage?.({ data: msg }));
39
Object.assign(globalThis, {
40
self: global,
41
postMessage: (msg) => parentPort['postMessage'](msg),
42
});
43
// Node.js Workers do not pass postMessage()s and uncaught exception events to the parent
44
// thread necessarily in the same order where they were generated in sequential program order.
45
// See https://github.com/nodejs/node/issues/59617
46
// To remedy this, capture all uncaughtExceptions in the Worker, and sequentialize those over
47
// to the same postMessage pipe that other messages use.
48
process.on("uncaughtException", (err) => {
49
#if PTHREADS_DEBUG
50
dbg(`uncaughtException on worker thread: ${err.message}`);
51
#endif
52
postMessage({ cmd: 'uncaughtException', error: err });
53
// Also shut down the Worker to match the same semantics as if this uncaughtException
54
// handler was not registered.
55
// (n.b. this will not shut down the whole Node.js app process, but just the Worker)
56
process.exit(1);
57
});
58
}
59
#endif // (PTHREADS || WASM_WORKERS) && (ENVIRONMENT_MAY_BE_NODE && !WASM_ESM_INTEGRATION)
60
61
#if PTHREADS
62
#include "runtime_pthread.js"
63
#endif
64
65
#if WASM_WORKERS
66
#include "wasm_worker.js"
67
#endif
68
69
#if AUDIO_WORKLET
70
#include "audio_worklet.js"
71
#endif
72
73
// Memory management
74
75
#if !WASM_ESM_INTEGRATION || IMPORTED_MEMORY
76
var wasmMemory;
77
#endif
78
79
var
80
/** @type {!Int8Array} */
81
HEAP8,
82
/** @type {!Uint8Array} */
83
HEAPU8,
84
/** @type {!Int16Array} */
85
HEAP16,
86
/** @type {!Uint16Array} */
87
HEAPU16,
88
/** @type {!Int32Array} */
89
HEAP32,
90
/** @type {!Uint32Array} */
91
HEAPU32,
92
/** @type {!Float32Array} */
93
HEAPF32,
94
/** @type {!Float64Array} */
95
HEAPF64;
96
97
#if WASM_BIGINT
98
// BigInt64Array type is not correctly defined in closure
99
var
100
/** not-@type {!BigInt64Array} */
101
HEAP64,
102
/* BigUint64Array type is not correctly defined in closure
103
/** not-@type {!BigUint64Array} */
104
HEAPU64;
105
#endif
106
107
#if SUPPORT_BIG_ENDIAN
108
/** @type {!DataView} */
109
var HEAP_DATA_VIEW;
110
#endif
111
112
#if !MINIMAL_RUNTIME || ASSERTIONS || SAFE_HEAP || USE_ASAN || MODULARIZE
113
var runtimeInitialized = false;
114
#endif
115
116
#if EXIT_RUNTIME
117
var runtimeExited = false;
118
#endif
119
120
{{{
121
// Helper function to export a heap symbol on the module object,
122
// if requested.
123
const shouldExportHeap = (x) => {
124
let shouldExport = false;
125
if (MODULARIZE && EXPORT_ALL) {
126
shouldExport = true;
127
} else if (EXPORTED_RUNTIME_METHODS.includes(x)) {
128
shouldExport = true;
129
}
130
return shouldExport;
131
}
132
const maybeExportHeap = (x) => {
133
if (shouldExportHeap(x) && MODULARIZE != 'instance') {
134
return `Module['${x}'] = `;
135
}
136
return '';
137
};
138
}}}
139
140
function updateMemoryViews() {
141
#if GROWABLE_ARRAYBUFFERS
142
var b = wasmMemory.toResizableBuffer();
143
#else
144
var b = wasmMemory.buffer;
145
#endif
146
{{{ maybeExportHeap('HEAP8') }}}HEAP8 = new Int8Array(b);
147
{{{ maybeExportHeap('HEAP16') }}}HEAP16 = new Int16Array(b);
148
{{{ maybeExportHeap('HEAPU8') }}}HEAPU8 = new Uint8Array(b);
149
{{{ maybeExportHeap('HEAPU16') }}}HEAPU16 = new Uint16Array(b);
150
{{{ maybeExportHeap('HEAP32') }}}HEAP32 = new Int32Array(b);
151
{{{ maybeExportHeap('HEAPU32') }}}HEAPU32 = new Uint32Array(b);
152
{{{ maybeExportHeap('HEAPF32') }}}HEAPF32 = new Float32Array(b);
153
{{{ maybeExportHeap('HEAPF64') }}}HEAPF64 = new Float64Array(b);
154
#if WASM_BIGINT
155
{{{ maybeExportHeap('HEAP64') }}}HEAP64 = new BigInt64Array(b);
156
{{{ maybeExportHeap('HEAPU64') }}}HEAPU64 = new BigUint64Array(b);
157
#endif
158
#if SUPPORT_BIG_ENDIAN
159
{{{ maybeExportHeap('HEAP_DATA_VIEW') }}} HEAP_DATA_VIEW = new DataView(b);
160
LE_HEAP_UPDATE();
161
#endif
162
}
163
164
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
165
// The performance global was added to node in v16.0.0:
166
// https://nodejs.org/api/globals.html#performance
167
if (ENVIRONMENT_IS_NODE) {
168
// This is needed for emscripten_get_now and for pthreads support which
169
// depends on it for accurate timing.
170
// Use `global` rather than `globalThis` here since older versions of node
171
// don't have `globalThis`.
172
global.performance ??= require('perf_hooks').performance;
173
}
174
#endif
175
176
#if IMPORTED_MEMORY
177
// In non-standalone/normal mode, we create the memory here.
178
#include "runtime_init_memory.js"
179
#endif // !IMPORTED_MEMORY && ASSERTIONS
180
181
#include "memoryprofiler.js"
182
183