klio 1.4.9 → 1.5.1

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,3934 @@
1
+ // include: shell.js
2
+ // include: minimum_runtime_check.js
3
+ // end include: minimum_runtime_check.js
4
+ // The Module object: Our interface to the outside world. We import
5
+ // and export values on it. There are various ways Module can be used:
6
+ // 1. Not defined. We create it here
7
+ // 2. A function parameter, function(moduleArg) => Promise<Module>
8
+ // 3. pre-run appended it, var Module = {}; ..generated code..
9
+ // 4. External script tag defines var Module.
10
+ // We need to check if Module already exists (e.g. case 3 above).
11
+ // Substitution will be replaced with actual code on later stage of the build,
12
+ // this way Closure Compiler will not mangle it (e.g. case 4. above).
13
+ // Note that if you want to run closure, and also to use Module
14
+ // after the generated code, you will need to define var Module = {};
15
+ // before the code. Then that object will be used in the code, and you
16
+ // can continue to use Module afterwards as well.
17
+ var Module = typeof Module != 'undefined' ? Module : {};
18
+
19
+ // Determine the runtime environment we are in. You can customize this by
20
+ // setting the ENVIRONMENT setting at compile time (see settings.js).
21
+
22
+ // Attempt to auto-detect the environment
23
+ var ENVIRONMENT_IS_WEB = !!globalThis.window;
24
+ var ENVIRONMENT_IS_WORKER = !!globalThis.WorkerGlobalScope;
25
+ // N.b. Electron.js environment is simultaneously a NODE-environment, but
26
+ // also a web environment.
27
+ var ENVIRONMENT_IS_NODE = globalThis.process?.versions?.node && globalThis.process?.type != 'renderer';
28
+ var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
29
+
30
+ // --pre-jses are emitted after the Module integration code, so that they can
31
+ // refer to Module (if they choose; they can also define Module)
32
+ // include: /tmp/tmpokcc7h34.js
33
+
34
+ if (!Module['expectedDataFileDownloads']) Module['expectedDataFileDownloads'] = 0;
35
+ Module['expectedDataFileDownloads']++;
36
+ (() => {
37
+ // Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
38
+ var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
39
+ var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
40
+ if (isPthread || isWasmWorker) return;
41
+ var isNode = globalThis.process && globalThis.process.versions && globalThis.process.versions.node && globalThis.process.type != 'renderer';
42
+ async function loadPackage(metadata) {
43
+
44
+ var PACKAGE_PATH = '';
45
+ if (typeof window === 'object') {
46
+ PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/');
47
+ } else if (typeof process === 'undefined' && typeof location !== 'undefined') {
48
+ // web worker
49
+ PACKAGE_PATH = encodeURIComponent(location.pathname.substring(0, location.pathname.lastIndexOf('/')) + '/');
50
+ }
51
+ var PACKAGE_NAME = 'astro.data';
52
+ var REMOTE_PACKAGE_BASE = 'astro.data';
53
+ var REMOTE_PACKAGE_NAME = Module['locateFile'] ? Module['locateFile'](REMOTE_PACKAGE_BASE, '') : REMOTE_PACKAGE_BASE;
54
+ var REMOTE_PACKAGE_SIZE = metadata['remote_package_size'];
55
+
56
+ async function fetchRemotePackage(packageName, packageSize) {
57
+ if (isNode) {
58
+ var contents = require('fs').readFileSync(packageName);
59
+ return new Uint8Array(contents).buffer;
60
+ }
61
+ if (!Module['dataFileDownloads']) Module['dataFileDownloads'] = {};
62
+ try {
63
+ var response = await fetch(packageName);
64
+ } catch (e) {
65
+ throw new Error(`Network Error: ${packageName}`, {e});
66
+ }
67
+ if (!response.ok) {
68
+ throw new Error(`${response.status}: ${response.url}`);
69
+ }
70
+
71
+ const chunks = [];
72
+ const headers = response.headers;
73
+ const total = Number(headers.get('Content-Length') || packageSize);
74
+ let loaded = 0;
75
+
76
+ Module['setStatus'] && Module['setStatus']('Downloading data...');
77
+ const reader = response.body.getReader();
78
+
79
+ while (1) {
80
+ var {done, value} = await reader.read();
81
+ if (done) break;
82
+ chunks.push(value);
83
+ loaded += value.length;
84
+ Module['dataFileDownloads'][packageName] = {loaded, total};
85
+
86
+ let totalLoaded = 0;
87
+ let totalSize = 0;
88
+
89
+ for (const download of Object.values(Module['dataFileDownloads'])) {
90
+ totalLoaded += download.loaded;
91
+ totalSize += download.total;
92
+ }
93
+
94
+ Module['setStatus'] && Module['setStatus'](`Downloading data... (${totalLoaded}/${totalSize})`);
95
+ }
96
+
97
+ const packageData = new Uint8Array(chunks.map((c) => c.length).reduce((a, b) => a + b, 0));
98
+ let offset = 0;
99
+ for (const chunk of chunks) {
100
+ packageData.set(chunk, offset);
101
+ offset += chunk.length;
102
+ }
103
+ return packageData.buffer;
104
+ }
105
+
106
+ var fetchPromise;
107
+ var fetched = Module['getPreloadedPackage'] && Module['getPreloadedPackage'](REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE);
108
+
109
+ if (!fetched) {
110
+ // Note that we don't use await here because we want to execute the
111
+ // the rest of this function immediately.
112
+ fetchPromise = fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE);
113
+ }
114
+
115
+ async function runWithFS(Module) {
116
+
117
+ function assert(check, msg) {
118
+ if (!check) throw new Error(msg);
119
+ }
120
+ Module['FS_createPath']("/", "eph", true, true);
121
+
122
+ for (var file of metadata['files']) {
123
+ var name = file['filename']
124
+ Module['addRunDependency'](`fp ${name}`);
125
+ }
126
+
127
+ async function processPackageData(arrayBuffer) {
128
+ assert(arrayBuffer, 'Loading data file failed.');
129
+ assert(arrayBuffer.constructor.name === ArrayBuffer.name, 'bad input to processPackageData ' + arrayBuffer.constructor.name);
130
+ var byteArray = new Uint8Array(arrayBuffer);
131
+ var curr;
132
+ // Reuse the bytearray from the XHR as the source for file reads.
133
+ for (var file of metadata['files']) {
134
+ var name = file['filename'];
135
+ var data = byteArray.subarray(file['start'], file['end']);
136
+ // canOwn this data in the filesystem, it is a slice into the heap that will never change
137
+ Module['FS_createDataFile'](name, null, data, true, true, true);
138
+ Module['removeRunDependency'](`fp ${name}`);
139
+ }
140
+ Module['removeRunDependency']('datafile_astro.data');
141
+ }
142
+ Module['addRunDependency']('datafile_astro.data');
143
+
144
+ if (!Module['preloadResults']) Module['preloadResults'] = {};
145
+
146
+ Module['preloadResults'][PACKAGE_NAME] = {fromCache: false};
147
+ if (!fetched) {
148
+ fetched = await fetchPromise;
149
+ }
150
+ processPackageData(fetched);
151
+
152
+ }
153
+ if (Module['calledRun']) {
154
+ runWithFS(Module);
155
+ } else {
156
+ if (!Module['preRun']) Module['preRun'] = [];
157
+ Module['preRun'].push(runWithFS); // FS is not initialized yet, wait for it
158
+ }
159
+
160
+ }
161
+ loadPackage({"files": [{"filename": "/eph/seas_18.se1", "start": 0, "end": 223002}, {"filename": "/eph/semo_18.se1", "start": 223002, "end": 1527773}, {"filename": "/eph/sepl_18.se1", "start": 1527773, "end": 2011828}], "remote_package_size": 2011828});
162
+
163
+ })();
164
+
165
+ // end include: /tmp/tmpokcc7h34.js
166
+
167
+
168
+ var arguments_ = [];
169
+ var thisProgram = './this.program';
170
+ var quit_ = (status, toThrow) => {
171
+ throw toThrow;
172
+ };
173
+
174
+ // In MODULARIZE mode _scriptName needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
175
+ // before the page load. In non-MODULARIZE modes generate it here.
176
+ var _scriptName = globalThis.document?.currentScript?.src;
177
+
178
+ if (typeof __filename != 'undefined') { // Node
179
+ _scriptName = __filename;
180
+ } else
181
+ if (ENVIRONMENT_IS_WORKER) {
182
+ _scriptName = self.location.href;
183
+ }
184
+
185
+ // `/` should be present at the end if `scriptDirectory` is not empty
186
+ var scriptDirectory = '';
187
+ function locateFile(path) {
188
+ if (Module['locateFile']) {
189
+ return Module['locateFile'](path, scriptDirectory);
190
+ }
191
+ return scriptDirectory + path;
192
+ }
193
+
194
+ // Hooks that are implemented differently in different runtime environments.
195
+ var readAsync, readBinary;
196
+
197
+ if (ENVIRONMENT_IS_NODE) {
198
+
199
+ // These modules will usually be used on Node.js. Load them eagerly to avoid
200
+ // the complexity of lazy-loading.
201
+ var fs = require('node:fs');
202
+
203
+ scriptDirectory = __dirname + '/';
204
+
205
+ // include: node_shell_read.js
206
+ readBinary = (filename) => {
207
+ // We need to re-wrap `file://` strings to URLs.
208
+ filename = isFileURI(filename) ? new URL(filename) : filename;
209
+ var ret = fs.readFileSync(filename);
210
+ return ret;
211
+ };
212
+
213
+ readAsync = async (filename, binary = true) => {
214
+ // See the comment in the `readBinary` function.
215
+ filename = isFileURI(filename) ? new URL(filename) : filename;
216
+ var ret = fs.readFileSync(filename, binary ? undefined : 'utf8');
217
+ return ret;
218
+ };
219
+ // end include: node_shell_read.js
220
+ if (process.argv.length > 1) {
221
+ thisProgram = process.argv[1].replace(/\\/g, '/');
222
+ }
223
+
224
+ arguments_ = process.argv.slice(2);
225
+
226
+ // MODULARIZE will export the module in the proper place outside, we don't need to export here
227
+ if (typeof module != 'undefined') {
228
+ module['exports'] = Module;
229
+ }
230
+
231
+ quit_ = (status, toThrow) => {
232
+ process.exitCode = status;
233
+ throw toThrow;
234
+ };
235
+
236
+ } else
237
+
238
+ // Note that this includes Node.js workers when relevant (pthreads is enabled).
239
+ // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and
240
+ // ENVIRONMENT_IS_NODE.
241
+ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
242
+ try {
243
+ scriptDirectory = new URL('.', _scriptName).href; // includes trailing slash
244
+ } catch {
245
+ // Must be a `blob:` or `data:` URL (e.g. `blob:http://site.com/etc/etc`), we cannot
246
+ // infer anything from them.
247
+ }
248
+
249
+ {
250
+ // include: web_or_worker_shell_read.js
251
+ if (ENVIRONMENT_IS_WORKER) {
252
+ readBinary = (url) => {
253
+ var xhr = new XMLHttpRequest();
254
+ xhr.open('GET', url, false);
255
+ xhr.responseType = 'arraybuffer';
256
+ xhr.send(null);
257
+ return new Uint8Array(/** @type{!ArrayBuffer} */(xhr.response));
258
+ };
259
+ }
260
+
261
+ readAsync = async (url) => {
262
+ // Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
263
+ // See https://github.com/github/fetch/pull/92#issuecomment-140665932
264
+ // Cordova or Electron apps are typically loaded from a file:// url.
265
+ // So use XHR on webview if URL is a file URL.
266
+ if (isFileURI(url)) {
267
+ return new Promise((resolve, reject) => {
268
+ var xhr = new XMLHttpRequest();
269
+ xhr.open('GET', url, true);
270
+ xhr.responseType = 'arraybuffer';
271
+ xhr.onload = () => {
272
+ if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
273
+ resolve(xhr.response);
274
+ return;
275
+ }
276
+ reject(xhr.status);
277
+ };
278
+ xhr.onerror = reject;
279
+ xhr.send(null);
280
+ });
281
+ }
282
+ var response = await fetch(url, { credentials: 'same-origin' });
283
+ if (response.ok) {
284
+ return response.arrayBuffer();
285
+ }
286
+ throw new Error(response.status + ' : ' + response.url);
287
+ };
288
+ // end include: web_or_worker_shell_read.js
289
+ }
290
+ } else
291
+ {
292
+ }
293
+
294
+ var out = console.log.bind(console);
295
+ var err = console.error.bind(console);
296
+
297
+ // end include: shell.js
298
+
299
+ // include: preamble.js
300
+ // === Preamble library stuff ===
301
+
302
+ // Documentation for the public APIs defined in this file must be updated in:
303
+ // site/source/docs/api_reference/preamble.js.rst
304
+ // A prebuilt local version of the documentation is available at:
305
+ // site/build/text/docs/api_reference/preamble.js.txt
306
+ // You can also build docs locally as HTML or other formats in site/
307
+ // An online HTML version (which may be of a different version of Emscripten)
308
+ // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
309
+
310
+ var wasmBinary;
311
+
312
+ // Wasm globals
313
+
314
+ //========================================
315
+ // Runtime essentials
316
+ //========================================
317
+
318
+ // whether we are quitting the application. no code should run after this.
319
+ // set in exit() and abort()
320
+ var ABORT = false;
321
+
322
+ // set by exit() and abort(). Passed to 'onExit' handler.
323
+ // NOTE: This is also used as the process return code in shell environments
324
+ // but only when noExitRuntime is false.
325
+ var EXITSTATUS;
326
+
327
+ // In STRICT mode, we only define assert() when ASSERTIONS is set. i.e. we
328
+ // don't define it at all in release modes. This matches the behaviour of
329
+ // MINIMAL_RUNTIME.
330
+ // TODO(sbc): Make this the default even without STRICT enabled.
331
+ /** @type {function(*, string=)} */
332
+ function assert(condition, text) {
333
+ if (!condition) {
334
+ // This build was created without ASSERTIONS defined. `assert()` should not
335
+ // ever be called in this configuration but in case there are callers in
336
+ // the wild leave this simple abort() implementation here for now.
337
+ abort(text);
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Indicates whether filename is delivered via file protocol (as opposed to http/https)
343
+ * @noinline
344
+ */
345
+ var isFileURI = (filename) => filename.startsWith('file://');
346
+
347
+ // include: runtime_common.js
348
+ // include: runtime_stack_check.js
349
+ // end include: runtime_stack_check.js
350
+ // include: runtime_exceptions.js
351
+ // end include: runtime_exceptions.js
352
+ // include: runtime_debug.js
353
+ // end include: runtime_debug.js
354
+ // Memory management
355
+ var
356
+ /** @type {!Int8Array} */
357
+ HEAP8,
358
+ /** @type {!Uint8Array} */
359
+ HEAPU8,
360
+ /** @type {!Int16Array} */
361
+ HEAP16,
362
+ /** @type {!Uint16Array} */
363
+ HEAPU16,
364
+ /** @type {!Int32Array} */
365
+ HEAP32,
366
+ /** @type {!Uint32Array} */
367
+ HEAPU32,
368
+ /** @type {!Float32Array} */
369
+ HEAPF32,
370
+ /** @type {!Float64Array} */
371
+ HEAPF64;
372
+
373
+ // BigInt64Array type is not correctly defined in closure
374
+ var
375
+ /** not-@type {!BigInt64Array} */
376
+ HEAP64,
377
+ /* BigUint64Array type is not correctly defined in closure
378
+ /** not-@type {!BigUint64Array} */
379
+ HEAPU64;
380
+
381
+ var runtimeInitialized = false;
382
+
383
+
384
+
385
+ function updateMemoryViews() {
386
+ var b = wasmMemory.buffer;
387
+ HEAP8 = new Int8Array(b);
388
+ HEAP16 = new Int16Array(b);
389
+ HEAPU8 = new Uint8Array(b);
390
+ HEAPU16 = new Uint16Array(b);
391
+ HEAP32 = new Int32Array(b);
392
+ HEAPU32 = new Uint32Array(b);
393
+ HEAPF32 = new Float32Array(b);
394
+ HEAPF64 = new Float64Array(b);
395
+ HEAP64 = new BigInt64Array(b);
396
+ HEAPU64 = new BigUint64Array(b);
397
+ }
398
+
399
+ // include: memoryprofiler.js
400
+ // end include: memoryprofiler.js
401
+ // end include: runtime_common.js
402
+ function preRun() {
403
+ if (Module['preRun']) {
404
+ if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
405
+ while (Module['preRun'].length) {
406
+ addOnPreRun(Module['preRun'].shift());
407
+ }
408
+ }
409
+ // Begin ATPRERUNS hooks
410
+ callRuntimeCallbacks(onPreRuns);
411
+ // End ATPRERUNS hooks
412
+ }
413
+
414
+ function initRuntime() {
415
+ runtimeInitialized = true;
416
+
417
+ // Begin ATINITS hooks
418
+ if (!Module['noFSInit'] && !FS.initialized) FS.init();
419
+ TTY.init();
420
+ // End ATINITS hooks
421
+
422
+ wasmExports['__wasm_call_ctors']();
423
+
424
+ // Begin ATPOSTCTORS hooks
425
+ FS.ignorePermissions = false;
426
+ // End ATPOSTCTORS hooks
427
+ }
428
+
429
+ function postRun() {
430
+ // PThreads reuse the runtime from the main thread.
431
+
432
+ if (Module['postRun']) {
433
+ if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
434
+ while (Module['postRun'].length) {
435
+ addOnPostRun(Module['postRun'].shift());
436
+ }
437
+ }
438
+
439
+ // Begin ATPOSTRUNS hooks
440
+ callRuntimeCallbacks(onPostRuns);
441
+ // End ATPOSTRUNS hooks
442
+ }
443
+
444
+ /** @param {string|number=} what */
445
+ function abort(what) {
446
+ Module['onAbort']?.(what);
447
+
448
+ what = 'Aborted(' + what + ')';
449
+ // TODO(sbc): Should we remove printing and leave it up to whoever
450
+ // catches the exception?
451
+ err(what);
452
+
453
+ ABORT = true;
454
+
455
+ what += '. Build with -sASSERTIONS for more info.';
456
+
457
+ // Use a wasm runtime error, because a JS error might be seen as a foreign
458
+ // exception, which means we'd run destructors on it. We need the error to
459
+ // simply make the program stop.
460
+ // FIXME This approach does not work in Wasm EH because it currently does not assume
461
+ // all RuntimeErrors are from traps; it decides whether a RuntimeError is from
462
+ // a trap or not based on a hidden field within the object. So at the moment
463
+ // we don't have a way of throwing a wasm trap from JS. TODO Make a JS API that
464
+ // allows this in the wasm spec.
465
+
466
+ // Suppress closure compiler warning here. Closure compiler's builtin extern
467
+ // definition for WebAssembly.RuntimeError claims it takes no arguments even
468
+ // though it can.
469
+ // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure gets fixed.
470
+ /** @suppress {checkTypes} */
471
+ var e = new WebAssembly.RuntimeError(what);
472
+
473
+ // Throw the error whether or not MODULARIZE is set because abort is used
474
+ // in code paths apart from instantiation where an exception is expected
475
+ // to be thrown when abort is called.
476
+ throw e;
477
+ }
478
+
479
+ var wasmBinaryFile;
480
+
481
+ function findWasmBinary() {
482
+ return locateFile('astro.wasm');
483
+ }
484
+
485
+ function getBinarySync(file) {
486
+ if (file == wasmBinaryFile && wasmBinary) {
487
+ return new Uint8Array(wasmBinary);
488
+ }
489
+ if (readBinary) {
490
+ return readBinary(file);
491
+ }
492
+ // Throwing a plain string here, even though it not normally advisable since
493
+ // this gets turning into an `abort` in instantiateArrayBuffer.
494
+ throw 'both async and sync fetching of the wasm failed';
495
+ }
496
+
497
+ async function getWasmBinary(binaryFile) {
498
+ // If we don't have the binary yet, load it asynchronously using readAsync.
499
+ if (!wasmBinary) {
500
+ // Fetch the binary using readAsync
501
+ try {
502
+ var response = await readAsync(binaryFile);
503
+ return new Uint8Array(response);
504
+ } catch {
505
+ // Fall back to getBinarySync below;
506
+ }
507
+ }
508
+
509
+ // Otherwise, getBinarySync should be able to get it synchronously
510
+ return getBinarySync(binaryFile);
511
+ }
512
+
513
+ async function instantiateArrayBuffer(binaryFile, imports) {
514
+ try {
515
+ var binary = await getWasmBinary(binaryFile);
516
+ var instance = await WebAssembly.instantiate(binary, imports);
517
+ return instance;
518
+ } catch (reason) {
519
+ err(`failed to asynchronously prepare wasm: ${reason}`);
520
+
521
+ abort(reason);
522
+ }
523
+ }
524
+
525
+ async function instantiateAsync(binary, binaryFile, imports) {
526
+ if (!binary
527
+ // Don't use streaming for file:// delivered objects in a webview, fetch them synchronously.
528
+ && !isFileURI(binaryFile)
529
+ // Avoid instantiateStreaming() on Node.js environment for now, as while
530
+ // Node.js v18.1.0 implements it, it does not have a full fetch()
531
+ // implementation yet.
532
+ //
533
+ // Reference:
534
+ // https://github.com/emscripten-core/emscripten/pull/16917
535
+ && !ENVIRONMENT_IS_NODE
536
+ ) {
537
+ try {
538
+ var response = fetch(binaryFile, { credentials: 'same-origin' });
539
+ var instantiationResult = await WebAssembly.instantiateStreaming(response, imports);
540
+ return instantiationResult;
541
+ } catch (reason) {
542
+ // We expect the most common failure cause to be a bad MIME type for the binary,
543
+ // in which case falling back to ArrayBuffer instantiation should work.
544
+ err(`wasm streaming compile failed: ${reason}`);
545
+ err('falling back to ArrayBuffer instantiation');
546
+ // fall back of instantiateArrayBuffer below
547
+ };
548
+ }
549
+ return instantiateArrayBuffer(binaryFile, imports);
550
+ }
551
+
552
+ function getWasmImports() {
553
+ // prepare imports
554
+ var imports = {
555
+ 'env': wasmImports,
556
+ 'wasi_snapshot_preview1': wasmImports,
557
+ };
558
+ return imports;
559
+ }
560
+
561
+ // Create the wasm instance.
562
+ // Receives the wasm imports, returns the exports.
563
+ async function createWasm() {
564
+ // Load the wasm module and create an instance of using native support in the JS engine.
565
+ // handle a generated wasm instance, receiving its exports and
566
+ // performing other necessary setup
567
+ /** @param {WebAssembly.Module=} module*/
568
+ function receiveInstance(instance, module) {
569
+ wasmExports = instance.exports;
570
+
571
+ assignWasmExports(wasmExports);
572
+
573
+ updateMemoryViews();
574
+
575
+ removeRunDependency('wasm-instantiate');
576
+ return wasmExports;
577
+ }
578
+ addRunDependency('wasm-instantiate');
579
+
580
+ // Prefer streaming instantiation if available.
581
+ function receiveInstantiationResult(result) {
582
+ // 'result' is a ResultObject object which has both the module and instance.
583
+ // receiveInstance() will swap in the exports (to Module.asm) so they can be called
584
+ // 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.
585
+ // When the regression is fixed, can restore the above PTHREADS-enabled path.
586
+ return receiveInstance(result['instance']);
587
+ }
588
+
589
+ var info = getWasmImports();
590
+
591
+ // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
592
+ // to manually instantiate the Wasm module themselves. This allows pages to
593
+ // run the instantiation parallel to any other async startup actions they are
594
+ // performing.
595
+ // Also pthreads and wasm workers initialize the wasm instance through this
596
+ // path.
597
+ if (Module['instantiateWasm']) {
598
+ return new Promise((resolve, reject) => {
599
+ Module['instantiateWasm'](info, (inst, mod) => {
600
+ resolve(receiveInstance(inst, mod));
601
+ });
602
+ });
603
+ }
604
+
605
+ wasmBinaryFile ??= findWasmBinary();
606
+ var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
607
+ var exports = receiveInstantiationResult(result);
608
+ return exports;
609
+ }
610
+
611
+ // end include: preamble.js
612
+
613
+ // Begin JS library code
614
+
615
+
616
+ class ExitStatus {
617
+ name = 'ExitStatus';
618
+ constructor(status) {
619
+ this.message = `Program terminated with exit(${status})`;
620
+ this.status = status;
621
+ }
622
+ }
623
+
624
+ var callRuntimeCallbacks = (callbacks) => {
625
+ while (callbacks.length > 0) {
626
+ // Pass the module as the first argument.
627
+ callbacks.shift()(Module);
628
+ }
629
+ };
630
+ var onPostRuns = [];
631
+ var addOnPostRun = (cb) => onPostRuns.push(cb);
632
+
633
+ var onPreRuns = [];
634
+ var addOnPreRun = (cb) => onPreRuns.push(cb);
635
+
636
+ var runDependencies = 0;
637
+
638
+
639
+ var dependenciesFulfilled = null;
640
+ var removeRunDependency = (id) => {
641
+ runDependencies--;
642
+
643
+ Module['monitorRunDependencies']?.(runDependencies);
644
+
645
+ if (runDependencies == 0) {
646
+ if (dependenciesFulfilled) {
647
+ var callback = dependenciesFulfilled;
648
+ dependenciesFulfilled = null;
649
+ callback(); // can add another dependenciesFulfilled
650
+ }
651
+ }
652
+ };
653
+ var addRunDependency = (id) => {
654
+ runDependencies++;
655
+
656
+ Module['monitorRunDependencies']?.(runDependencies);
657
+
658
+ };
659
+
660
+
661
+
662
+ /**
663
+ * @param {number} ptr
664
+ * @param {string} type
665
+ */
666
+ function getValue(ptr, type = 'i8') {
667
+ if (type.endsWith('*')) type = '*';
668
+ switch (type) {
669
+ case 'i1': return HEAP8[ptr];
670
+ case 'i8': return HEAP8[ptr];
671
+ case 'i16': return HEAP16[((ptr)>>1)];
672
+ case 'i32': return HEAP32[((ptr)>>2)];
673
+ case 'i64': return HEAP64[((ptr)>>3)];
674
+ case 'float': return HEAPF32[((ptr)>>2)];
675
+ case 'double': return HEAPF64[((ptr)>>3)];
676
+ case '*': return HEAPU32[((ptr)>>2)];
677
+ default: abort(`invalid type for getValue: ${type}`);
678
+ }
679
+ }
680
+
681
+ var noExitRuntime = true;
682
+
683
+
684
+
685
+ /**
686
+ * @param {number} ptr
687
+ * @param {number} value
688
+ * @param {string} type
689
+ */
690
+ function setValue(ptr, value, type = 'i8') {
691
+ if (type.endsWith('*')) type = '*';
692
+ switch (type) {
693
+ case 'i1': HEAP8[ptr] = value; break;
694
+ case 'i8': HEAP8[ptr] = value; break;
695
+ case 'i16': HEAP16[((ptr)>>1)] = value; break;
696
+ case 'i32': HEAP32[((ptr)>>2)] = value; break;
697
+ case 'i64': HEAP64[((ptr)>>3)] = BigInt(value); break;
698
+ case 'float': HEAPF32[((ptr)>>2)] = value; break;
699
+ case 'double': HEAPF64[((ptr)>>3)] = value; break;
700
+ case '*': HEAPU32[((ptr)>>2)] = value; break;
701
+ default: abort(`invalid type for setValue: ${type}`);
702
+ }
703
+ }
704
+
705
+ var stackRestore = (val) => __emscripten_stack_restore(val);
706
+
707
+ var stackSave = () => _emscripten_stack_get_current();
708
+
709
+
710
+
711
+ var syscallGetVarargI = () => {
712
+ // the `+` prepended here is necessary to convince the JSCompiler that varargs is indeed a number.
713
+ var ret = HEAP32[((+SYSCALLS.varargs)>>2)];
714
+ SYSCALLS.varargs += 4;
715
+ return ret;
716
+ };
717
+ var syscallGetVarargP = syscallGetVarargI;
718
+
719
+
720
+ var PATH = {
721
+ isAbs:(path) => path.charAt(0) === '/',
722
+ splitPath:(filename) => {
723
+ var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
724
+ return splitPathRe.exec(filename).slice(1);
725
+ },
726
+ normalizeArray:(parts, allowAboveRoot) => {
727
+ // if the path tries to go above the root, `up` ends up > 0
728
+ var up = 0;
729
+ for (var i = parts.length - 1; i >= 0; i--) {
730
+ var last = parts[i];
731
+ if (last === '.') {
732
+ parts.splice(i, 1);
733
+ } else if (last === '..') {
734
+ parts.splice(i, 1);
735
+ up++;
736
+ } else if (up) {
737
+ parts.splice(i, 1);
738
+ up--;
739
+ }
740
+ }
741
+ // if the path is allowed to go above the root, restore leading ..s
742
+ if (allowAboveRoot) {
743
+ for (; up; up--) {
744
+ parts.unshift('..');
745
+ }
746
+ }
747
+ return parts;
748
+ },
749
+ normalize:(path) => {
750
+ var isAbsolute = PATH.isAbs(path),
751
+ trailingSlash = path.slice(-1) === '/';
752
+ // Normalize the path
753
+ path = PATH.normalizeArray(path.split('/').filter((p) => !!p), !isAbsolute).join('/');
754
+ if (!path && !isAbsolute) {
755
+ path = '.';
756
+ }
757
+ if (path && trailingSlash) {
758
+ path += '/';
759
+ }
760
+ return (isAbsolute ? '/' : '') + path;
761
+ },
762
+ dirname:(path) => {
763
+ var result = PATH.splitPath(path),
764
+ root = result[0],
765
+ dir = result[1];
766
+ if (!root && !dir) {
767
+ // No dirname whatsoever
768
+ return '.';
769
+ }
770
+ if (dir) {
771
+ // It has a dirname, strip trailing slash
772
+ dir = dir.slice(0, -1);
773
+ }
774
+ return root + dir;
775
+ },
776
+ basename:(path) => path && path.match(/([^\/]+|\/)\/*$/)[1],
777
+ join:(...paths) => PATH.normalize(paths.join('/')),
778
+ join2:(l, r) => PATH.normalize(l + '/' + r),
779
+ };
780
+
781
+ var initRandomFill = () => {
782
+ // This block is not needed on v19+ since crypto.getRandomValues is builtin
783
+ if (ENVIRONMENT_IS_NODE) {
784
+ var nodeCrypto = require('node:crypto');
785
+ return (view) => nodeCrypto.randomFillSync(view);
786
+ }
787
+
788
+ return (view) => crypto.getRandomValues(view);
789
+ };
790
+ var randomFill = (view) => {
791
+ // Lazily init on the first invocation.
792
+ (randomFill = initRandomFill())(view);
793
+ };
794
+
795
+
796
+
797
+ var PATH_FS = {
798
+ resolve:(...args) => {
799
+ var resolvedPath = '',
800
+ resolvedAbsolute = false;
801
+ for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
802
+ var path = (i >= 0) ? args[i] : FS.cwd();
803
+ // Skip empty and invalid entries
804
+ if (typeof path != 'string') {
805
+ throw new TypeError('Arguments to path.resolve must be strings');
806
+ } else if (!path) {
807
+ return ''; // an invalid portion invalidates the whole thing
808
+ }
809
+ resolvedPath = path + '/' + resolvedPath;
810
+ resolvedAbsolute = PATH.isAbs(path);
811
+ }
812
+ // At this point the path should be resolved to a full absolute path, but
813
+ // handle relative paths to be safe (might happen when process.cwd() fails)
814
+ resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter((p) => !!p), !resolvedAbsolute).join('/');
815
+ return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
816
+ },
817
+ relative:(from, to) => {
818
+ from = PATH_FS.resolve(from).slice(1);
819
+ to = PATH_FS.resolve(to).slice(1);
820
+ function trim(arr) {
821
+ var start = 0;
822
+ for (; start < arr.length; start++) {
823
+ if (arr[start] !== '') break;
824
+ }
825
+ var end = arr.length - 1;
826
+ for (; end >= 0; end--) {
827
+ if (arr[end] !== '') break;
828
+ }
829
+ if (start > end) return [];
830
+ return arr.slice(start, end - start + 1);
831
+ }
832
+ var fromParts = trim(from.split('/'));
833
+ var toParts = trim(to.split('/'));
834
+ var length = Math.min(fromParts.length, toParts.length);
835
+ var samePartsLength = length;
836
+ for (var i = 0; i < length; i++) {
837
+ if (fromParts[i] !== toParts[i]) {
838
+ samePartsLength = i;
839
+ break;
840
+ }
841
+ }
842
+ var outputParts = [];
843
+ for (var i = samePartsLength; i < fromParts.length; i++) {
844
+ outputParts.push('..');
845
+ }
846
+ outputParts = outputParts.concat(toParts.slice(samePartsLength));
847
+ return outputParts.join('/');
848
+ },
849
+ };
850
+
851
+
852
+ var UTF8Decoder = globalThis.TextDecoder && new TextDecoder();
853
+
854
+ var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => {
855
+ var maxIdx = idx + maxBytesToRead;
856
+ if (ignoreNul) return maxIdx;
857
+ // TextDecoder needs to know the byte length in advance, it doesn't stop on
858
+ // null terminator by itself.
859
+ // As a tiny code save trick, compare idx against maxIdx using a negation,
860
+ // so that maxBytesToRead=undefined/NaN means Infinity.
861
+ while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx;
862
+ return idx;
863
+ };
864
+
865
+ /**
866
+ * Given a pointer 'idx' to a null-terminated UTF8-encoded string in the given
867
+ * array that contains uint8 values, returns a copy of that string as a
868
+ * Javascript String object.
869
+ * heapOrArray is either a regular array, or a JavaScript typed array view.
870
+ * @param {number=} idx
871
+ * @param {number=} maxBytesToRead
872
+ * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
873
+ * @return {string}
874
+ */
875
+ var UTF8ArrayToString = (heapOrArray, idx = 0, maxBytesToRead, ignoreNul) => {
876
+
877
+ var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul);
878
+
879
+ // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it.
880
+ if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
881
+ return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
882
+ }
883
+ var str = '';
884
+ while (idx < endPtr) {
885
+ // For UTF8 byte structure, see:
886
+ // http://en.wikipedia.org/wiki/UTF-8#Description
887
+ // https://www.ietf.org/rfc/rfc2279.txt
888
+ // https://tools.ietf.org/html/rfc3629
889
+ var u0 = heapOrArray[idx++];
890
+ if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; }
891
+ var u1 = heapOrArray[idx++] & 63;
892
+ if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; }
893
+ var u2 = heapOrArray[idx++] & 63;
894
+ if ((u0 & 0xF0) == 0xE0) {
895
+ u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
896
+ } else {
897
+ u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heapOrArray[idx++] & 63);
898
+ }
899
+
900
+ if (u0 < 0x10000) {
901
+ str += String.fromCharCode(u0);
902
+ } else {
903
+ var ch = u0 - 0x10000;
904
+ str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
905
+ }
906
+ }
907
+ return str;
908
+ };
909
+
910
+ var FS_stdin_getChar_buffer = [];
911
+
912
+ var lengthBytesUTF8 = (str) => {
913
+ var len = 0;
914
+ for (var i = 0; i < str.length; ++i) {
915
+ // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code
916
+ // unit, not a Unicode code point of the character! So decode
917
+ // UTF16->UTF32->UTF8.
918
+ // See http://unicode.org/faq/utf_bom.html#utf16-3
919
+ var c = str.charCodeAt(i); // possibly a lead surrogate
920
+ if (c <= 0x7F) {
921
+ len++;
922
+ } else if (c <= 0x7FF) {
923
+ len += 2;
924
+ } else if (c >= 0xD800 && c <= 0xDFFF) {
925
+ len += 4; ++i;
926
+ } else {
927
+ len += 3;
928
+ }
929
+ }
930
+ return len;
931
+ };
932
+
933
+ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
934
+ // Parameter maxBytesToWrite is not optional. Negative values, 0, null,
935
+ // undefined and false each don't write out any bytes.
936
+ if (!(maxBytesToWrite > 0))
937
+ return 0;
938
+
939
+ var startIdx = outIdx;
940
+ var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator.
941
+ for (var i = 0; i < str.length; ++i) {
942
+ // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description
943
+ // and https://www.ietf.org/rfc/rfc2279.txt
944
+ // and https://tools.ietf.org/html/rfc3629
945
+ var u = str.codePointAt(i);
946
+ if (u <= 0x7F) {
947
+ if (outIdx >= endIdx) break;
948
+ heap[outIdx++] = u;
949
+ } else if (u <= 0x7FF) {
950
+ if (outIdx + 1 >= endIdx) break;
951
+ heap[outIdx++] = 0xC0 | (u >> 6);
952
+ heap[outIdx++] = 0x80 | (u & 63);
953
+ } else if (u <= 0xFFFF) {
954
+ if (outIdx + 2 >= endIdx) break;
955
+ heap[outIdx++] = 0xE0 | (u >> 12);
956
+ heap[outIdx++] = 0x80 | ((u >> 6) & 63);
957
+ heap[outIdx++] = 0x80 | (u & 63);
958
+ } else {
959
+ if (outIdx + 3 >= endIdx) break;
960
+ heap[outIdx++] = 0xF0 | (u >> 18);
961
+ heap[outIdx++] = 0x80 | ((u >> 12) & 63);
962
+ heap[outIdx++] = 0x80 | ((u >> 6) & 63);
963
+ heap[outIdx++] = 0x80 | (u & 63);
964
+ // Gotcha: if codePoint is over 0xFFFF, it is represented as a surrogate pair in UTF-16.
965
+ // We need to manually skip over the second code unit for correct iteration.
966
+ i++;
967
+ }
968
+ }
969
+ // Null-terminate the pointer to the buffer.
970
+ heap[outIdx] = 0;
971
+ return outIdx - startIdx;
972
+ };
973
+ /** @type {function(string, boolean=, number=)} */
974
+ var intArrayFromString = (stringy, dontAddNull, length) => {
975
+ var len = length > 0 ? length : lengthBytesUTF8(stringy)+1;
976
+ var u8array = new Array(len);
977
+ var numBytesWritten = stringToUTF8Array(stringy, u8array, 0, u8array.length);
978
+ if (dontAddNull) u8array.length = numBytesWritten;
979
+ return u8array;
980
+ };
981
+ var FS_stdin_getChar = () => {
982
+ if (!FS_stdin_getChar_buffer.length) {
983
+ var result = null;
984
+ if (ENVIRONMENT_IS_NODE) {
985
+ // we will read data by chunks of BUFSIZE
986
+ var BUFSIZE = 256;
987
+ var buf = Buffer.alloc(BUFSIZE);
988
+ var bytesRead = 0;
989
+
990
+ // For some reason we must suppress a closure warning here, even though
991
+ // fd definitely exists on process.stdin, and is even the proper way to
992
+ // get the fd of stdin,
993
+ // https://github.com/nodejs/help/issues/2136#issuecomment-523649904
994
+ // This started to happen after moving this logic out of library_tty.js,
995
+ // so it is related to the surrounding code in some unclear manner.
996
+ /** @suppress {missingProperties} */
997
+ var fd = process.stdin.fd;
998
+
999
+ try {
1000
+ bytesRead = fs.readSync(fd, buf, 0, BUFSIZE);
1001
+ } catch(e) {
1002
+ // Cross-platform differences: on Windows, reading EOF throws an
1003
+ // exception, but on other OSes, reading EOF returns 0. Uniformize
1004
+ // behavior by treating the EOF exception to return 0.
1005
+ if (e.toString().includes('EOF')) bytesRead = 0;
1006
+ else throw e;
1007
+ }
1008
+
1009
+ if (bytesRead > 0) {
1010
+ result = buf.slice(0, bytesRead).toString('utf-8');
1011
+ }
1012
+ } else
1013
+ if (globalThis.window?.prompt) {
1014
+ // Browser.
1015
+ result = window.prompt('Input: '); // returns null on cancel
1016
+ if (result !== null) {
1017
+ result += '\n';
1018
+ }
1019
+ } else
1020
+ {}
1021
+ if (!result) {
1022
+ return null;
1023
+ }
1024
+ FS_stdin_getChar_buffer = intArrayFromString(result, true);
1025
+ }
1026
+ return FS_stdin_getChar_buffer.shift();
1027
+ };
1028
+ var TTY = {
1029
+ ttys:[],
1030
+ init() {
1031
+ // https://github.com/emscripten-core/emscripten/pull/1555
1032
+ // if (ENVIRONMENT_IS_NODE) {
1033
+ // // currently, FS.init does not distinguish if process.stdin is a file or TTY
1034
+ // // device, it always assumes it's a TTY device. because of this, we're forcing
1035
+ // // process.stdin to UTF8 encoding to at least make stdin reading compatible
1036
+ // // with text files until FS.init can be refactored.
1037
+ // process.stdin.setEncoding('utf8');
1038
+ // }
1039
+ },
1040
+ shutdown() {
1041
+ // https://github.com/emscripten-core/emscripten/pull/1555
1042
+ // if (ENVIRONMENT_IS_NODE) {
1043
+ // // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)?
1044
+ // // 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
1045
+ // // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists?
1046
+ // // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle
1047
+ // // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call
1048
+ // process.stdin.pause();
1049
+ // }
1050
+ },
1051
+ register(dev, ops) {
1052
+ TTY.ttys[dev] = { input: [], output: [], ops: ops };
1053
+ FS.registerDevice(dev, TTY.stream_ops);
1054
+ },
1055
+ stream_ops:{
1056
+ open(stream) {
1057
+ var tty = TTY.ttys[stream.node.rdev];
1058
+ if (!tty) {
1059
+ throw new FS.ErrnoError(43);
1060
+ }
1061
+ stream.tty = tty;
1062
+ stream.seekable = false;
1063
+ },
1064
+ close(stream) {
1065
+ // flush any pending line data
1066
+ stream.tty.ops.fsync(stream.tty);
1067
+ },
1068
+ fsync(stream) {
1069
+ stream.tty.ops.fsync(stream.tty);
1070
+ },
1071
+ read(stream, buffer, offset, length, pos /* ignored */) {
1072
+ if (!stream.tty || !stream.tty.ops.get_char) {
1073
+ throw new FS.ErrnoError(60);
1074
+ }
1075
+ var bytesRead = 0;
1076
+ for (var i = 0; i < length; i++) {
1077
+ var result;
1078
+ try {
1079
+ result = stream.tty.ops.get_char(stream.tty);
1080
+ } catch (e) {
1081
+ throw new FS.ErrnoError(29);
1082
+ }
1083
+ if (result === undefined && bytesRead === 0) {
1084
+ throw new FS.ErrnoError(6);
1085
+ }
1086
+ if (result === null || result === undefined) break;
1087
+ bytesRead++;
1088
+ buffer[offset+i] = result;
1089
+ }
1090
+ if (bytesRead) {
1091
+ stream.node.atime = Date.now();
1092
+ }
1093
+ return bytesRead;
1094
+ },
1095
+ write(stream, buffer, offset, length, pos) {
1096
+ if (!stream.tty || !stream.tty.ops.put_char) {
1097
+ throw new FS.ErrnoError(60);
1098
+ }
1099
+ try {
1100
+ for (var i = 0; i < length; i++) {
1101
+ stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
1102
+ }
1103
+ } catch (e) {
1104
+ throw new FS.ErrnoError(29);
1105
+ }
1106
+ if (length) {
1107
+ stream.node.mtime = stream.node.ctime = Date.now();
1108
+ }
1109
+ return i;
1110
+ },
1111
+ },
1112
+ default_tty_ops:{
1113
+ get_char(tty) {
1114
+ return FS_stdin_getChar();
1115
+ },
1116
+ put_char(tty, val) {
1117
+ if (val === null || val === 10) {
1118
+ out(UTF8ArrayToString(tty.output));
1119
+ tty.output = [];
1120
+ } else {
1121
+ if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
1122
+ }
1123
+ },
1124
+ fsync(tty) {
1125
+ if (tty.output?.length > 0) {
1126
+ out(UTF8ArrayToString(tty.output));
1127
+ tty.output = [];
1128
+ }
1129
+ },
1130
+ ioctl_tcgets(tty) {
1131
+ // typical setting
1132
+ return {
1133
+ c_iflag: 25856,
1134
+ c_oflag: 5,
1135
+ c_cflag: 191,
1136
+ c_lflag: 35387,
1137
+ c_cc: [
1138
+ 0x03, 0x1c, 0x7f, 0x15, 0x04, 0x00, 0x01, 0x00, 0x11, 0x13, 0x1a, 0x00,
1139
+ 0x12, 0x0f, 0x17, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1140
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1141
+ ]
1142
+ };
1143
+ },
1144
+ ioctl_tcsets(tty, optional_actions, data) {
1145
+ // currently just ignore
1146
+ return 0;
1147
+ },
1148
+ ioctl_tiocgwinsz(tty) {
1149
+ return [24, 80];
1150
+ },
1151
+ },
1152
+ default_tty1_ops:{
1153
+ put_char(tty, val) {
1154
+ if (val === null || val === 10) {
1155
+ err(UTF8ArrayToString(tty.output));
1156
+ tty.output = [];
1157
+ } else {
1158
+ if (val != 0) tty.output.push(val);
1159
+ }
1160
+ },
1161
+ fsync(tty) {
1162
+ if (tty.output?.length > 0) {
1163
+ err(UTF8ArrayToString(tty.output));
1164
+ tty.output = [];
1165
+ }
1166
+ },
1167
+ },
1168
+ };
1169
+
1170
+
1171
+ var mmapAlloc = (size) => {
1172
+ abort();
1173
+ };
1174
+ var MEMFS = {
1175
+ ops_table:null,
1176
+ mount(mount) {
1177
+ return MEMFS.createNode(null, '/', 16895, 0);
1178
+ },
1179
+ createNode(parent, name, mode, dev) {
1180
+ if (FS.isBlkdev(mode) || FS.isFIFO(mode)) {
1181
+ // not supported
1182
+ throw new FS.ErrnoError(63);
1183
+ }
1184
+ MEMFS.ops_table ||= {
1185
+ dir: {
1186
+ node: {
1187
+ getattr: MEMFS.node_ops.getattr,
1188
+ setattr: MEMFS.node_ops.setattr,
1189
+ lookup: MEMFS.node_ops.lookup,
1190
+ mknod: MEMFS.node_ops.mknod,
1191
+ rename: MEMFS.node_ops.rename,
1192
+ unlink: MEMFS.node_ops.unlink,
1193
+ rmdir: MEMFS.node_ops.rmdir,
1194
+ readdir: MEMFS.node_ops.readdir,
1195
+ symlink: MEMFS.node_ops.symlink
1196
+ },
1197
+ stream: {
1198
+ llseek: MEMFS.stream_ops.llseek
1199
+ }
1200
+ },
1201
+ file: {
1202
+ node: {
1203
+ getattr: MEMFS.node_ops.getattr,
1204
+ setattr: MEMFS.node_ops.setattr
1205
+ },
1206
+ stream: {
1207
+ llseek: MEMFS.stream_ops.llseek,
1208
+ read: MEMFS.stream_ops.read,
1209
+ write: MEMFS.stream_ops.write,
1210
+ mmap: MEMFS.stream_ops.mmap,
1211
+ msync: MEMFS.stream_ops.msync
1212
+ }
1213
+ },
1214
+ link: {
1215
+ node: {
1216
+ getattr: MEMFS.node_ops.getattr,
1217
+ setattr: MEMFS.node_ops.setattr,
1218
+ readlink: MEMFS.node_ops.readlink
1219
+ },
1220
+ stream: {}
1221
+ },
1222
+ chrdev: {
1223
+ node: {
1224
+ getattr: MEMFS.node_ops.getattr,
1225
+ setattr: MEMFS.node_ops.setattr
1226
+ },
1227
+ stream: FS.chrdev_stream_ops
1228
+ }
1229
+ };
1230
+ var node = FS.createNode(parent, name, mode, dev);
1231
+ if (FS.isDir(node.mode)) {
1232
+ node.node_ops = MEMFS.ops_table.dir.node;
1233
+ node.stream_ops = MEMFS.ops_table.dir.stream;
1234
+ node.contents = {};
1235
+ } else if (FS.isFile(node.mode)) {
1236
+ node.node_ops = MEMFS.ops_table.file.node;
1237
+ node.stream_ops = MEMFS.ops_table.file.stream;
1238
+ node.usedBytes = 0; // The actual number of bytes used in the typed array, as opposed to contents.length which gives the whole capacity.
1239
+ // When the byte data of the file is populated, this will point to either a typed array, or a normal JS array. Typed arrays are preferred
1240
+ // for performance, and used by default. However, typed arrays are not resizable like normal JS arrays are, so there is a small disk size
1241
+ // penalty involved for appending file writes that continuously grow a file similar to std::vector capacity vs used -scheme.
1242
+ node.contents = null;
1243
+ } else if (FS.isLink(node.mode)) {
1244
+ node.node_ops = MEMFS.ops_table.link.node;
1245
+ node.stream_ops = MEMFS.ops_table.link.stream;
1246
+ } else if (FS.isChrdev(node.mode)) {
1247
+ node.node_ops = MEMFS.ops_table.chrdev.node;
1248
+ node.stream_ops = MEMFS.ops_table.chrdev.stream;
1249
+ }
1250
+ node.atime = node.mtime = node.ctime = Date.now();
1251
+ // add the new node to the parent
1252
+ if (parent) {
1253
+ parent.contents[name] = node;
1254
+ parent.atime = parent.mtime = parent.ctime = node.atime;
1255
+ }
1256
+ return node;
1257
+ },
1258
+ getFileDataAsTypedArray(node) {
1259
+ if (!node.contents) return new Uint8Array(0);
1260
+ if (node.contents.subarray) return node.contents.subarray(0, node.usedBytes); // Make sure to not return excess unused bytes.
1261
+ return new Uint8Array(node.contents);
1262
+ },
1263
+ expandFileStorage(node, newCapacity) {
1264
+ var prevCapacity = node.contents ? node.contents.length : 0;
1265
+ if (prevCapacity >= newCapacity) return; // No need to expand, the storage was already large enough.
1266
+ // Don't expand strictly to the given requested limit if it's only a very small increase, but instead geometrically grow capacity.
1267
+ // For small filesizes (<1MB), perform size*2 geometric increase, but for large sizes, do a much more conservative size*1.125 increase to
1268
+ // avoid overshooting the allocation cap by a very large margin.
1269
+ var CAPACITY_DOUBLING_MAX = 1024 * 1024;
1270
+ newCapacity = Math.max(newCapacity, (prevCapacity * (prevCapacity < CAPACITY_DOUBLING_MAX ? 2.0 : 1.125)) >>> 0);
1271
+ if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); // At minimum allocate 256b for each file when expanding.
1272
+ var oldContents = node.contents;
1273
+ node.contents = new Uint8Array(newCapacity); // Allocate new storage.
1274
+ if (node.usedBytes > 0) node.contents.set(oldContents.subarray(0, node.usedBytes), 0); // Copy old data over to the new storage.
1275
+ },
1276
+ resizeFileStorage(node, newSize) {
1277
+ if (node.usedBytes == newSize) return;
1278
+ if (newSize == 0) {
1279
+ node.contents = null; // Fully decommit when requesting a resize to zero.
1280
+ node.usedBytes = 0;
1281
+ } else {
1282
+ var oldContents = node.contents;
1283
+ node.contents = new Uint8Array(newSize); // Allocate new storage.
1284
+ if (oldContents) {
1285
+ node.contents.set(oldContents.subarray(0, Math.min(newSize, node.usedBytes))); // Copy old data over to the new storage.
1286
+ }
1287
+ node.usedBytes = newSize;
1288
+ }
1289
+ },
1290
+ node_ops:{
1291
+ getattr(node) {
1292
+ var attr = {};
1293
+ // device numbers reuse inode numbers.
1294
+ attr.dev = FS.isChrdev(node.mode) ? node.id : 1;
1295
+ attr.ino = node.id;
1296
+ attr.mode = node.mode;
1297
+ attr.nlink = 1;
1298
+ attr.uid = 0;
1299
+ attr.gid = 0;
1300
+ attr.rdev = node.rdev;
1301
+ if (FS.isDir(node.mode)) {
1302
+ attr.size = 4096;
1303
+ } else if (FS.isFile(node.mode)) {
1304
+ attr.size = node.usedBytes;
1305
+ } else if (FS.isLink(node.mode)) {
1306
+ attr.size = node.link.length;
1307
+ } else {
1308
+ attr.size = 0;
1309
+ }
1310
+ attr.atime = new Date(node.atime);
1311
+ attr.mtime = new Date(node.mtime);
1312
+ attr.ctime = new Date(node.ctime);
1313
+ // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize),
1314
+ // but this is not required by the standard.
1315
+ attr.blksize = 4096;
1316
+ attr.blocks = Math.ceil(attr.size / attr.blksize);
1317
+ return attr;
1318
+ },
1319
+ setattr(node, attr) {
1320
+ for (const key of ["mode", "atime", "mtime", "ctime"]) {
1321
+ if (attr[key] != null) {
1322
+ node[key] = attr[key];
1323
+ }
1324
+ }
1325
+ if (attr.size !== undefined) {
1326
+ MEMFS.resizeFileStorage(node, attr.size);
1327
+ }
1328
+ },
1329
+ lookup(parent, name) {
1330
+ // This error may happen quite a bit. To avoid overhead we reuse it (and
1331
+ // suffer a lack of stack info).
1332
+ if (!MEMFS.doesNotExistError) {
1333
+ MEMFS.doesNotExistError = new FS.ErrnoError(44);
1334
+ /** @suppress {checkTypes} */
1335
+ MEMFS.doesNotExistError.stack = '<generic error, no stack>';
1336
+ }
1337
+ throw MEMFS.doesNotExistError;
1338
+ },
1339
+ mknod(parent, name, mode, dev) {
1340
+ return MEMFS.createNode(parent, name, mode, dev);
1341
+ },
1342
+ rename(old_node, new_dir, new_name) {
1343
+ var new_node;
1344
+ try {
1345
+ new_node = FS.lookupNode(new_dir, new_name);
1346
+ } catch (e) {}
1347
+ if (new_node) {
1348
+ if (FS.isDir(old_node.mode)) {
1349
+ // if we're overwriting a directory at new_name, make sure it's empty.
1350
+ for (var i in new_node.contents) {
1351
+ throw new FS.ErrnoError(55);
1352
+ }
1353
+ }
1354
+ FS.hashRemoveNode(new_node);
1355
+ }
1356
+ // do the internal rewiring
1357
+ delete old_node.parent.contents[old_node.name];
1358
+ new_dir.contents[new_name] = old_node;
1359
+ old_node.name = new_name;
1360
+ new_dir.ctime = new_dir.mtime = old_node.parent.ctime = old_node.parent.mtime = Date.now();
1361
+ },
1362
+ unlink(parent, name) {
1363
+ delete parent.contents[name];
1364
+ parent.ctime = parent.mtime = Date.now();
1365
+ },
1366
+ rmdir(parent, name) {
1367
+ var node = FS.lookupNode(parent, name);
1368
+ for (var i in node.contents) {
1369
+ throw new FS.ErrnoError(55);
1370
+ }
1371
+ delete parent.contents[name];
1372
+ parent.ctime = parent.mtime = Date.now();
1373
+ },
1374
+ readdir(node) {
1375
+ return ['.', '..', ...Object.keys(node.contents)];
1376
+ },
1377
+ symlink(parent, newname, oldpath) {
1378
+ var node = MEMFS.createNode(parent, newname, 0o777 | 40960, 0);
1379
+ node.link = oldpath;
1380
+ return node;
1381
+ },
1382
+ readlink(node) {
1383
+ if (!FS.isLink(node.mode)) {
1384
+ throw new FS.ErrnoError(28);
1385
+ }
1386
+ return node.link;
1387
+ },
1388
+ },
1389
+ stream_ops:{
1390
+ read(stream, buffer, offset, length, position) {
1391
+ var contents = stream.node.contents;
1392
+ if (position >= stream.node.usedBytes) return 0;
1393
+ var size = Math.min(stream.node.usedBytes - position, length);
1394
+ if (size > 8 && contents.subarray) { // non-trivial, and typed array
1395
+ buffer.set(contents.subarray(position, position + size), offset);
1396
+ } else {
1397
+ for (var i = 0; i < size; i++) buffer[offset + i] = contents[position + i];
1398
+ }
1399
+ return size;
1400
+ },
1401
+ write(stream, buffer, offset, length, position, canOwn) {
1402
+ // If the buffer is located in main memory (HEAP), and if
1403
+ // memory can grow, we can't hold on to references of the
1404
+ // memory buffer, as they may get invalidated. That means we
1405
+ // need to copy its contents.
1406
+ if (buffer.buffer === HEAP8.buffer) {
1407
+ canOwn = false;
1408
+ }
1409
+
1410
+ if (!length) return 0;
1411
+ var node = stream.node;
1412
+ node.mtime = node.ctime = Date.now();
1413
+
1414
+ if (buffer.subarray && (!node.contents || node.contents.subarray)) { // This write is from a typed array to a typed array?
1415
+ if (canOwn) {
1416
+ node.contents = buffer.subarray(offset, offset + length);
1417
+ node.usedBytes = length;
1418
+ return length;
1419
+ } 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.
1420
+ node.contents = buffer.slice(offset, offset + length);
1421
+ node.usedBytes = length;
1422
+ return length;
1423
+ } else if (position + length <= node.usedBytes) { // Writing to an already allocated and used subrange of the file?
1424
+ node.contents.set(buffer.subarray(offset, offset + length), position);
1425
+ return length;
1426
+ }
1427
+ }
1428
+
1429
+ // Appending to an existing file and we need to reallocate, or source data did not come as a typed array.
1430
+ MEMFS.expandFileStorage(node, position+length);
1431
+ if (node.contents.subarray && buffer.subarray) {
1432
+ // Use typed array write which is available.
1433
+ node.contents.set(buffer.subarray(offset, offset + length), position);
1434
+ } else {
1435
+ for (var i = 0; i < length; i++) {
1436
+ node.contents[position + i] = buffer[offset + i]; // Or fall back to manual write if not.
1437
+ }
1438
+ }
1439
+ node.usedBytes = Math.max(node.usedBytes, position + length);
1440
+ return length;
1441
+ },
1442
+ llseek(stream, offset, whence) {
1443
+ var position = offset;
1444
+ if (whence === 1) {
1445
+ position += stream.position;
1446
+ } else if (whence === 2) {
1447
+ if (FS.isFile(stream.node.mode)) {
1448
+ position += stream.node.usedBytes;
1449
+ }
1450
+ }
1451
+ if (position < 0) {
1452
+ throw new FS.ErrnoError(28);
1453
+ }
1454
+ return position;
1455
+ },
1456
+ mmap(stream, length, position, prot, flags) {
1457
+ if (!FS.isFile(stream.node.mode)) {
1458
+ throw new FS.ErrnoError(43);
1459
+ }
1460
+ var ptr;
1461
+ var allocated;
1462
+ var contents = stream.node.contents;
1463
+ // Only make a new copy when MAP_PRIVATE is specified.
1464
+ if (!(flags & 2) && contents && contents.buffer === HEAP8.buffer) {
1465
+ // We can't emulate MAP_SHARED when the file is not backed by the
1466
+ // buffer we're mapping to (e.g. the HEAP buffer).
1467
+ allocated = false;
1468
+ ptr = contents.byteOffset;
1469
+ } else {
1470
+ allocated = true;
1471
+ ptr = mmapAlloc(length);
1472
+ if (!ptr) {
1473
+ throw new FS.ErrnoError(48);
1474
+ }
1475
+ if (contents) {
1476
+ // Try to avoid unnecessary slices.
1477
+ if (position > 0 || position + length < contents.length) {
1478
+ if (contents.subarray) {
1479
+ contents = contents.subarray(position, position + length);
1480
+ } else {
1481
+ contents = Array.prototype.slice.call(contents, position, position + length);
1482
+ }
1483
+ }
1484
+ HEAP8.set(contents, ptr);
1485
+ }
1486
+ }
1487
+ return { ptr, allocated };
1488
+ },
1489
+ msync(stream, buffer, offset, length, mmapFlags) {
1490
+ MEMFS.stream_ops.write(stream, buffer, 0, length, offset, false);
1491
+ // should we check if bytesWritten and length are the same?
1492
+ return 0;
1493
+ },
1494
+ },
1495
+ };
1496
+
1497
+ var FS_modeStringToFlags = (str) => {
1498
+ var flagModes = {
1499
+ 'r': 0,
1500
+ 'r+': 2,
1501
+ 'w': 512 | 64 | 1,
1502
+ 'w+': 512 | 64 | 2,
1503
+ 'a': 1024 | 64 | 1,
1504
+ 'a+': 1024 | 64 | 2,
1505
+ };
1506
+ var flags = flagModes[str];
1507
+ if (typeof flags == 'undefined') {
1508
+ throw new Error(`Unknown file open mode: ${str}`);
1509
+ }
1510
+ return flags;
1511
+ };
1512
+
1513
+ var FS_getMode = (canRead, canWrite) => {
1514
+ var mode = 0;
1515
+ if (canRead) mode |= 292 | 73;
1516
+ if (canWrite) mode |= 146;
1517
+ return mode;
1518
+ };
1519
+
1520
+
1521
+ var asyncLoad = async (url) => {
1522
+ var arrayBuffer = await readAsync(url);
1523
+ return new Uint8Array(arrayBuffer);
1524
+ };
1525
+
1526
+
1527
+ var FS_createDataFile = (...args) => FS.createDataFile(...args);
1528
+
1529
+ var getUniqueRunDependency = (id) => {
1530
+ return id;
1531
+ };
1532
+
1533
+
1534
+
1535
+ var preloadPlugins = [];
1536
+ var FS_handledByPreloadPlugin = async (byteArray, fullname) => {
1537
+ // Ensure plugins are ready.
1538
+ if (typeof Browser != 'undefined') Browser.init();
1539
+
1540
+ for (var plugin of preloadPlugins) {
1541
+ if (plugin['canHandle'](fullname)) {
1542
+ return plugin['handle'](byteArray, fullname);
1543
+ }
1544
+ }
1545
+ // If no plugin handled this file then return the original/unmodified
1546
+ // byteArray.
1547
+ return byteArray;
1548
+ };
1549
+ var FS_preloadFile = async (parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish) => {
1550
+ // TODO we should allow people to just pass in a complete filename instead
1551
+ // of parent and name being that we just join them anyways
1552
+ var fullname = name ? PATH_FS.resolve(PATH.join2(parent, name)) : parent;
1553
+ var dep = getUniqueRunDependency(`cp ${fullname}`); // might have several active requests for the same fullname
1554
+ addRunDependency(dep);
1555
+
1556
+ try {
1557
+ var byteArray = url;
1558
+ if (typeof url == 'string') {
1559
+ byteArray = await asyncLoad(url);
1560
+ }
1561
+
1562
+ byteArray = await FS_handledByPreloadPlugin(byteArray, fullname);
1563
+ preFinish?.();
1564
+ if (!dontCreateFile) {
1565
+ FS_createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
1566
+ }
1567
+ } finally {
1568
+ removeRunDependency(dep);
1569
+ }
1570
+ };
1571
+ var FS_createPreloadedFile = (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
1572
+ FS_preloadFile(parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish).then(onload).catch(onerror);
1573
+ };
1574
+ var FS = {
1575
+ root:null,
1576
+ mounts:[],
1577
+ devices:{
1578
+ },
1579
+ streams:[],
1580
+ nextInode:1,
1581
+ nameTable:null,
1582
+ currentPath:"/",
1583
+ initialized:false,
1584
+ ignorePermissions:true,
1585
+ filesystems:null,
1586
+ syncFSRequests:0,
1587
+ readFiles:{
1588
+ },
1589
+ ErrnoError:class {
1590
+ name = 'ErrnoError';
1591
+ // We set the `name` property to be able to identify `FS.ErrnoError`
1592
+ // - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
1593
+ // - when using PROXYFS, an error can come from an underlying FS
1594
+ // as different FS objects have their own FS.ErrnoError each,
1595
+ // the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
1596
+ // we'll use the reliable test `err.name == "ErrnoError"` instead
1597
+ constructor(errno) {
1598
+ this.errno = errno;
1599
+ }
1600
+ },
1601
+ FSStream:class {
1602
+ shared = {};
1603
+ get object() {
1604
+ return this.node;
1605
+ }
1606
+ set object(val) {
1607
+ this.node = val;
1608
+ }
1609
+ get isRead() {
1610
+ return (this.flags & 2097155) !== 1;
1611
+ }
1612
+ get isWrite() {
1613
+ return (this.flags & 2097155) !== 0;
1614
+ }
1615
+ get isAppend() {
1616
+ return (this.flags & 1024);
1617
+ }
1618
+ get flags() {
1619
+ return this.shared.flags;
1620
+ }
1621
+ set flags(val) {
1622
+ this.shared.flags = val;
1623
+ }
1624
+ get position() {
1625
+ return this.shared.position;
1626
+ }
1627
+ set position(val) {
1628
+ this.shared.position = val;
1629
+ }
1630
+ },
1631
+ FSNode:class {
1632
+ node_ops = {};
1633
+ stream_ops = {};
1634
+ readMode = 292 | 73;
1635
+ writeMode = 146;
1636
+ mounted = null;
1637
+ constructor(parent, name, mode, rdev) {
1638
+ if (!parent) {
1639
+ parent = this; // root node sets parent to itself
1640
+ }
1641
+ this.parent = parent;
1642
+ this.mount = parent.mount;
1643
+ this.id = FS.nextInode++;
1644
+ this.name = name;
1645
+ this.mode = mode;
1646
+ this.rdev = rdev;
1647
+ this.atime = this.mtime = this.ctime = Date.now();
1648
+ }
1649
+ get read() {
1650
+ return (this.mode & this.readMode) === this.readMode;
1651
+ }
1652
+ set read(val) {
1653
+ val ? this.mode |= this.readMode : this.mode &= ~this.readMode;
1654
+ }
1655
+ get write() {
1656
+ return (this.mode & this.writeMode) === this.writeMode;
1657
+ }
1658
+ set write(val) {
1659
+ val ? this.mode |= this.writeMode : this.mode &= ~this.writeMode;
1660
+ }
1661
+ get isFolder() {
1662
+ return FS.isDir(this.mode);
1663
+ }
1664
+ get isDevice() {
1665
+ return FS.isChrdev(this.mode);
1666
+ }
1667
+ },
1668
+ lookupPath(path, opts = {}) {
1669
+ if (!path) {
1670
+ throw new FS.ErrnoError(44);
1671
+ }
1672
+ opts.follow_mount ??= true
1673
+
1674
+ if (!PATH.isAbs(path)) {
1675
+ path = FS.cwd() + '/' + path;
1676
+ }
1677
+
1678
+ // limit max consecutive symlinks to 40 (SYMLOOP_MAX).
1679
+ linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) {
1680
+ // split the absolute path
1681
+ var parts = path.split('/').filter((p) => !!p);
1682
+
1683
+ // start at the root
1684
+ var current = FS.root;
1685
+ var current_path = '/';
1686
+
1687
+ for (var i = 0; i < parts.length; i++) {
1688
+ var islast = (i === parts.length-1);
1689
+ if (islast && opts.parent) {
1690
+ // stop resolving
1691
+ break;
1692
+ }
1693
+
1694
+ if (parts[i] === '.') {
1695
+ continue;
1696
+ }
1697
+
1698
+ if (parts[i] === '..') {
1699
+ current_path = PATH.dirname(current_path);
1700
+ if (FS.isRoot(current)) {
1701
+ path = current_path + '/' + parts.slice(i + 1).join('/');
1702
+ // We're making progress here, don't let many consecutive ..'s
1703
+ // lead to ELOOP
1704
+ nlinks--;
1705
+ continue linkloop;
1706
+ } else {
1707
+ current = current.parent;
1708
+ }
1709
+ continue;
1710
+ }
1711
+
1712
+ current_path = PATH.join2(current_path, parts[i]);
1713
+ try {
1714
+ current = FS.lookupNode(current, parts[i]);
1715
+ } catch (e) {
1716
+ // if noent_okay is true, suppress a ENOENT in the last component
1717
+ // and return an object with an undefined node. This is needed for
1718
+ // resolving symlinks in the path when creating a file.
1719
+ if ((e?.errno === 44) && islast && opts.noent_okay) {
1720
+ return { path: current_path };
1721
+ }
1722
+ throw e;
1723
+ }
1724
+
1725
+ // jump to the mount's root node if this is a mountpoint
1726
+ if (FS.isMountpoint(current) && (!islast || opts.follow_mount)) {
1727
+ current = current.mounted.root;
1728
+ }
1729
+
1730
+ // by default, lookupPath will not follow a symlink if it is the final path component.
1731
+ // setting opts.follow = true will override this behavior.
1732
+ if (FS.isLink(current.mode) && (!islast || opts.follow)) {
1733
+ if (!current.node_ops.readlink) {
1734
+ throw new FS.ErrnoError(52);
1735
+ }
1736
+ var link = current.node_ops.readlink(current);
1737
+ if (!PATH.isAbs(link)) {
1738
+ link = PATH.dirname(current_path) + '/' + link;
1739
+ }
1740
+ path = link + '/' + parts.slice(i + 1).join('/');
1741
+ continue linkloop;
1742
+ }
1743
+ }
1744
+ return { path: current_path, node: current };
1745
+ }
1746
+ throw new FS.ErrnoError(32);
1747
+ },
1748
+ getPath(node) {
1749
+ var path;
1750
+ while (true) {
1751
+ if (FS.isRoot(node)) {
1752
+ var mount = node.mount.mountpoint;
1753
+ if (!path) return mount;
1754
+ return mount[mount.length-1] !== '/' ? `${mount}/${path}` : mount + path;
1755
+ }
1756
+ path = path ? `${node.name}/${path}` : node.name;
1757
+ node = node.parent;
1758
+ }
1759
+ },
1760
+ hashName(parentid, name) {
1761
+ var hash = 0;
1762
+
1763
+ for (var i = 0; i < name.length; i++) {
1764
+ hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0;
1765
+ }
1766
+ return ((parentid + hash) >>> 0) % FS.nameTable.length;
1767
+ },
1768
+ hashAddNode(node) {
1769
+ var hash = FS.hashName(node.parent.id, node.name);
1770
+ node.name_next = FS.nameTable[hash];
1771
+ FS.nameTable[hash] = node;
1772
+ },
1773
+ hashRemoveNode(node) {
1774
+ var hash = FS.hashName(node.parent.id, node.name);
1775
+ if (FS.nameTable[hash] === node) {
1776
+ FS.nameTable[hash] = node.name_next;
1777
+ } else {
1778
+ var current = FS.nameTable[hash];
1779
+ while (current) {
1780
+ if (current.name_next === node) {
1781
+ current.name_next = node.name_next;
1782
+ break;
1783
+ }
1784
+ current = current.name_next;
1785
+ }
1786
+ }
1787
+ },
1788
+ lookupNode(parent, name) {
1789
+ var errCode = FS.mayLookup(parent);
1790
+ if (errCode) {
1791
+ throw new FS.ErrnoError(errCode);
1792
+ }
1793
+ var hash = FS.hashName(parent.id, name);
1794
+ for (var node = FS.nameTable[hash]; node; node = node.name_next) {
1795
+ var nodeName = node.name;
1796
+ if (node.parent.id === parent.id && nodeName === name) {
1797
+ return node;
1798
+ }
1799
+ }
1800
+ // if we failed to find it in the cache, call into the VFS
1801
+ return FS.lookup(parent, name);
1802
+ },
1803
+ createNode(parent, name, mode, rdev) {
1804
+ var node = new FS.FSNode(parent, name, mode, rdev);
1805
+
1806
+ FS.hashAddNode(node);
1807
+
1808
+ return node;
1809
+ },
1810
+ destroyNode(node) {
1811
+ FS.hashRemoveNode(node);
1812
+ },
1813
+ isRoot(node) {
1814
+ return node === node.parent;
1815
+ },
1816
+ isMountpoint(node) {
1817
+ return !!node.mounted;
1818
+ },
1819
+ isFile(mode) {
1820
+ return (mode & 61440) === 32768;
1821
+ },
1822
+ isDir(mode) {
1823
+ return (mode & 61440) === 16384;
1824
+ },
1825
+ isLink(mode) {
1826
+ return (mode & 61440) === 40960;
1827
+ },
1828
+ isChrdev(mode) {
1829
+ return (mode & 61440) === 8192;
1830
+ },
1831
+ isBlkdev(mode) {
1832
+ return (mode & 61440) === 24576;
1833
+ },
1834
+ isFIFO(mode) {
1835
+ return (mode & 61440) === 4096;
1836
+ },
1837
+ isSocket(mode) {
1838
+ return (mode & 49152) === 49152;
1839
+ },
1840
+ flagsToPermissionString(flag) {
1841
+ var perms = ['r', 'w', 'rw'][flag & 3];
1842
+ if ((flag & 512)) {
1843
+ perms += 'w';
1844
+ }
1845
+ return perms;
1846
+ },
1847
+ nodePermissions(node, perms) {
1848
+ if (FS.ignorePermissions) {
1849
+ return 0;
1850
+ }
1851
+ // return 0 if any user, group or owner bits are set.
1852
+ if (perms.includes('r') && !(node.mode & 292)) {
1853
+ return 2;
1854
+ } else if (perms.includes('w') && !(node.mode & 146)) {
1855
+ return 2;
1856
+ } else if (perms.includes('x') && !(node.mode & 73)) {
1857
+ return 2;
1858
+ }
1859
+ return 0;
1860
+ },
1861
+ mayLookup(dir) {
1862
+ if (!FS.isDir(dir.mode)) return 54;
1863
+ var errCode = FS.nodePermissions(dir, 'x');
1864
+ if (errCode) return errCode;
1865
+ if (!dir.node_ops.lookup) return 2;
1866
+ return 0;
1867
+ },
1868
+ mayCreate(dir, name) {
1869
+ if (!FS.isDir(dir.mode)) {
1870
+ return 54;
1871
+ }
1872
+ try {
1873
+ var node = FS.lookupNode(dir, name);
1874
+ return 20;
1875
+ } catch (e) {
1876
+ }
1877
+ return FS.nodePermissions(dir, 'wx');
1878
+ },
1879
+ mayDelete(dir, name, isdir) {
1880
+ var node;
1881
+ try {
1882
+ node = FS.lookupNode(dir, name);
1883
+ } catch (e) {
1884
+ return e.errno;
1885
+ }
1886
+ var errCode = FS.nodePermissions(dir, 'wx');
1887
+ if (errCode) {
1888
+ return errCode;
1889
+ }
1890
+ if (isdir) {
1891
+ if (!FS.isDir(node.mode)) {
1892
+ return 54;
1893
+ }
1894
+ if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) {
1895
+ return 10;
1896
+ }
1897
+ } else {
1898
+ if (FS.isDir(node.mode)) {
1899
+ return 31;
1900
+ }
1901
+ }
1902
+ return 0;
1903
+ },
1904
+ mayOpen(node, flags) {
1905
+ if (!node) {
1906
+ return 44;
1907
+ }
1908
+ if (FS.isLink(node.mode)) {
1909
+ return 32;
1910
+ } else if (FS.isDir(node.mode)) {
1911
+ if (FS.flagsToPermissionString(flags) !== 'r' // opening for write
1912
+ || (flags & (512 | 64))) { // TODO: check for O_SEARCH? (== search for dir only)
1913
+ return 31;
1914
+ }
1915
+ }
1916
+ return FS.nodePermissions(node, FS.flagsToPermissionString(flags));
1917
+ },
1918
+ checkOpExists(op, err) {
1919
+ if (!op) {
1920
+ throw new FS.ErrnoError(err);
1921
+ }
1922
+ return op;
1923
+ },
1924
+ MAX_OPEN_FDS:4096,
1925
+ nextfd() {
1926
+ for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) {
1927
+ if (!FS.streams[fd]) {
1928
+ return fd;
1929
+ }
1930
+ }
1931
+ throw new FS.ErrnoError(33);
1932
+ },
1933
+ getStreamChecked(fd) {
1934
+ var stream = FS.getStream(fd);
1935
+ if (!stream) {
1936
+ throw new FS.ErrnoError(8);
1937
+ }
1938
+ return stream;
1939
+ },
1940
+ getStream:(fd) => FS.streams[fd],
1941
+ createStream(stream, fd = -1) {
1942
+
1943
+ // clone it, so we can return an instance of FSStream
1944
+ stream = Object.assign(new FS.FSStream(), stream);
1945
+ if (fd == -1) {
1946
+ fd = FS.nextfd();
1947
+ }
1948
+ stream.fd = fd;
1949
+ FS.streams[fd] = stream;
1950
+ return stream;
1951
+ },
1952
+ closeStream(fd) {
1953
+ FS.streams[fd] = null;
1954
+ },
1955
+ dupStream(origStream, fd = -1) {
1956
+ var stream = FS.createStream(origStream, fd);
1957
+ stream.stream_ops?.dup?.(stream);
1958
+ return stream;
1959
+ },
1960
+ doSetAttr(stream, node, attr) {
1961
+ var setattr = stream?.stream_ops.setattr;
1962
+ var arg = setattr ? stream : node;
1963
+ setattr ??= node.node_ops.setattr;
1964
+ FS.checkOpExists(setattr, 63)
1965
+ setattr(arg, attr);
1966
+ },
1967
+ chrdev_stream_ops:{
1968
+ open(stream) {
1969
+ var device = FS.getDevice(stream.node.rdev);
1970
+ // override node's stream ops with the device's
1971
+ stream.stream_ops = device.stream_ops;
1972
+ // forward the open call
1973
+ stream.stream_ops.open?.(stream);
1974
+ },
1975
+ llseek() {
1976
+ throw new FS.ErrnoError(70);
1977
+ },
1978
+ },
1979
+ major:(dev) => ((dev) >> 8),
1980
+ minor:(dev) => ((dev) & 0xff),
1981
+ makedev:(ma, mi) => ((ma) << 8 | (mi)),
1982
+ registerDevice(dev, ops) {
1983
+ FS.devices[dev] = { stream_ops: ops };
1984
+ },
1985
+ getDevice:(dev) => FS.devices[dev],
1986
+ getMounts(mount) {
1987
+ var mounts = [];
1988
+ var check = [mount];
1989
+
1990
+ while (check.length) {
1991
+ var m = check.pop();
1992
+
1993
+ mounts.push(m);
1994
+
1995
+ check.push(...m.mounts);
1996
+ }
1997
+
1998
+ return mounts;
1999
+ },
2000
+ syncfs(populate, callback) {
2001
+ if (typeof populate == 'function') {
2002
+ callback = populate;
2003
+ populate = false;
2004
+ }
2005
+
2006
+ FS.syncFSRequests++;
2007
+
2008
+ if (FS.syncFSRequests > 1) {
2009
+ err(`warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work`);
2010
+ }
2011
+
2012
+ var mounts = FS.getMounts(FS.root.mount);
2013
+ var completed = 0;
2014
+
2015
+ function doCallback(errCode) {
2016
+ FS.syncFSRequests--;
2017
+ return callback(errCode);
2018
+ }
2019
+
2020
+ function done(errCode) {
2021
+ if (errCode) {
2022
+ if (!done.errored) {
2023
+ done.errored = true;
2024
+ return doCallback(errCode);
2025
+ }
2026
+ return;
2027
+ }
2028
+ if (++completed >= mounts.length) {
2029
+ doCallback(null);
2030
+ }
2031
+ };
2032
+
2033
+ // sync all mounts
2034
+ for (var mount of mounts) {
2035
+ if (mount.type.syncfs) {
2036
+ mount.type.syncfs(mount, populate, done);
2037
+ } else {
2038
+ done(null);
2039
+ }
2040
+ }
2041
+ },
2042
+ mount(type, opts, mountpoint) {
2043
+ var root = mountpoint === '/';
2044
+ var pseudo = !mountpoint;
2045
+ var node;
2046
+
2047
+ if (root && FS.root) {
2048
+ throw new FS.ErrnoError(10);
2049
+ } else if (!root && !pseudo) {
2050
+ var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
2051
+
2052
+ mountpoint = lookup.path; // use the absolute path
2053
+ node = lookup.node;
2054
+
2055
+ if (FS.isMountpoint(node)) {
2056
+ throw new FS.ErrnoError(10);
2057
+ }
2058
+
2059
+ if (!FS.isDir(node.mode)) {
2060
+ throw new FS.ErrnoError(54);
2061
+ }
2062
+ }
2063
+
2064
+ var mount = {
2065
+ type,
2066
+ opts,
2067
+ mountpoint,
2068
+ mounts: []
2069
+ };
2070
+
2071
+ // create a root node for the fs
2072
+ var mountRoot = type.mount(mount);
2073
+ mountRoot.mount = mount;
2074
+ mount.root = mountRoot;
2075
+
2076
+ if (root) {
2077
+ FS.root = mountRoot;
2078
+ } else if (node) {
2079
+ // set as a mountpoint
2080
+ node.mounted = mount;
2081
+
2082
+ // add the new mount to the current mount's children
2083
+ if (node.mount) {
2084
+ node.mount.mounts.push(mount);
2085
+ }
2086
+ }
2087
+
2088
+ return mountRoot;
2089
+ },
2090
+ unmount(mountpoint) {
2091
+ var lookup = FS.lookupPath(mountpoint, { follow_mount: false });
2092
+
2093
+ if (!FS.isMountpoint(lookup.node)) {
2094
+ throw new FS.ErrnoError(28);
2095
+ }
2096
+
2097
+ // destroy the nodes for this mount, and all its child mounts
2098
+ var node = lookup.node;
2099
+ var mount = node.mounted;
2100
+ var mounts = FS.getMounts(mount);
2101
+
2102
+ for (var [hash, current] of Object.entries(FS.nameTable)) {
2103
+ while (current) {
2104
+ var next = current.name_next;
2105
+
2106
+ if (mounts.includes(current.mount)) {
2107
+ FS.destroyNode(current);
2108
+ }
2109
+
2110
+ current = next;
2111
+ }
2112
+ }
2113
+
2114
+ // no longer a mountpoint
2115
+ node.mounted = null;
2116
+
2117
+ // remove this mount from the child mounts
2118
+ var idx = node.mount.mounts.indexOf(mount);
2119
+ node.mount.mounts.splice(idx, 1);
2120
+ },
2121
+ lookup(parent, name) {
2122
+ return parent.node_ops.lookup(parent, name);
2123
+ },
2124
+ mknod(path, mode, dev) {
2125
+ var lookup = FS.lookupPath(path, { parent: true });
2126
+ var parent = lookup.node;
2127
+ var name = PATH.basename(path);
2128
+ if (!name) {
2129
+ throw new FS.ErrnoError(28);
2130
+ }
2131
+ if (name === '.' || name === '..') {
2132
+ throw new FS.ErrnoError(20);
2133
+ }
2134
+ var errCode = FS.mayCreate(parent, name);
2135
+ if (errCode) {
2136
+ throw new FS.ErrnoError(errCode);
2137
+ }
2138
+ if (!parent.node_ops.mknod) {
2139
+ throw new FS.ErrnoError(63);
2140
+ }
2141
+ return parent.node_ops.mknod(parent, name, mode, dev);
2142
+ },
2143
+ statfs(path) {
2144
+ return FS.statfsNode(FS.lookupPath(path, {follow: true}).node);
2145
+ },
2146
+ statfsStream(stream) {
2147
+ // We keep a separate statfsStream function because noderawfs overrides
2148
+ // it. In noderawfs, stream.node is sometimes null. Instead, we need to
2149
+ // look at stream.path.
2150
+ return FS.statfsNode(stream.node);
2151
+ },
2152
+ statfsNode(node) {
2153
+ // NOTE: None of the defaults here are true. We're just returning safe and
2154
+ // sane values. Currently nodefs and rawfs replace these defaults,
2155
+ // other file systems leave them alone.
2156
+ var rtn = {
2157
+ bsize: 4096,
2158
+ frsize: 4096,
2159
+ blocks: 1e6,
2160
+ bfree: 5e5,
2161
+ bavail: 5e5,
2162
+ files: FS.nextInode,
2163
+ ffree: FS.nextInode - 1,
2164
+ fsid: 42,
2165
+ flags: 2,
2166
+ namelen: 255,
2167
+ };
2168
+
2169
+ if (node.node_ops.statfs) {
2170
+ Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root));
2171
+ }
2172
+ return rtn;
2173
+ },
2174
+ create(path, mode = 0o666) {
2175
+ mode &= 4095;
2176
+ mode |= 32768;
2177
+ return FS.mknod(path, mode, 0);
2178
+ },
2179
+ mkdir(path, mode = 0o777) {
2180
+ mode &= 511 | 512;
2181
+ mode |= 16384;
2182
+ return FS.mknod(path, mode, 0);
2183
+ },
2184
+ mkdirTree(path, mode) {
2185
+ var dirs = path.split('/');
2186
+ var d = '';
2187
+ for (var dir of dirs) {
2188
+ if (!dir) continue;
2189
+ if (d || PATH.isAbs(path)) d += '/';
2190
+ d += dir;
2191
+ try {
2192
+ FS.mkdir(d, mode);
2193
+ } catch(e) {
2194
+ if (e.errno != 20) throw e;
2195
+ }
2196
+ }
2197
+ },
2198
+ mkdev(path, mode, dev) {
2199
+ if (typeof dev == 'undefined') {
2200
+ dev = mode;
2201
+ mode = 0o666;
2202
+ }
2203
+ mode |= 8192;
2204
+ return FS.mknod(path, mode, dev);
2205
+ },
2206
+ symlink(oldpath, newpath) {
2207
+ if (!PATH_FS.resolve(oldpath)) {
2208
+ throw new FS.ErrnoError(44);
2209
+ }
2210
+ var lookup = FS.lookupPath(newpath, { parent: true });
2211
+ var parent = lookup.node;
2212
+ if (!parent) {
2213
+ throw new FS.ErrnoError(44);
2214
+ }
2215
+ var newname = PATH.basename(newpath);
2216
+ var errCode = FS.mayCreate(parent, newname);
2217
+ if (errCode) {
2218
+ throw new FS.ErrnoError(errCode);
2219
+ }
2220
+ if (!parent.node_ops.symlink) {
2221
+ throw new FS.ErrnoError(63);
2222
+ }
2223
+ return parent.node_ops.symlink(parent, newname, oldpath);
2224
+ },
2225
+ rename(old_path, new_path) {
2226
+ var old_dirname = PATH.dirname(old_path);
2227
+ var new_dirname = PATH.dirname(new_path);
2228
+ var old_name = PATH.basename(old_path);
2229
+ var new_name = PATH.basename(new_path);
2230
+ // parents must exist
2231
+ var lookup, old_dir, new_dir;
2232
+
2233
+ // let the errors from non existent directories percolate up
2234
+ lookup = FS.lookupPath(old_path, { parent: true });
2235
+ old_dir = lookup.node;
2236
+ lookup = FS.lookupPath(new_path, { parent: true });
2237
+ new_dir = lookup.node;
2238
+
2239
+ if (!old_dir || !new_dir) throw new FS.ErrnoError(44);
2240
+ // need to be part of the same mount
2241
+ if (old_dir.mount !== new_dir.mount) {
2242
+ throw new FS.ErrnoError(75);
2243
+ }
2244
+ // source must exist
2245
+ var old_node = FS.lookupNode(old_dir, old_name);
2246
+ // old path should not be an ancestor of the new path
2247
+ var relative = PATH_FS.relative(old_path, new_dirname);
2248
+ if (relative.charAt(0) !== '.') {
2249
+ throw new FS.ErrnoError(28);
2250
+ }
2251
+ // new path should not be an ancestor of the old path
2252
+ relative = PATH_FS.relative(new_path, old_dirname);
2253
+ if (relative.charAt(0) !== '.') {
2254
+ throw new FS.ErrnoError(55);
2255
+ }
2256
+ // see if the new path already exists
2257
+ var new_node;
2258
+ try {
2259
+ new_node = FS.lookupNode(new_dir, new_name);
2260
+ } catch (e) {
2261
+ // not fatal
2262
+ }
2263
+ // early out if nothing needs to change
2264
+ if (old_node === new_node) {
2265
+ return;
2266
+ }
2267
+ // we'll need to delete the old entry
2268
+ var isdir = FS.isDir(old_node.mode);
2269
+ var errCode = FS.mayDelete(old_dir, old_name, isdir);
2270
+ if (errCode) {
2271
+ throw new FS.ErrnoError(errCode);
2272
+ }
2273
+ // need delete permissions if we'll be overwriting.
2274
+ // need create permissions if new doesn't already exist.
2275
+ errCode = new_node ?
2276
+ FS.mayDelete(new_dir, new_name, isdir) :
2277
+ FS.mayCreate(new_dir, new_name);
2278
+ if (errCode) {
2279
+ throw new FS.ErrnoError(errCode);
2280
+ }
2281
+ if (!old_dir.node_ops.rename) {
2282
+ throw new FS.ErrnoError(63);
2283
+ }
2284
+ if (FS.isMountpoint(old_node) || (new_node && FS.isMountpoint(new_node))) {
2285
+ throw new FS.ErrnoError(10);
2286
+ }
2287
+ // if we are going to change the parent, check write permissions
2288
+ if (new_dir !== old_dir) {
2289
+ errCode = FS.nodePermissions(old_dir, 'w');
2290
+ if (errCode) {
2291
+ throw new FS.ErrnoError(errCode);
2292
+ }
2293
+ }
2294
+ // remove the node from the lookup hash
2295
+ FS.hashRemoveNode(old_node);
2296
+ // do the underlying fs rename
2297
+ try {
2298
+ old_dir.node_ops.rename(old_node, new_dir, new_name);
2299
+ // update old node (we do this here to avoid each backend
2300
+ // needing to)
2301
+ old_node.parent = new_dir;
2302
+ } catch (e) {
2303
+ throw e;
2304
+ } finally {
2305
+ // add the node back to the hash (in case node_ops.rename
2306
+ // changed its name)
2307
+ FS.hashAddNode(old_node);
2308
+ }
2309
+ },
2310
+ rmdir(path) {
2311
+ var lookup = FS.lookupPath(path, { parent: true });
2312
+ var parent = lookup.node;
2313
+ var name = PATH.basename(path);
2314
+ var node = FS.lookupNode(parent, name);
2315
+ var errCode = FS.mayDelete(parent, name, true);
2316
+ if (errCode) {
2317
+ throw new FS.ErrnoError(errCode);
2318
+ }
2319
+ if (!parent.node_ops.rmdir) {
2320
+ throw new FS.ErrnoError(63);
2321
+ }
2322
+ if (FS.isMountpoint(node)) {
2323
+ throw new FS.ErrnoError(10);
2324
+ }
2325
+ parent.node_ops.rmdir(parent, name);
2326
+ FS.destroyNode(node);
2327
+ },
2328
+ readdir(path) {
2329
+ var lookup = FS.lookupPath(path, { follow: true });
2330
+ var node = lookup.node;
2331
+ var readdir = FS.checkOpExists(node.node_ops.readdir, 54);
2332
+ return readdir(node);
2333
+ },
2334
+ unlink(path) {
2335
+ var lookup = FS.lookupPath(path, { parent: true });
2336
+ var parent = lookup.node;
2337
+ if (!parent) {
2338
+ throw new FS.ErrnoError(44);
2339
+ }
2340
+ var name = PATH.basename(path);
2341
+ var node = FS.lookupNode(parent, name);
2342
+ var errCode = FS.mayDelete(parent, name, false);
2343
+ if (errCode) {
2344
+ // According to POSIX, we should map EISDIR to EPERM, but
2345
+ // we instead do what Linux does (and we must, as we use
2346
+ // the musl linux libc).
2347
+ throw new FS.ErrnoError(errCode);
2348
+ }
2349
+ if (!parent.node_ops.unlink) {
2350
+ throw new FS.ErrnoError(63);
2351
+ }
2352
+ if (FS.isMountpoint(node)) {
2353
+ throw new FS.ErrnoError(10);
2354
+ }
2355
+ parent.node_ops.unlink(parent, name);
2356
+ FS.destroyNode(node);
2357
+ },
2358
+ readlink(path) {
2359
+ var lookup = FS.lookupPath(path);
2360
+ var link = lookup.node;
2361
+ if (!link) {
2362
+ throw new FS.ErrnoError(44);
2363
+ }
2364
+ if (!link.node_ops.readlink) {
2365
+ throw new FS.ErrnoError(28);
2366
+ }
2367
+ return link.node_ops.readlink(link);
2368
+ },
2369
+ stat(path, dontFollow) {
2370
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2371
+ var node = lookup.node;
2372
+ var getattr = FS.checkOpExists(node.node_ops.getattr, 63);
2373
+ return getattr(node);
2374
+ },
2375
+ fstat(fd) {
2376
+ var stream = FS.getStreamChecked(fd);
2377
+ var node = stream.node;
2378
+ var getattr = stream.stream_ops.getattr;
2379
+ var arg = getattr ? stream : node;
2380
+ getattr ??= node.node_ops.getattr;
2381
+ FS.checkOpExists(getattr, 63)
2382
+ return getattr(arg);
2383
+ },
2384
+ lstat(path) {
2385
+ return FS.stat(path, true);
2386
+ },
2387
+ doChmod(stream, node, mode, dontFollow) {
2388
+ FS.doSetAttr(stream, node, {
2389
+ mode: (mode & 4095) | (node.mode & ~4095),
2390
+ ctime: Date.now(),
2391
+ dontFollow
2392
+ });
2393
+ },
2394
+ chmod(path, mode, dontFollow) {
2395
+ var node;
2396
+ if (typeof path == 'string') {
2397
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2398
+ node = lookup.node;
2399
+ } else {
2400
+ node = path;
2401
+ }
2402
+ FS.doChmod(null, node, mode, dontFollow);
2403
+ },
2404
+ lchmod(path, mode) {
2405
+ FS.chmod(path, mode, true);
2406
+ },
2407
+ fchmod(fd, mode) {
2408
+ var stream = FS.getStreamChecked(fd);
2409
+ FS.doChmod(stream, stream.node, mode, false);
2410
+ },
2411
+ doChown(stream, node, dontFollow) {
2412
+ FS.doSetAttr(stream, node, {
2413
+ timestamp: Date.now(),
2414
+ dontFollow
2415
+ // we ignore the uid / gid for now
2416
+ });
2417
+ },
2418
+ chown(path, uid, gid, dontFollow) {
2419
+ var node;
2420
+ if (typeof path == 'string') {
2421
+ var lookup = FS.lookupPath(path, { follow: !dontFollow });
2422
+ node = lookup.node;
2423
+ } else {
2424
+ node = path;
2425
+ }
2426
+ FS.doChown(null, node, dontFollow);
2427
+ },
2428
+ lchown(path, uid, gid) {
2429
+ FS.chown(path, uid, gid, true);
2430
+ },
2431
+ fchown(fd, uid, gid) {
2432
+ var stream = FS.getStreamChecked(fd);
2433
+ FS.doChown(stream, stream.node, false);
2434
+ },
2435
+ doTruncate(stream, node, len) {
2436
+ if (FS.isDir(node.mode)) {
2437
+ throw new FS.ErrnoError(31);
2438
+ }
2439
+ if (!FS.isFile(node.mode)) {
2440
+ throw new FS.ErrnoError(28);
2441
+ }
2442
+ var errCode = FS.nodePermissions(node, 'w');
2443
+ if (errCode) {
2444
+ throw new FS.ErrnoError(errCode);
2445
+ }
2446
+ FS.doSetAttr(stream, node, {
2447
+ size: len,
2448
+ timestamp: Date.now()
2449
+ });
2450
+ },
2451
+ truncate(path, len) {
2452
+ if (len < 0) {
2453
+ throw new FS.ErrnoError(28);
2454
+ }
2455
+ var node;
2456
+ if (typeof path == 'string') {
2457
+ var lookup = FS.lookupPath(path, { follow: true });
2458
+ node = lookup.node;
2459
+ } else {
2460
+ node = path;
2461
+ }
2462
+ FS.doTruncate(null, node, len);
2463
+ },
2464
+ ftruncate(fd, len) {
2465
+ var stream = FS.getStreamChecked(fd);
2466
+ if (len < 0 || (stream.flags & 2097155) === 0) {
2467
+ throw new FS.ErrnoError(28);
2468
+ }
2469
+ FS.doTruncate(stream, stream.node, len);
2470
+ },
2471
+ utime(path, atime, mtime) {
2472
+ var lookup = FS.lookupPath(path, { follow: true });
2473
+ var node = lookup.node;
2474
+ var setattr = FS.checkOpExists(node.node_ops.setattr, 63);
2475
+ setattr(node, {
2476
+ atime: atime,
2477
+ mtime: mtime
2478
+ });
2479
+ },
2480
+ open(path, flags, mode = 0o666) {
2481
+ if (path === "") {
2482
+ throw new FS.ErrnoError(44);
2483
+ }
2484
+ flags = typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags;
2485
+ if ((flags & 64)) {
2486
+ mode = (mode & 4095) | 32768;
2487
+ } else {
2488
+ mode = 0;
2489
+ }
2490
+ var node;
2491
+ var isDirPath;
2492
+ if (typeof path == 'object') {
2493
+ node = path;
2494
+ } else {
2495
+ isDirPath = path.endsWith("/");
2496
+ // noent_okay makes it so that if the final component of the path
2497
+ // doesn't exist, lookupPath returns `node: undefined`. `path` will be
2498
+ // updated to point to the target of all symlinks.
2499
+ var lookup = FS.lookupPath(path, {
2500
+ follow: !(flags & 131072),
2501
+ noent_okay: true
2502
+ });
2503
+ node = lookup.node;
2504
+ path = lookup.path;
2505
+ }
2506
+ // perhaps we need to create the node
2507
+ var created = false;
2508
+ if ((flags & 64)) {
2509
+ if (node) {
2510
+ // if O_CREAT and O_EXCL are set, error out if the node already exists
2511
+ if ((flags & 128)) {
2512
+ throw new FS.ErrnoError(20);
2513
+ }
2514
+ } else if (isDirPath) {
2515
+ throw new FS.ErrnoError(31);
2516
+ } else {
2517
+ // node doesn't exist, try to create it
2518
+ // Ignore the permission bits here to ensure we can `open` this new
2519
+ // file below. We use chmod below to apply the permissions once the
2520
+ // file is open.
2521
+ node = FS.mknod(path, mode | 0o777, 0);
2522
+ created = true;
2523
+ }
2524
+ }
2525
+ if (!node) {
2526
+ throw new FS.ErrnoError(44);
2527
+ }
2528
+ // can't truncate a device
2529
+ if (FS.isChrdev(node.mode)) {
2530
+ flags &= ~512;
2531
+ }
2532
+ // if asked only for a directory, then this must be one
2533
+ if ((flags & 65536) && !FS.isDir(node.mode)) {
2534
+ throw new FS.ErrnoError(54);
2535
+ }
2536
+ // check permissions, if this is not a file we just created now (it is ok to
2537
+ // create and write to a file with read-only permissions; it is read-only
2538
+ // for later use)
2539
+ if (!created) {
2540
+ var errCode = FS.mayOpen(node, flags);
2541
+ if (errCode) {
2542
+ throw new FS.ErrnoError(errCode);
2543
+ }
2544
+ }
2545
+ // do truncation if necessary
2546
+ if ((flags & 512) && !created) {
2547
+ FS.truncate(node, 0);
2548
+ }
2549
+ // we've already handled these, don't pass down to the underlying vfs
2550
+ flags &= ~(128 | 512 | 131072);
2551
+
2552
+ // register the stream with the filesystem
2553
+ var stream = FS.createStream({
2554
+ node,
2555
+ path: FS.getPath(node), // we want the absolute path to the node
2556
+ flags,
2557
+ seekable: true,
2558
+ position: 0,
2559
+ stream_ops: node.stream_ops,
2560
+ // used by the file family libc calls (fopen, fwrite, ferror, etc.)
2561
+ ungotten: [],
2562
+ error: false
2563
+ });
2564
+ // call the new stream's open function
2565
+ if (stream.stream_ops.open) {
2566
+ stream.stream_ops.open(stream);
2567
+ }
2568
+ if (created) {
2569
+ FS.chmod(node, mode & 0o777);
2570
+ }
2571
+ if (Module['logReadFiles'] && !(flags & 1)) {
2572
+ if (!(path in FS.readFiles)) {
2573
+ FS.readFiles[path] = 1;
2574
+ }
2575
+ }
2576
+ return stream;
2577
+ },
2578
+ close(stream) {
2579
+ if (FS.isClosed(stream)) {
2580
+ throw new FS.ErrnoError(8);
2581
+ }
2582
+ if (stream.getdents) stream.getdents = null; // free readdir state
2583
+ try {
2584
+ if (stream.stream_ops.close) {
2585
+ stream.stream_ops.close(stream);
2586
+ }
2587
+ } catch (e) {
2588
+ throw e;
2589
+ } finally {
2590
+ FS.closeStream(stream.fd);
2591
+ }
2592
+ stream.fd = null;
2593
+ },
2594
+ isClosed(stream) {
2595
+ return stream.fd === null;
2596
+ },
2597
+ llseek(stream, offset, whence) {
2598
+ if (FS.isClosed(stream)) {
2599
+ throw new FS.ErrnoError(8);
2600
+ }
2601
+ if (!stream.seekable || !stream.stream_ops.llseek) {
2602
+ throw new FS.ErrnoError(70);
2603
+ }
2604
+ if (whence != 0 && whence != 1 && whence != 2) {
2605
+ throw new FS.ErrnoError(28);
2606
+ }
2607
+ stream.position = stream.stream_ops.llseek(stream, offset, whence);
2608
+ stream.ungotten = [];
2609
+ return stream.position;
2610
+ },
2611
+ read(stream, buffer, offset, length, position) {
2612
+ if (length < 0 || position < 0) {
2613
+ throw new FS.ErrnoError(28);
2614
+ }
2615
+ if (FS.isClosed(stream)) {
2616
+ throw new FS.ErrnoError(8);
2617
+ }
2618
+ if ((stream.flags & 2097155) === 1) {
2619
+ throw new FS.ErrnoError(8);
2620
+ }
2621
+ if (FS.isDir(stream.node.mode)) {
2622
+ throw new FS.ErrnoError(31);
2623
+ }
2624
+ if (!stream.stream_ops.read) {
2625
+ throw new FS.ErrnoError(28);
2626
+ }
2627
+ var seeking = typeof position != 'undefined';
2628
+ if (!seeking) {
2629
+ position = stream.position;
2630
+ } else if (!stream.seekable) {
2631
+ throw new FS.ErrnoError(70);
2632
+ }
2633
+ var bytesRead = stream.stream_ops.read(stream, buffer, offset, length, position);
2634
+ if (!seeking) stream.position += bytesRead;
2635
+ return bytesRead;
2636
+ },
2637
+ write(stream, buffer, offset, length, position, canOwn) {
2638
+ if (length < 0 || position < 0) {
2639
+ throw new FS.ErrnoError(28);
2640
+ }
2641
+ if (FS.isClosed(stream)) {
2642
+ throw new FS.ErrnoError(8);
2643
+ }
2644
+ if ((stream.flags & 2097155) === 0) {
2645
+ throw new FS.ErrnoError(8);
2646
+ }
2647
+ if (FS.isDir(stream.node.mode)) {
2648
+ throw new FS.ErrnoError(31);
2649
+ }
2650
+ if (!stream.stream_ops.write) {
2651
+ throw new FS.ErrnoError(28);
2652
+ }
2653
+ if (stream.seekable && stream.flags & 1024) {
2654
+ // seek to the end before writing in append mode
2655
+ FS.llseek(stream, 0, 2);
2656
+ }
2657
+ var seeking = typeof position != 'undefined';
2658
+ if (!seeking) {
2659
+ position = stream.position;
2660
+ } else if (!stream.seekable) {
2661
+ throw new FS.ErrnoError(70);
2662
+ }
2663
+ var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn);
2664
+ if (!seeking) stream.position += bytesWritten;
2665
+ return bytesWritten;
2666
+ },
2667
+ mmap(stream, length, position, prot, flags) {
2668
+ // User requests writing to file (prot & PROT_WRITE != 0).
2669
+ // Checking if we have permissions to write to the file unless
2670
+ // MAP_PRIVATE flag is set. According to POSIX spec it is possible
2671
+ // to write to file opened in read-only mode with MAP_PRIVATE flag,
2672
+ // as all modifications will be visible only in the memory of
2673
+ // the current process.
2674
+ if ((prot & 2) !== 0
2675
+ && (flags & 2) === 0
2676
+ && (stream.flags & 2097155) !== 2) {
2677
+ throw new FS.ErrnoError(2);
2678
+ }
2679
+ if ((stream.flags & 2097155) === 1) {
2680
+ throw new FS.ErrnoError(2);
2681
+ }
2682
+ if (!stream.stream_ops.mmap) {
2683
+ throw new FS.ErrnoError(43);
2684
+ }
2685
+ if (!length) {
2686
+ throw new FS.ErrnoError(28);
2687
+ }
2688
+ return stream.stream_ops.mmap(stream, length, position, prot, flags);
2689
+ },
2690
+ msync(stream, buffer, offset, length, mmapFlags) {
2691
+ if (!stream.stream_ops.msync) {
2692
+ return 0;
2693
+ }
2694
+ return stream.stream_ops.msync(stream, buffer, offset, length, mmapFlags);
2695
+ },
2696
+ ioctl(stream, cmd, arg) {
2697
+ if (!stream.stream_ops.ioctl) {
2698
+ throw new FS.ErrnoError(59);
2699
+ }
2700
+ return stream.stream_ops.ioctl(stream, cmd, arg);
2701
+ },
2702
+ readFile(path, opts = {}) {
2703
+ opts.flags = opts.flags || 0;
2704
+ opts.encoding = opts.encoding || 'binary';
2705
+ if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
2706
+ abort(`Invalid encoding type "${opts.encoding}"`);
2707
+ }
2708
+ var stream = FS.open(path, opts.flags);
2709
+ var stat = FS.stat(path);
2710
+ var length = stat.size;
2711
+ var buf = new Uint8Array(length);
2712
+ FS.read(stream, buf, 0, length, 0);
2713
+ if (opts.encoding === 'utf8') {
2714
+ buf = UTF8ArrayToString(buf);
2715
+ }
2716
+ FS.close(stream);
2717
+ return buf;
2718
+ },
2719
+ writeFile(path, data, opts = {}) {
2720
+ opts.flags = opts.flags || 577;
2721
+ var stream = FS.open(path, opts.flags, opts.mode);
2722
+ if (typeof data == 'string') {
2723
+ data = new Uint8Array(intArrayFromString(data, true));
2724
+ }
2725
+ if (ArrayBuffer.isView(data)) {
2726
+ FS.write(stream, data, 0, data.byteLength, undefined, opts.canOwn);
2727
+ } else {
2728
+ abort('Unsupported data type');
2729
+ }
2730
+ FS.close(stream);
2731
+ },
2732
+ cwd:() => FS.currentPath,
2733
+ chdir(path) {
2734
+ var lookup = FS.lookupPath(path, { follow: true });
2735
+ if (lookup.node === null) {
2736
+ throw new FS.ErrnoError(44);
2737
+ }
2738
+ if (!FS.isDir(lookup.node.mode)) {
2739
+ throw new FS.ErrnoError(54);
2740
+ }
2741
+ var errCode = FS.nodePermissions(lookup.node, 'x');
2742
+ if (errCode) {
2743
+ throw new FS.ErrnoError(errCode);
2744
+ }
2745
+ FS.currentPath = lookup.path;
2746
+ },
2747
+ createDefaultDirectories() {
2748
+ FS.mkdir('/tmp');
2749
+ FS.mkdir('/home');
2750
+ FS.mkdir('/home/web_user');
2751
+ },
2752
+ createDefaultDevices() {
2753
+ // create /dev
2754
+ FS.mkdir('/dev');
2755
+ // setup /dev/null
2756
+ FS.registerDevice(FS.makedev(1, 3), {
2757
+ read: () => 0,
2758
+ write: (stream, buffer, offset, length, pos) => length,
2759
+ llseek: () => 0,
2760
+ });
2761
+ FS.mkdev('/dev/null', FS.makedev(1, 3));
2762
+ // setup /dev/tty and /dev/tty1
2763
+ // stderr needs to print output using err() rather than out()
2764
+ // so we register a second tty just for it.
2765
+ TTY.register(FS.makedev(5, 0), TTY.default_tty_ops);
2766
+ TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops);
2767
+ FS.mkdev('/dev/tty', FS.makedev(5, 0));
2768
+ FS.mkdev('/dev/tty1', FS.makedev(6, 0));
2769
+ // setup /dev/[u]random
2770
+ // use a buffer to avoid overhead of individual crypto calls per byte
2771
+ var randomBuffer = new Uint8Array(1024), randomLeft = 0;
2772
+ var randomByte = () => {
2773
+ if (randomLeft === 0) {
2774
+ randomFill(randomBuffer);
2775
+ randomLeft = randomBuffer.byteLength;
2776
+ }
2777
+ return randomBuffer[--randomLeft];
2778
+ };
2779
+ FS.createDevice('/dev', 'random', randomByte);
2780
+ FS.createDevice('/dev', 'urandom', randomByte);
2781
+ // we're not going to emulate the actual shm device,
2782
+ // just create the tmp dirs that reside in it commonly
2783
+ FS.mkdir('/dev/shm');
2784
+ FS.mkdir('/dev/shm/tmp');
2785
+ },
2786
+ createSpecialDirectories() {
2787
+ // create /proc/self/fd which allows /proc/self/fd/6 => readlink gives the
2788
+ // name of the stream for fd 6 (see test_unistd_ttyname)
2789
+ FS.mkdir('/proc');
2790
+ var proc_self = FS.mkdir('/proc/self');
2791
+ FS.mkdir('/proc/self/fd');
2792
+ FS.mount({
2793
+ mount() {
2794
+ var node = FS.createNode(proc_self, 'fd', 16895, 73);
2795
+ node.stream_ops = {
2796
+ llseek: MEMFS.stream_ops.llseek,
2797
+ };
2798
+ node.node_ops = {
2799
+ lookup(parent, name) {
2800
+ var fd = +name;
2801
+ var stream = FS.getStreamChecked(fd);
2802
+ var ret = {
2803
+ parent: null,
2804
+ mount: { mountpoint: 'fake' },
2805
+ node_ops: { readlink: () => stream.path },
2806
+ id: fd + 1,
2807
+ };
2808
+ ret.parent = ret; // make it look like a simple root node
2809
+ return ret;
2810
+ },
2811
+ readdir() {
2812
+ return Array.from(FS.streams.entries())
2813
+ .filter(([k, v]) => v)
2814
+ .map(([k, v]) => k.toString());
2815
+ }
2816
+ };
2817
+ return node;
2818
+ }
2819
+ }, {}, '/proc/self/fd');
2820
+ },
2821
+ createStandardStreams(input, output, error) {
2822
+ // TODO deprecate the old functionality of a single
2823
+ // input / output callback and that utilizes FS.createDevice
2824
+ // and instead require a unique set of stream ops
2825
+
2826
+ // by default, we symlink the standard streams to the
2827
+ // default tty devices. however, if the standard streams
2828
+ // have been overwritten we create a unique device for
2829
+ // them instead.
2830
+ if (input) {
2831
+ FS.createDevice('/dev', 'stdin', input);
2832
+ } else {
2833
+ FS.symlink('/dev/tty', '/dev/stdin');
2834
+ }
2835
+ if (output) {
2836
+ FS.createDevice('/dev', 'stdout', null, output);
2837
+ } else {
2838
+ FS.symlink('/dev/tty', '/dev/stdout');
2839
+ }
2840
+ if (error) {
2841
+ FS.createDevice('/dev', 'stderr', null, error);
2842
+ } else {
2843
+ FS.symlink('/dev/tty1', '/dev/stderr');
2844
+ }
2845
+
2846
+ // open default streams for the stdin, stdout and stderr devices
2847
+ var stdin = FS.open('/dev/stdin', 0);
2848
+ var stdout = FS.open('/dev/stdout', 1);
2849
+ var stderr = FS.open('/dev/stderr', 1);
2850
+ },
2851
+ staticInit() {
2852
+ FS.nameTable = new Array(4096);
2853
+
2854
+ FS.mount(MEMFS, {}, '/');
2855
+
2856
+ FS.createDefaultDirectories();
2857
+ FS.createDefaultDevices();
2858
+ FS.createSpecialDirectories();
2859
+
2860
+ FS.filesystems = {
2861
+ 'MEMFS': MEMFS,
2862
+ };
2863
+ },
2864
+ init(input, output, error) {
2865
+ FS.initialized = true;
2866
+
2867
+ // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here
2868
+ input ??= Module['stdin'];
2869
+ output ??= Module['stdout'];
2870
+ error ??= Module['stderr'];
2871
+
2872
+ FS.createStandardStreams(input, output, error);
2873
+ },
2874
+ quit() {
2875
+ FS.initialized = false;
2876
+ // force-flush all streams, so we get musl std streams printed out
2877
+ // close all of our streams
2878
+ for (var stream of FS.streams) {
2879
+ if (stream) {
2880
+ FS.close(stream);
2881
+ }
2882
+ }
2883
+ },
2884
+ findObject(path, dontResolveLastLink) {
2885
+ var ret = FS.analyzePath(path, dontResolveLastLink);
2886
+ if (!ret.exists) {
2887
+ return null;
2888
+ }
2889
+ return ret.object;
2890
+ },
2891
+ analyzePath(path, dontResolveLastLink) {
2892
+ // operate from within the context of the symlink's target
2893
+ try {
2894
+ var lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
2895
+ path = lookup.path;
2896
+ } catch (e) {
2897
+ }
2898
+ var ret = {
2899
+ isRoot: false, exists: false, error: 0, name: null, path: null, object: null,
2900
+ parentExists: false, parentPath: null, parentObject: null
2901
+ };
2902
+ try {
2903
+ var lookup = FS.lookupPath(path, { parent: true });
2904
+ ret.parentExists = true;
2905
+ ret.parentPath = lookup.path;
2906
+ ret.parentObject = lookup.node;
2907
+ ret.name = PATH.basename(path);
2908
+ lookup = FS.lookupPath(path, { follow: !dontResolveLastLink });
2909
+ ret.exists = true;
2910
+ ret.path = lookup.path;
2911
+ ret.object = lookup.node;
2912
+ ret.name = lookup.node.name;
2913
+ ret.isRoot = lookup.path === '/';
2914
+ } catch (e) {
2915
+ ret.error = e.errno;
2916
+ };
2917
+ return ret;
2918
+ },
2919
+ createPath(parent, path, canRead, canWrite) {
2920
+ parent = typeof parent == 'string' ? parent : FS.getPath(parent);
2921
+ var parts = path.split('/').reverse();
2922
+ while (parts.length) {
2923
+ var part = parts.pop();
2924
+ if (!part) continue;
2925
+ var current = PATH.join2(parent, part);
2926
+ try {
2927
+ FS.mkdir(current);
2928
+ } catch (e) {
2929
+ if (e.errno != 20) throw e;
2930
+ }
2931
+ parent = current;
2932
+ }
2933
+ return current;
2934
+ },
2935
+ createFile(parent, name, properties, canRead, canWrite) {
2936
+ var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
2937
+ var mode = FS_getMode(canRead, canWrite);
2938
+ return FS.create(path, mode);
2939
+ },
2940
+ createDataFile(parent, name, data, canRead, canWrite, canOwn) {
2941
+ var path = name;
2942
+ if (parent) {
2943
+ parent = typeof parent == 'string' ? parent : FS.getPath(parent);
2944
+ path = name ? PATH.join2(parent, name) : parent;
2945
+ }
2946
+ var mode = FS_getMode(canRead, canWrite);
2947
+ var node = FS.create(path, mode);
2948
+ if (data) {
2949
+ if (typeof data == 'string') {
2950
+ var arr = new Array(data.length);
2951
+ for (var i = 0, len = data.length; i < len; ++i) arr[i] = data.charCodeAt(i);
2952
+ data = arr;
2953
+ }
2954
+ // make sure we can write to the file
2955
+ FS.chmod(node, mode | 146);
2956
+ var stream = FS.open(node, 577);
2957
+ FS.write(stream, data, 0, data.length, 0, canOwn);
2958
+ FS.close(stream);
2959
+ FS.chmod(node, mode);
2960
+ }
2961
+ },
2962
+ createDevice(parent, name, input, output) {
2963
+ var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
2964
+ var mode = FS_getMode(!!input, !!output);
2965
+ FS.createDevice.major ??= 64;
2966
+ var dev = FS.makedev(FS.createDevice.major++, 0);
2967
+ // Create a fake device that a set of stream ops to emulate
2968
+ // the old behavior.
2969
+ FS.registerDevice(dev, {
2970
+ open(stream) {
2971
+ stream.seekable = false;
2972
+ },
2973
+ close(stream) {
2974
+ // flush any pending line data
2975
+ if (output?.buffer?.length) {
2976
+ output(10);
2977
+ }
2978
+ },
2979
+ read(stream, buffer, offset, length, pos /* ignored */) {
2980
+ var bytesRead = 0;
2981
+ for (var i = 0; i < length; i++) {
2982
+ var result;
2983
+ try {
2984
+ result = input();
2985
+ } catch (e) {
2986
+ throw new FS.ErrnoError(29);
2987
+ }
2988
+ if (result === undefined && bytesRead === 0) {
2989
+ throw new FS.ErrnoError(6);
2990
+ }
2991
+ if (result === null || result === undefined) break;
2992
+ bytesRead++;
2993
+ buffer[offset+i] = result;
2994
+ }
2995
+ if (bytesRead) {
2996
+ stream.node.atime = Date.now();
2997
+ }
2998
+ return bytesRead;
2999
+ },
3000
+ write(stream, buffer, offset, length, pos) {
3001
+ for (var i = 0; i < length; i++) {
3002
+ try {
3003
+ output(buffer[offset+i]);
3004
+ } catch (e) {
3005
+ throw new FS.ErrnoError(29);
3006
+ }
3007
+ }
3008
+ if (length) {
3009
+ stream.node.mtime = stream.node.ctime = Date.now();
3010
+ }
3011
+ return i;
3012
+ }
3013
+ });
3014
+ return FS.mkdev(path, mode, dev);
3015
+ },
3016
+ forceLoadFile(obj) {
3017
+ if (obj.isDevice || obj.isFolder || obj.link || obj.contents) return true;
3018
+ if (globalThis.XMLHttpRequest) {
3019
+ 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.");
3020
+ } else { // Command-line.
3021
+ try {
3022
+ obj.contents = readBinary(obj.url);
3023
+ } catch (e) {
3024
+ throw new FS.ErrnoError(29);
3025
+ }
3026
+ }
3027
+ },
3028
+ createLazyFile(parent, name, url, canRead, canWrite) {
3029
+ // Lazy chunked Uint8Array (implements get and length from Uint8Array).
3030
+ // Actual getting is abstracted away for eventual reuse.
3031
+ class LazyUint8Array {
3032
+ lengthKnown = false;
3033
+ chunks = []; // Loaded chunks. Index is the chunk number
3034
+ get(idx) {
3035
+ if (idx > this.length-1 || idx < 0) {
3036
+ return undefined;
3037
+ }
3038
+ var chunkOffset = idx % this.chunkSize;
3039
+ var chunkNum = (idx / this.chunkSize)|0;
3040
+ return this.getter(chunkNum)[chunkOffset];
3041
+ }
3042
+ setDataGetter(getter) {
3043
+ this.getter = getter;
3044
+ }
3045
+ cacheLength() {
3046
+ // Find length
3047
+ var xhr = new XMLHttpRequest();
3048
+ xhr.open('HEAD', url, false);
3049
+ xhr.send(null);
3050
+ if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
3051
+ var datalength = Number(xhr.getResponseHeader("Content-length"));
3052
+ var header;
3053
+ var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes";
3054
+ var usesGzip = (header = xhr.getResponseHeader("Content-Encoding")) && header === "gzip";
3055
+
3056
+ var chunkSize = 1024*1024; // Chunk size in bytes
3057
+
3058
+ if (!hasByteServing) chunkSize = datalength;
3059
+
3060
+ // Function to get a range from the remote URL.
3061
+ var doXHR = (from, to) => {
3062
+ if (from > to) abort("invalid range (" + from + ", " + to + ") or no bytes requested!");
3063
+ if (to > datalength-1) abort("only " + datalength + " bytes available! programmer error!");
3064
+
3065
+ // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available.
3066
+ var xhr = new XMLHttpRequest();
3067
+ xhr.open('GET', url, false);
3068
+ if (datalength !== chunkSize) xhr.setRequestHeader("Range", "bytes=" + from + "-" + to);
3069
+
3070
+ // Some hints to the browser that we want binary data.
3071
+ xhr.responseType = 'arraybuffer';
3072
+ if (xhr.overrideMimeType) {
3073
+ xhr.overrideMimeType('text/plain; charset=x-user-defined');
3074
+ }
3075
+
3076
+ xhr.send(null);
3077
+ if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
3078
+ if (xhr.response !== undefined) {
3079
+ return new Uint8Array(/** @type{Array<number>} */(xhr.response || []));
3080
+ }
3081
+ return intArrayFromString(xhr.responseText || '', true);
3082
+ };
3083
+ var lazyArray = this;
3084
+ lazyArray.setDataGetter((chunkNum) => {
3085
+ var start = chunkNum * chunkSize;
3086
+ var end = (chunkNum+1) * chunkSize - 1; // including this byte
3087
+ end = Math.min(end, datalength-1); // if datalength-1 is selected, this is the last block
3088
+ if (typeof lazyArray.chunks[chunkNum] == 'undefined') {
3089
+ lazyArray.chunks[chunkNum] = doXHR(start, end);
3090
+ }
3091
+ if (typeof lazyArray.chunks[chunkNum] == 'undefined') abort('doXHR failed!');
3092
+ return lazyArray.chunks[chunkNum];
3093
+ });
3094
+
3095
+ if (usesGzip || !datalength) {
3096
+ // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length
3097
+ chunkSize = datalength = 1; // this will force getter(0)/doXHR do download the whole file
3098
+ datalength = this.getter(0).length;
3099
+ chunkSize = datalength;
3100
+ out("LazyFiles on gzip forces download of the whole file when length is accessed");
3101
+ }
3102
+
3103
+ this._length = datalength;
3104
+ this._chunkSize = chunkSize;
3105
+ this.lengthKnown = true;
3106
+ }
3107
+ get length() {
3108
+ if (!this.lengthKnown) {
3109
+ this.cacheLength();
3110
+ }
3111
+ return this._length;
3112
+ }
3113
+ get chunkSize() {
3114
+ if (!this.lengthKnown) {
3115
+ this.cacheLength();
3116
+ }
3117
+ return this._chunkSize;
3118
+ }
3119
+ }
3120
+
3121
+ if (globalThis.XMLHttpRequest) {
3122
+ if (!ENVIRONMENT_IS_WORKER) abort('Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc');
3123
+ var lazyArray = new LazyUint8Array();
3124
+ var properties = { isDevice: false, contents: lazyArray };
3125
+ } else {
3126
+ var properties = { isDevice: false, url: url };
3127
+ }
3128
+
3129
+ var node = FS.createFile(parent, name, properties, canRead, canWrite);
3130
+ // This is a total hack, but I want to get this lazy file code out of the
3131
+ // core of MEMFS. If we want to keep this lazy file concept I feel it should
3132
+ // be its own thin LAZYFS proxying calls to MEMFS.
3133
+ if (properties.contents) {
3134
+ node.contents = properties.contents;
3135
+ } else if (properties.url) {
3136
+ node.contents = null;
3137
+ node.url = properties.url;
3138
+ }
3139
+ // Add a function that defers querying the file size until it is asked the first time.
3140
+ Object.defineProperties(node, {
3141
+ usedBytes: {
3142
+ get: function() { return this.contents.length; }
3143
+ }
3144
+ });
3145
+ // override each stream op with one that tries to force load the lazy file first
3146
+ var stream_ops = {};
3147
+ for (const [key, fn] of Object.entries(node.stream_ops)) {
3148
+ stream_ops[key] = (...args) => {
3149
+ FS.forceLoadFile(node);
3150
+ return fn(...args);
3151
+ };
3152
+ }
3153
+ function writeChunks(stream, buffer, offset, length, position) {
3154
+ var contents = stream.node.contents;
3155
+ if (position >= contents.length)
3156
+ return 0;
3157
+ var size = Math.min(contents.length - position, length);
3158
+ if (contents.slice) { // normal array
3159
+ for (var i = 0; i < size; i++) {
3160
+ buffer[offset + i] = contents[position + i];
3161
+ }
3162
+ } else {
3163
+ for (var i = 0; i < size; i++) { // LazyUint8Array from sync binary XHR
3164
+ buffer[offset + i] = contents.get(position + i);
3165
+ }
3166
+ }
3167
+ return size;
3168
+ }
3169
+ // use a custom read function
3170
+ stream_ops.read = (stream, buffer, offset, length, position) => {
3171
+ FS.forceLoadFile(node);
3172
+ return writeChunks(stream, buffer, offset, length, position)
3173
+ };
3174
+ // use a custom mmap function
3175
+ stream_ops.mmap = (stream, length, position, prot, flags) => {
3176
+ FS.forceLoadFile(node);
3177
+ var ptr = mmapAlloc(length);
3178
+ if (!ptr) {
3179
+ throw new FS.ErrnoError(48);
3180
+ }
3181
+ writeChunks(stream, HEAP8, ptr, length, position);
3182
+ return { ptr, allocated: true };
3183
+ };
3184
+ node.stream_ops = stream_ops;
3185
+ return node;
3186
+ },
3187
+ };
3188
+
3189
+
3190
+ /**
3191
+ * Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
3192
+ * emscripten HEAP, returns a copy of that string as a Javascript String object.
3193
+ *
3194
+ * @param {number} ptr
3195
+ * @param {number=} maxBytesToRead - An optional length that specifies the
3196
+ * maximum number of bytes to read. You can omit this parameter to scan the
3197
+ * string until the first 0 byte. If maxBytesToRead is passed, and the string
3198
+ * at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the
3199
+ * string will cut short at that byte index.
3200
+ * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
3201
+ * @return {string}
3202
+ */
3203
+ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => {
3204
+ return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : '';
3205
+ };
3206
+ var SYSCALLS = {
3207
+ calculateAt(dirfd, path, allowEmpty) {
3208
+ if (PATH.isAbs(path)) {
3209
+ return path;
3210
+ }
3211
+ // relative path
3212
+ var dir;
3213
+ if (dirfd === -100) {
3214
+ dir = FS.cwd();
3215
+ } else {
3216
+ var dirstream = SYSCALLS.getStreamFromFD(dirfd);
3217
+ dir = dirstream.path;
3218
+ }
3219
+ if (path.length == 0) {
3220
+ if (!allowEmpty) {
3221
+ throw new FS.ErrnoError(44);;
3222
+ }
3223
+ return dir;
3224
+ }
3225
+ return dir + '/' + path;
3226
+ },
3227
+ writeStat(buf, stat) {
3228
+ HEAPU32[((buf)>>2)] = stat.dev;
3229
+ HEAPU32[(((buf)+(4))>>2)] = stat.mode;
3230
+ HEAPU32[(((buf)+(8))>>2)] = stat.nlink;
3231
+ HEAPU32[(((buf)+(12))>>2)] = stat.uid;
3232
+ HEAPU32[(((buf)+(16))>>2)] = stat.gid;
3233
+ HEAPU32[(((buf)+(20))>>2)] = stat.rdev;
3234
+ HEAP64[(((buf)+(24))>>3)] = BigInt(stat.size);
3235
+ HEAP32[(((buf)+(32))>>2)] = 4096;
3236
+ HEAP32[(((buf)+(36))>>2)] = stat.blocks;
3237
+ var atime = stat.atime.getTime();
3238
+ var mtime = stat.mtime.getTime();
3239
+ var ctime = stat.ctime.getTime();
3240
+ HEAP64[(((buf)+(40))>>3)] = BigInt(Math.floor(atime / 1000));
3241
+ HEAPU32[(((buf)+(48))>>2)] = (atime % 1000) * 1000 * 1000;
3242
+ HEAP64[(((buf)+(56))>>3)] = BigInt(Math.floor(mtime / 1000));
3243
+ HEAPU32[(((buf)+(64))>>2)] = (mtime % 1000) * 1000 * 1000;
3244
+ HEAP64[(((buf)+(72))>>3)] = BigInt(Math.floor(ctime / 1000));
3245
+ HEAPU32[(((buf)+(80))>>2)] = (ctime % 1000) * 1000 * 1000;
3246
+ HEAP64[(((buf)+(88))>>3)] = BigInt(stat.ino);
3247
+ return 0;
3248
+ },
3249
+ writeStatFs(buf, stats) {
3250
+ HEAPU32[(((buf)+(4))>>2)] = stats.bsize;
3251
+ HEAPU32[(((buf)+(60))>>2)] = stats.bsize;
3252
+ HEAP64[(((buf)+(8))>>3)] = BigInt(stats.blocks);
3253
+ HEAP64[(((buf)+(16))>>3)] = BigInt(stats.bfree);
3254
+ HEAP64[(((buf)+(24))>>3)] = BigInt(stats.bavail);
3255
+ HEAP64[(((buf)+(32))>>3)] = BigInt(stats.files);
3256
+ HEAP64[(((buf)+(40))>>3)] = BigInt(stats.ffree);
3257
+ HEAPU32[(((buf)+(48))>>2)] = stats.fsid;
3258
+ HEAPU32[(((buf)+(64))>>2)] = stats.flags; // ST_NOSUID
3259
+ HEAPU32[(((buf)+(56))>>2)] = stats.namelen;
3260
+ },
3261
+ doMsync(addr, stream, len, flags, offset) {
3262
+ if (!FS.isFile(stream.node.mode)) {
3263
+ throw new FS.ErrnoError(43);
3264
+ }
3265
+ if (flags & 2) {
3266
+ // MAP_PRIVATE calls need not to be synced back to underlying fs
3267
+ return 0;
3268
+ }
3269
+ var buffer = HEAPU8.slice(addr, addr + len);
3270
+ FS.msync(stream, buffer, offset, len, flags);
3271
+ },
3272
+ getStreamFromFD(fd) {
3273
+ var stream = FS.getStreamChecked(fd);
3274
+ return stream;
3275
+ },
3276
+ varargs:undefined,
3277
+ getStr(ptr) {
3278
+ var ret = UTF8ToString(ptr);
3279
+ return ret;
3280
+ },
3281
+ };
3282
+ function ___syscall_fcntl64(fd, cmd, varargs) {
3283
+ SYSCALLS.varargs = varargs;
3284
+ try {
3285
+
3286
+ var stream = SYSCALLS.getStreamFromFD(fd);
3287
+ switch (cmd) {
3288
+ case 0: {
3289
+ var arg = syscallGetVarargI();
3290
+ if (arg < 0) {
3291
+ return -28;
3292
+ }
3293
+ while (FS.streams[arg]) {
3294
+ arg++;
3295
+ }
3296
+ var newStream;
3297
+ newStream = FS.dupStream(stream, arg);
3298
+ return newStream.fd;
3299
+ }
3300
+ case 1:
3301
+ case 2:
3302
+ return 0; // FD_CLOEXEC makes no sense for a single process.
3303
+ case 3:
3304
+ return stream.flags;
3305
+ case 4: {
3306
+ var arg = syscallGetVarargI();
3307
+ stream.flags |= arg;
3308
+ return 0;
3309
+ }
3310
+ case 12: {
3311
+ var arg = syscallGetVarargP();
3312
+ var offset = 0;
3313
+ // We're always unlocked.
3314
+ HEAP16[(((arg)+(offset))>>1)] = 2;
3315
+ return 0;
3316
+ }
3317
+ case 13:
3318
+ case 14:
3319
+ // Pretend that the locking is successful. These are process-level locks,
3320
+ // and Emscripten programs are a single process. If we supported linking a
3321
+ // filesystem between programs, we'd need to do more here.
3322
+ // See https://github.com/emscripten-core/emscripten/issues/23697
3323
+ return 0;
3324
+ }
3325
+ return -28;
3326
+ } catch (e) {
3327
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3328
+ return -e.errno;
3329
+ }
3330
+ }
3331
+
3332
+
3333
+ function ___syscall_ioctl(fd, op, varargs) {
3334
+ SYSCALLS.varargs = varargs;
3335
+ try {
3336
+
3337
+ var stream = SYSCALLS.getStreamFromFD(fd);
3338
+ switch (op) {
3339
+ case 21509: {
3340
+ if (!stream.tty) return -59;
3341
+ return 0;
3342
+ }
3343
+ case 21505: {
3344
+ if (!stream.tty) return -59;
3345
+ if (stream.tty.ops.ioctl_tcgets) {
3346
+ var termios = stream.tty.ops.ioctl_tcgets(stream);
3347
+ var argp = syscallGetVarargP();
3348
+ HEAP32[((argp)>>2)] = termios.c_iflag || 0;
3349
+ HEAP32[(((argp)+(4))>>2)] = termios.c_oflag || 0;
3350
+ HEAP32[(((argp)+(8))>>2)] = termios.c_cflag || 0;
3351
+ HEAP32[(((argp)+(12))>>2)] = termios.c_lflag || 0;
3352
+ for (var i = 0; i < 32; i++) {
3353
+ HEAP8[(argp + i)+(17)] = termios.c_cc[i] || 0;
3354
+ }
3355
+ return 0;
3356
+ }
3357
+ return 0;
3358
+ }
3359
+ case 21510:
3360
+ case 21511:
3361
+ case 21512: {
3362
+ if (!stream.tty) return -59;
3363
+ return 0; // no-op, not actually adjusting terminal settings
3364
+ }
3365
+ case 21506:
3366
+ case 21507:
3367
+ case 21508: {
3368
+ if (!stream.tty) return -59;
3369
+ if (stream.tty.ops.ioctl_tcsets) {
3370
+ var argp = syscallGetVarargP();
3371
+ var c_iflag = HEAP32[((argp)>>2)];
3372
+ var c_oflag = HEAP32[(((argp)+(4))>>2)];
3373
+ var c_cflag = HEAP32[(((argp)+(8))>>2)];
3374
+ var c_lflag = HEAP32[(((argp)+(12))>>2)];
3375
+ var c_cc = []
3376
+ for (var i = 0; i < 32; i++) {
3377
+ c_cc.push(HEAP8[(argp + i)+(17)]);
3378
+ }
3379
+ return stream.tty.ops.ioctl_tcsets(stream.tty, op, { c_iflag, c_oflag, c_cflag, c_lflag, c_cc });
3380
+ }
3381
+ return 0; // no-op, not actually adjusting terminal settings
3382
+ }
3383
+ case 21519: {
3384
+ if (!stream.tty) return -59;
3385
+ var argp = syscallGetVarargP();
3386
+ HEAP32[((argp)>>2)] = 0;
3387
+ return 0;
3388
+ }
3389
+ case 21520: {
3390
+ if (!stream.tty) return -59;
3391
+ return -28; // not supported
3392
+ }
3393
+ case 21537:
3394
+ case 21531: {
3395
+ var argp = syscallGetVarargP();
3396
+ return FS.ioctl(stream, op, argp);
3397
+ }
3398
+ case 21523: {
3399
+ // TODO: in theory we should write to the winsize struct that gets
3400
+ // passed in, but for now musl doesn't read anything on it
3401
+ if (!stream.tty) return -59;
3402
+ if (stream.tty.ops.ioctl_tiocgwinsz) {
3403
+ var winsize = stream.tty.ops.ioctl_tiocgwinsz(stream.tty);
3404
+ var argp = syscallGetVarargP();
3405
+ HEAP16[((argp)>>1)] = winsize[0];
3406
+ HEAP16[(((argp)+(2))>>1)] = winsize[1];
3407
+ }
3408
+ return 0;
3409
+ }
3410
+ case 21524: {
3411
+ // TODO: technically, this ioctl call should change the window size.
3412
+ // but, since emscripten doesn't have any concept of a terminal window
3413
+ // yet, we'll just silently throw it away as we do TIOCGWINSZ
3414
+ if (!stream.tty) return -59;
3415
+ return 0;
3416
+ }
3417
+ case 21515: {
3418
+ if (!stream.tty) return -59;
3419
+ return 0;
3420
+ }
3421
+ default: return -28; // not supported
3422
+ }
3423
+ } catch (e) {
3424
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3425
+ return -e.errno;
3426
+ }
3427
+ }
3428
+
3429
+
3430
+ function ___syscall_openat(dirfd, path, flags, varargs) {
3431
+ SYSCALLS.varargs = varargs;
3432
+ try {
3433
+
3434
+ path = SYSCALLS.getStr(path);
3435
+ path = SYSCALLS.calculateAt(dirfd, path);
3436
+ var mode = varargs ? syscallGetVarargI() : 0;
3437
+ return FS.open(path, flags, mode).fd;
3438
+ } catch (e) {
3439
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3440
+ return -e.errno;
3441
+ }
3442
+ }
3443
+
3444
+ var getHeapMax = () =>
3445
+ // Stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate
3446
+ // full 4GB Wasm memories, the size will wrap back to 0 bytes in Wasm side
3447
+ // for any code that deals with heap sizes, which would require special
3448
+ // casing all heap size related code to treat 0 specially.
3449
+ 1073741824;
3450
+
3451
+ var alignMemory = (size, alignment) => {
3452
+ return Math.ceil(size / alignment) * alignment;
3453
+ };
3454
+
3455
+ var growMemory = (size) => {
3456
+ var oldHeapSize = wasmMemory.buffer.byteLength;
3457
+ var pages = ((size - oldHeapSize + 65535) / 65536) | 0;
3458
+ try {
3459
+ // round size grow request up to wasm page size (fixed 64KB per spec)
3460
+ wasmMemory.grow(pages); // .grow() takes a delta compared to the previous size
3461
+ updateMemoryViews();
3462
+ return 1 /*success*/;
3463
+ } catch(e) {
3464
+ }
3465
+ // implicit 0 return to save code size (caller will cast "undefined" into 0
3466
+ // anyhow)
3467
+ };
3468
+ var _emscripten_resize_heap = (requestedSize) => {
3469
+ var oldSize = HEAPU8.length;
3470
+ // With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned.
3471
+ requestedSize >>>= 0;
3472
+ // With multithreaded builds, races can happen (another thread might increase the size
3473
+ // in between), so return a failure, and let the caller retry.
3474
+
3475
+ // Memory resize rules:
3476
+ // 1. Always increase heap size to at least the requested size, rounded up
3477
+ // to next page multiple.
3478
+ // 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap
3479
+ // geometrically: increase the heap size according to
3480
+ // MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%), At most
3481
+ // overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB).
3482
+ // 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap
3483
+ // linearly: increase the heap size by at least
3484
+ // MEMORY_GROWTH_LINEAR_STEP bytes.
3485
+ // 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by
3486
+ // MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest
3487
+ // 4. If we were unable to allocate as much memory, it may be due to
3488
+ // over-eager decision to excessively reserve due to (3) above.
3489
+ // Hence if an allocation fails, cut down on the amount of excess
3490
+ // growth, in an attempt to succeed to perform a smaller allocation.
3491
+
3492
+ // A limit is set for how much we can grow. We should not exceed that
3493
+ // (the wasm binary specifies it, so if we tried, we'd fail anyhow).
3494
+ var maxHeapSize = getHeapMax();
3495
+ if (requestedSize > maxHeapSize) {
3496
+ return false;
3497
+ }
3498
+
3499
+ // Loop through potential heap size increases. If we attempt a too eager
3500
+ // reservation that fails, cut down on the attempted size and reserve a
3501
+ // smaller bump instead. (max 3 times, chosen somewhat arbitrarily)
3502
+ for (var cutDown = 1; cutDown <= 4; cutDown *= 2) {
3503
+ var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); // ensure geometric growth
3504
+ // but limit overreserving (default to capping at +96MB overgrowth at most)
3505
+ overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296 );
3506
+
3507
+ var newSize = Math.min(maxHeapSize, alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536));
3508
+
3509
+ var replacement = growMemory(newSize);
3510
+ if (replacement) {
3511
+
3512
+ return true;
3513
+ }
3514
+ }
3515
+ return false;
3516
+ };
3517
+
3518
+ var ENV = {
3519
+ };
3520
+
3521
+ var getExecutableName = () => thisProgram || './this.program';
3522
+ var getEnvStrings = () => {
3523
+ if (!getEnvStrings.strings) {
3524
+ // Default values.
3525
+ // Browser language detection #8751
3526
+ var lang = (globalThis.navigator?.language ?? 'C').replace('-', '_') + '.UTF-8';
3527
+ var env = {
3528
+ 'USER': 'web_user',
3529
+ 'LOGNAME': 'web_user',
3530
+ 'PATH': '/',
3531
+ 'PWD': '/',
3532
+ 'HOME': '/home/web_user',
3533
+ 'LANG': lang,
3534
+ '_': getExecutableName()
3535
+ };
3536
+ // Apply the user-provided values, if any.
3537
+ for (var x in ENV) {
3538
+ // x is a key in ENV; if ENV[x] is undefined, that means it was
3539
+ // explicitly set to be so. We allow user code to do that to
3540
+ // force variables with default values to remain unset.
3541
+ if (ENV[x] === undefined) delete env[x];
3542
+ else env[x] = ENV[x];
3543
+ }
3544
+ var strings = [];
3545
+ for (var x in env) {
3546
+ strings.push(`${x}=${env[x]}`);
3547
+ }
3548
+ getEnvStrings.strings = strings;
3549
+ }
3550
+ return getEnvStrings.strings;
3551
+ };
3552
+
3553
+ var stringToUTF8 = (str, outPtr, maxBytesToWrite) => {
3554
+ return stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);
3555
+ };
3556
+ var _environ_get = (__environ, environ_buf) => {
3557
+ var bufSize = 0;
3558
+ var envp = 0;
3559
+ for (var string of getEnvStrings()) {
3560
+ var ptr = environ_buf + bufSize;
3561
+ HEAPU32[(((__environ)+(envp))>>2)] = ptr;
3562
+ bufSize += stringToUTF8(string, ptr, Infinity) + 1;
3563
+ envp += 4;
3564
+ }
3565
+ return 0;
3566
+ };
3567
+
3568
+
3569
+ var _environ_sizes_get = (penviron_count, penviron_buf_size) => {
3570
+ var strings = getEnvStrings();
3571
+ HEAPU32[((penviron_count)>>2)] = strings.length;
3572
+ var bufSize = 0;
3573
+ for (var string of strings) {
3574
+ bufSize += lengthBytesUTF8(string) + 1;
3575
+ }
3576
+ HEAPU32[((penviron_buf_size)>>2)] = bufSize;
3577
+ return 0;
3578
+ };
3579
+
3580
+ function _fd_close(fd) {
3581
+ try {
3582
+
3583
+ var stream = SYSCALLS.getStreamFromFD(fd);
3584
+ FS.close(stream);
3585
+ return 0;
3586
+ } catch (e) {
3587
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3588
+ return e.errno;
3589
+ }
3590
+ }
3591
+
3592
+ /** @param {number=} offset */
3593
+ var doReadv = (stream, iov, iovcnt, offset) => {
3594
+ var ret = 0;
3595
+ for (var i = 0; i < iovcnt; i++) {
3596
+ var ptr = HEAPU32[((iov)>>2)];
3597
+ var len = HEAPU32[(((iov)+(4))>>2)];
3598
+ iov += 8;
3599
+ var curr = FS.read(stream, HEAP8, ptr, len, offset);
3600
+ if (curr < 0) return -1;
3601
+ ret += curr;
3602
+ if (curr < len) break; // nothing more to read
3603
+ if (typeof offset != 'undefined') {
3604
+ offset += curr;
3605
+ }
3606
+ }
3607
+ return ret;
3608
+ };
3609
+
3610
+ function _fd_read(fd, iov, iovcnt, pnum) {
3611
+ try {
3612
+
3613
+ var stream = SYSCALLS.getStreamFromFD(fd);
3614
+ var num = doReadv(stream, iov, iovcnt);
3615
+ HEAPU32[((pnum)>>2)] = num;
3616
+ return 0;
3617
+ } catch (e) {
3618
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3619
+ return e.errno;
3620
+ }
3621
+ }
3622
+
3623
+
3624
+ var INT53_MAX = 9007199254740992;
3625
+
3626
+ var INT53_MIN = -9007199254740992;
3627
+ var bigintToI53Checked = (num) => (num < INT53_MIN || num > INT53_MAX) ? NaN : Number(num);
3628
+ function _fd_seek(fd, offset, whence, newOffset) {
3629
+ offset = bigintToI53Checked(offset);
3630
+
3631
+
3632
+ try {
3633
+
3634
+ if (isNaN(offset)) return 61;
3635
+ var stream = SYSCALLS.getStreamFromFD(fd);
3636
+ FS.llseek(stream, offset, whence);
3637
+ HEAP64[((newOffset)>>3)] = BigInt(stream.position);
3638
+ if (stream.getdents && offset === 0 && whence === 0) stream.getdents = null; // reset readdir state
3639
+ return 0;
3640
+ } catch (e) {
3641
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3642
+ return e.errno;
3643
+ }
3644
+ ;
3645
+ }
3646
+
3647
+ /** @param {number=} offset */
3648
+ var doWritev = (stream, iov, iovcnt, offset) => {
3649
+ var ret = 0;
3650
+ for (var i = 0; i < iovcnt; i++) {
3651
+ var ptr = HEAPU32[((iov)>>2)];
3652
+ var len = HEAPU32[(((iov)+(4))>>2)];
3653
+ iov += 8;
3654
+ var curr = FS.write(stream, HEAP8, ptr, len, offset);
3655
+ if (curr < 0) return -1;
3656
+ ret += curr;
3657
+ if (curr < len) {
3658
+ // No more space to write.
3659
+ break;
3660
+ }
3661
+ if (typeof offset != 'undefined') {
3662
+ offset += curr;
3663
+ }
3664
+ }
3665
+ return ret;
3666
+ };
3667
+
3668
+ function _fd_write(fd, iov, iovcnt, pnum) {
3669
+ try {
3670
+
3671
+ var stream = SYSCALLS.getStreamFromFD(fd);
3672
+ var num = doWritev(stream, iov, iovcnt);
3673
+ HEAPU32[((pnum)>>2)] = num;
3674
+ return 0;
3675
+ } catch (e) {
3676
+ if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e;
3677
+ return e.errno;
3678
+ }
3679
+ }
3680
+
3681
+
3682
+ var getCFunc = (ident) => {
3683
+ var func = Module['_' + ident]; // closure exported function
3684
+ return func;
3685
+ };
3686
+
3687
+ var writeArrayToMemory = (array, buffer) => {
3688
+ HEAP8.set(array, buffer);
3689
+ };
3690
+
3691
+
3692
+
3693
+ var stackAlloc = (sz) => __emscripten_stack_alloc(sz);
3694
+ var stringToUTF8OnStack = (str) => {
3695
+ var size = lengthBytesUTF8(str) + 1;
3696
+ var ret = stackAlloc(size);
3697
+ stringToUTF8(str, ret, size);
3698
+ return ret;
3699
+ };
3700
+
3701
+
3702
+
3703
+
3704
+
3705
+ /**
3706
+ * @param {string|null=} returnType
3707
+ * @param {Array=} argTypes
3708
+ * @param {Array=} args
3709
+ * @param {Object=} opts
3710
+ */
3711
+ var ccall = (ident, returnType, argTypes, args, opts) => {
3712
+ // For fast lookup of conversion functions
3713
+ var toC = {
3714
+ 'string': (str) => {
3715
+ var ret = 0;
3716
+ if (str !== null && str !== undefined && str !== 0) { // null string
3717
+ ret = stringToUTF8OnStack(str);
3718
+ }
3719
+ return ret;
3720
+ },
3721
+ 'array': (arr) => {
3722
+ var ret = stackAlloc(arr.length);
3723
+ writeArrayToMemory(arr, ret);
3724
+ return ret;
3725
+ }
3726
+ };
3727
+
3728
+ function convertReturnValue(ret) {
3729
+ if (returnType === 'string') {
3730
+ return UTF8ToString(ret);
3731
+ }
3732
+ if (returnType === 'boolean') return Boolean(ret);
3733
+ return ret;
3734
+ }
3735
+
3736
+ var func = getCFunc(ident);
3737
+ var cArgs = [];
3738
+ var stack = 0;
3739
+ if (args) {
3740
+ for (var i = 0; i < args.length; i++) {
3741
+ var converter = toC[argTypes[i]];
3742
+ if (converter) {
3743
+ if (stack === 0) stack = stackSave();
3744
+ cArgs[i] = converter(args[i]);
3745
+ } else {
3746
+ cArgs[i] = args[i];
3747
+ }
3748
+ }
3749
+ }
3750
+ var ret = func(...cArgs);
3751
+ function onDone(ret) {
3752
+ if (stack !== 0) stackRestore(stack);
3753
+ return convertReturnValue(ret);
3754
+ }
3755
+
3756
+ ret = onDone(ret);
3757
+ return ret;
3758
+ };
3759
+
3760
+
3761
+
3762
+ /**
3763
+ * @param {string=} returnType
3764
+ * @param {Array=} argTypes
3765
+ * @param {Object=} opts
3766
+ */
3767
+ var cwrap = (ident, returnType, argTypes, opts) => {
3768
+ // When the function takes numbers and returns a number, we can just return
3769
+ // the original function
3770
+ var numericArgs = !argTypes || argTypes.every((type) => type === 'number' || type === 'boolean');
3771
+ var numericRet = returnType !== 'string';
3772
+ if (numericRet && numericArgs && !opts) {
3773
+ return getCFunc(ident);
3774
+ }
3775
+ return (...args) => ccall(ident, returnType, argTypes, args, opts);
3776
+ };
3777
+
3778
+ var FS_createPath = (...args) => FS.createPath(...args);
3779
+
3780
+
3781
+
3782
+ var FS_unlink = (...args) => FS.unlink(...args);
3783
+
3784
+ var FS_createLazyFile = (...args) => FS.createLazyFile(...args);
3785
+
3786
+ var FS_createDevice = (...args) => FS.createDevice(...args);
3787
+
3788
+
3789
+
3790
+ FS.createPreloadedFile = FS_createPreloadedFile;
3791
+ FS.preloadFile = FS_preloadFile;
3792
+ FS.staticInit();;
3793
+ // End JS library code
3794
+
3795
+ // include: postlibrary.js
3796
+ // This file is included after the automatically-generated JS library code
3797
+ // but before the wasm module is created.
3798
+
3799
+ {
3800
+
3801
+ // Begin ATMODULES hooks
3802
+ if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime'];
3803
+ if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins'];
3804
+ if (Module['print']) out = Module['print'];
3805
+ if (Module['printErr']) err = Module['printErr'];
3806
+ if (Module['wasmBinary']) wasmBinary = Module['wasmBinary'];
3807
+ // End ATMODULES hooks
3808
+
3809
+ if (Module['arguments']) arguments_ = Module['arguments'];
3810
+ if (Module['thisProgram']) thisProgram = Module['thisProgram'];
3811
+
3812
+ if (Module['preInit']) {
3813
+ if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
3814
+ while (Module['preInit'].length > 0) {
3815
+ Module['preInit'].shift()();
3816
+ }
3817
+ }
3818
+ }
3819
+
3820
+ // Begin runtime exports
3821
+ Module['addRunDependency'] = addRunDependency;
3822
+ Module['removeRunDependency'] = removeRunDependency;
3823
+ Module['ccall'] = ccall;
3824
+ Module['cwrap'] = cwrap;
3825
+ Module['FS_preloadFile'] = FS_preloadFile;
3826
+ Module['FS_unlink'] = FS_unlink;
3827
+ Module['FS_createPath'] = FS_createPath;
3828
+ Module['FS_createDevice'] = FS_createDevice;
3829
+ Module['FS_createDataFile'] = FS_createDataFile;
3830
+ Module['FS_createLazyFile'] = FS_createLazyFile;
3831
+ // End runtime exports
3832
+ // Begin JS library exports
3833
+ // End JS library exports
3834
+
3835
+ // end include: postlibrary.js
3836
+
3837
+
3838
+ // Imports from the Wasm binary.
3839
+ var _get,
3840
+ __emscripten_stack_restore,
3841
+ __emscripten_stack_alloc,
3842
+ _emscripten_stack_get_current,
3843
+ memory,
3844
+ __indirect_function_table,
3845
+ wasmMemory;
3846
+
3847
+
3848
+ function assignWasmExports(wasmExports) {
3849
+ _get = Module['_get'] = wasmExports['get'];
3850
+ __emscripten_stack_restore = wasmExports['_emscripten_stack_restore'];
3851
+ __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc'];
3852
+ _emscripten_stack_get_current = wasmExports['emscripten_stack_get_current'];
3853
+ memory = wasmMemory = wasmExports['memory'];
3854
+ __indirect_function_table = wasmExports['__indirect_function_table'];
3855
+ }
3856
+
3857
+ var wasmImports = {
3858
+ /** @export */
3859
+ __syscall_fcntl64: ___syscall_fcntl64,
3860
+ /** @export */
3861
+ __syscall_ioctl: ___syscall_ioctl,
3862
+ /** @export */
3863
+ __syscall_openat: ___syscall_openat,
3864
+ /** @export */
3865
+ emscripten_resize_heap: _emscripten_resize_heap,
3866
+ /** @export */
3867
+ environ_get: _environ_get,
3868
+ /** @export */
3869
+ environ_sizes_get: _environ_sizes_get,
3870
+ /** @export */
3871
+ fd_close: _fd_close,
3872
+ /** @export */
3873
+ fd_read: _fd_read,
3874
+ /** @export */
3875
+ fd_seek: _fd_seek,
3876
+ /** @export */
3877
+ fd_write: _fd_write
3878
+ };
3879
+
3880
+
3881
+ // include: postamble.js
3882
+ // === Auto-generated postamble setup entry stuff ===
3883
+
3884
+ function run() {
3885
+
3886
+ if (runDependencies > 0) {
3887
+ dependenciesFulfilled = run;
3888
+ return;
3889
+ }
3890
+
3891
+ preRun();
3892
+
3893
+ // a preRun added a dependency, run will be called later
3894
+ if (runDependencies > 0) {
3895
+ dependenciesFulfilled = run;
3896
+ return;
3897
+ }
3898
+
3899
+ function doRun() {
3900
+ // run may have just been called through dependencies being fulfilled just in this very frame,
3901
+ // or while the async setStatus time below was happening
3902
+ Module['calledRun'] = true;
3903
+
3904
+ if (ABORT) return;
3905
+
3906
+ initRuntime();
3907
+
3908
+ Module['onRuntimeInitialized']?.();
3909
+
3910
+ postRun();
3911
+ }
3912
+
3913
+ if (Module['setStatus']) {
3914
+ Module['setStatus']('Running...');
3915
+ setTimeout(() => {
3916
+ setTimeout(() => Module['setStatus'](''), 1);
3917
+ doRun();
3918
+ }, 1);
3919
+ } else
3920
+ {
3921
+ doRun();
3922
+ }
3923
+ }
3924
+
3925
+ var wasmExports;
3926
+
3927
+ // With async instantation wasmExports is assigned asynchronously when the
3928
+ // instance is received.
3929
+ createWasm();
3930
+
3931
+ run();
3932
+
3933
+ // end include: postamble.js
3934
+