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