normaliz.wasm 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4985 @@
1
+ // This code implements the `-sMODULARIZE` settings by taking the generated
2
+ // JS program code (INNER_JS_CODE) and wrapping it in a factory function.
3
+
4
+ // When targeting node and ES6 we use `await import ..` in the generated code
5
+ // so the outer function needs to be marked as async.
6
+ async function createNormaliz(moduleArg = {}) {
7
+ var moduleRtn;
8
+
9
+ // include: shell.js
10
+ // include: minimum_runtime_check.js
11
+ (function() {
12
+ // "30.0.0" -> 300000
13
+ function humanReadableVersionToPacked(str) {
14
+ str = str.split('-')[0]; // Remove any trailing part from e.g. "12.53.3-alpha"
15
+ var vers = str.split('.').slice(0, 3);
16
+ while(vers.length < 3) vers.push('00');
17
+ vers = vers.map((n, i, arr) => n.padStart(2, '0'));
18
+ return vers.join('');
19
+ }
20
+ // 300000 -> "30.0.0"
21
+ var packedVersionToHumanReadable = n => [n / 10000 | 0, (n / 100 | 0) % 100, n % 100].join('.');
22
+
23
+ var TARGET_NOT_SUPPORTED = 2147483647;
24
+
25
+ // Note: We use a typeof check here instead of optional chaining using
26
+ // globalThis because older browsers might not have globalThis defined.
27
+ var currentNodeVersion = typeof process !== 'undefined' && process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED;
28
+ if (currentNodeVersion < 160000) {
29
+ throw new Error(`This emscripten-generated code requires node v${ packedVersionToHumanReadable(160000) } (detected v${packedVersionToHumanReadable(currentNodeVersion)})`);
30
+ }
31
+
32
+ var userAgent = typeof navigator !== 'undefined' && navigator.userAgent;
33
+ if (!userAgent) {
34
+ return;
35
+ }
36
+
37
+ var currentSafariVersion = userAgent.includes("Safari/") && !userAgent.includes("Chrome/") && userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/) ? humanReadableVersionToPacked(userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/)[1]) : TARGET_NOT_SUPPORTED;
38
+ if (currentSafariVersion < 150000) {
39
+ throw new Error(`This emscripten-generated code requires Safari v${ packedVersionToHumanReadable(150000) } (detected v${currentSafariVersion})`);
40
+ }
41
+
42
+ var currentFirefoxVersion = userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
43
+ if (currentFirefoxVersion < 79) {
44
+ throw new Error(`This emscripten-generated code requires Firefox v79 (detected v${currentFirefoxVersion})`);
45
+ }
46
+
47
+ var currentChromeVersion = userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
48
+ if (currentChromeVersion < 85) {
49
+ throw new Error(`This emscripten-generated code requires Chrome v85 (detected v${currentChromeVersion})`);
50
+ }
51
+ })();
52
+
53
+ // end include: minimum_runtime_check.js
54
+ // The Module object: Our interface to the outside world. We import
55
+ // and export values on it. There are various ways Module can be used:
56
+ // 1. Not defined. We create it here
57
+ // 2. A function parameter, function(moduleArg) => Promise<Module>
58
+ // 3. pre-run appended it, var Module = {}; ..generated code..
59
+ // 4. External script tag defines var Module.
60
+ // We need to check if Module already exists (e.g. case 3 above).
61
+ // Substitution will be replaced with actual code on later stage of the build,
62
+ // this way Closure Compiler will not mangle it (e.g. case 4. above).
63
+ // Note that if you want to run closure, and also to use Module
64
+ // after the generated code, you will need to define var Module = {};
65
+ // before the code. Then that object will be used in the code, and you
66
+ // can continue to use Module afterwards as well.
67
+ var Module = moduleArg;
68
+
69
+ // Determine the runtime environment we are in. You can customize this by
70
+ // setting the ENVIRONMENT setting at compile time (see settings.js).
71
+
72
+ // Attempt to auto-detect the environment
73
+ var ENVIRONMENT_IS_WEB = !!globalThis.window;
74
+ var ENVIRONMENT_IS_WORKER = !!globalThis.WorkerGlobalScope;
75
+ // N.b. Electron.js environment is simultaneously a NODE-environment, but
76
+ // also a web environment.
77
+ var ENVIRONMENT_IS_NODE = globalThis.process?.versions?.node && globalThis.process?.type != 'renderer';
78
+ var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
79
+
80
+ if (ENVIRONMENT_IS_NODE) {
81
+ // When building an ES module `require` is not normally available.
82
+ // We need to use `createRequire()` to construct the require()` function.
83
+ const { createRequire } = await import('node:module');
84
+ /** @suppress{duplicate} */
85
+ var require = createRequire(import.meta.url);
86
+
87
+ }
88
+
89
+ // --pre-jses are emitted after the Module integration code, so that they can
90
+ // refer to Module (if they choose; they can also define Module)
91
+
92
+
93
+ var arguments_ = [];
94
+ var thisProgram = './this.program';
95
+ var quit_ = (status, toThrow) => {
96
+ throw toThrow;
97
+ };
98
+
99
+ var _scriptName = import.meta.url;
100
+
101
+ // `/` should be present at the end if `scriptDirectory` is not empty
102
+ var scriptDirectory = '';
103
+ function locateFile(path) {
104
+ if (Module['locateFile']) {
105
+ return Module['locateFile'](path, scriptDirectory);
106
+ }
107
+ return scriptDirectory + path;
108
+ }
109
+
110
+ // Hooks that are implemented differently in different runtime environments.
111
+ var readAsync, readBinary;
112
+
113
+ if (ENVIRONMENT_IS_NODE) {
114
+ const isNode = globalThis.process?.versions?.node && globalThis.process?.type != 'renderer';
115
+ if (!isNode) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
116
+
117
+ // These modules will usually be used on Node.js. Load them eagerly to avoid
118
+ // the complexity of lazy-loading.
119
+ var fs = require('node:fs');
120
+
121
+ if (_scriptName.startsWith('file:')) {
122
+ scriptDirectory = require('node:path').dirname(require('node:url').fileURLToPath(_scriptName)) + '/';
123
+ }
124
+
125
+ // include: node_shell_read.js
126
+ readBinary = (filename) => {
127
+ // We need to re-wrap `file://` strings to URLs.
128
+ filename = isFileURI(filename) ? new URL(filename) : filename;
129
+ var ret = fs.readFileSync(filename);
130
+ assert(Buffer.isBuffer(ret));
131
+ return ret;
132
+ };
133
+
134
+ readAsync = async (filename, binary = true) => {
135
+ // See the comment in the `readBinary` function.
136
+ filename = isFileURI(filename) ? new URL(filename) : filename;
137
+ var ret = fs.readFileSync(filename, binary ? undefined : 'utf8');
138
+ assert(binary ? Buffer.isBuffer(ret) : typeof ret == 'string');
139
+ return ret;
140
+ };
141
+ // end include: node_shell_read.js
142
+ if (process.argv.length > 1) {
143
+ thisProgram = process.argv[1].replace(/\\/g, '/');
144
+ }
145
+
146
+ arguments_ = process.argv.slice(2);
147
+
148
+ quit_ = (status, toThrow) => {
149
+ process.exitCode = status;
150
+ throw toThrow;
151
+ };
152
+
153
+ } else
154
+ if (ENVIRONMENT_IS_SHELL) {
155
+
156
+ } else
157
+
158
+ // Note that this includes Node.js workers when relevant (pthreads is enabled).
159
+ // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and
160
+ // ENVIRONMENT_IS_NODE.
161
+ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
162
+ try {
163
+ scriptDirectory = new URL('.', _scriptName).href; // includes trailing slash
164
+ } catch {
165
+ // Must be a `blob:` or `data:` URL (e.g. `blob:http://site.com/etc/etc`), we cannot
166
+ // infer anything from them.
167
+ }
168
+
169
+ if (!(globalThis.window || globalThis.WorkerGlobalScope)) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
170
+
171
+ {
172
+ // include: web_or_worker_shell_read.js
173
+ readAsync = async (url) => {
174
+ assert(!isFileURI(url), "readAsync does not work with file:// URLs");
175
+ var response = await fetch(url, { credentials: 'same-origin' });
176
+ if (response.ok) {
177
+ return response.arrayBuffer();
178
+ }
179
+ throw new Error(response.status + ' : ' + response.url);
180
+ };
181
+ // end include: web_or_worker_shell_read.js
182
+ }
183
+ } else
184
+ {
185
+ throw new Error('environment detection error');
186
+ }
187
+
188
+ var out = console.log.bind(console);
189
+ var err = console.error.bind(console);
190
+
191
+ var IDBFS = 'IDBFS is no longer included by default; build with -lidbfs.js';
192
+ var PROXYFS = 'PROXYFS is no longer included by default; build with -lproxyfs.js';
193
+ var WORKERFS = 'WORKERFS is no longer included by default; build with -lworkerfs.js';
194
+ var FETCHFS = 'FETCHFS is no longer included by default; build with -lfetchfs.js';
195
+ var ICASEFS = 'ICASEFS is no longer included by default; build with -licasefs.js';
196
+ var JSFILEFS = 'JSFILEFS is no longer included by default; build with -ljsfilefs.js';
197
+ var OPFS = 'OPFS is no longer included by default; build with -lopfs.js';
198
+
199
+ var NODEFS = 'NODEFS is no longer included by default; build with -lnodefs.js';
200
+
201
+ // perform assertions in shell.js after we set up out() and err(), as otherwise
202
+ // if an assertion fails it cannot print the message
203
+
204
+ assert(!ENVIRONMENT_IS_WORKER, 'worker environment detected but not enabled at build time. Add `worker` to `-sENVIRONMENT` to enable.');
205
+
206
+ assert(!ENVIRONMENT_IS_SHELL, 'shell environment detected but not enabled at build time. Add `shell` to `-sENVIRONMENT` to enable.');
207
+
208
+ // end include: shell.js
209
+
210
+ // include: preamble.js
211
+ // === Preamble library stuff ===
212
+
213
+ // Documentation for the public APIs defined in this file must be updated in:
214
+ // site/source/docs/api_reference/preamble.js.rst
215
+ // A prebuilt local version of the documentation is available at:
216
+ // site/build/text/docs/api_reference/preamble.js.txt
217
+ // You can also build docs locally as HTML or other formats in site/
218
+ // An online HTML version (which may be of a different version of Emscripten)
219
+ // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
220
+
221
+ var wasmBinary;
222
+
223
+ if (!globalThis.WebAssembly) {
224
+ err('no native wasm support detected');
225
+ }
226
+
227
+ // Wasm globals
228
+
229
+ //========================================
230
+ // Runtime essentials
231
+ //========================================
232
+
233
+ // whether we are quitting the application. no code should run after this.
234
+ // set in exit() and abort()
235
+ var ABORT = false;
236
+
237
+ // set by exit() and abort(). Passed to 'onExit' handler.
238
+ // NOTE: This is also used as the process return code in shell environments
239
+ // but only when noExitRuntime is false.
240
+ var EXITSTATUS;
241
+
242
+ // In STRICT mode, we only define assert() when ASSERTIONS is set. i.e. we
243
+ // don't define it at all in release modes. This matches the behaviour of
244
+ // MINIMAL_RUNTIME.
245
+ // TODO(sbc): Make this the default even without STRICT enabled.
246
+ /** @type {function(*, string=)} */
247
+ function assert(condition, text) {
248
+ if (!condition) {
249
+ abort('Assertion failed' + (text ? ': ' + text : ''));
250
+ }
251
+ }
252
+
253
+ // We used to include malloc/free by default in the past. Show a helpful error in
254
+ // builds with assertions.
255
+ function _malloc() {
256
+ abort('malloc() called but not included in the build - add `_malloc` to EXPORTED_FUNCTIONS');
257
+ }
258
+ function _free() {
259
+ // Show a helpful error since we used to include free by default in the past.
260
+ abort('free() called but not included in the build - add `_free` to EXPORTED_FUNCTIONS');
261
+ }
262
+
263
+ /**
264
+ * Indicates whether filename is delivered via file protocol (as opposed to http/https)
265
+ * @noinline
266
+ */
267
+ var isFileURI = (filename) => filename.startsWith('file://');
268
+
269
+ // include: runtime_common.js
270
+ // include: runtime_stack_check.js
271
+ // Initializes the stack cookie. Called at the startup of main and at the startup of each thread in pthreads mode.
272
+ function writeStackCookie() {
273
+ var max = _emscripten_stack_get_end();
274
+ assert((max & 3) == 0);
275
+ // If the stack ends at address zero we write our cookies 4 bytes into the
276
+ // stack. This prevents interference with SAFE_HEAP and ASAN which also
277
+ // monitor writes to address zero.
278
+ if (max == 0) {
279
+ max += 4;
280
+ }
281
+ // The stack grow downwards towards _emscripten_stack_get_end.
282
+ // We write cookies to the final two words in the stack and detect if they are
283
+ // ever overwritten.
284
+ HEAPU32[((max)>>2)] = 0x02135467;
285
+ HEAPU32[(((max)+(4))>>2)] = 0x89BACDFE;
286
+ // Also test the global address 0 for integrity.
287
+ HEAPU32[((0)>>2)] = 1668509029;
288
+ }
289
+
290
+ function checkStackCookie() {
291
+ if (ABORT) return;
292
+ var max = _emscripten_stack_get_end();
293
+ // See writeStackCookie().
294
+ if (max == 0) {
295
+ max += 4;
296
+ }
297
+ var cookie1 = HEAPU32[((max)>>2)];
298
+ var cookie2 = HEAPU32[(((max)+(4))>>2)];
299
+ if (cookie1 != 0x02135467 || cookie2 != 0x89BACDFE) {
300
+ abort(`Stack overflow! Stack cookie has been overwritten at ${ptrToString(max)}, expected hex dwords 0x89BACDFE and 0x2135467, but received ${ptrToString(cookie2)} ${ptrToString(cookie1)}`);
301
+ }
302
+ // Also test the global address 0 for integrity.
303
+ if (HEAPU32[((0)>>2)] != 0x63736d65 /* 'emsc' */) {
304
+ abort('Runtime error: The application has corrupted its heap memory area (address zero)!');
305
+ }
306
+ }
307
+ // end include: runtime_stack_check.js
308
+ // include: runtime_exceptions.js
309
+ // end include: runtime_exceptions.js
310
+ // include: runtime_debug.js
311
+ var runtimeDebug = true; // Switch to false at runtime to disable logging at the right times
312
+
313
+ // Used by XXXXX_DEBUG settings to output debug messages.
314
+ function dbg(...args) {
315
+ if (!runtimeDebug && typeof runtimeDebug != 'undefined') return;
316
+ // TODO(sbc): Make this configurable somehow. Its not always convenient for
317
+ // logging to show up as warnings.
318
+ console.warn(...args);
319
+ }
320
+
321
+ // Endianness check
322
+ (() => {
323
+ var h16 = new Int16Array(1);
324
+ var h8 = new Int8Array(h16.buffer);
325
+ h16[0] = 0x6373;
326
+ if (h8[0] !== 0x73 || h8[1] !== 0x63) abort('Runtime error: expected the system to be little-endian! (Run with -sSUPPORT_BIG_ENDIAN to bypass)');
327
+ })();
328
+
329
+ function consumedModuleProp(prop) {
330
+ if (!Object.getOwnPropertyDescriptor(Module, prop)) {
331
+ Object.defineProperty(Module, prop, {
332
+ configurable: true,
333
+ set() {
334
+ abort(`Attempt to set \`Module.${prop}\` after it has already been processed. This can happen, for example, when code is injected via '--post-js' rather than '--pre-js'`);
335
+
336
+ }
337
+ });
338
+ }
339
+ }
340
+
341
+ function makeInvalidEarlyAccess(name) {
342
+ return () => assert(false, `call to '${name}' via reference taken before Wasm module initialization`);
343
+
344
+ }
345
+
346
+ function ignoredModuleProp(prop) {
347
+ if (Object.getOwnPropertyDescriptor(Module, prop)) {
348
+ abort(`\`Module.${prop}\` was supplied but \`${prop}\` not included in INCOMING_MODULE_JS_API`);
349
+ }
350
+ }
351
+
352
+ // forcing the filesystem exports a few things by default
353
+ function isExportedByForceFilesystem(name) {
354
+ return name === 'FS_createPath' ||
355
+ name === 'FS_createDataFile' ||
356
+ name === 'FS_createPreloadedFile' ||
357
+ name === 'FS_preloadFile' ||
358
+ name === 'FS_unlink' ||
359
+ name === 'addRunDependency' ||
360
+ // The old FS has some functionality that WasmFS lacks.
361
+ name === 'FS_createLazyFile' ||
362
+ name === 'FS_createDevice' ||
363
+ name === 'removeRunDependency';
364
+ }
365
+
366
+ function missingLibrarySymbol(sym) {
367
+
368
+ // Any symbol that is not included from the JS library is also (by definition)
369
+ // not exported on the Module object.
370
+ unexportedRuntimeSymbol(sym);
371
+ }
372
+
373
+ function unexportedRuntimeSymbol(sym) {
374
+ if (!Object.getOwnPropertyDescriptor(Module, sym)) {
375
+ Object.defineProperty(Module, sym, {
376
+ configurable: true,
377
+ get() {
378
+ var msg = `'${sym}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the Emscripten FAQ)`;
379
+ if (isExportedByForceFilesystem(sym)) {
380
+ msg += '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you';
381
+ }
382
+ abort(msg);
383
+ },
384
+ });
385
+ }
386
+ }
387
+
388
+ // end include: runtime_debug.js
389
+ var readyPromiseResolve, readyPromiseReject;
390
+
391
+ // Memory management
392
+
393
+ var runtimeInitialized = false;
394
+
395
+
396
+
397
+ function updateMemoryViews() {
398
+ var b = wasmMemory.buffer;
399
+ HEAP8 = new Int8Array(b);
400
+ HEAP16 = new Int16Array(b);
401
+ HEAPU8 = new Uint8Array(b);
402
+ HEAPU16 = new Uint16Array(b);
403
+ HEAP32 = new Int32Array(b);
404
+ HEAPU32 = new Uint32Array(b);
405
+ HEAPF32 = new Float32Array(b);
406
+ HEAPF64 = new Float64Array(b);
407
+ HEAP64 = new BigInt64Array(b);
408
+ HEAPU64 = new BigUint64Array(b);
409
+ }
410
+
411
+ // include: memoryprofiler.js
412
+ // end include: memoryprofiler.js
413
+ // end include: runtime_common.js
414
+ assert(globalThis.Int32Array && globalThis.Float64Array && Int32Array.prototype.subarray && Int32Array.prototype.set,
415
+ 'JS engine does not provide full typed array support');
416
+
417
+ function preRun() {
418
+ if (Module['preRun']) {
419
+ if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
420
+ while (Module['preRun'].length) {
421
+ addOnPreRun(Module['preRun'].shift());
422
+ }
423
+ }
424
+ consumedModuleProp('preRun');
425
+ // Begin ATPRERUNS hooks
426
+ callRuntimeCallbacks(onPreRuns);
427
+ // End ATPRERUNS hooks
428
+ }
429
+
430
+ function initRuntime() {
431
+ assert(!runtimeInitialized);
432
+ runtimeInitialized = true;
433
+
434
+ checkStackCookie();
435
+
436
+ // Begin ATINITS hooks
437
+ if (!Module['noFSInit'] && !FS.initialized) FS.init();
438
+ TTY.init();
439
+ // End ATINITS hooks
440
+
441
+ wasmExports['__wasm_call_ctors']();
442
+
443
+ // Begin ATPOSTCTORS hooks
444
+ FS.ignorePermissions = false;
445
+ // End ATPOSTCTORS hooks
446
+ }
447
+
448
+ function preMain() {
449
+ checkStackCookie();
450
+ // No ATMAINS hooks
451
+ }
452
+
453
+ function postRun() {
454
+ checkStackCookie();
455
+ // PThreads reuse the runtime from the main thread.
456
+
457
+ if (Module['postRun']) {
458
+ if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
459
+ while (Module['postRun'].length) {
460
+ addOnPostRun(Module['postRun'].shift());
461
+ }
462
+ }
463
+ consumedModuleProp('postRun');
464
+
465
+ // Begin ATPOSTRUNS hooks
466
+ callRuntimeCallbacks(onPostRuns);
467
+ // End ATPOSTRUNS hooks
468
+ }
469
+
470
+ /** @param {string|number=} what */
471
+ function abort(what) {
472
+ Module['onAbort']?.(what);
473
+
474
+ what = 'Aborted(' + what + ')';
475
+ // TODO(sbc): Should we remove printing and leave it up to whoever
476
+ // catches the exception?
477
+ err(what);
478
+
479
+ ABORT = true;
480
+
481
+ // Use a wasm runtime error, because a JS error might be seen as a foreign
482
+ // exception, which means we'd run destructors on it. We need the error to
483
+ // simply make the program stop.
484
+ // FIXME This approach does not work in Wasm EH because it currently does not assume
485
+ // all RuntimeErrors are from traps; it decides whether a RuntimeError is from
486
+ // a trap or not based on a hidden field within the object. So at the moment
487
+ // we don't have a way of throwing a wasm trap from JS. TODO Make a JS API that
488
+ // allows this in the wasm spec.
489
+
490
+ // Suppress closure compiler warning here. Closure compiler's builtin extern
491
+ // definition for WebAssembly.RuntimeError claims it takes no arguments even
492
+ // though it can.
493
+ // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure gets fixed.
494
+ /** @suppress {checkTypes} */
495
+ var e = new WebAssembly.RuntimeError(what);
496
+
497
+ readyPromiseReject?.(e);
498
+ // Throw the error whether or not MODULARIZE is set because abort is used
499
+ // in code paths apart from instantiation where an exception is expected
500
+ // to be thrown when abort is called.
501
+ throw e;
502
+ }
503
+
504
+ function createExportWrapper(name, nargs) {
505
+ return (...args) => {
506
+ assert(runtimeInitialized, `native function \`${name}\` called before runtime initialization`);
507
+ var f = wasmExports[name];
508
+ assert(f, `exported native function \`${name}\` not found`);
509
+ // Only assert for too many arguments. Too few can be valid since the missing arguments will be zero filled.
510
+ assert(args.length <= nargs, `native function \`${name}\` called with ${args.length} args but expects ${nargs}`);
511
+ return f(...args);
512
+ };
513
+ }
514
+
515
+ var wasmBinaryFile;
516
+
517
+ function findWasmBinary() {
518
+
519
+ if (Module['locateFile']) {
520
+ return locateFile('normaliz.wasm');
521
+ }
522
+
523
+ // Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
524
+ return new URL('normaliz.wasm', import.meta.url).href;
525
+
526
+ }
527
+
528
+ function getBinarySync(file) {
529
+ if (file == wasmBinaryFile && wasmBinary) {
530
+ return new Uint8Array(wasmBinary);
531
+ }
532
+ if (readBinary) {
533
+ return readBinary(file);
534
+ }
535
+ // Throwing a plain string here, even though it not normally advisable since
536
+ // this gets turning into an `abort` in instantiateArrayBuffer.
537
+ throw 'both async and sync fetching of the wasm failed';
538
+ }
539
+
540
+ async function getWasmBinary(binaryFile) {
541
+ // If we don't have the binary yet, load it asynchronously using readAsync.
542
+ if (!wasmBinary) {
543
+ // Fetch the binary using readAsync
544
+ try {
545
+ var response = await readAsync(binaryFile);
546
+ return new Uint8Array(response);
547
+ } catch {
548
+ // Fall back to getBinarySync below;
549
+ }
550
+ }
551
+
552
+ // Otherwise, getBinarySync should be able to get it synchronously
553
+ return getBinarySync(binaryFile);
554
+ }
555
+
556
+ async function instantiateArrayBuffer(binaryFile, imports) {
557
+ try {
558
+ var binary = await getWasmBinary(binaryFile);
559
+ var instance = await WebAssembly.instantiate(binary, imports);
560
+ return instance;
561
+ } catch (reason) {
562
+ err(`failed to asynchronously prepare wasm: ${reason}`);
563
+
564
+ // Warn on some common problems.
565
+ if (isFileURI(binaryFile)) {
566
+ err(`warning: Loading from a file URI (${binaryFile}) is not supported in most browsers. See https://emscripten.org/docs/getting_started/FAQ.html#how-do-i-run-a-local-webserver-for-testing-why-does-my-program-stall-in-downloading-or-preparing`);
567
+ }
568
+ abort(reason);
569
+ }
570
+ }
571
+
572
+ async function instantiateAsync(binary, binaryFile, imports) {
573
+ if (!binary
574
+ // Avoid instantiateStreaming() on Node.js environment for now, as while
575
+ // Node.js v18.1.0 implements it, it does not have a full fetch()
576
+ // implementation yet.
577
+ //
578
+ // Reference:
579
+ // https://github.com/emscripten-core/emscripten/pull/16917
580
+ && !ENVIRONMENT_IS_NODE
581
+ ) {
582
+ try {
583
+ var response = fetch(binaryFile, { credentials: 'same-origin' });
584
+ var instantiationResult = await WebAssembly.instantiateStreaming(response, imports);
585
+ return instantiationResult;
586
+ } catch (reason) {
587
+ // We expect the most common failure cause to be a bad MIME type for the binary,
588
+ // in which case falling back to ArrayBuffer instantiation should work.
589
+ err(`wasm streaming compile failed: ${reason}`);
590
+ err('falling back to ArrayBuffer instantiation');
591
+ // fall back of instantiateArrayBuffer below
592
+ };
593
+ }
594
+ return instantiateArrayBuffer(binaryFile, imports);
595
+ }
596
+
597
+ function getWasmImports() {
598
+ // prepare imports
599
+ var imports = {
600
+ 'env': wasmImports,
601
+ 'wasi_snapshot_preview1': wasmImports,
602
+ };
603
+ return imports;
604
+ }
605
+
606
+ // Create the wasm instance.
607
+ // Receives the wasm imports, returns the exports.
608
+ async function createWasm() {
609
+ // Load the wasm module and create an instance of using native support in the JS engine.
610
+ // handle a generated wasm instance, receiving its exports and
611
+ // performing other necessary setup
612
+ /** @param {WebAssembly.Module=} module*/
613
+ function receiveInstance(instance, module) {
614
+ wasmExports = instance.exports;
615
+
616
+ assignWasmExports(wasmExports);
617
+
618
+ updateMemoryViews();
619
+
620
+ return wasmExports;
621
+ }
622
+
623
+ // Prefer streaming instantiation if available.
624
+ // Async compilation can be confusing when an error on the page overwrites Module
625
+ // (for example, if the order of elements is wrong, and the one defining Module is
626
+ // later), so we save Module and check it later.
627
+ var trueModule = Module;
628
+ function receiveInstantiationResult(result) {
629
+ // 'result' is a ResultObject object which has both the module and instance.
630
+ // receiveInstance() will swap in the exports (to Module.asm) so they can be called
631
+ assert(Module === trueModule, 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?');
632
+ trueModule = null;
633
+ // TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193, the above line no longer optimizes out down to the following line.
634
+ // When the regression is fixed, can restore the above PTHREADS-enabled path.
635
+ return receiveInstance(result['instance']);
636
+ }
637
+
638
+ var info = getWasmImports();
639
+
640
+ // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
641
+ // to manually instantiate the Wasm module themselves. This allows pages to
642
+ // run the instantiation parallel to any other async startup actions they are
643
+ // performing.
644
+ // Also pthreads and wasm workers initialize the wasm instance through this
645
+ // path.
646
+ if (Module['instantiateWasm']) {
647
+ return new Promise((resolve, reject) => {
648
+ try {
649
+ Module['instantiateWasm'](info, (inst, mod) => {
650
+ resolve(receiveInstance(inst, mod));
651
+ });
652
+ } catch(e) {
653
+ err(`Module.instantiateWasm callback failed with error: ${e}`);
654
+ reject(e);
655
+ }
656
+ });
657
+ }
658
+
659
+ wasmBinaryFile ??= findWasmBinary();
660
+ var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
661
+ var exports = receiveInstantiationResult(result);
662
+ return exports;
663
+ }
664
+
665
+ // end include: preamble.js
666
+
667
+ // Begin JS library code
668
+
669
+
670
+ class ExitStatus {
671
+ name = 'ExitStatus';
672
+ constructor(status) {
673
+ this.message = `Program terminated with exit(${status})`;
674
+ this.status = status;
675
+ }
676
+ }
677
+
678
+ /** @type {!Int16Array} */
679
+ var HEAP16;
680
+
681
+ /** @type {!Int32Array} */
682
+ var HEAP32;
683
+
684
+ /** not-@type {!BigInt64Array} */
685
+ var HEAP64;
686
+
687
+ /** @type {!Int8Array} */
688
+ var HEAP8;
689
+
690
+ /** @type {!Float32Array} */
691
+ var HEAPF32;
692
+
693
+ /** @type {!Float64Array} */
694
+ var HEAPF64;
695
+
696
+ /** @type {!Uint16Array} */
697
+ var HEAPU16;
698
+
699
+ /** @type {!Uint32Array} */
700
+ var HEAPU32;
701
+
702
+ /** not-@type {!BigUint64Array} */
703
+ var HEAPU64;
704
+
705
+ /** @type {!Uint8Array} */
706
+ var HEAPU8;
707
+
708
+ var callRuntimeCallbacks = (callbacks) => {
709
+ while (callbacks.length > 0) {
710
+ // Pass the module as the first argument.
711
+ callbacks.shift()(Module);
712
+ }
713
+ };
714
+ var onPostRuns = [];
715
+ var addOnPostRun = (cb) => onPostRuns.push(cb);
716
+
717
+ var onPreRuns = [];
718
+ var addOnPreRun = (cb) => onPreRuns.push(cb);
719
+
720
+
721
+
722
+ /**
723
+ * @param {number} ptr
724
+ * @param {string} type
725
+ */
726
+ function getValue(ptr, type = 'i8') {
727
+ if (type.endsWith('*')) type = '*';
728
+ switch (type) {
729
+ case 'i1': return HEAP8[ptr];
730
+ case 'i8': return HEAP8[ptr];
731
+ case 'i16': return HEAP16[((ptr)>>1)];
732
+ case 'i32': return HEAP32[((ptr)>>2)];
733
+ case 'i64': return HEAP64[((ptr)>>3)];
734
+ case 'float': return HEAPF32[((ptr)>>2)];
735
+ case 'double': return HEAPF64[((ptr)>>3)];
736
+ case '*': return HEAPU32[((ptr)>>2)];
737
+ default: abort(`invalid type for getValue: ${type}`);
738
+ }
739
+ }
740
+
741
+ var noExitRuntime = true;
742
+
743
+ function ptrToString(ptr) {
744
+ assert(typeof ptr === 'number', `ptrToString expects a number, got ${typeof ptr}`);
745
+ // Convert to 32-bit unsigned value
746
+ ptr >>>= 0;
747
+ return '0x' + ptr.toString(16).padStart(8, '0');
748
+ }
749
+
750
+
751
+ /**
752
+ * @param {number} ptr
753
+ * @param {number} value
754
+ * @param {string} type
755
+ */
756
+ function setValue(ptr, value, type = 'i8') {
757
+ if (type.endsWith('*')) type = '*';
758
+ switch (type) {
759
+ case 'i1': HEAP8[ptr] = value; break;
760
+ case 'i8': HEAP8[ptr] = value; break;
761
+ case 'i16': HEAP16[((ptr)>>1)] = value; break;
762
+ case 'i32': HEAP32[((ptr)>>2)] = value; break;
763
+ case 'i64': HEAP64[((ptr)>>3)] = BigInt(value); break;
764
+ case 'float': HEAPF32[((ptr)>>2)] = value; break;
765
+ case 'double': HEAPF64[((ptr)>>3)] = value; break;
766
+ case '*': HEAPU32[((ptr)>>2)] = value; break;
767
+ default: abort(`invalid type for setValue: ${type}`);
768
+ }
769
+ }
770
+
771
+ var stackRestore = (val) => __emscripten_stack_restore(val);
772
+
773
+ var stackSave = () => _emscripten_stack_get_current();
774
+
775
+ var warnOnce = (text) => {
776
+ warnOnce.shown ||= {};
777
+ if (!warnOnce.shown[text]) {
778
+ warnOnce.shown[text] = 1;
779
+ if (ENVIRONMENT_IS_NODE) text = 'warning: ' + text;
780
+ err(text);
781
+ }
782
+ };
783
+
784
+
785
+
786
+ var wasmTableMirror = [];
787
+
788
+
789
+ var getWasmTableEntry = (funcPtr) => {
790
+ var func = wasmTableMirror[funcPtr];
791
+ if (!func) {
792
+ /** @suppress {checkTypes} */
793
+ wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr);
794
+ }
795
+ /** @suppress {checkTypes} */
796
+ assert(wasmTable.get(funcPtr) == func, 'JavaScript-side Wasm function table mirror is out of date!');
797
+ return func;
798
+ };
799
+ var ___call_sighandler = (fp, sig) => getWasmTableEntry(fp)(sig);
800
+
801
+ class ExceptionInfo {
802
+ // excPtr - Thrown object pointer to wrap. Metadata pointer is calculated from it.
803
+ constructor(excPtr) {
804
+ this.excPtr = excPtr;
805
+ this.ptr = excPtr - 24;
806
+ }
807
+
808
+ set_type(type) {
809
+ HEAPU32[(((this.ptr)+(4))>>2)] = type;
810
+ }
811
+
812
+ get_type() {
813
+ return HEAPU32[(((this.ptr)+(4))>>2)];
814
+ }
815
+
816
+ set_destructor(destructor) {
817
+ HEAPU32[(((this.ptr)+(8))>>2)] = destructor;
818
+ }
819
+
820
+ get_destructor() {
821
+ return HEAPU32[(((this.ptr)+(8))>>2)];
822
+ }
823
+
824
+ set_caught(caught) {
825
+ caught = caught ? 1 : 0;
826
+ HEAP8[(this.ptr)+(12)] = caught;
827
+ }
828
+
829
+ get_caught() {
830
+ return HEAP8[(this.ptr)+(12)] != 0;
831
+ }
832
+
833
+ set_rethrown(rethrown) {
834
+ rethrown = rethrown ? 1 : 0;
835
+ HEAP8[(this.ptr)+(13)] = rethrown;
836
+ }
837
+
838
+ get_rethrown() {
839
+ return HEAP8[(this.ptr)+(13)] != 0;
840
+ }
841
+
842
+ // Initialize native structure fields. Should be called once after allocated.
843
+ init(type, destructor) {
844
+ this.set_adjusted_ptr(0);
845
+ this.set_type(type);
846
+ this.set_destructor(destructor);
847
+ }
848
+
849
+ set_adjusted_ptr(adjustedPtr) {
850
+ HEAPU32[(((this.ptr)+(16))>>2)] = adjustedPtr;
851
+ }
852
+
853
+ get_adjusted_ptr() {
854
+ return HEAPU32[(((this.ptr)+(16))>>2)];
855
+ }
856
+ }
857
+
858
+ var exceptionLast = 0;
859
+
860
+ var uncaughtExceptionCount = 0;
861
+ var ___cxa_throw = (ptr, type, destructor) => {
862
+ var info = new ExceptionInfo(ptr);
863
+ // Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
864
+ info.init(type, destructor);
865
+ exceptionLast = ptr;
866
+ uncaughtExceptionCount++;
867
+ assert(false, 'Exception thrown, but exception catching is not enabled. Compile with -sNO_DISABLE_EXCEPTION_CATCHING or -sEXCEPTION_CATCHING_ALLOWED=[..] to catch.');
868
+ };
869
+
870
+ var syscallGetVarargI = () => {
871
+ assert(SYSCALLS.varargs != undefined);
872
+ // the `+` prepended here is necessary to convince the JSCompiler that varargs is indeed a number.
873
+ var ret = HEAP32[((+SYSCALLS.varargs)>>2)];
874
+ SYSCALLS.varargs += 4;
875
+ return ret;
876
+ };
877
+ var syscallGetVarargP = syscallGetVarargI;
878
+
879
+
880
+ var PATH = {
881
+ isAbs:(path) => path.charAt(0) === '/',
882
+ splitPath:(filename) => {
883
+ var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
884
+ return splitPathRe.exec(filename).slice(1);
885
+ },
886
+ normalizeArray:(parts, allowAboveRoot) => {
887
+ // if the path tries to go above the root, `up` ends up > 0
888
+ var up = 0;
889
+ for (var i = parts.length - 1; i >= 0; i--) {
890
+ var last = parts[i];
891
+ if (last === '.') {
892
+ parts.splice(i, 1);
893
+ } else if (last === '..') {
894
+ parts.splice(i, 1);
895
+ up++;
896
+ } else if (up) {
897
+ parts.splice(i, 1);
898
+ up--;
899
+ }
900
+ }
901
+ // if the path is allowed to go above the root, restore leading ..s
902
+ if (allowAboveRoot) {
903
+ for (; up; up--) {
904
+ parts.unshift('..');
905
+ }
906
+ }
907
+ return parts;
908
+ },
909
+ normalize:(path) => {
910
+ var isAbsolute = PATH.isAbs(path),
911
+ trailingSlash = path.slice(-1) === '/';
912
+ // Normalize the path
913
+ path = PATH.normalizeArray(path.split('/').filter((p) => !!p), !isAbsolute).join('/');
914
+ if (!path && !isAbsolute) {
915
+ path = '.';
916
+ }
917
+ if (path && trailingSlash) {
918
+ path += '/';
919
+ }
920
+ return (isAbsolute ? '/' : '') + path;
921
+ },
922
+ dirname:(path) => {
923
+ var result = PATH.splitPath(path),
924
+ root = result[0],
925
+ dir = result[1];
926
+ if (!root && !dir) {
927
+ // No dirname whatsoever
928
+ return '.';
929
+ }
930
+ if (dir) {
931
+ // It has a dirname, strip trailing slash
932
+ dir = dir.slice(0, -1);
933
+ }
934
+ return root + dir;
935
+ },
936
+ basename:(path) => path && path.match(/([^\/]+|\/)\/*$/)[1],
937
+ join:(...paths) => PATH.normalize(paths.join('/')),
938
+ join2:(l, r) => PATH.normalize(l + '/' + r),
939
+ };
940
+
941
+ var initRandomFill = () => {
942
+ // This block is not needed on v19+ since crypto.getRandomValues is builtin
943
+ if (ENVIRONMENT_IS_NODE) {
944
+ var nodeCrypto = require('node:crypto');
945
+ return (view) => nodeCrypto.randomFillSync(view);
946
+ }
947
+
948
+ return (view) => crypto.getRandomValues(view);
949
+ };
950
+ var randomFill = (view) => {
951
+ // Lazily init on the first invocation.
952
+ (randomFill = initRandomFill())(view);
953
+ };
954
+
955
+
956
+
957
+ var PATH_FS = {
958
+ resolve:(...args) => {
959
+ var resolvedPath = '',
960
+ resolvedAbsolute = false;
961
+ for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
962
+ var path = (i >= 0) ? args[i] : FS.cwd();
963
+ // Skip empty and invalid entries
964
+ if (typeof path != 'string') {
965
+ throw new TypeError('Arguments to path.resolve must be strings');
966
+ } else if (!path) {
967
+ return ''; // an invalid portion invalidates the whole thing
968
+ }
969
+ resolvedPath = path + '/' + resolvedPath;
970
+ resolvedAbsolute = PATH.isAbs(path);
971
+ }
972
+ // At this point the path should be resolved to a full absolute path, but
973
+ // handle relative paths to be safe (might happen when process.cwd() fails)
974
+ resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter((p) => !!p), !resolvedAbsolute).join('/');
975
+ return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
976
+ },
977
+ relative:(from, to) => {
978
+ from = PATH_FS.resolve(from).slice(1);
979
+ to = PATH_FS.resolve(to).slice(1);
980
+ function trim(arr) {
981
+ var start = 0;
982
+ for (; start < arr.length; start++) {
983
+ if (arr[start] !== '') break;
984
+ }
985
+ var end = arr.length - 1;
986
+ for (; end >= 0; end--) {
987
+ if (arr[end] !== '') break;
988
+ }
989
+ if (start > end) return [];
990
+ return arr.slice(start, end - start + 1);
991
+ }
992
+ var fromParts = trim(from.split('/'));
993
+ var toParts = trim(to.split('/'));
994
+ var length = Math.min(fromParts.length, toParts.length);
995
+ var samePartsLength = length;
996
+ for (var i = 0; i < length; i++) {
997
+ if (fromParts[i] !== toParts[i]) {
998
+ samePartsLength = i;
999
+ break;
1000
+ }
1001
+ }
1002
+ var outputParts = [];
1003
+ for (var i = samePartsLength; i < fromParts.length; i++) {
1004
+ outputParts.push('..');
1005
+ }
1006
+ outputParts = outputParts.concat(toParts.slice(samePartsLength));
1007
+ return outputParts.join('/');
1008
+ },
1009
+ };
1010
+
1011
+
1012
+ var UTF8Decoder = globalThis.TextDecoder && new TextDecoder();
1013
+
1014
+ var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => {
1015
+ var maxIdx = idx + maxBytesToRead;
1016
+ if (ignoreNul) return maxIdx;
1017
+ // TextDecoder needs to know the byte length in advance, it doesn't stop on
1018
+ // null terminator by itself.
1019
+ // As a tiny code save trick, compare idx against maxIdx using a negation,
1020
+ // so that maxBytesToRead=undefined/NaN means Infinity.
1021
+ while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx;
1022
+ return idx;
1023
+ };
1024
+
1025
+
1026
+ /**
1027
+ * Given a pointer 'idx' to a null-terminated UTF8-encoded string in the given
1028
+ * array that contains uint8 values, returns a copy of that string as a
1029
+ * Javascript String object.
1030
+ * heapOrArray is either a regular array, or a JavaScript typed array view.
1031
+ * @param {number=} idx
1032
+ * @param {number=} maxBytesToRead
1033
+ * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
1034
+ * @return {string}
1035
+ */
1036
+ var UTF8ArrayToString = (heapOrArray, idx = 0, maxBytesToRead, ignoreNul) => {
1037
+
1038
+ var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul);
1039
+
1040
+ // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
1041
+ if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
1042
+ return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
1043
+ }
1044
+ var str = '';
1045
+ while (idx < endPtr) {
1046
+ // For UTF8 byte structure, see:
1047
+ // http://en.wikipedia.org/wiki/UTF-8#Description
1048
+ // https://www.ietf.org/rfc/rfc2279.txt
1049
+ // https://tools.ietf.org/html/rfc3629
1050
+ var u0 = heapOrArray[idx++];
1051
+ if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
1052
+ var u1 = heapOrArray[idx++] & 63;
1053
+ if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
1054
+ var u2 = heapOrArray[idx++] & 63;
1055
+ if ((u0 & 0xF0) == 0xE0) {
1056
+ u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
1057
+ } else {
1058
+ if ((u0 & 0xF8) != 0xF0) warnOnce('Invalid UTF-8 leading byte ' + ptrToString(u0) + ' encountered when deserializing a UTF-8 string in wasm memory to a JS string!');
1059
+ u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heapOrArray[idx++] & 63);
1060
+ }
1061
+
1062
+ if (u0 < 0x10000) {
1063
+ str += String.fromCharCode(u0);
1064
+ } else {
1065
+ var ch = u0 - 0x10000;
1066
+ str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
1067
+ }
1068
+ }
1069
+ return str;
1070
+ };
1071
+
1072
+ var FS_stdin_getChar_buffer = [];
1073
+
1074
+ var lengthBytesUTF8 = (str) => {
1075
+ var len = 0;
1076
+ for (var i = 0; i < str.length; ++i) {
1077
+ // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
1078
+ // unit, not a Unicode code point of the character! So decode
1079
+ // UTF16->UTF32->UTF8.
1080
+ // See http://unicode.org/faq/utf_bom.html#utf16-3
1081
+ var c = str.charCodeAt(i); // possibly a lead surrogate
1082
+ if (c <= 0x7F) {
1083
+ len++;
1084
+ } else if (c <= 0x7FF) {
1085
+ len += 2;
1086
+ } else if (c >= 0xD800 && c <= 0xDFFF) {
1087
+ len += 4; ++i;
1088
+ } else {
1089
+ len += 3;
1090
+ }
1091
+ }
1092
+ return len;
1093
+ };
1094
+
1095
+ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
1096
+ assert(typeof str === 'string', `stringToUTF8Array expects a string (got ${typeof str})`);
1097
+ // Parameter maxBytesToWrite is not optional. Negative values, 0, null,
1098
+ // undefined and false each don't write out any bytes.
1099
+ if (!(maxBytesToWrite > 0))
1100
+ return 0;
1101
+
1102
+ var startIdx = outIdx;
1103
+ var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator.
1104
+ for (var i = 0; i < str.length; ++i) {
1105
+ // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description
1106
+ // and https://www.ietf.org/rfc/rfc2279.txt
1107
+ // and https://tools.ietf.org/html/rfc3629
1108
+ var u = str.codePointAt(i);
1109
+ if (u <= 0x7F) {
1110
+ if (outIdx >= endIdx) break;
1111
+ heap[outIdx++] = u;
1112
+ } else if (u <= 0x7FF) {
1113
+ if (outIdx + 1 >= endIdx) break;
1114
+ heap[outIdx++] = 0xC0 | (u >> 6);
1115
+ heap[outIdx++] = 0x80 | (u & 63);
1116
+ } else if (u <= 0xFFFF) {
1117
+ if (outIdx + 2 >= endIdx) break;
1118
+ heap[outIdx++] = 0xE0 | (u >> 12);
1119
+ heap[outIdx++] = 0x80 | ((u >> 6) & 63);
1120
+ heap[outIdx++] = 0x80 | (u & 63);
1121
+ } else {
1122
+ if (outIdx + 3 >= endIdx) break;
1123
+ if (u > 0x10FFFF) warnOnce('Invalid Unicode code point ' + ptrToString(u) + ' encountered when serializing a JS string to a UTF-8 string in wasm memory! (Valid unicode code points should be in range 0-0x10FFFF).');
1124
+ heap[outIdx++] = 0xF0 | (u >> 18);
1125
+ heap[outIdx++] = 0x80 | ((u >> 12) & 63);
1126
+ heap[outIdx++] = 0x80 | ((u >> 6) & 63);
1127
+ heap[outIdx++] = 0x80 | (u & 63);
1128
+ // Gotcha: if codePoint is over 0xFFFF, it is represented as a surrogate pair in UTF-16.
1129
+ // We need to manually skip over the second code unit for correct iteration.
1130
+ i++;
1131
+ }
1132
+ }
1133
+ // Null-terminate the pointer to the buffer.
1134
+ heap[outIdx] = 0;
1135
+ return outIdx - startIdx;
1136
+ };
1137
+ /** @type {function(string, boolean=, number=)} */
1138
+ var intArrayFromString = (stringy, dontAddNull, length) => {
1139
+ var len = length > 0 ? length : lengthBytesUTF8(stringy)+1;
1140
+ var u8array = new Array(len);
1141
+ var numBytesWritten = stringToUTF8Array(stringy, u8array, 0, u8array.length);
1142
+ if (dontAddNull) u8array.length = numBytesWritten;
1143
+ return u8array;
1144
+ };
1145
+ var FS_stdin_getChar = () => {
1146
+ if (!FS_stdin_getChar_buffer.length) {
1147
+ var result = null;
1148
+ if (ENVIRONMENT_IS_NODE) {
1149
+ // we will read data by chunks of BUFSIZE
1150
+ var BUFSIZE = 256;
1151
+ var buf = Buffer.alloc(BUFSIZE);
1152
+ var bytesRead = 0;
1153
+
1154
+ // For some reason we must suppress a closure warning here, even though
1155
+ // fd definitely exists on process.stdin, and is even the proper way to
1156
+ // get the fd of stdin,
1157
+ // https://github.com/nodejs/help/issues/2136#issuecomment-523649904
1158
+ // This started to happen after moving this logic out of library_tty.js,
1159
+ // so it is related to the surrounding code in some unclear manner.
1160
+ /** @suppress {missingProperties} */
1161
+ var fd = process.stdin.fd;
1162
+
1163
+ try {
1164
+ bytesRead = fs.readSync(fd, buf, 0, BUFSIZE);
1165
+ } catch(e) {
1166
+ // Cross-platform differences: on Windows, reading EOF throws an
1167
+ // exception, but on other OSes, reading EOF returns 0. Uniformize
1168
+ // behavior by treating the EOF exception to return 0.
1169
+ if (e.toString().includes('EOF')) bytesRead = 0;
1170
+ else throw e;
1171
+ }
1172
+
1173
+ if (bytesRead > 0) {
1174
+ result = buf.slice(0, bytesRead).toString('utf-8');
1175
+ }
1176
+ } else
1177
+ if (globalThis.window?.prompt) {
1178
+ // Browser.
1179
+ result = window.prompt('Input: '); // returns null on cancel
1180
+ if (result !== null) {
1181
+ result += '\n';
1182
+ }
1183
+ } else
1184
+ {}
1185
+ if (!result) {
1186
+ return null;
1187
+ }
1188
+ FS_stdin_getChar_buffer = intArrayFromString(result, true);
1189
+ }
1190
+ return FS_stdin_getChar_buffer.shift();
1191
+ };
1192
+ var TTY = {
1193
+ ttys:[],
1194
+ init() {
1195
+ // https://github.com/emscripten-core/emscripten/pull/1555
1196
+ // if (ENVIRONMENT_IS_NODE) {
1197
+ // // currently, FS.init does not distinguish if process.stdin is a file or TTY
1198
+ // // device, it always assumes it's a TTY device. because of this, we're forcing
1199
+ // // process.stdin to UTF8 encoding to at least make stdin reading compatible
1200
+ // // with text files until FS.init can be refactored.
1201
+ // process.stdin.setEncoding('utf8');
1202
+ // }
1203
+ },
1204
+ shutdown() {
1205
+ // https://github.com/emscripten-core/emscripten/pull/1555
1206
+ // if (ENVIRONMENT_IS_NODE) {
1207
+ // // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)?
1208
+ // // isaacs: because now it's reading from the stream, you've expressed interest in it, so that read() kicks off a _read() which creates a ReadReq operation
1209
+ // // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists?
1210
+ // // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle
1211
+ // // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call
1212
+ // process.stdin.pause();
1213
+ // }
1214
+ },
1215
+ register(dev, ops) {
1216
+ TTY.ttys[dev] = { input: [], output: [], ops: ops };
1217
+ FS.registerDevice(dev, TTY.stream_ops);
1218
+ },
1219
+ stream_ops:{
1220
+ open(stream) {
1221
+ var tty = TTY.ttys[stream.node.rdev];
1222
+ if (!tty) {
1223
+ throw new FS.ErrnoError(43);
1224
+ }
1225
+ stream.tty = tty;
1226
+ stream.seekable = false;
1227
+ },
1228
+ close(stream) {
1229
+ // flush any pending line data
1230
+ stream.tty.ops.fsync(stream.tty);
1231
+ },
1232
+ fsync(stream) {
1233
+ stream.tty.ops.fsync(stream.tty);
1234
+ },
1235
+ read(stream, buffer, offset, length, pos /* ignored */) {
1236
+ if (!stream.tty || !stream.tty.ops.get_char) {
1237
+ throw new FS.ErrnoError(60);
1238
+ }
1239
+ var bytesRead = 0;
1240
+ for (var i = 0; i < length; i++) {
1241
+ var result;
1242
+ try {
1243
+ result = stream.tty.ops.get_char(stream.tty);
1244
+ } catch (e) {
1245
+ throw new FS.ErrnoError(29);
1246
+ }
1247
+ if (result === undefined && bytesRead === 0) {
1248
+ throw new FS.ErrnoError(6);
1249
+ }
1250
+ if (result === null || result === undefined) break;
1251
+ bytesRead++;
1252
+ buffer[offset+i] = result;
1253
+ }
1254
+ if (bytesRead) {
1255
+ stream.node.atime = Date.now();
1256
+ }
1257
+ return bytesRead;
1258
+ },
1259
+ write(stream, buffer, offset, length, pos) {
1260
+ if (!stream.tty || !stream.tty.ops.put_char) {
1261
+ throw new FS.ErrnoError(60);
1262
+ }
1263
+ try {
1264
+ for (var i = 0; i < length; i++) {
1265
+ stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
1266
+ }
1267
+ } catch (e) {
1268
+ throw new FS.ErrnoError(29);
1269
+ }
1270
+ if (length) {
1271
+ stream.node.mtime = stream.node.ctime = Date.now();
1272
+ }
1273
+ return i;
1274
+ },
1275
+ },
1276
+ default_tty_ops:{
1277
+ get_char(tty) {
1278
+ return FS_stdin_getChar();
1279
+ },
1280
+ put_char(tty, val) {
1281
+ if (val === null || val === 10) {
1282
+ out(UTF8ArrayToString(tty.output));
1283
+ tty.output = [];
1284
+ } else {
1285
+ if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
1286
+ }
1287
+ },
1288
+ fsync(tty) {
1289
+ if (tty.output?.length > 0) {
1290
+ out(UTF8ArrayToString(tty.output));
1291
+ tty.output = [];
1292
+ }
1293
+ },
1294
+ ioctl_tcgets(tty) {
1295
+ // typical setting
1296
+ return {
1297
+ c_iflag: 25856,
1298
+ c_oflag: 5,
1299
+ c_cflag: 191,
1300
+ c_lflag: 35387,
1301
+ c_cc: [
1302
+ 0x03, 0x1c, 0x7f, 0x15, 0x04, 0x00, 0x01, 0x00, 0x11, 0x13, 0x1a, 0x00,
1303
+ 0x12, 0x0f, 0x17, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1304
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1305
+ ]
1306
+ };
1307
+ },
1308
+ ioctl_tcsets(tty, optional_actions, data) {
1309
+ // currently just ignore
1310
+ return 0;
1311
+ },
1312
+ ioctl_tiocgwinsz(tty) {
1313
+ return [24, 80];
1314
+ },
1315
+ },
1316
+ default_tty1_ops:{
1317
+ put_char(tty, val) {
1318
+ if (val === null || val === 10) {
1319
+ err(UTF8ArrayToString(tty.output));
1320
+ tty.output = [];
1321
+ } else {
1322
+ if (val != 0) tty.output.push(val);
1323
+ }
1324
+ },
1325
+ fsync(tty) {
1326
+ if (tty.output?.length > 0) {
1327
+ err(UTF8ArrayToString(tty.output));
1328
+ tty.output = [];
1329
+ }
1330
+ },
1331
+ },
1332
+ };
1333
+
1334
+
1335
+ var mmapAlloc = (size) => {
1336
+ abort('internal error: mmapAlloc called but `emscripten_builtin_memalign` native symbol not exported');
1337
+ };
1338
+ var MEMFS = {
1339
+ ops_table:null,
1340
+ mount(mount) {
1341
+ return MEMFS.createNode(null, '/', 16895, 0);
1342
+ },
1343
+ createNode(parent, name, mode, dev) {
1344
+ if (FS.isBlkdev(mode) || FS.isFIFO(mode)) {
1345
+ // not supported
1346
+ throw new FS.ErrnoError(63);
1347
+ }
1348
+ MEMFS.ops_table ||= {
1349
+ dir: {
1350
+ node: {
1351
+ getattr: MEMFS.node_ops.getattr,
1352
+ setattr: MEMFS.node_ops.setattr,
1353
+ lookup: MEMFS.node_ops.lookup,
1354
+ mknod: MEMFS.node_ops.mknod,
1355
+ rename: MEMFS.node_ops.rename,
1356
+ unlink: MEMFS.node_ops.unlink,
1357
+ rmdir: MEMFS.node_ops.rmdir,
1358
+ readdir: MEMFS.node_ops.readdir,
1359
+ symlink: MEMFS.node_ops.symlink
1360
+ },
1361
+ stream: {
1362
+ llseek: MEMFS.stream_ops.llseek
1363
+ }
1364
+ },
1365
+ file: {
1366
+ node: {
1367
+ getattr: MEMFS.node_ops.getattr,
1368
+ setattr: MEMFS.node_ops.setattr
1369
+ },
1370
+ stream: {
1371
+ llseek: MEMFS.stream_ops.llseek,
1372
+ read: MEMFS.stream_ops.read,
1373
+ write: MEMFS.stream_ops.write,
1374
+ mmap: MEMFS.stream_ops.mmap,
1375
+ msync: MEMFS.stream_ops.msync
1376
+ }
1377
+ },
1378
+ link: {
1379
+ node: {
1380
+ getattr: MEMFS.node_ops.getattr,
1381
+ setattr: MEMFS.node_ops.setattr,
1382
+ readlink: MEMFS.node_ops.readlink
1383
+ },
1384
+ stream: {}
1385
+ },
1386
+ chrdev: {
1387
+ node: {
1388
+ getattr: MEMFS.node_ops.getattr,
1389
+ setattr: MEMFS.node_ops.setattr
1390
+ },
1391
+ stream: FS.chrdev_stream_ops
1392
+ }
1393
+ };
1394
+ var node = FS.createNode(parent, name, mode, dev);
1395
+ if (FS.isDir(node.mode)) {
1396
+ node.node_ops = MEMFS.ops_table.dir.node;
1397
+ node.stream_ops = MEMFS.ops_table.dir.stream;
1398
+ node.contents = {};
1399
+ } else if (FS.isFile(node.mode)) {
1400
+ node.node_ops = MEMFS.ops_table.file.node;
1401
+ node.stream_ops = MEMFS.ops_table.file.stream;
1402
+ // The actual number of bytes used in the typed array, as opposed to
1403
+ // contents.length which gives the whole capacity.
1404
+ node.usedBytes = 0;
1405
+ // The byte data of the file is stored in a typed array.
1406
+ // Note: typed arrays are not resizable like normal JS arrays are, so
1407
+ // there is a small penalty involved for appending file writes that
1408
+ // continuously grow a file similar to std::vector capacity vs used.
1409
+ node.contents = MEMFS.emptyFileContents ??= new Uint8Array(0);
1410
+ } else if (FS.isLink(node.mode)) {
1411
+ node.node_ops = MEMFS.ops_table.link.node;
1412
+ node.stream_ops = MEMFS.ops_table.link.stream;
1413
+ } else if (FS.isChrdev(node.mode)) {
1414
+ node.node_ops = MEMFS.ops_table.chrdev.node;
1415
+ node.stream_ops = MEMFS.ops_table.chrdev.stream;
1416
+ }
1417
+ node.atime = node.mtime = node.ctime = Date.now();
1418
+ // add the new node to the parent
1419
+ if (parent) {
1420
+ parent.contents[name] = node;
1421
+ parent.atime = parent.mtime = parent.ctime = node.atime;
1422
+ }
1423
+ return node;
1424
+ },
1425
+ getFileDataAsTypedArray(node) {
1426
+ assert(FS.isFile(node.mode), 'getFileDataAsTypedArray called on non-file');
1427
+ return node.contents.subarray(0, node.usedBytes); // Make sure to not return excess unused bytes.
1428
+ },
1429
+ expandFileStorage(node, newCapacity) {
1430
+ var prevCapacity = node.contents.length;
1431
+ if (prevCapacity >= newCapacity) return; // No need to expand, the storage was already large enough.
1432
+ // Don't expand strictly to the given requested limit if it's only a very
1433
+ // small increase, but instead geometrically grow capacity.
1434
+ // For small filesizes (<1MB), perform size*2 geometric increase, but for
1435
+ // large sizes, do a much more conservative size*1.125 increase to avoid
1436
+ // overshooting the allocation cap by a very large margin.
1437
+ var CAPACITY_DOUBLING_MAX = 1024 * 1024;
1438
+ newCapacity = Math.max(newCapacity, (prevCapacity * (prevCapacity < CAPACITY_DOUBLING_MAX ? 2.0 : 1.125)) >>> 0);
1439
+ if (prevCapacity) newCapacity = Math.max(newCapacity, 256); // At minimum allocate 256b for each file when expanding.
1440
+ var oldContents = MEMFS.getFileDataAsTypedArray(node);
1441
+ node.contents = new Uint8Array(newCapacity); // Allocate new storage.
1442
+ node.contents.set(oldContents);
1443
+ },
1444
+ resizeFileStorage(node, newSize) {
1445
+ if (node.usedBytes == newSize) return;
1446
+ var oldContents = node.contents;
1447
+ node.contents = new Uint8Array(newSize); // Allocate new storage.
1448
+ node.contents.set(oldContents.subarray(0, Math.min(newSize, node.usedBytes))); // Copy old data over to the new storage.
1449
+ node.usedBytes = newSize;
1450
+ },
1451
+ node_ops:{
1452
+ getattr(node) {
1453
+ var attr = {};
1454
+ // device numbers reuse inode numbers.
1455
+ attr.dev = FS.isChrdev(node.mode) ? node.id : 1;
1456
+ attr.ino = node.id;
1457
+ attr.mode = node.mode;
1458
+ attr.nlink = 1;
1459
+ attr.uid = 0;
1460
+ attr.gid = 0;
1461
+ attr.rdev = node.rdev;
1462
+ if (FS.isDir(node.mode)) {
1463
+ attr.size = 4096;
1464
+ } else if (FS.isFile(node.mode)) {
1465
+ attr.size = node.usedBytes;
1466
+ } else if (FS.isLink(node.mode)) {
1467
+ attr.size = node.link.length;
1468
+ } else {
1469
+ attr.size = 0;
1470
+ }
1471
+ attr.atime = new Date(node.atime);
1472
+ attr.mtime = new Date(node.mtime);
1473
+ attr.ctime = new Date(node.ctime);
1474
+ // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize),
1475
+ // but this is not required by the standard.
1476
+ attr.blksize = 4096;
1477
+ attr.blocks = Math.ceil(attr.size / attr.blksize);
1478
+ return attr;
1479
+ },
1480
+ setattr(node, attr) {
1481
+ for (const key of ["mode", "atime", "mtime", "ctime"]) {
1482
+ if (attr[key] != null) {
1483
+ node[key] = attr[key];
1484
+ }
1485
+ }
1486
+ if (attr.size !== undefined) {
1487
+ MEMFS.resizeFileStorage(node, attr.size);
1488
+ }
1489
+ },
1490
+ lookup(parent, name) {
1491
+ throw new FS.ErrnoError(44);
1492
+ },
1493
+ mknod(parent, name, mode, dev) {
1494
+ return MEMFS.createNode(parent, name, mode, dev);
1495
+ },
1496
+ rename(old_node, new_dir, new_name) {
1497
+ var new_node;
1498
+ try {
1499
+ new_node = FS.lookupNode(new_dir, new_name);
1500
+ } catch (e) {}
1501
+ if (new_node) {
1502
+ if (FS.isDir(old_node.mode)) {
1503
+ // if we're overwriting a directory at new_name, make sure it's empty.
1504
+ for (var i in new_node.contents) {
1505
+ throw new FS.ErrnoError(55);
1506
+ }
1507
+ }
1508
+ FS.hashRemoveNode(new_node);
1509
+ }
1510
+ // do the internal rewiring
1511
+ delete old_node.parent.contents[old_node.name];
1512
+ new_dir.contents[new_name] = old_node;
1513
+ old_node.name = new_name;
1514
+ new_dir.ctime = new_dir.mtime = old_node.parent.ctime = old_node.parent.mtime = Date.now();
1515
+ },
1516
+ unlink(parent, name) {
1517
+ delete parent.contents[name];
1518
+ parent.ctime = parent.mtime = Date.now();
1519
+ },
1520
+ rmdir(parent, name) {
1521
+ var node = FS.lookupNode(parent, name);
1522
+ for (var i in node.contents) {
1523
+ throw new FS.ErrnoError(55);
1524
+ }
1525
+ delete parent.contents[name];
1526
+ parent.ctime = parent.mtime = Date.now();
1527
+ },
1528
+ readdir(node) {
1529
+ return ['.', '..', ...Object.keys(node.contents)];
1530
+ },
1531
+ symlink(parent, newname, oldpath) {
1532
+ var node = MEMFS.createNode(parent, newname, 0o777 | 40960, 0);
1533
+ node.link = oldpath;
1534
+ return node;
1535
+ },
1536
+ readlink(node) {
1537
+ if (!FS.isLink(node.mode)) {
1538
+ throw new FS.ErrnoError(28);
1539
+ }
1540
+ return node.link;
1541
+ },
1542
+ },
1543
+ stream_ops:{
1544
+ read(stream, buffer, offset, length, position) {
1545
+ var contents = stream.node.contents;
1546
+ if (position >= stream.node.usedBytes) return 0;
1547
+ var size = Math.min(stream.node.usedBytes - position, length);
1548
+ assert(size >= 0);
1549
+ buffer.set(contents.subarray(position, position + size), offset);
1550
+ return size;
1551
+ },
1552
+ write(stream, buffer, offset, length, position, canOwn) {
1553
+ assert(buffer.subarray, 'FS.write expects a TypedArray');
1554
+ // If the buffer is located in main memory (HEAP), and if
1555
+ // memory can grow, we can't hold on to references of the
1556
+ // memory buffer, as they may get invalidated. That means we
1557
+ // need to copy its contents.
1558
+ if (buffer.buffer === HEAP8.buffer) {
1559
+ canOwn = false;
1560
+ }
1561
+
1562
+ if (!length) return 0;
1563
+ var node = stream.node;
1564
+ node.mtime = node.ctime = Date.now();
1565
+
1566
+ if (canOwn) {
1567
+ assert(position === 0, 'canOwn must imply no weird position inside the file');
1568
+ node.contents = buffer.subarray(offset, offset + length);
1569
+ node.usedBytes = length;
1570
+ } else if (node.usedBytes === 0 && position === 0) { // If this is a simple first write to an empty file, do a fast set since we don't need to care about old data.
1571
+ node.contents = buffer.slice(offset, offset + length);
1572
+ node.usedBytes = length;
1573
+ } else {
1574
+ MEMFS.expandFileStorage(node, position+length);
1575
+ // Use typed array write which is available.
1576
+ node.contents.set(buffer.subarray(offset, offset + length), position);
1577
+ node.usedBytes = Math.max(node.usedBytes, position + length);
1578
+ }
1579
+ return length;
1580
+ },
1581
+ llseek(stream, offset, whence) {
1582
+ var position = offset;
1583
+ if (whence === 1) {
1584
+ position += stream.position;
1585
+ } else if (whence === 2) {
1586
+ if (FS.isFile(stream.node.mode)) {
1587
+ position += stream.node.usedBytes;
1588
+ }
1589
+ }
1590
+ if (position < 0) {
1591
+ throw new FS.ErrnoError(28);
1592
+ }
1593
+ return position;
1594
+ },
1595
+ mmap(stream, length, position, prot, flags) {
1596
+ if (!FS.isFile(stream.node.mode)) {
1597
+ throw new FS.ErrnoError(43);
1598
+ }
1599
+ var ptr;
1600
+ var allocated;
1601
+ var contents = stream.node.contents;
1602
+ // Only make a new copy when MAP_PRIVATE is specified.
1603
+ if (!(flags & 2) && contents.buffer === HEAP8.buffer) {
1604
+ // We can't emulate MAP_SHARED when the file is not backed by the
1605
+ // buffer we're mapping to (e.g. the HEAP buffer).
1606
+ allocated = false;
1607
+ ptr = contents.byteOffset;
1608
+ } else {
1609
+ allocated = true;
1610
+ ptr = mmapAlloc(length);
1611
+ if (!ptr) {
1612
+ throw new FS.ErrnoError(48);
1613
+ }
1614
+ if (contents) {
1615
+ // Try to avoid unnecessary slices.
1616
+ if (position > 0 || position + length < contents.length) {
1617
+ if (contents.subarray) {
1618
+ contents = contents.subarray(position, position + length);
1619
+ } else {
1620
+ contents = Array.prototype.slice.call(contents, position, position + length);
1621
+ }
1622
+ }
1623
+ HEAP8.set(contents, ptr);
1624
+ }
1625
+ }
1626
+ return { ptr, allocated };
1627
+ },
1628
+ msync(stream, buffer, offset, length, mmapFlags) {
1629
+ MEMFS.stream_ops.write(stream, buffer, 0, length, offset, false);
1630
+ // should we check if bytesWritten and length are the same?
1631
+ return 0;
1632
+ },
1633
+ },
1634
+ };
1635
+
1636
+ var FS_modeStringToFlags = (str) => {
1637
+ if (typeof str != 'string') return str;
1638
+ var flagModes = {
1639
+ 'r': 0,
1640
+ 'r+': 2,
1641
+ 'w': 512 | 64 | 1,
1642
+ 'w+': 512 | 64 | 2,
1643
+ 'a': 1024 | 64 | 1,
1644
+ 'a+': 1024 | 64 | 2,
1645
+ };
1646
+ var flags = flagModes[str];
1647
+ if (typeof flags == 'undefined') {
1648
+ throw new Error(`Unknown file open mode: ${str}`);
1649
+ }
1650
+ return flags;
1651
+ };
1652
+
1653
+ var FS_fileDataToTypedArray = (data) => {
1654
+ if (typeof data == 'string') {
1655
+ data = intArrayFromString(data, true);
1656
+ }
1657
+ if (!data.subarray) {
1658
+ data = new Uint8Array(data);
1659
+ }
1660
+ return data;
1661
+ };
1662
+
1663
+ var FS_getMode = (canRead, canWrite) => {
1664
+ var mode = 0;
1665
+ if (canRead) mode |= 292 | 73;
1666
+ if (canWrite) mode |= 146;
1667
+ return mode;
1668
+ };
1669
+
1670
+
1671
+
1672
+
1673
+ /**
1674
+ * Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
1675
+ * emscripten HEAP, returns a copy of that string as a Javascript String object.
1676
+ *
1677
+ * @param {number} ptr
1678
+ * @param {number=} maxBytesToRead - An optional length that specifies the
1679
+ * maximum number of bytes to read. You can omit this parameter to scan the
1680
+ * string until the first 0 byte. If maxBytesToRead is passed, and the string
1681
+ * at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the
1682
+ * string will cut short at that byte index.
1683
+ * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
1684
+ * @return {string}
1685
+ */
1686
+ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => {
1687
+ assert(typeof ptr == 'number', `UTF8ToString expects a number (got ${typeof ptr})`);
1688
+ return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : '';
1689
+ };
1690
+
1691
+ var strError = (errno) => UTF8ToString(_strerror(errno));
1692
+
1693
+ var ERRNO_CODES = {
1694
+ 'EPERM': 63,
1695
+ 'ENOENT': 44,
1696
+ 'ESRCH': 71,
1697
+ 'EINTR': 27,
1698
+ 'EIO': 29,
1699
+ 'ENXIO': 60,
1700
+ 'E2BIG': 1,
1701
+ 'ENOEXEC': 45,
1702
+ 'EBADF': 8,
1703
+ 'ECHILD': 12,
1704
+ 'EAGAIN': 6,
1705
+ 'EWOULDBLOCK': 6,
1706
+ 'ENOMEM': 48,
1707
+ 'EACCES': 2,
1708
+ 'EFAULT': 21,
1709
+ 'ENOTBLK': 105,
1710
+ 'EBUSY': 10,
1711
+ 'EEXIST': 20,
1712
+ 'EXDEV': 75,
1713
+ 'ENODEV': 43,
1714
+ 'ENOTDIR': 54,
1715
+ 'EISDIR': 31,
1716
+ 'EINVAL': 28,
1717
+ 'ENFILE': 41,
1718
+ 'EMFILE': 33,
1719
+ 'ENOTTY': 59,
1720
+ 'ETXTBSY': 74,
1721
+ 'EFBIG': 22,
1722
+ 'ENOSPC': 51,
1723
+ 'ESPIPE': 70,
1724
+ 'EROFS': 69,
1725
+ 'EMLINK': 34,
1726
+ 'EPIPE': 64,
1727
+ 'EDOM': 18,
1728
+ 'ERANGE': 68,
1729
+ 'ENOMSG': 49,
1730
+ 'EIDRM': 24,
1731
+ 'ECHRNG': 106,
1732
+ 'EL2NSYNC': 156,
1733
+ 'EL3HLT': 107,
1734
+ 'EL3RST': 108,
1735
+ 'ELNRNG': 109,
1736
+ 'EUNATCH': 110,
1737
+ 'ENOCSI': 111,
1738
+ 'EL2HLT': 112,
1739
+ 'EDEADLK': 16,
1740
+ 'ENOLCK': 46,
1741
+ 'EBADE': 113,
1742
+ 'EBADR': 114,
1743
+ 'EXFULL': 115,
1744
+ 'ENOANO': 104,
1745
+ 'EBADRQC': 103,
1746
+ 'EBADSLT': 102,
1747
+ 'EDEADLOCK': 16,
1748
+ 'EBFONT': 101,
1749
+ 'ENOSTR': 100,
1750
+ 'ENODATA': 116,
1751
+ 'ETIME': 117,
1752
+ 'ENOSR': 118,
1753
+ 'ENONET': 119,
1754
+ 'ENOPKG': 120,
1755
+ 'EREMOTE': 121,
1756
+ 'ENOLINK': 47,
1757
+ 'EADV': 122,
1758
+ 'ESRMNT': 123,
1759
+ 'ECOMM': 124,
1760
+ 'EPROTO': 65,
1761
+ 'EMULTIHOP': 36,
1762
+ 'EDOTDOT': 125,
1763
+ 'EBADMSG': 9,
1764
+ 'ENOTUNIQ': 126,
1765
+ 'EBADFD': 127,
1766
+ 'EREMCHG': 128,
1767
+ 'ELIBACC': 129,
1768
+ 'ELIBBAD': 130,
1769
+ 'ELIBSCN': 131,
1770
+ 'ELIBMAX': 132,
1771
+ 'ELIBEXEC': 133,
1772
+ 'ENOSYS': 52,
1773
+ 'ENOTEMPTY': 55,
1774
+ 'ENAMETOOLONG': 37,
1775
+ 'ELOOP': 32,
1776
+ 'EOPNOTSUPP': 138,
1777
+ 'EPFNOSUPPORT': 139,
1778
+ 'ECONNRESET': 15,
1779
+ 'ENOBUFS': 42,
1780
+ 'EAFNOSUPPORT': 5,
1781
+ 'EPROTOTYPE': 67,
1782
+ 'ENOTSOCK': 57,
1783
+ 'ENOPROTOOPT': 50,
1784
+ 'ESHUTDOWN': 140,
1785
+ 'ECONNREFUSED': 14,
1786
+ 'EADDRINUSE': 3,
1787
+ 'ECONNABORTED': 13,
1788
+ 'ENETUNREACH': 40,
1789
+ 'ENETDOWN': 38,
1790
+ 'ETIMEDOUT': 73,
1791
+ 'EHOSTDOWN': 142,
1792
+ 'EHOSTUNREACH': 23,
1793
+ 'EINPROGRESS': 26,
1794
+ 'EALREADY': 7,
1795
+ 'EDESTADDRREQ': 17,
1796
+ 'EMSGSIZE': 35,
1797
+ 'EPROTONOSUPPORT': 66,
1798
+ 'ESOCKTNOSUPPORT': 137,
1799
+ 'EADDRNOTAVAIL': 4,
1800
+ 'ENETRESET': 39,
1801
+ 'EISCONN': 30,
1802
+ 'ENOTCONN': 53,
1803
+ 'ETOOMANYREFS': 141,
1804
+ 'EUSERS': 136,
1805
+ 'EDQUOT': 19,
1806
+ 'ESTALE': 72,
1807
+ 'ENOTSUP': 138,
1808
+ 'ENOMEDIUM': 148,
1809
+ 'EILSEQ': 25,
1810
+ 'EOVERFLOW': 61,
1811
+ 'ECANCELED': 11,
1812
+ 'ENOTRECOVERABLE': 56,
1813
+ 'EOWNERDEAD': 62,
1814
+ 'ESTRPIPE': 135,
1815
+ };
1816
+
1817
+ var asyncLoad = async (url) => {
1818
+ var arrayBuffer = await readAsync(url);
1819
+ assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
1820
+ return new Uint8Array(arrayBuffer);
1821
+ };
1822
+
1823
+
1824
+ var FS_createDataFile = (...args) => FS.createDataFile(...args);
1825
+
1826
+ var getUniqueRunDependency = (id) => {
1827
+ var orig = id;
1828
+ while (1) {
1829
+ if (!runDependencyTracking[id]) return id;
1830
+ id = orig + Math.random();
1831
+ }
1832
+ };
1833
+
1834
+ var runDependencies = 0;
1835
+
1836
+
1837
+ var dependenciesFulfilled = null;
1838
+
1839
+ var runDependencyTracking = {
1840
+ };
1841
+
1842
+ var runDependencyWatcher = null;
1843
+ var removeRunDependency = (id) => {
1844
+ runDependencies--;
1845
+
1846
+ Module['monitorRunDependencies']?.(runDependencies);
1847
+
1848
+ assert(id, 'removeRunDependency requires an ID');
1849
+ assert(runDependencyTracking[id]);
1850
+ delete runDependencyTracking[id];
1851
+ if (runDependencies == 0) {
1852
+ if (runDependencyWatcher !== null) {
1853
+ clearInterval(runDependencyWatcher);
1854
+ runDependencyWatcher = null;
1855
+ }
1856
+ if (dependenciesFulfilled) {
1857
+ var callback = dependenciesFulfilled;
1858
+ dependenciesFulfilled = null;
1859
+ callback(); // can add another dependenciesFulfilled
1860
+ }
1861
+ }
1862
+ };
1863
+
1864
+
1865
+ var addRunDependency = (id) => {
1866
+ runDependencies++;
1867
+
1868
+ Module['monitorRunDependencies']?.(runDependencies);
1869
+
1870
+ assert(id, 'addRunDependency requires an ID')
1871
+ assert(!runDependencyTracking[id]);
1872
+ runDependencyTracking[id] = 1;
1873
+ if (runDependencyWatcher === null && globalThis.setInterval) {
1874
+ // Check for missing dependencies every few seconds
1875
+ runDependencyWatcher = setInterval(() => {
1876
+ if (ABORT) {
1877
+ clearInterval(runDependencyWatcher);
1878
+ runDependencyWatcher = null;
1879
+ return;
1880
+ }
1881
+ var shown = false;
1882
+ for (var dep in runDependencyTracking) {
1883
+ if (!shown) {
1884
+ shown = true;
1885
+ err('still waiting on run dependencies:');
1886
+ }
1887
+ err(`dependency: ${dep}`);
1888
+ }
1889
+ if (shown) {
1890
+ err('(end of list)');
1891
+ }
1892
+ }, 10000);
1893
+ // Prevent this timer from keeping the runtime alive if nothing
1894
+ // else is.
1895
+ runDependencyWatcher.unref?.()
1896
+ }
1897
+ };
1898
+
1899
+
1900
+ var preloadPlugins = [];
1901
+ var FS_handledByPreloadPlugin = async (byteArray, fullname) => {
1902
+ // Ensure plugins are ready.
1903
+ if (typeof Browser != 'undefined') Browser.init();
1904
+
1905
+ for (var plugin of preloadPlugins) {
1906
+ if (plugin['canHandle'](fullname)) {
1907
+ assert(plugin['handle'].constructor.name === 'AsyncFunction', 'Filesystem plugin handlers must be async functions (See #24914)')
1908
+ return plugin['handle'](byteArray, fullname);
1909
+ }
1910
+ }
1911
+ // If no plugin handled this file then return the original/unmodified
1912
+ // byteArray.
1913
+ return byteArray;
1914
+ };
1915
+ var FS_preloadFile = async (parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish) => {
1916
+ // TODO we should allow people to just pass in a complete filename instead
1917
+ // of parent and name being that we just join them anyways
1918
+ var fullname = name ? PATH_FS.resolve(PATH.join2(parent, name)) : parent;
1919
+ var dep = getUniqueRunDependency(`cp ${fullname}`); // might have several active requests for the same fullname
1920
+ addRunDependency(dep);
1921
+
1922
+ try {
1923
+ var byteArray = url;
1924
+ if (typeof url == 'string') {
1925
+ byteArray = await asyncLoad(url);
1926
+ }
1927
+
1928
+ byteArray = await FS_handledByPreloadPlugin(byteArray, fullname);
1929
+ preFinish?.();
1930
+ if (!dontCreateFile) {
1931
+ FS_createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
1932
+ }
1933
+ } finally {
1934
+ removeRunDependency(dep);
1935
+ }
1936
+ };
1937
+ var FS_createPreloadedFile = (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
1938
+ FS_preloadFile(parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish).then(onload).catch(onerror);
1939
+ };
1940
+ var FS = {
1941
+ root:null,
1942
+ mounts:[],
1943
+ devices:{
1944
+ },
1945
+ streams:[],
1946
+ nextInode:1,
1947
+ nameTable:null,
1948
+ currentPath:"/",
1949
+ initialized:false,
1950
+ ignorePermissions:true,
1951
+ filesystems:null,
1952
+ syncFSRequests:0,
1953
+ ErrnoError:class extends Error {
1954
+ name = 'ErrnoError';
1955
+ // We set the `name` property to be able to identify `FS.ErrnoError`
1956
+ // - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
1957
+ // - when using PROXYFS, an error can come from an underlying FS
1958
+ // as different FS objects have their own FS.ErrnoError each,
1959
+ // the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
1960
+ // we'll use the reliable test `err.name == "ErrnoError"` instead
1961
+ constructor(errno) {
1962
+ super(runtimeInitialized ? strError(errno) : '');
1963
+ this.errno = errno;
1964
+ for (var key in ERRNO_CODES) {
1965
+ if (ERRNO_CODES[key] === errno) {
1966
+ this.code = key;
1967
+ break;
1968
+ }
1969
+ }
1970
+ }
1971
+ },
1972
+ FSStream:class {
1973
+ shared = {};
1974
+ get object() {
1975
+ return this.node;
1976
+ }
1977
+ set object(val) {
1978
+ this.node = val;
1979
+ }
1980
+ get isRead() {
1981
+ return (this.flags & 2097155) !== 1;
1982
+ }
1983
+ get isWrite() {
1984
+ return (this.flags & 2097155) !== 0;
1985
+ }
1986
+ get isAppend() {
1987
+ return (this.flags & 1024);
1988
+ }
1989
+ get flags() {
1990
+ return this.shared.flags;
1991
+ }
1992
+ set flags(val) {
1993
+ this.shared.flags = val;
1994
+ }
1995
+ get position() {
1996
+ return this.shared.position;
1997
+ }
1998
+ set position(val) {
1999
+ this.shared.position = val;
2000
+ }
2001
+ },
2002
+ FSNode:class {
2003
+ node_ops = {};
2004
+ stream_ops = {};
2005
+ readMode = 292 | 73;
2006
+ writeMode = 146;
2007
+ mounted = null;
2008
+ constructor(parent, name, mode, rdev) {
2009
+ if (!parent) {
2010
+ parent = this; // root node sets parent to itself
2011
+ }
2012
+ this.parent = parent;
2013
+ this.mount = parent.mount;
2014
+ this.id = FS.nextInode++;
2015
+ this.name = name;
2016
+ this.mode = mode;
2017
+ this.rdev = rdev;
2018
+ this.atime = this.mtime = this.ctime = Date.now();
2019
+ }
2020
+ get read() {
2021
+ return (this.mode & this.readMode) === this.readMode;
2022
+ }
2023
+ set read(val) {
2024
+ val ? this.mode |= this.readMode : this.mode &= ~this.readMode;
2025
+ }
2026
+ get write() {
2027
+ return (this.mode & this.writeMode) === this.writeMode;
2028
+ }
2029
+ set write(val) {
2030
+ val ? this.mode |= this.writeMode : this.mode &= ~this.writeMode;
2031
+ }
2032
+ get isFolder() {
2033
+ return FS.isDir(this.mode);
2034
+ }
2035
+ get isDevice() {
2036
+ return FS.isChrdev(this.mode);
2037
+ }
2038
+ },
2039
+ lookupPath(path, opts = {}) {
2040
+ if (!path) {
2041
+ throw new FS.ErrnoError(44);
2042
+ }
2043
+ opts.follow_mount ??= true
2044
+
2045
+ if (!PATH.isAbs(path)) {
2046
+ path = FS.cwd() + '/' + path;
2047
+ }
2048
+
2049
+ // limit max consecutive symlinks to 40 (SYMLOOP_MAX).
2050
+ linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) {
2051
+ // split the absolute path
2052
+ var parts = path.split('/').filter((p) => !!p);
2053
+
2054
+ // start at the root
2055
+ var current = FS.root;
2056
+ var current_path = '/';
2057
+
2058
+ for (var i = 0; i < parts.length; i++) {
2059
+ var islast = (i === parts.length-1);
2060
+ if (islast && opts.parent) {
2061
+ // stop resolving
2062
+ break;
2063
+ }
2064
+
2065
+ if (parts[i] === '.') {
2066
+ continue;
2067
+ }
2068
+
2069
+ if (parts[i] === '..') {
2070
+ current_path = PATH.dirname(current_path);
2071
+ if (FS.isRoot(current)) {
2072
+ path = current_path + '/' + parts.slice(i + 1).join('/');
2073
+ // We're making progress here, don't let many consecutive ..'s
2074
+ // lead to ELOOP
2075
+ nlinks--;
2076
+ continue linkloop;
2077
+ } else {
2078
+ current = current.parent;
2079
+ }
2080
+ continue;
2081
+ }
2082
+
2083
+ current_path = PATH.join2(current_path, parts[i]);
2084
+ try {
2085
+ current = FS.lookupNode(current, parts[i]);
2086
+ } catch (e) {
2087
+ // if noent_okay is true, suppress a ENOENT in the last component
2088
+ // and return an object with an undefined node. This is needed for
2089
+ // resolving symlinks in the path when creating a file.
2090
+ if ((e?.errno === 44) && islast && opts.noent_okay) {
2091
+ return { path: current_path };
2092
+ }
2093
+ throw e;
2094
+ }
2095
+
2096
+ // jump to the mount's root node if this is a mountpoint
2097
+ if (FS.isMountpoint(current) && (!islast || opts.follow_mount)) {
2098
+ current = current.mounted.root;
2099
+ }
2100
+
2101
+ // by default, lookupPath will not follow a symlink if it is the final path component.
2102
+ // setting opts.follow = true will override this behavior.
2103
+ if (FS.isLink(current.mode) && (!islast || opts.follow)) {
2104
+ if (!current.node_ops.readlink) {
2105
+ throw new FS.ErrnoError(52);
2106
+ }
2107
+ var link = current.node_ops.readlink(current);
2108
+ if (!PATH.isAbs(link)) {
2109
+ link = PATH.dirname(current_path) + '/' + link;
2110
+ }
2111
+ path = link + '/' + parts.slice(i + 1).join('/');
2112
+ continue linkloop;
2113
+ }
2114
+ }
2115
+ return { path: current_path, node: current };
2116
+ }
2117
+ throw new FS.ErrnoError(32);
2118
+ },
2119
+ getPath(node) {
2120
+ var path;
2121
+ while (true) {
2122
+ if (FS.isRoot(node)) {
2123
+ var mount = node.mount.mountpoint;
2124
+ if (!path) return mount;
2125
+ return mount[mount.length-1] !== '/' ? `${mount}/${path}` : mount + path;
2126
+ }
2127
+ path = path ? `${node.name}/${path}` : node.name;
2128
+ node = node.parent;
2129
+ }
2130
+ },
2131
+ hashName(parentid, name) {
2132
+ var hash = 0;
2133
+
2134
+ for (var i = 0; i < name.length; i++) {
2135
+ hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0;
2136
+ }
2137
+ return ((parentid + hash) >>> 0) % FS.nameTable.length;
2138
+ },
2139
+ hashAddNode(node) {
2140
+ var hash = FS.hashName(node.parent.id, node.name);
2141
+ node.name_next = FS.nameTable[hash];
2142
+ FS.nameTable[hash] = node;
2143
+ },
2144
+ hashRemoveNode(node) {
2145
+ var hash = FS.hashName(node.parent.id, node.name);
2146
+ if (FS.nameTable[hash] === node) {
2147
+ FS.nameTable[hash] = node.name_next;
2148
+ } else {
2149
+ var current = FS.nameTable[hash];
2150
+ while (current) {
2151
+ if (current.name_next === node) {
2152
+ current.name_next = node.name_next;
2153
+ break;
2154
+ }
2155
+ current = current.name_next;
2156
+ }
2157
+ }
2158
+ },
2159
+ lookupNode(parent, name) {
2160
+ var errCode = FS.mayLookup(parent);
2161
+ if (errCode) {
2162
+ throw new FS.ErrnoError(errCode);
2163
+ }
2164
+ var hash = FS.hashName(parent.id, name);
2165
+ for (var node = FS.nameTable[hash]; node; node = node.name_next) {
2166
+ var nodeName = node.name;
2167
+ if (node.parent.id === parent.id && nodeName === name) {
2168
+ return node;
2169
+ }
2170
+ }
2171
+ // if we failed to find it in the cache, call into the VFS
2172
+ return FS.lookup(parent, name);
2173
+ },
2174
+ createNode(parent, name, mode, rdev) {
2175
+ assert(typeof parent == 'object')
2176
+ var node = new FS.FSNode(parent, name, mode, rdev);
2177
+
2178
+ FS.hashAddNode(node);
2179
+
2180
+ return node;
2181
+ },
2182
+ destroyNode(node) {
2183
+ FS.hashRemoveNode(node);
2184
+ },
2185
+ isRoot(node) {
2186
+ return node === node.parent;
2187
+ },
2188
+ isMountpoint(node) {
2189
+ return !!node.mounted;
2190
+ },
2191
+ isFile(mode) {
2192
+ return (mode & 61440) === 32768;
2193
+ },
2194
+ isDir(mode) {
2195
+ return (mode & 61440) === 16384;
2196
+ },
2197
+ isLink(mode) {
2198
+ return (mode & 61440) === 40960;
2199
+ },
2200
+ isChrdev(mode) {
2201
+ return (mode & 61440) === 8192;
2202
+ },
2203
+ isBlkdev(mode) {
2204
+ return (mode & 61440) === 24576;
2205
+ },
2206
+ isFIFO(mode) {
2207
+ return (mode & 61440) === 4096;
2208
+ },
2209
+ isSocket(mode) {
2210
+ return (mode & 49152) === 49152;
2211
+ },
2212
+ flagsToPermissionString(flag) {
2213
+ var perms = ['r', 'w', 'rw'][flag & 3];
2214
+ if ((flag & 512)) {
2215
+ perms += 'w';
2216
+ }
2217
+ return perms;
2218
+ },
2219
+ nodePermissions(node, perms) {
2220
+ if (FS.ignorePermissions) {
2221
+ return 0;
2222
+ }
2223
+ // return 0 if any user, group or owner bits are set.
2224
+ if (perms.includes('r') && !(node.mode & 292)) {
2225
+ return 2;
2226
+ }
2227
+ if (perms.includes('w') && !(node.mode & 146)) {
2228
+ return 2;
2229
+ }
2230
+ if (perms.includes('x') && !(node.mode & 73)) {
2231
+ return 2;
2232
+ }
2233
+ return 0;
2234
+ },
2235
+ mayLookup(dir) {
2236
+ if (!FS.isDir(dir.mode)) return 54;
2237
+ var errCode = FS.nodePermissions(dir, 'x');
2238
+ if (errCode) return errCode;
2239
+ if (!dir.node_ops.lookup) return 2;
2240
+ return 0;
2241
+ },
2242
+ mayCreate(dir, name) {
2243
+ if (!FS.isDir(dir.mode)) {
2244
+ return 54;
2245
+ }
2246
+ try {
2247
+ var node = FS.lookupNode(dir, name);
2248
+ return 20;
2249
+ } catch (e) {
2250
+ }
2251
+ return FS.nodePermissions(dir, 'wx');
2252
+ },
2253
+ mayDelete(dir, name, isdir) {
2254
+ var node;
2255
+ try {
2256
+ node = FS.lookupNode(dir, name);
2257
+ } catch (e) {
2258
+ return e.errno;
2259
+ }
2260
+ var errCode = FS.nodePermissions(dir, 'wx');
2261
+ if (errCode) {
2262
+ return errCode;
2263
+ }
2264
+ if (isdir) {
2265
+ if (!FS.isDir(node.mode)) {
2266
+ return 54;
2267
+ }
2268
+ if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) {
2269
+ return 10;
2270
+ }
2271
+ } else if (FS.isDir(node.mode)) {
2272
+ return 31;
2273
+ }
2274
+ return 0;
2275
+ },
2276
+ mayOpen(node, flags) {
2277
+ if (!node) {
2278
+ return 44;
2279
+ }
2280
+ if (FS.isLink(node.mode)) {
2281
+ return 32;
2282
+ }
2283
+ var mode = FS.flagsToPermissionString(flags);
2284
+ if (FS.isDir(node.mode)) {
2285
+ // opening for write
2286
+ // TODO: check for O_SEARCH? (== search for dir only)
2287
+ if (mode !== 'r' || (flags & (512 | 64))) {
2288
+ return 31;
2289
+ }
2290
+ }
2291
+ return FS.nodePermissions(node, mode);
2292
+ },
2293
+ checkOpExists(op, err) {
2294
+ if (!op) {
2295
+ throw new FS.ErrnoError(err);
2296
+ }
2297
+ return op;
2298
+ },
2299
+ MAX_OPEN_FDS:4096,
2300
+ nextfd() {
2301
+ for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) {
2302
+ if (!FS.streams[fd]) {
2303
+ return fd;
2304
+ }
2305
+ }
2306
+ throw new FS.ErrnoError(33);
2307
+ },
2308
+ getStreamChecked(fd) {
2309
+ var stream = FS.getStream(fd);
2310
+ if (!stream) {
2311
+ throw new FS.ErrnoError(8);
2312
+ }
2313
+ return stream;
2314
+ },
2315
+ getStream:(fd) => FS.streams[fd],
2316
+ createStream(stream, fd = -1) {
2317
+ assert(fd >= -1);
2318
+
2319
+ // clone it, so we can return an instance of FSStream
2320
+ stream = Object.assign(new FS.FSStream(), stream);
2321
+ if (fd == -1) {
2322
+ fd = FS.nextfd();
2323
+ }
2324
+ stream.fd = fd;
2325
+ FS.streams[fd] = stream;
2326
+ return stream;
2327
+ },
2328
+ closeStream(fd) {
2329
+ FS.streams[fd] = null;
2330
+ },
2331
+ dupStream(origStream, fd = -1) {
2332
+ var stream = FS.createStream(origStream, fd);
2333
+ stream.stream_ops?.dup?.(stream);
2334
+ return stream;
2335
+ },
2336
+ doSetAttr(stream, node, attr) {
2337
+ var setattr = stream?.stream_ops.setattr;
2338
+ var arg = setattr ? stream : node;
2339
+ setattr ??= node.node_ops.setattr;
2340
+ FS.checkOpExists(setattr, 63)
2341
+ setattr(arg, attr);
2342
+ },
2343
+ chrdev_stream_ops:{
2344
+ open(stream) {
2345
+ var device = FS.getDevice(stream.node.rdev);
2346
+ // override node's stream ops with the device's
2347
+ stream.stream_ops = device.stream_ops;
2348
+ // forward the open call
2349
+ stream.stream_ops.open?.(stream);
2350
+ },
2351
+ llseek() {
2352
+ throw new FS.ErrnoError(70);
2353
+ },
2354
+ },
2355
+ major:(dev) => ((dev) >> 8),
2356
+ minor:(dev) => ((dev) & 0xff),
2357
+ makedev:(ma, mi) => ((ma) << 8 | (mi)),
2358
+ registerDevice(dev, ops) {
2359
+ FS.devices[dev] = { stream_ops: ops };
2360
+ },
2361
+ getDevice:(dev) => FS.devices[dev],
2362
+ getMounts(mount) {
2363
+ var mounts = [];
2364
+ var check = [mount];
2365
+
2366
+ while (check.length) {
2367
+ var m = check.pop();
2368
+
2369
+ mounts.push(m);
2370
+
2371
+ check.push(...m.mounts);
2372
+ }
2373
+
2374
+ return mounts;
2375
+ },
2376
+ syncfs(populate, callback) {
2377
+ if (typeof populate == 'function') {
2378
+ callback = populate;
2379
+ populate = false;
2380
+ }
2381
+
2382
+ FS.syncFSRequests++;
2383
+
2384
+ if (FS.syncFSRequests > 1) {
2385
+ err(`warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work`);
2386
+ }
2387
+
2388
+ var mounts = FS.getMounts(FS.root.mount);
2389
+ var completed = 0;
2390
+
2391
+ function doCallback(errCode) {
2392
+ assert(FS.syncFSRequests > 0);
2393
+ FS.syncFSRequests--;
2394
+ return callback(errCode);
2395
+ }
2396
+
2397
+ function done(errCode) {
2398
+ if (errCode) {
2399
+ if (!done.errored) {
2400
+ done.errored = true;
2401
+ return doCallback(errCode);
2402
+ }
2403
+ return;
2404
+ }
2405
+ if (++completed >= mounts.length) {
2406
+ doCallback(null);
2407
+ }
2408
+ };
2409
+
2410
+ // sync all mounts
2411
+ for (var mount of mounts) {
2412
+ if (mount.type.syncfs) {
2413
+ mount.type.syncfs(mount, populate, done);
2414
+ } else {
2415
+ done(null);
2416
+ }
2417
+ }
2418
+ },
2419
+ mount(type, opts, mountpoint) {
2420
+ if (typeof type == 'string') {
2421
+ // The filesystem was not included, and instead we have an error
2422
+ // message stored in the variable.
2423
+ throw type;
2424
+ }
2425
+ var root = mountpoint === '/';
2426
+ var pseudo = !mountpoint;
2427
+ var node;
2428
+
2429
+ if (root && FS.root) {
2430
+ throw new FS.ErrnoError(10);
2431
+ } else if (!root && !pseudo) {
2432
+ var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
2433
+
2434
+ mountpoint = lookup.path; // use the absolute path
2435
+ node = lookup.node;
2436
+
2437
+ if (FS.isMountpoint(node)) {
2438
+ throw new FS.ErrnoError(10);
2439
+ }
2440
+
2441
+ if (!FS.isDir(node.mode)) {
2442
+ throw new FS.ErrnoError(54);
2443
+ }
2444
+ }
2445
+
2446
+ var mount = {
2447
+ type,
2448
+ opts,
2449
+ mountpoint,
2450
+ mounts: []
2451
+ };
2452
+
2453
+ // create a root node for the fs
2454
+ var mountRoot = type.mount(mount);
2455
+ mountRoot.mount = mount;
2456
+ mount.root = mountRoot;
2457
+
2458
+ if (root) {
2459
+ FS.root = mountRoot;
2460
+ } else if (node) {
2461
+ // set as a mountpoint
2462
+ node.mounted = mount;
2463
+
2464
+ // add the new mount to the current mount's children
2465
+ if (node.mount) {
2466
+ node.mount.mounts.push(mount);
2467
+ }
2468
+ }
2469
+
2470
+ return mountRoot;
2471
+ },
2472
+ unmount(mountpoint) {
2473
+ var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
2474
+
2475
+ if (!FS.isMountpoint(lookup.node)) {
2476
+ throw new FS.ErrnoError(28);
2477
+ }
2478
+
2479
+ // destroy the nodes for this mount, and all its child mounts
2480
+ var node = lookup.node;
2481
+ var mount = node.mounted;
2482
+ var mounts = FS.getMounts(mount);
2483
+
2484
+ for (var [hash, current] of Object.entries(FS.nameTable)) {
2485
+ while (current) {
2486
+ var next = current.name_next;
2487
+
2488
+ if (mounts.includes(current.mount)) {
2489
+ FS.destroyNode(current);
2490
+ }
2491
+
2492
+ current = next;
2493
+ }
2494
+ }
2495
+
2496
+ // no longer a mountpoint
2497
+ node.mounted = null;
2498
+
2499
+ // remove this mount from the child mounts
2500
+ var idx = node.mount.mounts.indexOf(mount);
2501
+ assert(idx !== -1);
2502
+ node.mount.mounts.splice(idx, 1);
2503
+ },
2504
+ lookup(parent, name) {
2505
+ return parent.node_ops.lookup(parent, name);
2506
+ },
2507
+ mknod(path, mode, dev) {
2508
+ var lookup = FS.lookupPath(path, { parent: true });
2509
+ var parent = lookup.node;
2510
+ var name = PATH.basename(path);
2511
+ if (!name) {
2512
+ throw new FS.ErrnoError(28);
2513
+ }
2514
+ if (name === '.' || name === '..') {
2515
+ throw new FS.ErrnoError(20);
2516
+ }
2517
+ var errCode = FS.mayCreate(parent, name);
2518
+ if (errCode) {
2519
+ throw new FS.ErrnoError(errCode);
2520
+ }
2521
+ if (!parent.node_ops.mknod) {
2522
+ throw new FS.ErrnoError(63);
2523
+ }
2524
+ return parent.node_ops.mknod(parent, name, mode, dev);
2525
+ },
2526
+ statfs(path) {
2527
+ return FS.statfsNode(FS.lookupPath(path, {follow: true}).node);
2528
+ },
2529
+ statfsStream(stream) {
2530
+ // We keep a separate statfsStream function because noderawfs overrides
2531
+ // it. In noderawfs, stream.node is sometimes null. Instead, we need to
2532
+ // look at stream.path.
2533
+ return FS.statfsNode(stream.node);
2534
+ },
2535
+ statfsNode(node) {
2536
+ // NOTE: None of the defaults here are true. We're just returning safe and
2537
+ // sane values. Currently nodefs and rawfs replace these defaults,
2538
+ // other file systems leave them alone.
2539
+ var rtn = {
2540
+ bsize: 4096,
2541
+ frsize: 4096,
2542
+ blocks: 1e6,
2543
+ bfree: 5e5,
2544
+ bavail: 5e5,
2545
+ files: FS.nextInode,
2546
+ ffree: FS.nextInode - 1,
2547
+ fsid: 42,
2548
+ flags: 2,
2549
+ namelen: 255,
2550
+ };
2551
+
2552
+ if (node.node_ops.statfs) {
2553
+ Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root));
2554
+ }
2555
+ return rtn;
2556
+ },
2557
+ create(path, mode = 0o666) {
2558
+ mode &= 4095;
2559
+ mode |= 32768;
2560
+ return FS.mknod(path, mode, 0);
2561
+ },
2562
+ mkdir(path, mode = 0o777) {
2563
+ mode &= 511 | 512;
2564
+ mode |= 16384;
2565
+ return FS.mknod(path, mode, 0);
2566
+ },
2567
+ mkdirTree(path, mode) {
2568
+ var dirs = path.split('/');
2569
+ var d = '';
2570
+ for (var dir of dirs) {
2571
+ if (!dir) continue;
2572
+ if (d || PATH.isAbs(path)) d += '/';
2573
+ d += dir;
2574
+ try {
2575
+ FS.mkdir(d, mode);
2576
+ } catch(e) {
2577
+ if (e.errno != 20) throw e;
2578
+ }
2579
+ }
2580
+ },
2581
+ mkdev(path, mode, dev) {
2582
+ if (typeof dev == 'undefined') {
2583
+ dev = mode;
2584
+ mode = 0o666;
2585
+ }
2586
+ mode |= 8192;
2587
+ return FS.mknod(path, mode, dev);
2588
+ },
2589
+ symlink(oldpath, newpath) {
2590
+ if (!PATH_FS.resolve(oldpath)) {
2591
+ throw new FS.ErrnoError(44);
2592
+ }
2593
+ var lookup = FS.lookupPath(newpath, { parent: true });
2594
+ var parent = lookup.node;
2595
+ if (!parent) {
2596
+ throw new FS.ErrnoError(44);
2597
+ }
2598
+ var newname = PATH.basename(newpath);
2599
+ var errCode = FS.mayCreate(parent, newname);
2600
+ if (errCode) {
2601
+ throw new FS.ErrnoError(errCode);
2602
+ }
2603
+ if (!parent.node_ops.symlink) {
2604
+ throw new FS.ErrnoError(63);
2605
+ }
2606
+ return parent.node_ops.symlink(parent, newname, oldpath);
2607
+ },
2608
+ rename(old_path, new_path) {
2609
+ var old_dirname = PATH.dirname(old_path);
2610
+ var new_dirname = PATH.dirname(new_path);
2611
+ var old_name = PATH.basename(old_path);
2612
+ var new_name = PATH.basename(new_path);
2613
+ // parents must exist
2614
+ var lookup, old_dir, new_dir;
2615
+
2616
+ // let the errors from non existent directories percolate up
2617
+ lookup = FS.lookupPath(old_path, { parent: true });
2618
+ old_dir = lookup.node;
2619
+ lookup = FS.lookupPath(new_path, { parent: true });
2620
+ new_dir = lookup.node;
2621
+
2622
+ if (!old_dir || !new_dir) throw new FS.ErrnoError(44);
2623
+ // need to be part of the same mount
2624
+ if (old_dir.mount !== new_dir.mount) {
2625
+ throw new FS.ErrnoError(75);
2626
+ }
2627
+ // source must exist
2628
+ var old_node = FS.lookupNode(old_dir, old_name);
2629
+ // old path should not be an ancestor of the new path
2630
+ var relative = PATH_FS.relative(old_path, new_dirname);
2631
+ if (relative.charAt(0) !== '.') {
2632
+ throw new FS.ErrnoError(28);
2633
+ }
2634
+ // new path should not be an ancestor of the old path
2635
+ relative = PATH_FS.relative(new_path, old_dirname);
2636
+ if (relative.charAt(0) !== '.') {
2637
+ throw new FS.ErrnoError(55);
2638
+ }
2639
+ // see if the new path already exists
2640
+ var new_node;
2641
+ try {
2642
+ new_node = FS.lookupNode(new_dir, new_name);
2643
+ } catch (e) {
2644
+ // not fatal
2645
+ }
2646
+ // early out if nothing needs to change
2647
+ if (old_node === new_node) {
2648
+ return;
2649
+ }
2650
+ // we'll need to delete the old entry
2651
+ var isdir = FS.isDir(old_node.mode);
2652
+ var errCode = FS.mayDelete(old_dir, old_name, isdir);
2653
+ if (errCode) {
2654
+ throw new FS.ErrnoError(errCode);
2655
+ }
2656
+ // need delete permissions if we'll be overwriting.
2657
+ // need create permissions if new doesn't already exist.
2658
+ errCode = new_node ?
2659
+ FS.mayDelete(new_dir, new_name, isdir) :
2660
+ FS.mayCreate(new_dir, new_name);
2661
+ if (errCode) {
2662
+ throw new FS.ErrnoError(errCode);
2663
+ }
2664
+ if (!old_dir.node_ops.rename) {
2665
+ throw new FS.ErrnoError(63);
2666
+ }
2667
+ if (FS.isMountpoint(old_node) || (new_node && FS.isMountpoint(new_node))) {
2668
+ throw new FS.ErrnoError(10);
2669
+ }
2670
+ // if we are going to change the parent, check write permissions
2671
+ if (new_dir !== old_dir) {
2672
+ errCode = FS.nodePermissions(old_dir, 'w');
2673
+ if (errCode) {
2674
+ throw new FS.ErrnoError(errCode);
2675
+ }
2676
+ }
2677
+ // remove the node from the lookup hash
2678
+ FS.hashRemoveNode(old_node);
2679
+ // do the underlying fs rename
2680
+ try {
2681
+ old_dir.node_ops.rename(old_node, new_dir, new_name);
2682
+ // update old node (we do this here to avoid each backend
2683
+ // needing to)
2684
+ old_node.parent = new_dir;
2685
+ } catch (e) {
2686
+ throw e;
2687
+ } finally {
2688
+ // add the node back to the hash (in case node_ops.rename
2689
+ // changed its name)
2690
+ FS.hashAddNode(old_node);
2691
+ }
2692
+ },
2693
+ rmdir(path) {
2694
+ var lookup = FS.lookupPath(path, { parent: true });
2695
+ var parent = lookup.node;
2696
+ var name = PATH.basename(path);
2697
+ var node = FS.lookupNode(parent, name);
2698
+ var errCode = FS.mayDelete(parent, name, true);
2699
+ if (errCode) {
2700
+ throw new FS.ErrnoError(errCode);
2701
+ }
2702
+ if (!parent.node_ops.rmdir) {
2703
+ throw new FS.ErrnoError(63);
2704
+ }
2705
+ if (FS.isMountpoint(node)) {
2706
+ throw new FS.ErrnoError(10);
2707
+ }
2708
+ parent.node_ops.rmdir(parent, name);
2709
+ FS.destroyNode(node);
2710
+ },
2711
+ readdir(path) {
2712
+ var lookup = FS.lookupPath(path, { follow: true });
2713
+ var node = lookup.node;
2714
+ var readdir = FS.checkOpExists(node.node_ops.readdir, 54);
2715
+ return readdir(node);
2716
+ },
2717
+ unlink(path) {
2718
+ var lookup = FS.lookupPath(path, { parent: true });
2719
+ var parent = lookup.node;
2720
+ if (!parent) {
2721
+ throw new FS.ErrnoError(44);
2722
+ }
2723
+ var name = PATH.basename(path);
2724
+ var node = FS.lookupNode(parent, name);
2725
+ var errCode = FS.mayDelete(parent, name, false);
2726
+ if (errCode) {
2727
+ // According to POSIX, we should map EISDIR to EPERM, but
2728
+ // we instead do what Linux does (and we must, as we use
2729
+ // the musl linux libc).
2730
+ throw new FS.ErrnoError(errCode);
2731
+ }
2732
+ if (!parent.node_ops.unlink) {
2733
+ throw new FS.ErrnoError(63);
2734
+ }
2735
+ if (FS.isMountpoint(node)) {
2736
+ throw new FS.ErrnoError(10);
2737
+ }
2738
+ parent.node_ops.unlink(parent, name);
2739
+ FS.destroyNode(node);
2740
+ },
2741
+ readlink(path) {
2742
+ var lookup = FS.lookupPath(path);
2743
+ var link = lookup.node;
2744
+ if (!link) {
2745
+ throw new FS.ErrnoError(44);
2746
+ }
2747
+ if (!link.node_ops.readlink) {
2748
+ throw new FS.ErrnoError(28);
2749
+ }
2750
+ return link.node_ops.readlink(link);
2751
+ },
2752
+ stat(path, dontFollow) {
2753
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2754
+ var node = lookup.node;
2755
+ var getattr = FS.checkOpExists(node.node_ops.getattr, 63);
2756
+ return getattr(node);
2757
+ },
2758
+ fstat(fd) {
2759
+ var stream = FS.getStreamChecked(fd);
2760
+ var node = stream.node;
2761
+ var getattr = stream.stream_ops.getattr;
2762
+ var arg = getattr ? stream : node;
2763
+ getattr ??= node.node_ops.getattr;
2764
+ FS.checkOpExists(getattr, 63)
2765
+ return getattr(arg);
2766
+ },
2767
+ lstat(path) {
2768
+ return FS.stat(path, true);
2769
+ },
2770
+ doChmod(stream, node, mode, dontFollow) {
2771
+ FS.doSetAttr(stream, node, {
2772
+ mode: (mode & 4095) | (node.mode & ~4095),
2773
+ ctime: Date.now(),
2774
+ dontFollow
2775
+ });
2776
+ },
2777
+ chmod(path, mode, dontFollow) {
2778
+ var node;
2779
+ if (typeof path == 'string') {
2780
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2781
+ node = lookup.node;
2782
+ } else {
2783
+ node = path;
2784
+ }
2785
+ FS.doChmod(null, node, mode, dontFollow);
2786
+ },
2787
+ lchmod(path, mode) {
2788
+ FS.chmod(path, mode, true);
2789
+ },
2790
+ fchmod(fd, mode) {
2791
+ var stream = FS.getStreamChecked(fd);
2792
+ FS.doChmod(stream, stream.node, mode, false);
2793
+ },
2794
+ doChown(stream, node, dontFollow) {
2795
+ FS.doSetAttr(stream, node, {
2796
+ timestamp: Date.now(),
2797
+ dontFollow
2798
+ // we ignore the uid / gid for now
2799
+ });
2800
+ },
2801
+ chown(path, uid, gid, dontFollow) {
2802
+ var node;
2803
+ if (typeof path == 'string') {
2804
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2805
+ node = lookup.node;
2806
+ } else {
2807
+ node = path;
2808
+ }
2809
+ FS.doChown(null, node, dontFollow);
2810
+ },
2811
+ lchown(path, uid, gid) {
2812
+ FS.chown(path, uid, gid, true);
2813
+ },
2814
+ fchown(fd, uid, gid) {
2815
+ var stream = FS.getStreamChecked(fd);
2816
+ FS.doChown(stream, stream.node, false);
2817
+ },
2818
+ doTruncate(stream, node, len) {
2819
+ if (FS.isDir(node.mode)) {
2820
+ throw new FS.ErrnoError(31);
2821
+ }
2822
+ if (!FS.isFile(node.mode)) {
2823
+ throw new FS.ErrnoError(28);
2824
+ }
2825
+ var errCode = FS.nodePermissions(node, 'w');
2826
+ if (errCode) {
2827
+ throw new FS.ErrnoError(errCode);
2828
+ }
2829
+ FS.doSetAttr(stream, node, {
2830
+ size: len,
2831
+ timestamp: Date.now()
2832
+ });
2833
+ },
2834
+ truncate(path, len) {
2835
+ if (len < 0) {
2836
+ throw new FS.ErrnoError(28);
2837
+ }
2838
+ var node;
2839
+ if (typeof path == 'string') {
2840
+ var lookup = FS.lookupPath(path, { follow: true });
2841
+ node = lookup.node;
2842
+ } else {
2843
+ node = path;
2844
+ }
2845
+ FS.doTruncate(null, node, len);
2846
+ },
2847
+ ftruncate(fd, len) {
2848
+ var stream = FS.getStreamChecked(fd);
2849
+ if (len < 0 || (stream.flags & 2097155) === 0) {
2850
+ throw new FS.ErrnoError(28);
2851
+ }
2852
+ FS.doTruncate(stream, stream.node, len);
2853
+ },
2854
+ utime(path, atime, mtime) {
2855
+ var lookup = FS.lookupPath(path, { follow: true });
2856
+ var node = lookup.node;
2857
+ var setattr = FS.checkOpExists(node.node_ops.setattr, 63);
2858
+ setattr(node, {
2859
+ atime: atime,
2860
+ mtime: mtime
2861
+ });
2862
+ },
2863
+ open(path, flags, mode = 0o666) {
2864
+ if (path === "") {
2865
+ throw new FS.ErrnoError(44);
2866
+ }
2867
+ flags = FS_modeStringToFlags(flags);
2868
+ if ((flags & 64)) {
2869
+ mode = (mode & 4095) | 32768;
2870
+ } else {
2871
+ mode = 0;
2872
+ }
2873
+ var node;
2874
+ var isDirPath;
2875
+ if (typeof path == 'object') {
2876
+ node = path;
2877
+ } else {
2878
+ isDirPath = path.endsWith("/");
2879
+ // noent_okay makes it so that if the final component of the path
2880
+ // doesn't exist, lookupPath returns `node: undefined`. `path` will be
2881
+ // updated to point to the target of all symlinks.
2882
+ var lookup = FS.lookupPath(path, {
2883
+ follow: !(flags & 131072),
2884
+ noent_okay: true
2885
+ });
2886
+ node = lookup.node;
2887
+ path = lookup.path;
2888
+ }
2889
+ // perhaps we need to create the node
2890
+ var created = false;
2891
+ if ((flags & 64)) {
2892
+ if (node) {
2893
+ // if O_CREAT and O_EXCL are set, error out if the node already exists
2894
+ if ((flags & 128)) {
2895
+ throw new FS.ErrnoError(20);
2896
+ }
2897
+ } else if (isDirPath) {
2898
+ throw new FS.ErrnoError(31);
2899
+ } else {
2900
+ // node doesn't exist, try to create it
2901
+ // Ignore the permission bits here to ensure we can `open` this new
2902
+ // file below. We use chmod below to apply the permissions once the
2903
+ // file is open.
2904
+ node = FS.mknod(path, mode | 0o777, 0);
2905
+ created = true;
2906
+ }
2907
+ }
2908
+ if (!node) {
2909
+ throw new FS.ErrnoError(44);
2910
+ }
2911
+ // can't truncate a device
2912
+ if (FS.isChrdev(node.mode)) {
2913
+ flags &= ~512;
2914
+ }
2915
+ // if asked only for a directory, then this must be one
2916
+ if ((flags & 65536) && !FS.isDir(node.mode)) {
2917
+ throw new FS.ErrnoError(54);
2918
+ }
2919
+ // check permissions, if this is not a file we just created now (it is ok to
2920
+ // create and write to a file with read-only permissions; it is read-only
2921
+ // for later use)
2922
+ if (!created) {
2923
+ var errCode = FS.mayOpen(node, flags);
2924
+ if (errCode) {
2925
+ throw new FS.ErrnoError(errCode);
2926
+ }
2927
+ }
2928
+ // do truncation if necessary
2929
+ if ((flags & 512) && !created) {
2930
+ FS.truncate(node, 0);
2931
+ }
2932
+ // we've already handled these, don't pass down to the underlying vfs
2933
+ flags &= ~(128 | 512 | 131072);
2934
+
2935
+ // register the stream with the filesystem
2936
+ var stream = FS.createStream({
2937
+ node,
2938
+ path: FS.getPath(node), // we want the absolute path to the node
2939
+ flags,
2940
+ seekable: true,
2941
+ position: 0,
2942
+ stream_ops: node.stream_ops,
2943
+ // used by the file family libc calls (fopen, fwrite, ferror, etc.)
2944
+ ungotten: [],
2945
+ error: false
2946
+ });
2947
+ // call the new stream's open function
2948
+ if (stream.stream_ops.open) {
2949
+ stream.stream_ops.open(stream);
2950
+ }
2951
+ if (created) {
2952
+ FS.chmod(node, mode & 0o777);
2953
+ }
2954
+ return stream;
2955
+ },
2956
+ close(stream) {
2957
+ if (FS.isClosed(stream)) {
2958
+ throw new FS.ErrnoError(8);
2959
+ }
2960
+ if (stream.getdents) stream.getdents = null; // free readdir state
2961
+ try {
2962
+ if (stream.stream_ops.close) {
2963
+ stream.stream_ops.close(stream);
2964
+ }
2965
+ } catch (e) {
2966
+ throw e;
2967
+ } finally {
2968
+ FS.closeStream(stream.fd);
2969
+ }
2970
+ stream.fd = null;
2971
+ },
2972
+ isClosed(stream) {
2973
+ return stream.fd === null;
2974
+ },
2975
+ llseek(stream, offset, whence) {
2976
+ if (FS.isClosed(stream)) {
2977
+ throw new FS.ErrnoError(8);
2978
+ }
2979
+ if (!stream.seekable || !stream.stream_ops.llseek) {
2980
+ throw new FS.ErrnoError(70);
2981
+ }
2982
+ if (whence != 0 && whence != 1 && whence != 2) {
2983
+ throw new FS.ErrnoError(28);
2984
+ }
2985
+ stream.position = stream.stream_ops.llseek(stream, offset, whence);
2986
+ stream.ungotten = [];
2987
+ return stream.position;
2988
+ },
2989
+ read(stream, buffer, offset, length, position) {
2990
+ assert(offset >= 0);
2991
+ if (length < 0 || position < 0) {
2992
+ throw new FS.ErrnoError(28);
2993
+ }
2994
+ if (FS.isClosed(stream)) {
2995
+ throw new FS.ErrnoError(8);
2996
+ }
2997
+ if ((stream.flags & 2097155) === 1) {
2998
+ throw new FS.ErrnoError(8);
2999
+ }
3000
+ if (FS.isDir(stream.node.mode)) {
3001
+ throw new FS.ErrnoError(31);
3002
+ }
3003
+ if (!stream.stream_ops.read) {
3004
+ throw new FS.ErrnoError(28);
3005
+ }
3006
+ var seeking = typeof position != 'undefined';
3007
+ if (!seeking) {
3008
+ position = stream.position;
3009
+ } else if (!stream.seekable) {
3010
+ throw new FS.ErrnoError(70);
3011
+ }
3012
+ var bytesRead = stream.stream_ops.read(stream, buffer, offset, length, position);
3013
+ if (!seeking) stream.position += bytesRead;
3014
+ return bytesRead;
3015
+ },
3016
+ write(stream, buffer, offset, length, position, canOwn) {
3017
+ assert(offset >= 0);
3018
+ assert(buffer.subarray, 'FS.write expects a TypedArray');
3019
+ if (length < 0 || position < 0) {
3020
+ throw new FS.ErrnoError(28);
3021
+ }
3022
+ if (FS.isClosed(stream)) {
3023
+ throw new FS.ErrnoError(8);
3024
+ }
3025
+ if ((stream.flags & 2097155) === 0) {
3026
+ throw new FS.ErrnoError(8);
3027
+ }
3028
+ if (FS.isDir(stream.node.mode)) {
3029
+ throw new FS.ErrnoError(31);
3030
+ }
3031
+ if (!stream.stream_ops.write) {
3032
+ throw new FS.ErrnoError(28);
3033
+ }
3034
+ if (stream.seekable && stream.flags & 1024) {
3035
+ // seek to the end before writing in append mode
3036
+ FS.llseek(stream, 0, 2);
3037
+ }
3038
+ var seeking = typeof position != 'undefined';
3039
+ if (!seeking) {
3040
+ position = stream.position;
3041
+ } else if (!stream.seekable) {
3042
+ throw new FS.ErrnoError(70);
3043
+ }
3044
+ var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn);
3045
+ if (!seeking) stream.position += bytesWritten;
3046
+ return bytesWritten;
3047
+ },
3048
+ mmap(stream, length, position, prot, flags) {
3049
+ // User requests writing to file (prot & PROT_WRITE != 0).
3050
+ // Checking if we have permissions to write to the file unless
3051
+ // MAP_PRIVATE flag is set. According to POSIX spec it is possible
3052
+ // to write to file opened in read-only mode with MAP_PRIVATE flag,
3053
+ // as all modifications will be visible only in the memory of
3054
+ // the current process.
3055
+ if ((prot & 2) !== 0
3056
+ && (flags & 2) === 0
3057
+ && (stream.flags & 2097155) !== 2) {
3058
+ throw new FS.ErrnoError(2);
3059
+ }
3060
+ if ((stream.flags & 2097155) === 1) {
3061
+ throw new FS.ErrnoError(2);
3062
+ }
3063
+ if (!stream.stream_ops.mmap) {
3064
+ throw new FS.ErrnoError(43);
3065
+ }
3066
+ if (!length) {
3067
+ throw new FS.ErrnoError(28);
3068
+ }
3069
+ return stream.stream_ops.mmap(stream, length, position, prot, flags);
3070
+ },
3071
+ msync(stream, buffer, offset, length, mmapFlags) {
3072
+ assert(offset >= 0);
3073
+ if (!stream.stream_ops.msync) {
3074
+ return 0;
3075
+ }
3076
+ return stream.stream_ops.msync(stream, buffer, offset, length, mmapFlags);
3077
+ },
3078
+ ioctl(stream, cmd, arg) {
3079
+ if (!stream.stream_ops.ioctl) {
3080
+ throw new FS.ErrnoError(59);
3081
+ }
3082
+ return stream.stream_ops.ioctl(stream, cmd, arg);
3083
+ },
3084
+ readFile(path, opts = {}) {
3085
+ opts.flags = opts.flags || 0;
3086
+ opts.encoding = opts.encoding || 'binary';
3087
+ if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
3088
+ abort(`Invalid encoding type "${opts.encoding}"`);
3089
+ }
3090
+ var stream = FS.open(path, opts.flags);
3091
+ var stat = FS.stat(path);
3092
+ var length = stat.size;
3093
+ var buf = new Uint8Array(length);
3094
+ FS.read(stream, buf, 0, length, 0);
3095
+ if (opts.encoding === 'utf8') {
3096
+ buf = UTF8ArrayToString(buf);
3097
+ }
3098
+ FS.close(stream);
3099
+ return buf;
3100
+ },
3101
+ writeFile(path, data, opts = {}) {
3102
+ opts.flags = opts.flags || 577;
3103
+ var stream = FS.open(path, opts.flags, opts.mode);
3104
+ data = FS_fileDataToTypedArray(data);
3105
+ FS.write(stream, data, 0, data.byteLength, undefined, opts.canOwn);
3106
+ FS.close(stream);
3107
+ },
3108
+ cwd:() => FS.currentPath,
3109
+ chdir(path) {
3110
+ var lookup = FS.lookupPath(path, { follow: true });
3111
+ if (lookup.node === null) {
3112
+ throw new FS.ErrnoError(44);
3113
+ }
3114
+ if (!FS.isDir(lookup.node.mode)) {
3115
+ throw new FS.ErrnoError(54);
3116
+ }
3117
+ var errCode = FS.nodePermissions(lookup.node, 'x');
3118
+ if (errCode) {
3119
+ throw new FS.ErrnoError(errCode);
3120
+ }
3121
+ FS.currentPath = lookup.path;
3122
+ },
3123
+ createDefaultDirectories() {
3124
+ FS.mkdir('/tmp');
3125
+ FS.mkdir('/home');
3126
+ FS.mkdir('/home/web_user');
3127
+ },
3128
+ createDefaultDevices() {
3129
+ // create /dev
3130
+ FS.mkdir('/dev');
3131
+ // setup /dev/null
3132
+ FS.registerDevice(FS.makedev(1, 3), {
3133
+ read: () => 0,
3134
+ write: (stream, buffer, offset, length, pos) => length,
3135
+ llseek: () => 0,
3136
+ });
3137
+ FS.mkdev('/dev/null', FS.makedev(1, 3));
3138
+ // setup /dev/tty and /dev/tty1
3139
+ // stderr needs to print output using err() rather than out()
3140
+ // so we register a second tty just for it.
3141
+ TTY.register(FS.makedev(5, 0), TTY.default_tty_ops);
3142
+ TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops);
3143
+ FS.mkdev('/dev/tty', FS.makedev(5, 0));
3144
+ FS.mkdev('/dev/tty1', FS.makedev(6, 0));
3145
+ // setup /dev/[u]random
3146
+ // use a buffer to avoid overhead of individual crypto calls per byte
3147
+ var randomBuffer = new Uint8Array(1024), randomLeft = 0;
3148
+ var randomByte = () => {
3149
+ if (randomLeft === 0) {
3150
+ randomFill(randomBuffer);
3151
+ randomLeft = randomBuffer.byteLength;
3152
+ }
3153
+ return randomBuffer[--randomLeft];
3154
+ };
3155
+ FS.createDevice('/dev', 'random', randomByte);
3156
+ FS.createDevice('/dev', 'urandom', randomByte);
3157
+ // we're not going to emulate the actual shm device,
3158
+ // just create the tmp dirs that reside in it commonly
3159
+ FS.mkdir('/dev/shm');
3160
+ FS.mkdir('/dev/shm/tmp');
3161
+ },
3162
+ createSpecialDirectories() {
3163
+ // create /proc/self/fd which allows /proc/self/fd/6 => readlink gives the
3164
+ // name of the stream for fd 6 (see test_unistd_ttyname)
3165
+ FS.mkdir('/proc');
3166
+ var proc_self = FS.mkdir('/proc/self');
3167
+ FS.mkdir('/proc/self/fd');
3168
+ FS.mount({
3169
+ mount() {
3170
+ var node = FS.createNode(proc_self, 'fd', 16895, 73);
3171
+ node.stream_ops = {
3172
+ llseek: MEMFS.stream_ops.llseek,
3173
+ };
3174
+ node.node_ops = {
3175
+ lookup(parent, name) {
3176
+ var fd = +name;
3177
+ var stream = FS.getStreamChecked(fd);
3178
+ var ret = {
3179
+ parent: null,
3180
+ mount: { mountpoint: 'fake' },
3181
+ node_ops: { readlink: () => stream.path },
3182
+ id: fd + 1,
3183
+ };
3184
+ ret.parent = ret; // make it look like a simple root node
3185
+ return ret;
3186
+ },
3187
+ readdir() {
3188
+ return Array.from(FS.streams.entries())
3189
+ .filter(([k, v]) => v)
3190
+ .map(([k, v]) => k.toString());
3191
+ }
3192
+ };
3193
+ return node;
3194
+ }
3195
+ }, {}, '/proc/self/fd');
3196
+ },
3197
+ createStandardStreams(input, output, error) {
3198
+ // TODO deprecate the old functionality of a single
3199
+ // input / output callback and that utilizes FS.createDevice
3200
+ // and instead require a unique set of stream ops
3201
+
3202
+ // by default, we symlink the standard streams to the
3203
+ // default tty devices. however, if the standard streams
3204
+ // have been overwritten we create a unique device for
3205
+ // them instead.
3206
+ if (input) {
3207
+ FS.createDevice('/dev', 'stdin', input);
3208
+ } else {
3209
+ FS.symlink('/dev/tty', '/dev/stdin');
3210
+ }
3211
+ if (output) {
3212
+ FS.createDevice('/dev', 'stdout', null, output);
3213
+ } else {
3214
+ FS.symlink('/dev/tty', '/dev/stdout');
3215
+ }
3216
+ if (error) {
3217
+ FS.createDevice('/dev', 'stderr', null, error);
3218
+ } else {
3219
+ FS.symlink('/dev/tty1', '/dev/stderr');
3220
+ }
3221
+
3222
+ // open default streams for the stdin, stdout and stderr devices
3223
+ var stdin = FS.open('/dev/stdin', 0);
3224
+ var stdout = FS.open('/dev/stdout', 1);
3225
+ var stderr = FS.open('/dev/stderr', 1);
3226
+ assert(stdin.fd === 0, `invalid handle for stdin (${stdin.fd})`);
3227
+ assert(stdout.fd === 1, `invalid handle for stdout (${stdout.fd})`);
3228
+ assert(stderr.fd === 2, `invalid handle for stderr (${stderr.fd})`);
3229
+ },
3230
+ staticInit() {
3231
+ FS.nameTable = new Array(4096);
3232
+
3233
+ FS.mount(MEMFS, {}, '/');
3234
+
3235
+ FS.createDefaultDirectories();
3236
+ FS.createDefaultDevices();
3237
+ FS.createSpecialDirectories();
3238
+
3239
+ FS.filesystems = {
3240
+ 'MEMFS': MEMFS,
3241
+ };
3242
+ },
3243
+ init(input, output, error) {
3244
+ assert(!FS.initialized, 'FS.init was previously called. If you want to initialize later with custom parameters, remove any earlier calls (note that one is automatically added to the generated code)');
3245
+ FS.initialized = true;
3246
+
3247
+ // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here
3248
+ input ??= Module['stdin'];
3249
+ output ??= Module['stdout'];
3250
+ error ??= Module['stderr'];
3251
+
3252
+ FS.createStandardStreams(input, output, error);
3253
+ },
3254
+ quit() {
3255
+ FS.initialized = false;
3256
+ // force-flush all streams, so we get musl std streams printed out
3257
+ _fflush(0);
3258
+ // close all of our streams
3259
+ for (var stream of FS.streams) {
3260
+ if (stream) {
3261
+ FS.close(stream);
3262
+ }
3263
+ }
3264
+ },
3265
+ findObject(path, dontResolveLastLink) {
3266
+ var ret = FS.analyzePath(path, dontResolveLastLink);
3267
+ if (!ret.exists) {
3268
+ return null;
3269
+ }
3270
+ return ret.object;
3271
+ },
3272
+ analyzePath(path, dontResolveLastLink) {
3273
+ // operate from within the context of the symlink's target
3274
+ try {
3275
+ var lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
3276
+ path = lookup.path;
3277
+ } catch (e) {
3278
+ }
3279
+ var ret = {
3280
+ isRoot: false, exists: false, error: 0, name: null, path: null, object: null,
3281
+ parentExists: false, parentPath: null, parentObject: null
3282
+ };
3283
+ try {
3284
+ var lookup = FS.lookupPath(path, { parent: true });
3285
+ ret.parentExists = true;
3286
+ ret.parentPath = lookup.path;
3287
+ ret.parentObject = lookup.node;
3288
+ ret.name = PATH.basename(path);
3289
+ lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
3290
+ ret.exists = true;
3291
+ ret.path = lookup.path;
3292
+ ret.object = lookup.node;
3293
+ ret.name = lookup.node.name;
3294
+ ret.isRoot = lookup.path === '/';
3295
+ } catch (e) {
3296
+ ret.error = e.errno;
3297
+ };
3298
+ return ret;
3299
+ },
3300
+ createPath(parent, path, canRead, canWrite) {
3301
+ parent = typeof parent == 'string' ? parent : FS.getPath(parent);
3302
+ var parts = path.split('/').reverse();
3303
+ while (parts.length) {
3304
+ var part = parts.pop();
3305
+ if (!part) continue;
3306
+ var current = PATH.join2(parent, part);
3307
+ try {
3308
+ FS.mkdir(current);
3309
+ } catch (e) {
3310
+ if (e.errno != 20) throw e;
3311
+ }
3312
+ parent = current;
3313
+ }
3314
+ return current;
3315
+ },
3316
+ createFile(parent, name, properties, canRead, canWrite) {
3317
+ var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
3318
+ var mode = FS_getMode(canRead, canWrite);
3319
+ return FS.create(path, mode);
3320
+ },
3321
+ createDataFile(parent, name, data, canRead, canWrite, canOwn) {
3322
+ var path = name;
3323
+ if (parent) {
3324
+ parent = typeof parent == 'string' ? parent : FS.getPath(parent);
3325
+ path = name ? PATH.join2(parent, name) : parent;
3326
+ }
3327
+ var mode = FS_getMode(canRead, canWrite);
3328
+ var node = FS.create(path, mode);
3329
+ if (data) {
3330
+ data = FS_fileDataToTypedArray(data);
3331
+ // make sure we can write to the file
3332
+ FS.chmod(node, mode | 146);
3333
+ var stream = FS.open(node, 577);
3334
+ FS.write(stream, data, 0, data.length, 0, canOwn);
3335
+ FS.close(stream);
3336
+ FS.chmod(node, mode);
3337
+ }
3338
+ },
3339
+ createDevice(parent, name, input, output) {
3340
+ var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
3341
+ var mode = FS_getMode(!!input, !!output);
3342
+ FS.createDevice.major ??= 64;
3343
+ var dev = FS.makedev(FS.createDevice.major++, 0);
3344
+ // Create a fake device that a set of stream ops to emulate
3345
+ // the old behavior.
3346
+ FS.registerDevice(dev, {
3347
+ open(stream) {
3348
+ stream.seekable = false;
3349
+ },
3350
+ close(stream) {
3351
+ // flush any pending line data
3352
+ if (output?.buffer?.length) {
3353
+ output(10);
3354
+ }
3355
+ },
3356
+ read(stream, buffer, offset, length, pos /* ignored */) {
3357
+ var bytesRead = 0;
3358
+ for (var i = 0; i < length; i++) {
3359
+ var result;
3360
+ try {
3361
+ result = input();
3362
+ } catch (e) {
3363
+ throw new FS.ErrnoError(29);
3364
+ }
3365
+ if (result === undefined && bytesRead === 0) {
3366
+ throw new FS.ErrnoError(6);
3367
+ }
3368
+ if (result === null || result === undefined) break;
3369
+ bytesRead++;
3370
+ buffer[offset+i] = result;
3371
+ }
3372
+ if (bytesRead) {
3373
+ stream.node.atime = Date.now();
3374
+ }
3375
+ return bytesRead;
3376
+ },
3377
+ write(stream, buffer, offset, length, pos) {
3378
+ for (var i = 0; i < length; i++) {
3379
+ try {
3380
+ output(buffer[offset+i]);
3381
+ } catch (e) {
3382
+ throw new FS.ErrnoError(29);
3383
+ }
3384
+ }
3385
+ if (length) {
3386
+ stream.node.mtime = stream.node.ctime = Date.now();
3387
+ }
3388
+ return i;
3389
+ }
3390
+ });
3391
+ return FS.mkdev(path, mode, dev);
3392
+ },
3393
+ forceLoadFile(obj) {
3394
+ if (obj.isDevice || obj.isFolder || obj.link || obj.contents) return true;
3395
+ if (globalThis.XMLHttpRequest) {
3396
+ abort("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.");
3397
+ } else { // Command-line.
3398
+ try {
3399
+ obj.contents = readBinary(obj.url);
3400
+ } catch (e) {
3401
+ throw new FS.ErrnoError(29);
3402
+ }
3403
+ }
3404
+ },
3405
+ createLazyFile(parent, name, url, canRead, canWrite) {
3406
+ // Lazy chunked Uint8Array (implements get and length from Uint8Array).
3407
+ // Actual getting is abstracted away for eventual reuse.
3408
+ class LazyUint8Array {
3409
+ lengthKnown = false;
3410
+ chunks = []; // Loaded chunks. Index is the chunk number
3411
+ get(idx) {
3412
+ if (idx > this.length-1 || idx < 0) {
3413
+ return undefined;
3414
+ }
3415
+ var chunkOffset = idx % this.chunkSize;
3416
+ var chunkNum = (idx / this.chunkSize)|0;
3417
+ return this.getter(chunkNum)[chunkOffset];
3418
+ }
3419
+ setDataGetter(getter) {
3420
+ this.getter = getter;
3421
+ }
3422
+ cacheLength() {
3423
+ // Find length
3424
+ var xhr = new XMLHttpRequest();
3425
+ xhr.open('HEAD', url, false);
3426
+ xhr.send(null);
3427
+ if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
3428
+ var datalength = Number(xhr.getResponseHeader("Content-length"));
3429
+ var header;
3430
+ var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes";
3431
+ var usesGzip = (header = xhr.getResponseHeader("Content-Encoding")) && header === "gzip";
3432
+
3433
+ var chunkSize = 1024*1024; // Chunk size in bytes
3434
+
3435
+ if (!hasByteServing) chunkSize = datalength;
3436
+
3437
+ // Function to get a range from the remote URL.
3438
+ var doXHR = (from, to) => {
3439
+ if (from > to) abort("invalid range (" + from + ", " + to + ") or no bytes requested!");
3440
+ if (to > datalength-1) abort("only " + datalength + " bytes available! programmer error!");
3441
+
3442
+ // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available.
3443
+ var xhr = new XMLHttpRequest();
3444
+ xhr.open('GET', url, false);
3445
+ if (datalength !== chunkSize) xhr.setRequestHeader("Range", "bytes=" + from + "-" + to);
3446
+
3447
+ // Some hints to the browser that we want binary data.
3448
+ xhr.responseType = 'arraybuffer';
3449
+ if (xhr.overrideMimeType) {
3450
+ xhr.overrideMimeType('text/plain; charset=x-user-defined');
3451
+ }
3452
+
3453
+ xhr.send(null);
3454
+ if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
3455
+ if (xhr.response !== undefined) {
3456
+ return new Uint8Array(/** @type{Array<number>} */(xhr.response || []));
3457
+ }
3458
+ return intArrayFromString(xhr.responseText || '', true);
3459
+ };
3460
+ var lazyArray = this;
3461
+ lazyArray.setDataGetter((chunkNum) => {
3462
+ var start = chunkNum * chunkSize;
3463
+ var end = (chunkNum+1) * chunkSize - 1; // including this byte
3464
+ end = Math.min(end, datalength-1); // if datalength-1 is selected, this is the last block
3465
+ if (typeof lazyArray.chunks[chunkNum] == 'undefined') {
3466
+ lazyArray.chunks[chunkNum] = doXHR(start, end);
3467
+ }
3468
+ if (typeof lazyArray.chunks[chunkNum] == 'undefined') abort('doXHR failed!');
3469
+ return lazyArray.chunks[chunkNum];
3470
+ });
3471
+
3472
+ if (usesGzip || !datalength) {
3473
+ // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length
3474
+ chunkSize = datalength = 1; // this will force getter(0)/doXHR do download the whole file
3475
+ datalength = this.getter(0).length;
3476
+ chunkSize = datalength;
3477
+ out("LazyFiles on gzip forces download of the whole file when length is accessed");
3478
+ }
3479
+
3480
+ this._length = datalength;
3481
+ this._chunkSize = chunkSize;
3482
+ this.lengthKnown = true;
3483
+ }
3484
+ get length() {
3485
+ if (!this.lengthKnown) {
3486
+ this.cacheLength();
3487
+ }
3488
+ return this._length;
3489
+ }
3490
+ get chunkSize() {
3491
+ if (!this.lengthKnown) {
3492
+ this.cacheLength();
3493
+ }
3494
+ return this._chunkSize;
3495
+ }
3496
+ }
3497
+
3498
+ if (globalThis.XMLHttpRequest) {
3499
+ if (!ENVIRONMENT_IS_WORKER) abort('Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc');
3500
+ var lazyArray = new LazyUint8Array();
3501
+ var properties = { isDevice: false, contents: lazyArray };
3502
+ } else {
3503
+ var properties = { isDevice: false, url: url };
3504
+ }
3505
+
3506
+ var node = FS.createFile(parent, name, properties, canRead, canWrite);
3507
+ // This is a total hack, but I want to get this lazy file code out of the
3508
+ // core of MEMFS. If we want to keep this lazy file concept I feel it should
3509
+ // be its own thin LAZYFS proxying calls to MEMFS.
3510
+ if (properties.contents) {
3511
+ node.contents = properties.contents;
3512
+ } else if (properties.url) {
3513
+ node.contents = null;
3514
+ node.url = properties.url;
3515
+ }
3516
+ // Add a function that defers querying the file size until it is asked the first time.
3517
+ Object.defineProperties(node, {
3518
+ usedBytes: {
3519
+ get: function() { return this.contents.length; }
3520
+ }
3521
+ });
3522
+ // override each stream op with one that tries to force load the lazy file first
3523
+ var stream_ops = {};
3524
+ for (const [key, fn] of Object.entries(node.stream_ops)) {
3525
+ stream_ops[key] = (...args) => {
3526
+ FS.forceLoadFile(node);
3527
+ return fn(...args);
3528
+ };
3529
+ }
3530
+ function writeChunks(stream, buffer, offset, length, position) {
3531
+ var contents = stream.node.contents;
3532
+ if (position >= contents.length)
3533
+ return 0;
3534
+ var size = Math.min(contents.length - position, length);
3535
+ assert(size >= 0);
3536
+ if (contents.slice) { // normal array
3537
+ for (var i = 0; i < size; i++) {
3538
+ buffer[offset + i] = contents[position + i];
3539
+ }
3540
+ } else {
3541
+ for (var i = 0; i < size; i++) { // LazyUint8Array from sync binary XHR
3542
+ buffer[offset + i] = contents.get(position + i);
3543
+ }
3544
+ }
3545
+ return size;
3546
+ }
3547
+ // use a custom read function
3548
+ stream_ops.read = (stream, buffer, offset, length, position) => {
3549
+ FS.forceLoadFile(node);
3550
+ return writeChunks(stream, buffer, offset, length, position)
3551
+ };
3552
+ // use a custom mmap function
3553
+ stream_ops.mmap = (stream, length, position, prot, flags) => {
3554
+ FS.forceLoadFile(node);
3555
+ var ptr = mmapAlloc(length);
3556
+ if (!ptr) {
3557
+ throw new FS.ErrnoError(48);
3558
+ }
3559
+ writeChunks(stream, HEAP8, ptr, length, position);
3560
+ return { ptr, allocated: true };
3561
+ };
3562
+ node.stream_ops = stream_ops;
3563
+ return node;
3564
+ },
3565
+ };
3566
+
3567
+ var SYSCALLS = {
3568
+ calculateAt(dirfd, path, allowEmpty) {
3569
+ if (PATH.isAbs(path)) {
3570
+ return path;
3571
+ }
3572
+ // relative path
3573
+ var dir;
3574
+ if (dirfd === -100) {
3575
+ dir = FS.cwd();
3576
+ } else {
3577
+ var dirstream = SYSCALLS.getStreamFromFD(dirfd);
3578
+ dir = dirstream.path;
3579
+ }
3580
+ if (path.length == 0) {
3581
+ if (!allowEmpty) {
3582
+ throw new FS.ErrnoError(44);;
3583
+ }
3584
+ return dir;
3585
+ }
3586
+ return dir + '/' + path;
3587
+ },
3588
+ writeStat(buf, stat) {
3589
+ HEAPU32[((buf)>>2)] = stat.dev;
3590
+ HEAPU32[(((buf)+(4))>>2)] = stat.mode;
3591
+ HEAPU32[(((buf)+(8))>>2)] = stat.nlink;
3592
+ HEAPU32[(((buf)+(12))>>2)] = stat.uid;
3593
+ HEAPU32[(((buf)+(16))>>2)] = stat.gid;
3594
+ HEAPU32[(((buf)+(20))>>2)] = stat.rdev;
3595
+ HEAP64[(((buf)+(24))>>3)] = BigInt(stat.size);
3596
+ HEAP32[(((buf)+(32))>>2)] = 4096;
3597
+ HEAP32[(((buf)+(36))>>2)] = stat.blocks;
3598
+ var atime = stat.atime.getTime();
3599
+ var mtime = stat.mtime.getTime();
3600
+ var ctime = stat.ctime.getTime();
3601
+ HEAP64[(((buf)+(40))>>3)] = BigInt(Math.floor(atime / 1000));
3602
+ HEAPU32[(((buf)+(48))>>2)] = (atime % 1000) * 1000 * 1000;
3603
+ HEAP64[(((buf)+(56))>>3)] = BigInt(Math.floor(mtime / 1000));
3604
+ HEAPU32[(((buf)+(64))>>2)] = (mtime % 1000) * 1000 * 1000;
3605
+ HEAP64[(((buf)+(72))>>3)] = BigInt(Math.floor(ctime / 1000));
3606
+ HEAPU32[(((buf)+(80))>>2)] = (ctime % 1000) * 1000 * 1000;
3607
+ HEAP64[(((buf)+(88))>>3)] = BigInt(stat.ino);
3608
+ return 0;
3609
+ },
3610
+ writeStatFs(buf, stats) {
3611
+ HEAPU32[(((buf)+(4))>>2)] = stats.bsize;
3612
+ HEAPU32[(((buf)+(60))>>2)] = stats.bsize;
3613
+ HEAP64[(((buf)+(8))>>3)] = BigInt(stats.blocks);
3614
+ HEAP64[(((buf)+(16))>>3)] = BigInt(stats.bfree);
3615
+ HEAP64[(((buf)+(24))>>3)] = BigInt(stats.bavail);
3616
+ HEAP64[(((buf)+(32))>>3)] = BigInt(stats.files);
3617
+ HEAP64[(((buf)+(40))>>3)] = BigInt(stats.ffree);
3618
+ HEAPU32[(((buf)+(48))>>2)] = stats.fsid;
3619
+ HEAPU32[(((buf)+(64))>>2)] = stats.flags; // ST_NOSUID
3620
+ HEAPU32[(((buf)+(56))>>2)] = stats.namelen;
3621
+ },
3622
+ doMsync(addr, stream, len, flags, offset) {
3623
+ if (!FS.isFile(stream.node.mode)) {
3624
+ throw new FS.ErrnoError(43);
3625
+ }
3626
+ if (flags & 2) {
3627
+ // MAP_PRIVATE calls need not to be synced back to underlying fs
3628
+ return 0;
3629
+ }
3630
+ var buffer = HEAPU8.slice(addr, addr + len);
3631
+ FS.msync(stream, buffer, offset, len, flags);
3632
+ },
3633
+ getStreamFromFD(fd) {
3634
+ var stream = FS.getStreamChecked(fd);
3635
+ return stream;
3636
+ },
3637
+ varargs:undefined,
3638
+ getStr(ptr) {
3639
+ var ret = UTF8ToString(ptr);
3640
+ return ret;
3641
+ },
3642
+ };
3643
+ function ___syscall_fcntl64(fd, cmd, varargs) {
3644
+ SYSCALLS.varargs = varargs;
3645
+ try {
3646
+
3647
+ var stream = SYSCALLS.getStreamFromFD(fd);
3648
+ switch (cmd) {
3649
+ case 0: {
3650
+ var arg = syscallGetVarargI();
3651
+ if (arg < 0) {
3652
+ return -28;
3653
+ }
3654
+ while (FS.streams[arg]) {
3655
+ arg++;
3656
+ }
3657
+ var newStream;
3658
+ newStream = FS.dupStream(stream, arg);
3659
+ return newStream.fd;
3660
+ }
3661
+ case 1:
3662
+ case 2:
3663
+ return 0; // FD_CLOEXEC makes no sense for a single process.
3664
+ case 3:
3665
+ return stream.flags;
3666
+ case 4: {
3667
+ var arg = syscallGetVarargI();
3668
+ stream.flags |= arg;
3669
+ return 0;
3670
+ }
3671
+ case 12: {
3672
+ var arg = syscallGetVarargP();
3673
+ var offset = 0;
3674
+ // We're always unlocked.
3675
+ HEAP16[(((arg)+(offset))>>1)] = 2;
3676
+ return 0;
3677
+ }
3678
+ case 13:
3679
+ case 14:
3680
+ // Pretend that the locking is successful. These are process-level locks,
3681
+ // and Emscripten programs are a single process. If we supported linking a
3682
+ // filesystem between programs, we'd need to do more here.
3683
+ // See https://github.com/emscripten-core/emscripten/issues/23697
3684
+ return 0;
3685
+ }
3686
+ return -28;
3687
+ } catch (e) {
3688
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3689
+ return -e.errno;
3690
+ }
3691
+ }
3692
+
3693
+
3694
+ function ___syscall_ioctl(fd, op, varargs) {
3695
+ SYSCALLS.varargs = varargs;
3696
+ try {
3697
+
3698
+ var stream = SYSCALLS.getStreamFromFD(fd);
3699
+ switch (op) {
3700
+ case 21509: {
3701
+ if (!stream.tty) return -59;
3702
+ return 0;
3703
+ }
3704
+ case 21505: {
3705
+ if (!stream.tty) return -59;
3706
+ if (stream.tty.ops.ioctl_tcgets) {
3707
+ var termios = stream.tty.ops.ioctl_tcgets(stream);
3708
+ var argp = syscallGetVarargP();
3709
+ HEAP32[((argp)>>2)] = termios.c_iflag || 0;
3710
+ HEAP32[(((argp)+(4))>>2)] = termios.c_oflag || 0;
3711
+ HEAP32[(((argp)+(8))>>2)] = termios.c_cflag || 0;
3712
+ HEAP32[(((argp)+(12))>>2)] = termios.c_lflag || 0;
3713
+ for (var i = 0; i < 32; i++) {
3714
+ HEAP8[(argp + i)+(17)] = termios.c_cc[i] || 0;
3715
+ }
3716
+ return 0;
3717
+ }
3718
+ return 0;
3719
+ }
3720
+ case 21510:
3721
+ case 21511:
3722
+ case 21512: {
3723
+ if (!stream.tty) return -59;
3724
+ return 0; // no-op, not actually adjusting terminal settings
3725
+ }
3726
+ case 21506:
3727
+ case 21507:
3728
+ case 21508: {
3729
+ if (!stream.tty) return -59;
3730
+ if (stream.tty.ops.ioctl_tcsets) {
3731
+ var argp = syscallGetVarargP();
3732
+ var c_iflag = HEAP32[((argp)>>2)];
3733
+ var c_oflag = HEAP32[(((argp)+(4))>>2)];
3734
+ var c_cflag = HEAP32[(((argp)+(8))>>2)];
3735
+ var c_lflag = HEAP32[(((argp)+(12))>>2)];
3736
+ var c_cc = []
3737
+ for (var i = 0; i < 32; i++) {
3738
+ c_cc.push(HEAP8[(argp + i)+(17)]);
3739
+ }
3740
+ return stream.tty.ops.ioctl_tcsets(stream.tty, op, { c_iflag, c_oflag, c_cflag, c_lflag, c_cc });
3741
+ }
3742
+ return 0; // no-op, not actually adjusting terminal settings
3743
+ }
3744
+ case 21519: {
3745
+ if (!stream.tty) return -59;
3746
+ var argp = syscallGetVarargP();
3747
+ HEAP32[((argp)>>2)] = 0;
3748
+ return 0;
3749
+ }
3750
+ case 21520: {
3751
+ if (!stream.tty) return -59;
3752
+ return -28; // not supported
3753
+ }
3754
+ case 21537:
3755
+ case 21531: {
3756
+ var argp = syscallGetVarargP();
3757
+ return FS.ioctl(stream, op, argp);
3758
+ }
3759
+ case 21523: {
3760
+ // TODO: in theory we should write to the winsize struct that gets
3761
+ // passed in, but for now musl doesn't read anything on it
3762
+ if (!stream.tty) return -59;
3763
+ if (stream.tty.ops.ioctl_tiocgwinsz) {
3764
+ var winsize = stream.tty.ops.ioctl_tiocgwinsz(stream.tty);
3765
+ var argp = syscallGetVarargP();
3766
+ HEAP16[((argp)>>1)] = winsize[0];
3767
+ HEAP16[(((argp)+(2))>>1)] = winsize[1];
3768
+ }
3769
+ return 0;
3770
+ }
3771
+ case 21524: {
3772
+ // TODO: technically, this ioctl call should change the window size.
3773
+ // but, since emscripten doesn't have any concept of a terminal window
3774
+ // yet, we'll just silently throw it away as we do TIOCGWINSZ
3775
+ if (!stream.tty) return -59;
3776
+ return 0;
3777
+ }
3778
+ case 21515: {
3779
+ if (!stream.tty) return -59;
3780
+ return 0;
3781
+ }
3782
+ default: return -28; // not supported
3783
+ }
3784
+ } catch (e) {
3785
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3786
+ return -e.errno;
3787
+ }
3788
+ }
3789
+
3790
+
3791
+ function ___syscall_openat(dirfd, path, flags, varargs) {
3792
+ SYSCALLS.varargs = varargs;
3793
+ try {
3794
+
3795
+ path = SYSCALLS.getStr(path);
3796
+ path = SYSCALLS.calculateAt(dirfd, path);
3797
+ var mode = varargs ? syscallGetVarargI() : 0;
3798
+ return FS.open(path, flags, mode).fd;
3799
+ } catch (e) {
3800
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3801
+ return -e.errno;
3802
+ }
3803
+ }
3804
+
3805
+ var __abort_js = () =>
3806
+ abort('native code called abort()');
3807
+
3808
+ var runtimeKeepaliveCounter = 0;
3809
+ var __emscripten_runtime_keepalive_clear = () => {
3810
+ noExitRuntime = false;
3811
+ runtimeKeepaliveCounter = 0;
3812
+ };
3813
+
3814
+ var __emscripten_system = (command) => {
3815
+ if (ENVIRONMENT_IS_NODE) {
3816
+ if (!command) return 1; // shell is available
3817
+
3818
+ var cmdstr = UTF8ToString(command);
3819
+ if (!cmdstr.length) return 0; // this is what glibc seems to do (shell works test?)
3820
+
3821
+ var cp = require('node:child_process');
3822
+ var ret = cp.spawnSync(cmdstr, [], {shell:true, stdio:'inherit'});
3823
+
3824
+ var _W_EXITCODE = (ret, sig) => ((ret) << 8 | (sig));
3825
+
3826
+ // this really only can happen if process is killed by signal
3827
+ if (ret.status === null) {
3828
+ // sadly node doesn't expose such function
3829
+ var signalToNumber = (sig) => {
3830
+ // implement only the most common ones, and fallback to SIGINT
3831
+ switch (sig) {
3832
+ case 'SIGHUP': return 1;
3833
+ case 'SIGQUIT': return 3;
3834
+ case 'SIGFPE': return 8;
3835
+ case 'SIGKILL': return 9;
3836
+ case 'SIGALRM': return 14;
3837
+ case 'SIGTERM': return 15;
3838
+ default: return 2;
3839
+ }
3840
+ }
3841
+ return _W_EXITCODE(0, signalToNumber(ret.signal));
3842
+ }
3843
+
3844
+ return _W_EXITCODE(ret.status, 0);
3845
+ }
3846
+ // int system(const char *command);
3847
+ // http://pubs.opengroup.org/onlinepubs/000095399/functions/system.html
3848
+ // Can't call external programs.
3849
+ if (!command) return 0; // no shell available
3850
+ return -52;
3851
+ };
3852
+
3853
+ var stringToUTF8 = (str, outPtr, maxBytesToWrite) => {
3854
+ assert(typeof maxBytesToWrite == 'number', 'stringToUTF8(str, outPtr, maxBytesToWrite) is missing the third parameter that specifies the length of the output buffer!');
3855
+ return stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);
3856
+ };
3857
+
3858
+ var __tzset_js = (timezone, daylight, std_name, dst_name) => {
3859
+ // TODO: Use (malleable) environment variables instead of system settings.
3860
+ var currentYear = new Date().getFullYear();
3861
+ var winter = new Date(currentYear, 0, 1);
3862
+ var summer = new Date(currentYear, 6, 1);
3863
+ var winterOffset = winter.getTimezoneOffset();
3864
+ var summerOffset = summer.getTimezoneOffset();
3865
+
3866
+ // Local standard timezone offset. Local standard time is not adjusted for
3867
+ // daylight savings. This code uses the fact that getTimezoneOffset returns
3868
+ // a greater value during Standard Time versus Daylight Saving Time (DST).
3869
+ // Thus it determines the expected output during Standard Time, and it
3870
+ // compares whether the output of the given date the same (Standard) or less
3871
+ // (DST).
3872
+ var stdTimezoneOffset = Math.max(winterOffset, summerOffset);
3873
+
3874
+ // timezone is specified as seconds west of UTC ("The external variable
3875
+ // `timezone` shall be set to the difference, in seconds, between
3876
+ // Coordinated Universal Time (UTC) and local standard time."), the same
3877
+ // as returned by stdTimezoneOffset.
3878
+ // See http://pubs.opengroup.org/onlinepubs/009695399/functions/tzset.html
3879
+ HEAPU32[((timezone)>>2)] = stdTimezoneOffset * 60;
3880
+
3881
+ HEAP32[((daylight)>>2)] = Number(winterOffset != summerOffset);
3882
+
3883
+ var extractZone = (timezoneOffset) => {
3884
+ // Why inverse sign?
3885
+ // Read here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
3886
+ var sign = timezoneOffset >= 0 ? "-" : "+";
3887
+
3888
+ var absOffset = Math.abs(timezoneOffset)
3889
+ var hours = String(Math.floor(absOffset / 60)).padStart(2, "0");
3890
+ var minutes = String(absOffset % 60).padStart(2, "0");
3891
+
3892
+ return `UTC${sign}${hours}${minutes}`;
3893
+ }
3894
+
3895
+ var winterName = extractZone(winterOffset);
3896
+ var summerName = extractZone(summerOffset);
3897
+ assert(winterName);
3898
+ assert(summerName);
3899
+ assert(lengthBytesUTF8(winterName) <= 16, `timezone name truncated to fit in TZNAME_MAX (${winterName})`);
3900
+ assert(lengthBytesUTF8(summerName) <= 16, `timezone name truncated to fit in TZNAME_MAX (${summerName})`);
3901
+ if (summerOffset < winterOffset) {
3902
+ // Northern hemisphere
3903
+ stringToUTF8(winterName, std_name, 17);
3904
+ stringToUTF8(summerName, dst_name, 17);
3905
+ } else {
3906
+ stringToUTF8(winterName, dst_name, 17);
3907
+ stringToUTF8(summerName, std_name, 17);
3908
+ }
3909
+ };
3910
+
3911
+ var _emscripten_get_now = () => performance.now();
3912
+
3913
+ var _emscripten_date_now = () => Date.now();
3914
+
3915
+ var nowIsMonotonic = 1;
3916
+
3917
+ var checkWasiClock = (clock_id) => clock_id >= 0 && clock_id <= 3;
3918
+
3919
+ var INT53_MAX = 9007199254740992;
3920
+
3921
+ var INT53_MIN = -9007199254740992;
3922
+ var bigintToI53Checked = (num) => (num < INT53_MIN || num > INT53_MAX) ? NaN : Number(num);
3923
+ function _clock_time_get(clk_id, ignored_precision, ptime) {
3924
+ ignored_precision = bigintToI53Checked(ignored_precision);
3925
+
3926
+
3927
+ if (!checkWasiClock(clk_id)) {
3928
+ return 28;
3929
+ }
3930
+ var now;
3931
+ // all wasi clocks but realtime are monotonic
3932
+ if (clk_id === 0) {
3933
+ now = _emscripten_date_now();
3934
+ } else if (nowIsMonotonic) {
3935
+ now = _emscripten_get_now();
3936
+ } else {
3937
+ return 52;
3938
+ }
3939
+ // "now" is in ms, and wasi times are in ns.
3940
+ var nsec = Math.round(now * 1000 * 1000);
3941
+ HEAP64[((ptime)>>3)] = BigInt(nsec);
3942
+ return 0;
3943
+ ;
3944
+ }
3945
+
3946
+
3947
+ var getHeapMax = () =>
3948
+ // Stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate
3949
+ // full 4GB Wasm memories, the size will wrap back to 0 bytes in Wasm side
3950
+ // for any code that deals with heap sizes, which would require special
3951
+ // casing all heap size related code to treat 0 specially.
3952
+ 2147483648;
3953
+ var _emscripten_get_heap_max = () => getHeapMax();
3954
+
3955
+
3956
+ var alignMemory = (size, alignment) => {
3957
+ assert(alignment, "alignment argument is required");
3958
+ return Math.ceil(size / alignment) * alignment;
3959
+ };
3960
+
3961
+ var growMemory = (size) => {
3962
+ var oldHeapSize = wasmMemory.buffer.byteLength;
3963
+ var pages = ((size - oldHeapSize + 65535) / 65536) | 0;
3964
+ try {
3965
+ // round size grow request up to wasm page size (fixed 64KB per spec)
3966
+ wasmMemory.grow(pages); // .grow() takes a delta compared to the previous size
3967
+ updateMemoryViews();
3968
+ return 1 /*success*/;
3969
+ } catch(e) {
3970
+ err(`growMemory: Attempted to grow heap from ${oldHeapSize} bytes to ${size} bytes, but got error: ${e}`);
3971
+ }
3972
+ // implicit 0 return to save code size (caller will cast "undefined" into 0
3973
+ // anyhow)
3974
+ };
3975
+ var _emscripten_resize_heap = (requestedSize) => {
3976
+ var oldSize = HEAPU8.length;
3977
+ // With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned.
3978
+ requestedSize >>>= 0;
3979
+ // With multithreaded builds, races can happen (another thread might increase the size
3980
+ // in between), so return a failure, and let the caller retry.
3981
+ assert(requestedSize > oldSize);
3982
+
3983
+ // Memory resize rules:
3984
+ // 1. Always increase heap size to at least the requested size, rounded up
3985
+ // to next page multiple.
3986
+ // 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap
3987
+ // geometrically: increase the heap size according to
3988
+ // MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%), At most
3989
+ // overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB).
3990
+ // 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap
3991
+ // linearly: increase the heap size by at least
3992
+ // MEMORY_GROWTH_LINEAR_STEP bytes.
3993
+ // 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by
3994
+ // MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest
3995
+ // 4. If we were unable to allocate as much memory, it may be due to
3996
+ // over-eager decision to excessively reserve due to (3) above.
3997
+ // Hence if an allocation fails, cut down on the amount of excess
3998
+ // growth, in an attempt to succeed to perform a smaller allocation.
3999
+
4000
+ // A limit is set for how much we can grow. We should not exceed that
4001
+ // (the wasm binary specifies it, so if we tried, we'd fail anyhow).
4002
+ var maxHeapSize = getHeapMax();
4003
+ if (requestedSize > maxHeapSize) {
4004
+ err(`Cannot enlarge memory, requested ${requestedSize} bytes, but the limit is ${maxHeapSize} bytes!`);
4005
+ return false;
4006
+ }
4007
+
4008
+ // Loop through potential heap size increases. If we attempt a too eager
4009
+ // reservation that fails, cut down on the attempted size and reserve a
4010
+ // smaller bump instead. (max 3 times, chosen somewhat arbitrarily)
4011
+ for (var cutDown = 1; cutDown <= 4; cutDown *= 2) {
4012
+ var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); // ensure geometric growth
4013
+ // but limit overreserving (default to capping at +96MB overgrowth at most)
4014
+ overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296 );
4015
+
4016
+ var newSize = Math.min(maxHeapSize, alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536));
4017
+
4018
+ var replacement = growMemory(newSize);
4019
+ if (replacement) {
4020
+
4021
+ return true;
4022
+ }
4023
+ }
4024
+ err(`Failed to grow the heap from ${oldSize} bytes to ${newSize} bytes, not enough memory!`);
4025
+ return false;
4026
+ };
4027
+
4028
+ var ENV = {
4029
+ };
4030
+
4031
+ var getExecutableName = () => thisProgram || './this.program';
4032
+ var getEnvStrings = () => {
4033
+ if (!getEnvStrings.strings) {
4034
+ // Default values.
4035
+ // Browser language detection #8751
4036
+ var lang = (globalThis.navigator?.language ?? 'C').replace('-', '_') + '.UTF-8';
4037
+ var env = {
4038
+ 'USER': 'web_user',
4039
+ 'LOGNAME': 'web_user',
4040
+ 'PATH': '/',
4041
+ 'PWD': '/',
4042
+ 'HOME': '/home/web_user',
4043
+ 'LANG': lang,
4044
+ '_': getExecutableName()
4045
+ };
4046
+ // Apply the user-provided values, if any.
4047
+ for (var x in ENV) {
4048
+ // x is a key in ENV; if ENV[x] is undefined, that means it was
4049
+ // explicitly set to be so. We allow user code to do that to
4050
+ // force variables with default values to remain unset.
4051
+ if (ENV[x] === undefined) delete env[x];
4052
+ else env[x] = ENV[x];
4053
+ }
4054
+ var strings = [];
4055
+ for (var x in env) {
4056
+ strings.push(`${x}=${env[x]}`);
4057
+ }
4058
+ getEnvStrings.strings = strings;
4059
+ }
4060
+ return getEnvStrings.strings;
4061
+ };
4062
+
4063
+ var _environ_get = (__environ, environ_buf) => {
4064
+ var bufSize = 0;
4065
+ var envp = 0;
4066
+ for (var string of getEnvStrings()) {
4067
+ var ptr = environ_buf + bufSize;
4068
+ HEAPU32[(((__environ)+(envp))>>2)] = ptr;
4069
+ bufSize += stringToUTF8(string, ptr, Infinity) + 1;
4070
+ envp += 4;
4071
+ }
4072
+ return 0;
4073
+ };
4074
+
4075
+
4076
+ var _environ_sizes_get = (penviron_count, penviron_buf_size) => {
4077
+ var strings = getEnvStrings();
4078
+ HEAPU32[((penviron_count)>>2)] = strings.length;
4079
+ var bufSize = 0;
4080
+ for (var string of strings) {
4081
+ bufSize += lengthBytesUTF8(string) + 1;
4082
+ }
4083
+ HEAPU32[((penviron_buf_size)>>2)] = bufSize;
4084
+ return 0;
4085
+ };
4086
+
4087
+
4088
+ var keepRuntimeAlive = () => noExitRuntime || runtimeKeepaliveCounter > 0;
4089
+ var _proc_exit = (code) => {
4090
+ EXITSTATUS = code;
4091
+ if (!keepRuntimeAlive()) {
4092
+ Module['onExit']?.(code);
4093
+ ABORT = true;
4094
+ }
4095
+ quit_(code, new ExitStatus(code));
4096
+ };
4097
+
4098
+
4099
+ /** @param {boolean|number=} implicit */
4100
+ var exitJS = (status, implicit) => {
4101
+ EXITSTATUS = status;
4102
+
4103
+ checkUnflushedContent();
4104
+
4105
+ // if exit() was called explicitly, warn the user if the runtime isn't actually being shut down
4106
+ if (keepRuntimeAlive() && !implicit) {
4107
+ var msg = `program exited (with status: ${status}), but keepRuntimeAlive() is set (counter=${runtimeKeepaliveCounter}) due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)`;
4108
+ readyPromiseReject?.(msg);
4109
+ err(msg);
4110
+ }
4111
+
4112
+ _proc_exit(status);
4113
+ };
4114
+ var _exit = exitJS;
4115
+
4116
+ function _fd_close(fd) {
4117
+ try {
4118
+
4119
+ var stream = SYSCALLS.getStreamFromFD(fd);
4120
+ FS.close(stream);
4121
+ return 0;
4122
+ } catch (e) {
4123
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
4124
+ return e.errno;
4125
+ }
4126
+ }
4127
+
4128
+ /** @param {number=} offset */
4129
+ var doReadv = (stream, iov, iovcnt, offset) => {
4130
+ var ret = 0;
4131
+ for (var i = 0; i < iovcnt; i++) {
4132
+ var ptr = HEAPU32[((iov)>>2)];
4133
+ var len = HEAPU32[(((iov)+(4))>>2)];
4134
+ iov += 8;
4135
+ var curr = FS.read(stream, HEAP8, ptr, len, offset);
4136
+ if (curr < 0) return -1;
4137
+ ret += curr;
4138
+ if (curr < len) break; // nothing more to read
4139
+ if (typeof offset != 'undefined') {
4140
+ offset += curr;
4141
+ }
4142
+ }
4143
+ return ret;
4144
+ };
4145
+
4146
+ function _fd_read(fd, iov, iovcnt, pnum) {
4147
+ try {
4148
+
4149
+ var stream = SYSCALLS.getStreamFromFD(fd);
4150
+ var num = doReadv(stream, iov, iovcnt);
4151
+ HEAPU32[((pnum)>>2)] = num;
4152
+ return 0;
4153
+ } catch (e) {
4154
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
4155
+ return e.errno;
4156
+ }
4157
+ }
4158
+
4159
+
4160
+ function _fd_seek(fd, offset, whence, newOffset) {
4161
+ offset = bigintToI53Checked(offset);
4162
+
4163
+
4164
+ try {
4165
+
4166
+ if (isNaN(offset)) return 61;
4167
+ var stream = SYSCALLS.getStreamFromFD(fd);
4168
+ FS.llseek(stream, offset, whence);
4169
+ HEAP64[((newOffset)>>3)] = BigInt(stream.position);
4170
+ if (stream.getdents && offset === 0 && whence === 0) stream.getdents = null; // reset readdir state
4171
+ return 0;
4172
+ } catch (e) {
4173
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
4174
+ return e.errno;
4175
+ }
4176
+ ;
4177
+ }
4178
+
4179
+ /** @param {number=} offset */
4180
+ var doWritev = (stream, iov, iovcnt, offset) => {
4181
+ var ret = 0;
4182
+ for (var i = 0; i < iovcnt; i++) {
4183
+ var ptr = HEAPU32[((iov)>>2)];
4184
+ var len = HEAPU32[(((iov)+(4))>>2)];
4185
+ iov += 8;
4186
+ var curr = FS.write(stream, HEAP8, ptr, len, offset);
4187
+ if (curr < 0) return -1;
4188
+ ret += curr;
4189
+ if (curr < len) {
4190
+ // No more space to write.
4191
+ break;
4192
+ }
4193
+ if (typeof offset != 'undefined') {
4194
+ offset += curr;
4195
+ }
4196
+ }
4197
+ return ret;
4198
+ };
4199
+
4200
+ function _fd_write(fd, iov, iovcnt, pnum) {
4201
+ try {
4202
+
4203
+ var stream = SYSCALLS.getStreamFromFD(fd);
4204
+ var num = doWritev(stream, iov, iovcnt);
4205
+ HEAPU32[((pnum)>>2)] = num;
4206
+ return 0;
4207
+ } catch (e) {
4208
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
4209
+ return e.errno;
4210
+ }
4211
+ }
4212
+
4213
+
4214
+
4215
+ var handleException = (e) => {
4216
+ // Certain exception types we do not treat as errors since they are used for
4217
+ // internal control flow.
4218
+ // 1. ExitStatus, which is thrown by exit()
4219
+ // 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others
4220
+ // that wish to return to JS event loop.
4221
+ if (e instanceof ExitStatus || e == 'unwind') {
4222
+ return EXITSTATUS;
4223
+ }
4224
+ checkStackCookie();
4225
+ if (e instanceof WebAssembly.RuntimeError) {
4226
+ if (_emscripten_stack_get_current() <= 0) {
4227
+ err('Stack overflow detected. You can try increasing -sSTACK_SIZE (currently set to 65536)');
4228
+ }
4229
+ }
4230
+ quit_(1, e);
4231
+ };
4232
+
4233
+
4234
+
4235
+ var stackAlloc = (sz) => __emscripten_stack_alloc(sz);
4236
+ var stringToUTF8OnStack = (str) => {
4237
+ var size = lengthBytesUTF8(str) + 1;
4238
+ var ret = stackAlloc(size);
4239
+ stringToUTF8(str, ret, size);
4240
+ return ret;
4241
+ };
4242
+
4243
+
4244
+ FS.createPreloadedFile = FS_createPreloadedFile;
4245
+ FS.preloadFile = FS_preloadFile;
4246
+ FS.staticInit();;
4247
+ // End JS library code
4248
+
4249
+ // include: postlibrary.js
4250
+ // This file is included after the automatically-generated JS library code
4251
+ // but before the wasm module is created.
4252
+
4253
+ {
4254
+
4255
+ // Begin ATMODULES hooks
4256
+ if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime'];
4257
+ if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins'];
4258
+ if (Module['print']) out = Module['print'];
4259
+ if (Module['printErr']) err = Module['printErr'];
4260
+ if (Module['wasmBinary']) wasmBinary = Module['wasmBinary'];
4261
+ // End ATMODULES hooks
4262
+
4263
+ checkIncomingModuleAPI();
4264
+
4265
+ if (Module['arguments']) arguments_ = Module['arguments'];
4266
+ if (Module['thisProgram']) thisProgram = Module['thisProgram'];
4267
+
4268
+ // Assertions on removed incoming Module JS APIs.
4269
+ assert(typeof Module['memoryInitializerPrefixURL'] == 'undefined', 'Module.memoryInitializerPrefixURL option was removed, use Module.locateFile instead');
4270
+ assert(typeof Module['pthreadMainPrefixURL'] == 'undefined', 'Module.pthreadMainPrefixURL option was removed, use Module.locateFile instead');
4271
+ assert(typeof Module['cdInitializerPrefixURL'] == 'undefined', 'Module.cdInitializerPrefixURL option was removed, use Module.locateFile instead');
4272
+ assert(typeof Module['filePackagePrefixURL'] == 'undefined', 'Module.filePackagePrefixURL option was removed, use Module.locateFile instead');
4273
+ assert(typeof Module['read'] == 'undefined', 'Module.read option was removed');
4274
+ assert(typeof Module['readAsync'] == 'undefined', 'Module.readAsync option was removed (modify readAsync in JS)');
4275
+ assert(typeof Module['readBinary'] == 'undefined', 'Module.readBinary option was removed (modify readBinary in JS)');
4276
+ assert(typeof Module['setWindowTitle'] == 'undefined', 'Module.setWindowTitle option was removed (modify emscripten_set_window_title in JS)');
4277
+ assert(typeof Module['TOTAL_MEMORY'] == 'undefined', 'Module.TOTAL_MEMORY has been renamed Module.INITIAL_MEMORY');
4278
+ assert(typeof Module['ENVIRONMENT'] == 'undefined', 'Module.ENVIRONMENT has been deprecated. To force the environment, use the ENVIRONMENT compile-time option (for example, -sENVIRONMENT=web or -sENVIRONMENT=node)');
4279
+ assert(typeof Module['STACK_SIZE'] == 'undefined', 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
4280
+ // If memory is defined in wasm, the user can't provide it, or set INITIAL_MEMORY
4281
+ assert(typeof Module['wasmMemory'] == 'undefined', 'Use of `wasmMemory` detected. Use -sIMPORTED_MEMORY to define wasmMemory externally');
4282
+ assert(typeof Module['INITIAL_MEMORY'] == 'undefined', 'Detected runtime INITIAL_MEMORY setting. Use -sIMPORTED_MEMORY to define wasmMemory dynamically');
4283
+
4284
+ if (Module['preInit']) {
4285
+ if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
4286
+ while (Module['preInit'].length > 0) {
4287
+ Module['preInit'].shift()();
4288
+ }
4289
+ }
4290
+ consumedModuleProp('preInit');
4291
+ }
4292
+
4293
+ // Begin runtime exports
4294
+ Module['callMain'] = callMain;
4295
+ Module['FS'] = FS;
4296
+ var missingLibrarySymbols = [
4297
+ 'writeI53ToI64',
4298
+ 'writeI53ToI64Clamped',
4299
+ 'writeI53ToI64Signaling',
4300
+ 'writeI53ToU64Clamped',
4301
+ 'writeI53ToU64Signaling',
4302
+ 'readI53FromI64',
4303
+ 'readI53FromU64',
4304
+ 'convertI32PairToI53',
4305
+ 'convertI32PairToI53Checked',
4306
+ 'convertU32PairToI53',
4307
+ 'getTempRet0',
4308
+ 'setTempRet0',
4309
+ 'createNamedFunction',
4310
+ 'zeroMemory',
4311
+ 'withStackSave',
4312
+ 'inetPton4',
4313
+ 'inetNtop4',
4314
+ 'inetPton6',
4315
+ 'inetNtop6',
4316
+ 'readSockaddr',
4317
+ 'writeSockaddr',
4318
+ 'readEmAsmArgs',
4319
+ 'jstoi_q',
4320
+ 'autoResumeAudioContext',
4321
+ 'getDynCaller',
4322
+ 'dynCall',
4323
+ 'runtimeKeepalivePush',
4324
+ 'runtimeKeepalivePop',
4325
+ 'callUserCallback',
4326
+ 'maybeExit',
4327
+ 'asmjsMangle',
4328
+ 'HandleAllocator',
4329
+ 'addOnInit',
4330
+ 'addOnPostCtor',
4331
+ 'addOnPreMain',
4332
+ 'addOnExit',
4333
+ 'STACK_SIZE',
4334
+ 'STACK_ALIGN',
4335
+ 'POINTER_SIZE',
4336
+ 'ASSERTIONS',
4337
+ 'ccall',
4338
+ 'cwrap',
4339
+ 'convertJsFunctionToWasm',
4340
+ 'getEmptyTableSlot',
4341
+ 'updateTableMap',
4342
+ 'getFunctionAddress',
4343
+ 'addFunction',
4344
+ 'removeFunction',
4345
+ 'intArrayToString',
4346
+ 'AsciiToString',
4347
+ 'stringToAscii',
4348
+ 'UTF16ToString',
4349
+ 'stringToUTF16',
4350
+ 'lengthBytesUTF16',
4351
+ 'UTF32ToString',
4352
+ 'stringToUTF32',
4353
+ 'lengthBytesUTF32',
4354
+ 'stringToNewUTF8',
4355
+ 'writeArrayToMemory',
4356
+ 'registerKeyEventCallback',
4357
+ 'maybeCStringToJsString',
4358
+ 'findEventTarget',
4359
+ 'getBoundingClientRect',
4360
+ 'fillMouseEventData',
4361
+ 'registerMouseEventCallback',
4362
+ 'registerWheelEventCallback',
4363
+ 'registerUiEventCallback',
4364
+ 'registerFocusEventCallback',
4365
+ 'fillDeviceOrientationEventData',
4366
+ 'registerDeviceOrientationEventCallback',
4367
+ 'fillDeviceMotionEventData',
4368
+ 'registerDeviceMotionEventCallback',
4369
+ 'screenOrientation',
4370
+ 'fillOrientationChangeEventData',
4371
+ 'registerOrientationChangeEventCallback',
4372
+ 'fillFullscreenChangeEventData',
4373
+ 'registerFullscreenChangeEventCallback',
4374
+ 'JSEvents_requestFullscreen',
4375
+ 'JSEvents_resizeCanvasForFullscreen',
4376
+ 'registerRestoreOldStyle',
4377
+ 'hideEverythingExceptGivenElement',
4378
+ 'restoreHiddenElements',
4379
+ 'setLetterbox',
4380
+ 'softFullscreenResizeWebGLRenderTarget',
4381
+ 'doRequestFullscreen',
4382
+ 'fillPointerlockChangeEventData',
4383
+ 'registerPointerlockChangeEventCallback',
4384
+ 'registerPointerlockErrorEventCallback',
4385
+ 'requestPointerLock',
4386
+ 'fillVisibilityChangeEventData',
4387
+ 'registerVisibilityChangeEventCallback',
4388
+ 'registerTouchEventCallback',
4389
+ 'fillGamepadEventData',
4390
+ 'registerGamepadEventCallback',
4391
+ 'registerBeforeUnloadEventCallback',
4392
+ 'fillBatteryEventData',
4393
+ 'registerBatteryEventCallback',
4394
+ 'setCanvasElementSize',
4395
+ 'getCanvasElementSize',
4396
+ 'jsStackTrace',
4397
+ 'getCallstack',
4398
+ 'convertPCtoSourceLocation',
4399
+ 'wasiRightsToMuslOFlags',
4400
+ 'wasiOFlagsToMuslOFlags',
4401
+ 'safeSetTimeout',
4402
+ 'setImmediateWrapped',
4403
+ 'safeRequestAnimationFrame',
4404
+ 'clearImmediateWrapped',
4405
+ 'registerPostMainLoop',
4406
+ 'registerPreMainLoop',
4407
+ 'getPromise',
4408
+ 'makePromise',
4409
+ 'idsToPromises',
4410
+ 'makePromiseCallback',
4411
+ 'findMatchingCatch',
4412
+ 'Browser_asyncPrepareDataCounter',
4413
+ 'isLeapYear',
4414
+ 'ydayFromDate',
4415
+ 'arraySum',
4416
+ 'addDays',
4417
+ 'getSocketFromFD',
4418
+ 'getSocketAddress',
4419
+ 'FS_mkdirTree',
4420
+ '_setNetworkCallback',
4421
+ 'heapObjectForWebGLType',
4422
+ 'toTypedArrayIndex',
4423
+ 'webgl_enable_ANGLE_instanced_arrays',
4424
+ 'webgl_enable_OES_vertex_array_object',
4425
+ 'webgl_enable_WEBGL_draw_buffers',
4426
+ 'webgl_enable_WEBGL_multi_draw',
4427
+ 'webgl_enable_EXT_polygon_offset_clamp',
4428
+ 'webgl_enable_EXT_clip_control',
4429
+ 'webgl_enable_WEBGL_polygon_mode',
4430
+ 'emscriptenWebGLGet',
4431
+ 'computeUnpackAlignedImageSize',
4432
+ 'colorChannelsInGlTextureFormat',
4433
+ 'emscriptenWebGLGetTexPixelData',
4434
+ 'emscriptenWebGLGetUniform',
4435
+ 'webglGetUniformLocation',
4436
+ 'webglPrepareUniformLocationsBeforeFirstUse',
4437
+ 'webglGetLeftBracePos',
4438
+ 'emscriptenWebGLGetVertexAttrib',
4439
+ '__glGetActiveAttribOrUniform',
4440
+ 'writeGLArray',
4441
+ 'registerWebGlEventCallback',
4442
+ 'runAndAbortIfError',
4443
+ 'ALLOC_NORMAL',
4444
+ 'ALLOC_STACK',
4445
+ 'allocate',
4446
+ 'writeStringToMemory',
4447
+ 'writeAsciiToMemory',
4448
+ 'allocateUTF8',
4449
+ 'allocateUTF8OnStack',
4450
+ 'demangle',
4451
+ 'stackTrace',
4452
+ 'getNativeTypeSize',
4453
+ ];
4454
+ missingLibrarySymbols.forEach(missingLibrarySymbol)
4455
+
4456
+ var unexportedSymbols = [
4457
+ 'run',
4458
+ 'out',
4459
+ 'err',
4460
+ 'abort',
4461
+ 'wasmExports',
4462
+ 'writeStackCookie',
4463
+ 'checkStackCookie',
4464
+ 'INT53_MAX',
4465
+ 'INT53_MIN',
4466
+ 'bigintToI53Checked',
4467
+ 'HEAP8',
4468
+ 'HEAPU8',
4469
+ 'HEAP16',
4470
+ 'HEAPU16',
4471
+ 'HEAP32',
4472
+ 'HEAPU32',
4473
+ 'HEAPF32',
4474
+ 'HEAPF64',
4475
+ 'HEAP64',
4476
+ 'HEAPU64',
4477
+ 'stackSave',
4478
+ 'stackRestore',
4479
+ 'stackAlloc',
4480
+ 'ptrToString',
4481
+ 'exitJS',
4482
+ 'getHeapMax',
4483
+ 'growMemory',
4484
+ 'ENV',
4485
+ 'ERRNO_CODES',
4486
+ 'strError',
4487
+ 'DNS',
4488
+ 'Protocols',
4489
+ 'Sockets',
4490
+ 'timers',
4491
+ 'warnOnce',
4492
+ 'readEmAsmArgsArray',
4493
+ 'getExecutableName',
4494
+ 'handleException',
4495
+ 'keepRuntimeAlive',
4496
+ 'asyncLoad',
4497
+ 'alignMemory',
4498
+ 'mmapAlloc',
4499
+ 'wasmTable',
4500
+ 'wasmMemory',
4501
+ 'getUniqueRunDependency',
4502
+ 'noExitRuntime',
4503
+ 'addRunDependency',
4504
+ 'removeRunDependency',
4505
+ 'addOnPreRun',
4506
+ 'addOnPostRun',
4507
+ 'freeTableIndexes',
4508
+ 'functionsInTableMap',
4509
+ 'setValue',
4510
+ 'getValue',
4511
+ 'PATH',
4512
+ 'PATH_FS',
4513
+ 'UTF8Decoder',
4514
+ 'UTF8ArrayToString',
4515
+ 'UTF8ToString',
4516
+ 'stringToUTF8Array',
4517
+ 'stringToUTF8',
4518
+ 'lengthBytesUTF8',
4519
+ 'intArrayFromString',
4520
+ 'UTF16Decoder',
4521
+ 'stringToUTF8OnStack',
4522
+ 'JSEvents',
4523
+ 'specialHTMLTargets',
4524
+ 'findCanvasEventTarget',
4525
+ 'currentFullscreenStrategy',
4526
+ 'restoreOldWindowedStyle',
4527
+ 'UNWIND_CACHE',
4528
+ 'ExitStatus',
4529
+ 'getEnvStrings',
4530
+ 'checkWasiClock',
4531
+ 'doReadv',
4532
+ 'doWritev',
4533
+ 'initRandomFill',
4534
+ 'randomFill',
4535
+ 'emSetImmediate',
4536
+ 'emClearImmediate_deps',
4537
+ 'emClearImmediate',
4538
+ 'promiseMap',
4539
+ 'uncaughtExceptionCount',
4540
+ 'exceptionLast',
4541
+ 'exceptionCaught',
4542
+ 'ExceptionInfo',
4543
+ 'Browser',
4544
+ 'requestFullscreen',
4545
+ 'requestFullScreen',
4546
+ 'setCanvasSize',
4547
+ 'getUserMedia',
4548
+ 'createContext',
4549
+ 'getPreloadedImageData__data',
4550
+ 'wget',
4551
+ 'MONTH_DAYS_REGULAR',
4552
+ 'MONTH_DAYS_LEAP',
4553
+ 'MONTH_DAYS_REGULAR_CUMULATIVE',
4554
+ 'MONTH_DAYS_LEAP_CUMULATIVE',
4555
+ 'SYSCALLS',
4556
+ 'preloadPlugins',
4557
+ 'FS_createPreloadedFile',
4558
+ 'FS_preloadFile',
4559
+ 'FS_modeStringToFlags',
4560
+ 'FS_getMode',
4561
+ 'FS_fileDataToTypedArray',
4562
+ 'FS_stdin_getChar_buffer',
4563
+ 'FS_stdin_getChar',
4564
+ 'FS_unlink',
4565
+ 'FS_createPath',
4566
+ 'FS_createDevice',
4567
+ 'FS_readFile',
4568
+ 'FS_root',
4569
+ 'FS_mounts',
4570
+ 'FS_devices',
4571
+ 'FS_streams',
4572
+ 'FS_nextInode',
4573
+ 'FS_nameTable',
4574
+ 'FS_currentPath',
4575
+ 'FS_initialized',
4576
+ 'FS_ignorePermissions',
4577
+ 'FS_filesystems',
4578
+ 'FS_syncFSRequests',
4579
+ 'FS_lookupPath',
4580
+ 'FS_getPath',
4581
+ 'FS_hashName',
4582
+ 'FS_hashAddNode',
4583
+ 'FS_hashRemoveNode',
4584
+ 'FS_lookupNode',
4585
+ 'FS_createNode',
4586
+ 'FS_destroyNode',
4587
+ 'FS_isRoot',
4588
+ 'FS_isMountpoint',
4589
+ 'FS_isFile',
4590
+ 'FS_isDir',
4591
+ 'FS_isLink',
4592
+ 'FS_isChrdev',
4593
+ 'FS_isBlkdev',
4594
+ 'FS_isFIFO',
4595
+ 'FS_isSocket',
4596
+ 'FS_flagsToPermissionString',
4597
+ 'FS_nodePermissions',
4598
+ 'FS_mayLookup',
4599
+ 'FS_mayCreate',
4600
+ 'FS_mayDelete',
4601
+ 'FS_mayOpen',
4602
+ 'FS_checkOpExists',
4603
+ 'FS_nextfd',
4604
+ 'FS_getStreamChecked',
4605
+ 'FS_getStream',
4606
+ 'FS_createStream',
4607
+ 'FS_closeStream',
4608
+ 'FS_dupStream',
4609
+ 'FS_doSetAttr',
4610
+ 'FS_chrdev_stream_ops',
4611
+ 'FS_major',
4612
+ 'FS_minor',
4613
+ 'FS_makedev',
4614
+ 'FS_registerDevice',
4615
+ 'FS_getDevice',
4616
+ 'FS_getMounts',
4617
+ 'FS_syncfs',
4618
+ 'FS_mount',
4619
+ 'FS_unmount',
4620
+ 'FS_lookup',
4621
+ 'FS_mknod',
4622
+ 'FS_statfs',
4623
+ 'FS_statfsStream',
4624
+ 'FS_statfsNode',
4625
+ 'FS_create',
4626
+ 'FS_mkdir',
4627
+ 'FS_mkdev',
4628
+ 'FS_symlink',
4629
+ 'FS_rename',
4630
+ 'FS_rmdir',
4631
+ 'FS_readdir',
4632
+ 'FS_readlink',
4633
+ 'FS_stat',
4634
+ 'FS_fstat',
4635
+ 'FS_lstat',
4636
+ 'FS_doChmod',
4637
+ 'FS_chmod',
4638
+ 'FS_lchmod',
4639
+ 'FS_fchmod',
4640
+ 'FS_doChown',
4641
+ 'FS_chown',
4642
+ 'FS_lchown',
4643
+ 'FS_fchown',
4644
+ 'FS_doTruncate',
4645
+ 'FS_truncate',
4646
+ 'FS_ftruncate',
4647
+ 'FS_utime',
4648
+ 'FS_open',
4649
+ 'FS_close',
4650
+ 'FS_isClosed',
4651
+ 'FS_llseek',
4652
+ 'FS_read',
4653
+ 'FS_write',
4654
+ 'FS_mmap',
4655
+ 'FS_msync',
4656
+ 'FS_ioctl',
4657
+ 'FS_writeFile',
4658
+ 'FS_cwd',
4659
+ 'FS_chdir',
4660
+ 'FS_createDefaultDirectories',
4661
+ 'FS_createDefaultDevices',
4662
+ 'FS_createSpecialDirectories',
4663
+ 'FS_createStandardStreams',
4664
+ 'FS_staticInit',
4665
+ 'FS_init',
4666
+ 'FS_quit',
4667
+ 'FS_findObject',
4668
+ 'FS_analyzePath',
4669
+ 'FS_createFile',
4670
+ 'FS_createDataFile',
4671
+ 'FS_forceLoadFile',
4672
+ 'FS_createLazyFile',
4673
+ 'MEMFS',
4674
+ 'TTY',
4675
+ 'PIPEFS',
4676
+ 'SOCKFS',
4677
+ 'tempFixedLengthArray',
4678
+ 'miniTempWebGLFloatBuffers',
4679
+ 'miniTempWebGLIntBuffers',
4680
+ 'GL',
4681
+ 'AL',
4682
+ 'GLUT',
4683
+ 'EGL',
4684
+ 'GLEW',
4685
+ 'IDBStore',
4686
+ 'SDL',
4687
+ 'SDL_gfx',
4688
+ 'print',
4689
+ 'printErr',
4690
+ 'jstoi_s',
4691
+ ];
4692
+ unexportedSymbols.forEach(unexportedRuntimeSymbol);
4693
+
4694
+ // End runtime exports
4695
+ // Begin JS library exports
4696
+ // End JS library exports
4697
+
4698
+ // end include: postlibrary.js
4699
+
4700
+ function checkIncomingModuleAPI() {
4701
+ ignoredModuleProp('fetchSettings');
4702
+ ignoredModuleProp('logReadFiles');
4703
+ ignoredModuleProp('loadSplitModule');
4704
+ ignoredModuleProp('onMalloc');
4705
+ ignoredModuleProp('onRealloc');
4706
+ ignoredModuleProp('onFree');
4707
+ ignoredModuleProp('onSbrkGrow');
4708
+ }
4709
+
4710
+ // Imports from the Wasm binary.
4711
+ var _main = Module['_main'] = makeInvalidEarlyAccess('_main');
4712
+ var _fflush = makeInvalidEarlyAccess('_fflush');
4713
+ var _emscripten_stack_get_end = makeInvalidEarlyAccess('_emscripten_stack_get_end');
4714
+ var _emscripten_stack_get_base = makeInvalidEarlyAccess('_emscripten_stack_get_base');
4715
+ var _strerror = makeInvalidEarlyAccess('_strerror');
4716
+ var _emscripten_stack_init = makeInvalidEarlyAccess('_emscripten_stack_init');
4717
+ var _emscripten_stack_get_free = makeInvalidEarlyAccess('_emscripten_stack_get_free');
4718
+ var __emscripten_stack_restore = makeInvalidEarlyAccess('__emscripten_stack_restore');
4719
+ var __emscripten_stack_alloc = makeInvalidEarlyAccess('__emscripten_stack_alloc');
4720
+ var _emscripten_stack_get_current = makeInvalidEarlyAccess('_emscripten_stack_get_current');
4721
+ var memory = makeInvalidEarlyAccess('memory');
4722
+ var __indirect_function_table = makeInvalidEarlyAccess('__indirect_function_table');
4723
+ var wasmMemory = makeInvalidEarlyAccess('wasmMemory');
4724
+ var wasmTable = makeInvalidEarlyAccess('wasmTable');
4725
+
4726
+ function assignWasmExports(wasmExports) {
4727
+ assert(typeof wasmExports['__main_argc_argv'] != 'undefined', 'missing Wasm export: __main_argc_argv');
4728
+ assert(typeof wasmExports['fflush'] != 'undefined', 'missing Wasm export: fflush');
4729
+ assert(typeof wasmExports['emscripten_stack_get_end'] != 'undefined', 'missing Wasm export: emscripten_stack_get_end');
4730
+ assert(typeof wasmExports['emscripten_stack_get_base'] != 'undefined', 'missing Wasm export: emscripten_stack_get_base');
4731
+ assert(typeof wasmExports['strerror'] != 'undefined', 'missing Wasm export: strerror');
4732
+ assert(typeof wasmExports['emscripten_stack_init'] != 'undefined', 'missing Wasm export: emscripten_stack_init');
4733
+ assert(typeof wasmExports['emscripten_stack_get_free'] != 'undefined', 'missing Wasm export: emscripten_stack_get_free');
4734
+ assert(typeof wasmExports['_emscripten_stack_restore'] != 'undefined', 'missing Wasm export: _emscripten_stack_restore');
4735
+ assert(typeof wasmExports['_emscripten_stack_alloc'] != 'undefined', 'missing Wasm export: _emscripten_stack_alloc');
4736
+ assert(typeof wasmExports['emscripten_stack_get_current'] != 'undefined', 'missing Wasm export: emscripten_stack_get_current');
4737
+ assert(typeof wasmExports['memory'] != 'undefined', 'missing Wasm export: memory');
4738
+ assert(typeof wasmExports['__indirect_function_table'] != 'undefined', 'missing Wasm export: __indirect_function_table');
4739
+ _main = Module['_main'] = createExportWrapper('__main_argc_argv', 2);
4740
+ _fflush = createExportWrapper('fflush', 1);
4741
+ _emscripten_stack_get_end = wasmExports['emscripten_stack_get_end'];
4742
+ _emscripten_stack_get_base = wasmExports['emscripten_stack_get_base'];
4743
+ _strerror = createExportWrapper('strerror', 1);
4744
+ _emscripten_stack_init = wasmExports['emscripten_stack_init'];
4745
+ _emscripten_stack_get_free = wasmExports['emscripten_stack_get_free'];
4746
+ __emscripten_stack_restore = wasmExports['_emscripten_stack_restore'];
4747
+ __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc'];
4748
+ _emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'];
4749
+ memory = wasmMemory = wasmExports['memory'];
4750
+ __indirect_function_table = wasmTable = wasmExports['__indirect_function_table'];
4751
+ }
4752
+
4753
+ var wasmImports = {
4754
+ /** @export */
4755
+ __call_sighandler: ___call_sighandler,
4756
+ /** @export */
4757
+ __cxa_throw: ___cxa_throw,
4758
+ /** @export */
4759
+ __syscall_fcntl64: ___syscall_fcntl64,
4760
+ /** @export */
4761
+ __syscall_ioctl: ___syscall_ioctl,
4762
+ /** @export */
4763
+ __syscall_openat: ___syscall_openat,
4764
+ /** @export */
4765
+ _abort_js: __abort_js,
4766
+ /** @export */
4767
+ _emscripten_runtime_keepalive_clear: __emscripten_runtime_keepalive_clear,
4768
+ /** @export */
4769
+ _emscripten_system: __emscripten_system,
4770
+ /** @export */
4771
+ _tzset_js: __tzset_js,
4772
+ /** @export */
4773
+ clock_time_get: _clock_time_get,
4774
+ /** @export */
4775
+ emscripten_date_now: _emscripten_date_now,
4776
+ /** @export */
4777
+ emscripten_get_heap_max: _emscripten_get_heap_max,
4778
+ /** @export */
4779
+ emscripten_resize_heap: _emscripten_resize_heap,
4780
+ /** @export */
4781
+ environ_get: _environ_get,
4782
+ /** @export */
4783
+ environ_sizes_get: _environ_sizes_get,
4784
+ /** @export */
4785
+ exit: _exit,
4786
+ /** @export */
4787
+ fd_close: _fd_close,
4788
+ /** @export */
4789
+ fd_read: _fd_read,
4790
+ /** @export */
4791
+ fd_seek: _fd_seek,
4792
+ /** @export */
4793
+ fd_write: _fd_write,
4794
+ /** @export */
4795
+ proc_exit: _proc_exit
4796
+ };
4797
+
4798
+
4799
+ // include: postamble.js
4800
+ // === Auto-generated postamble setup entry stuff ===
4801
+
4802
+ var calledRun;
4803
+
4804
+ function callMain(args = []) {
4805
+ assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on Module["onRuntimeInitialized"])');
4806
+ assert(typeof onPreRuns === 'undefined' || onPreRuns.length == 0, 'cannot call main when preRun functions remain to be called');
4807
+
4808
+ var entryFunction = _main;
4809
+
4810
+ args.unshift(thisProgram);
4811
+
4812
+ var argc = args.length;
4813
+ var argv = stackAlloc((argc + 1) * 4);
4814
+ var argv_ptr = argv;
4815
+ for (var arg of args) {
4816
+ HEAPU32[((argv_ptr)>>2)] = stringToUTF8OnStack(arg);
4817
+ argv_ptr += 4;
4818
+ }
4819
+ HEAPU32[((argv_ptr)>>2)] = 0;
4820
+
4821
+ try {
4822
+
4823
+ var ret = entryFunction(argc, argv);
4824
+
4825
+ // if we're not running an evented main loop, it's time to exit
4826
+ exitJS(ret, /* implicit = */ true);
4827
+ return ret;
4828
+ } catch (e) {
4829
+ return handleException(e);
4830
+ }
4831
+ }
4832
+
4833
+ function stackCheckInit() {
4834
+ // This is normally called automatically during __wasm_call_ctors but need to
4835
+ // get these values before even running any of the ctors so we call it redundantly
4836
+ // here.
4837
+ _emscripten_stack_init();
4838
+ // TODO(sbc): Move writeStackCookie to native to to avoid this.
4839
+ writeStackCookie();
4840
+ }
4841
+
4842
+ function run(args = arguments_) {
4843
+
4844
+ if (runDependencies > 0) {
4845
+ dependenciesFulfilled = run;
4846
+ return;
4847
+ }
4848
+
4849
+ stackCheckInit();
4850
+
4851
+ preRun();
4852
+
4853
+ // a preRun added a dependency, run will be called later
4854
+ if (runDependencies > 0) {
4855
+ dependenciesFulfilled = run;
4856
+ return;
4857
+ }
4858
+
4859
+ function doRun() {
4860
+ // run may have just been called through dependencies being fulfilled just in this very frame,
4861
+ // or while the async setStatus time below was happening
4862
+ assert(!calledRun);
4863
+ calledRun = true;
4864
+ Module['calledRun'] = true;
4865
+
4866
+ if (ABORT) return;
4867
+
4868
+ initRuntime();
4869
+
4870
+ preMain();
4871
+
4872
+ readyPromiseResolve?.(Module);
4873
+ Module['onRuntimeInitialized']?.();
4874
+ consumedModuleProp('onRuntimeInitialized');
4875
+
4876
+ var noInitialRun = Module['noInitialRun'] || true;
4877
+ if (!noInitialRun) callMain(args);
4878
+
4879
+ postRun();
4880
+ }
4881
+
4882
+ if (Module['setStatus']) {
4883
+ Module['setStatus']('Running...');
4884
+ setTimeout(() => {
4885
+ setTimeout(() => Module['setStatus'](''), 1);
4886
+ doRun();
4887
+ }, 1);
4888
+ } else
4889
+ {
4890
+ doRun();
4891
+ }
4892
+ checkStackCookie();
4893
+ }
4894
+
4895
+ function checkUnflushedContent() {
4896
+ // Compiler settings do not allow exiting the runtime, so flushing
4897
+ // the streams is not possible. but in ASSERTIONS mode we check
4898
+ // if there was something to flush, and if so tell the user they
4899
+ // should request that the runtime be exitable.
4900
+ // Normally we would not even include flush() at all, but in ASSERTIONS
4901
+ // builds we do so just for this check, and here we see if there is any
4902
+ // content to flush, that is, we check if there would have been
4903
+ // something a non-ASSERTIONS build would have not seen.
4904
+ // How we flush the streams depends on whether we are in SYSCALLS_REQUIRE_FILESYSTEM=0
4905
+ // mode (which has its own special function for this; otherwise, all
4906
+ // the code is inside libc)
4907
+ var oldOut = out;
4908
+ var oldErr = err;
4909
+ var has = false;
4910
+ out = err = (x) => {
4911
+ has = true;
4912
+ }
4913
+ try { // it doesn't matter if it fails
4914
+ _fflush(0);
4915
+ // also flush in the JS FS layer
4916
+ for (var name of ['stdout', 'stderr']) {
4917
+ var info = FS.analyzePath('/dev/' + name);
4918
+ if (!info) return;
4919
+ var stream = info.object;
4920
+ var rdev = stream.rdev;
4921
+ var tty = TTY.ttys[rdev];
4922
+ if (tty?.output?.length) {
4923
+ has = true;
4924
+ }
4925
+ }
4926
+ } catch(e) {}
4927
+ out = oldOut;
4928
+ err = oldErr;
4929
+ if (has) {
4930
+ warnOnce('stdio streams had content in them that was not flushed. you should set EXIT_RUNTIME to 1 (see the Emscripten FAQ), or make sure to emit a newline when you printf etc.');
4931
+ }
4932
+ }
4933
+
4934
+ var wasmExports;
4935
+
4936
+ // In modularize mode the generated code is within a factory function so we
4937
+ // can use await here (since it's not top-level-await).
4938
+ wasmExports = await (createWasm());
4939
+
4940
+ run();
4941
+
4942
+ // end include: postamble.js
4943
+
4944
+ // include: postamble_modularize.js
4945
+ // In MODULARIZE mode we wrap the generated code in a factory function
4946
+ // and return either the Module itself, or a promise of the module.
4947
+ //
4948
+ // We assign to the `moduleRtn` global here and configure closure to see
4949
+ // this as an extern so it won't get minified.
4950
+
4951
+ if (runtimeInitialized) {
4952
+ moduleRtn = Module;
4953
+ } else {
4954
+ // Set up the promise that indicates the Module is initialized
4955
+ moduleRtn = new Promise((resolve, reject) => {
4956
+ readyPromiseResolve = resolve;
4957
+ readyPromiseReject = reject;
4958
+ });
4959
+ }
4960
+
4961
+ // Assertion for attempting to access module properties on the incoming
4962
+ // moduleArg. In the past we used this object as the prototype of the module
4963
+ // and assigned properties to it, but now we return a distinct object. This
4964
+ // keeps the instance private until it is ready (i.e the promise has been
4965
+ // resolved).
4966
+ for (const prop of Object.keys(Module)) {
4967
+ if (!(prop in moduleArg)) {
4968
+ Object.defineProperty(moduleArg, prop, {
4969
+ configurable: true,
4970
+ get() {
4971
+ abort(`Access to module property ('${prop}') is no longer possible via the module constructor argument; Instead, use the result of the module constructor.`)
4972
+ }
4973
+ });
4974
+ }
4975
+ }
4976
+ // end include: postamble_modularize.js
4977
+
4978
+
4979
+
4980
+ return moduleRtn;
4981
+ }
4982
+
4983
+ // Export using a UMD style export, or ES6 exports if selected
4984
+ export default createNormaliz;
4985
+