-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathemulator-worker.ts
835 lines (761 loc) · 27.9 KB
/
emulator-worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
import {
type EmulatorFallbackSetClipboardDataCommand,
type EmulatorCDROM,
type EmulatorChunkedFileSpec,
type EmulatorClipboardData,
type EmulatorFallbackCommand,
type EmulatorFallbackEthernetReceiveCommand,
type EmulatorFallbackInputCommand,
type EmulatorFallbackLoadCDROMCommand,
type EmulatorFallbackUploadFileCommand,
type EmulatorFileUpload,
type EmulatorInputEvent,
type EmulatorWorkerConfig,
type EmulatorWorkerVideoBlit,
type EmulatorWorkerVideoBlitRect,
isDiskImageFile,
ethernetMacAddressFromString,
InputBufferAddresses,
isCDROMBinFile,
} from "./emulator-common";
import {
type EmulatorWorkerAudio,
FallbackEmulatorWorkerAudio,
SharedMemoryEmulatorWorkerAudio,
} from "./emulator-worker-audio";
import {
type EmulatorWorkerInput,
FallbackEmulatorWorkerInput,
SharedMemoryEmulatorWorkerInput,
} from "./emulator-worker-input";
import {
type EmulatorWorkerVideo,
FallbackEmulatorWorkerVideo,
SharedMemoryEmulatorWorkerVideo,
} from "./emulator-worker-video";
import {
type EmulatorWorkerFiles,
FallbackEmulatorWorkerFiles,
getUploadData,
SharedMemoryEmulatorWorkerFiles,
} from "./emulator-worker-files";
import {
handleExtractionRequests,
initializeExtractor,
} from "./emulator-worker-extractor";
import {EmulatorWorkerChunkedDisk} from "./emulator-worker-chunked-disk";
import {
type EmulatorWorkerEthernet,
FallbackEmulatorWorkerEthernet,
SharedMemoryEmulatorWorkerEthernet,
handlePingPacket,
sendEthernetPacket,
} from "./emulator-worker-ethernet";
import {
type EmulatorWorkerClipboard,
FallbackEmulatorWorkerClipboard,
SharedMemoryEmulatorWorkerClipboard,
} from "./emulator-worker-clipboard";
import {
type EmulatorWorkerDisk,
EmulatorWorkerDisksApi,
} from "./emulator-worker-disks";
import {EmulatorWorkerUploadDisk} from "./emulator-worker-upload-disk";
import {
createEmulatorWorkerCDROMDisk,
EmulatorWorkerMode1SectorDisk,
} from "./emulator-worker-cdrom-disk";
import {
importEmulator,
importEmulatorFallback,
} from "./emulator-worker-emulators";
import {
EmulatorWorkerDiskSaver,
initDiskSavers,
} from "./emulator-worker-disk-saver";
import {emulatorNeedsDeviceImage} from "./emulator-common-emulators";
import {EmulatorWorkerDeviceImageDisk} from "./emulator-worker-device-image-disk";
addEventListener("message", async event => {
const {data} = event;
const {type} = data;
if (type === "start") {
await startEmulator(data.config);
}
});
addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => {
const reasonString = event.reason.toString();
if (reasonString.toLowerCase().includes("out of memory")) {
postMessage({type: "emulator_did_run_out_memory"});
} else {
postMessage({type: "emulator_did_have_error", error: reasonString});
}
});
class EmulatorWorkerApi {
InputBufferAddresses = InputBufferAddresses;
#emscriptenModule: EmscriptenModule;
#video: EmulatorWorkerVideo;
#input: EmulatorWorkerInput;
#audio: EmulatorWorkerAudio;
#files: EmulatorWorkerFiles;
#ethernet: EmulatorWorkerEthernet;
#clipboard: EmulatorWorkerClipboard;
#videoOpenTime = 0;
#lastBlitFrameId = 0;
#nextExpectedBlitTime = 0;
#lastIdleWaitFrameId = 0;
#markedQuiescent = false;
#handledStop = false;
disks: EmulatorWorkerDisksApi;
#delayedDiskSpecs: EmulatorChunkedFileSpec[] | undefined;
#diskSavers: EmulatorWorkerDiskSaver[] = [];
#ethernetMacAddress?: Uint8Array;
constructor(
config: EmulatorWorkerConfig,
emscriptenModule: EmscriptenModule
) {
this.#emscriptenModule = emscriptenModule;
const {
video: videoConfig,
input: inputConfig,
audio: audioConfig,
files: filesConfig,
ethernet: ethernetConfig,
clipboard: clipboardConfig,
disks,
delayedDisks,
diskFiles,
cdroms,
} = config;
const blitSender = (
data: EmulatorWorkerVideoBlit,
transfer: Transferable[] = []
) => postMessage({type: "emulator_blit", data}, transfer);
const audioSender = (data: Uint8Array) =>
postMessage({type: "emulator_audio", data}, [data.buffer]);
let fallbackEndpoint: EmulatorFallbackEndpoint | undefined;
function getFallbackEndpoint(): EmulatorFallbackEndpoint {
if (!fallbackEndpoint) {
fallbackEndpoint = new EmulatorFallbackEndpoint();
}
return fallbackEndpoint;
}
this.#video =
videoConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerVideo(videoConfig, blitSender)
: new FallbackEmulatorWorkerVideo(videoConfig, blitSender);
this.#input =
inputConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerInput(inputConfig)
: new FallbackEmulatorWorkerInput(
inputConfig,
getFallbackEndpoint()
);
this.#audio =
audioConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerAudio(audioConfig)
: new FallbackEmulatorWorkerAudio(audioConfig, audioSender);
this.#files =
filesConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerFiles(filesConfig)
: new FallbackEmulatorWorkerFiles(
filesConfig,
getFallbackEndpoint()
);
this.#ethernet =
ethernetConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerEthernet(ethernetConfig)
: new FallbackEmulatorWorkerEthernet(
ethernetConfig,
getFallbackEndpoint()
);
this.#clipboard =
clipboardConfig.type === "shared-memory"
? new SharedMemoryEmulatorWorkerClipboard(clipboardConfig)
: new FallbackEmulatorWorkerClipboard(
clipboardConfig,
getFallbackEndpoint()
);
const needsDeviceImage = emulatorNeedsDeviceImage(config.emulatorType);
this.disks = new EmulatorWorkerDisksApi(
[
...disks.map(spec => {
let disk;
if (spec.persistent) {
const saver = new EmulatorWorkerDiskSaver(spec, this);
this.#diskSavers.push(saver);
disk = new EmulatorWorkerChunkedDisk(spec, saver);
} else {
disk = new EmulatorWorkerChunkedDisk(spec, this);
}
if (
needsDeviceImage &&
!spec.isFloppy &&
!spec.hasDeviceImageHeader
) {
return new EmulatorWorkerDeviceImageDisk(
disk,
config.deviceImageHeader
);
}
return disk;
}),
...diskFiles.map(spec => {
const disk = new EmulatorWorkerUploadDisk(spec, this);
if (
needsDeviceImage &&
!spec.isCDROM &&
!spec.hasDeviceImageHeader
) {
return new EmulatorWorkerDeviceImageDisk(
disk,
config.deviceImageHeader
);
}
return disk;
}),
...cdroms.map(spec =>
createEmulatorWorkerCDROMDisk(spec, this)
),
],
config.usePlaceholderDisks,
this.#emscriptenModule
);
this.#delayedDiskSpecs = delayedDisks;
}
#abortError?: string;
setAbortError(error: string) {
this.#abortError = error;
}
emulatorDidHaveError(status: number, toThrow?: Error) {
if (toThrow) {
console.error(toThrow);
}
let error = toThrow
? toThrow.message || toThrow.toString()
: `Exit status ${status}`;
if (this.#abortError && error.toLowerCase().includes("aborted")) {
error = this.#abortError;
this.#abortError = undefined;
}
postMessage({type: "emulator_did_have_error", error});
}
exit() {
this.#handleStop(true);
for (;;) {
// Don't let the process keep executing (it will exit with a non-0
// status code), instead wait for the worker to be terminated.
}
}
didOpenVideo(width: number, height: number) {
postMessage({type: "emulator_video_open", width, height});
this.#videoOpenTime = performance.now();
}
blit(bufPtr: number, bufSize: number, rect?: EmulatorWorkerVideoBlitRect) {
this.#lastBlitFrameId++;
if (bufPtr) {
const data = this.#emscriptenModule.HEAPU8.subarray(
bufPtr,
bufPtr + bufSize
);
this.#video.blit(data, rect);
}
this.#nextExpectedBlitTime = performance.now() + 16;
}
didOpenAudio(sampleRate: number, sampleSize: number, channels: number) {
postMessage({
type: "emulator_audio_open",
sampleRate,
sampleSize,
channels,
});
}
audioBufferSize(): number {
return this.#audio.audioBufferSize();
}
enqueueAudio(bufPtr: number, nbytes: number) {
if (
!this.getInputValue(
InputBufferAddresses.audioContextRunningFlagAddr
)
) {
// No point in filling up the buffer if we can't play it yet.
return;
}
const newAudio = this.#emscriptenModule.HEAPU8.slice(
bufPtr,
bufPtr + nbytes
);
this.#audio.enqueueAudio(newAudio);
}
idleWait(): boolean {
try {
return this.#idleWait();
} catch (err) {
// idleWait is the main place where we invoke JS during the emulator
// loop, make sure that we return control so that it can keep
// running.
console.error("Error during idlewait", err);
return false;
}
}
/**
* Variant of idleWait that is called called more frequently (and with an
* expected waiting time) by Mini vMac.
*/
sleep(timeSeconds: number) {
try {
this.#sleep(timeSeconds);
} catch (err) {
console.error("Error during sleep", err);
}
}
#idleWait(): boolean {
this.#markQuiescent();
// Don't do more than one call per frame, otherwise we end up skipping
// frames.
// TODO: understand why IdleWait is called multiple times in a row
// before VideoRefresh is called again.
if (this.#lastIdleWaitFrameId === this.#lastBlitFrameId) {
return false;
}
this.#lastIdleWaitFrameId = this.#lastBlitFrameId;
// Time out such that we don't miss any frames (giving 2 milliseconds
// for blit and any CPU emulation to run).
const idleWaitTime = this.#nextExpectedBlitTime - performance.now() - 2;
const hadInput =
idleWaitTime > 0 ? this.#input.idleWait(idleWaitTime) : false;
this.#periodicTasks();
return hadInput;
}
#sleep(timeSeconds: number) {
if (timeSeconds) {
this.#input.idleWait(timeSeconds * 1000);
}
if (this.#lastIdleWaitFrameId !== this.#lastBlitFrameId) {
this.#lastIdleWaitFrameId = this.#lastBlitFrameId;
this.#periodicTasks();
// We don't have a more accurate way to determine when Mini vMac
// is idle/quiescent (the Mac has finished booting), so we wait for
// the disk writes done during boot to finish.
if (this.disks.isDoneWithDiskWrites()) {
this.#markQuiescent();
}
// System 7.0 doesn't have idlewait and doesn't do disk writes, so
// we assume that it's done 10 seconds after video started.
if (
this.#videoOpenTime !== 0 &&
performance.now() - this.#videoOpenTime > 10000
) {
this.#markQuiescent();
}
}
}
#markQuiescent() {
if (!this.#markedQuiescent) {
this.#markedQuiescent = true;
postMessage({type: "emulator_quiescent"});
this.disks.validate();
if (this.#delayedDiskSpecs) {
for (const spec of this.#delayedDiskSpecs) {
console.log("Mounting delayed disk", spec.name);
this.disks.addDisk(
new EmulatorWorkerChunkedDisk(spec, this)
);
}
this.#delayedDiskSpecs = undefined;
}
}
}
#periodicTasks() {
// TODO: better place to poll for this? On the other hand, only doing it
// when the machine is idle seems reasonable.
this.#handleFileRequests();
handleExtractionRequests();
if (this.getInputValue(InputBufferAddresses.stopFlagAddr)) {
this.#handleStop();
}
}
#handleFileRequests() {
const {uploads, cdroms} = this.#files.consumeRequests();
for (const upload of uploads) {
const isDiskImage = isDiskImageFile(upload);
if (isDiskImage) {
let disk: EmulatorWorkerDisk = new EmulatorWorkerUploadDisk(
upload,
this
);
if (isCDROMBinFile(upload)) {
disk = new EmulatorWorkerMode1SectorDisk(disk);
}
this.disks.addDisk(disk);
} else {
this.#handleFileUpload(upload);
}
}
for (const cdrom of cdroms) {
this.disks.addDisk(createEmulatorWorkerCDROMDisk(cdrom, this));
}
}
#handleFileUpload(upload: EmulatorFileUpload) {
let parent = "/Shared/Downloads/";
let name = upload.name;
const pathPieces = upload.name.split("/");
if (pathPieces.length > 1) {
for (let i = 0; i < pathPieces.length - 1; i++) {
const dir = parent + pathPieces.slice(0, i + 1).join("/");
if (!FS.analyzePath(dir).exists) {
FS.mkdir(dir);
}
}
parent += pathPieces.slice(0, pathPieces.length - 1).join("/");
name = pathPieces[pathPieces.length - 1];
}
const contents = getUploadData(upload);
FS.createDataFile(
parent,
name,
contents,
true, // canRead
true, // canWrite
true // canOwn - no need for another copy
);
}
#handleStop(isExit = false) {
if (this.#handledStop) {
return;
}
this.#closeDiskSavers();
this.#handledStop = true;
postMessage({type: "emulator_stopped", isExit});
}
acquireInputLock(): number {
return this.#input.acquireInputLock();
}
releaseInputLock() {
this.#input.releaseInputLock();
}
getInputValue(addr: number): number {
return this.#input.getInputValue(addr);
}
etherSeed(): number {
const seed = new Uint32Array(1);
crypto.getRandomValues(seed);
return seed[0];
}
etherInit(macAddress: string) {
this.#ethernetMacAddress = ethernetMacAddressFromString(macAddress);
postMessage({type: "emulator_ethernet_init", macAddress});
}
etherWrite(destination: string, packetPtr: number, packetLength: number) {
const packet = this.#emscriptenModule.HEAPU8.slice(
packetPtr,
packetPtr + packetLength
);
sendEthernetPacket(destination, packet);
}
etherRead(packetPtr: number, packetMaxLength: number): number {
const packet = new Uint8Array(
this.#emscriptenModule.HEAPU8.buffer,
this.#emscriptenModule.HEAPU8.byteOffset + packetPtr,
packetMaxLength
);
const length = this.#ethernet.read(packet);
if (
length &&
this.#ethernetMacAddress &&
handlePingPacket(packet, length, this.#ethernetMacAddress)
) {
return 0;
}
return length;
}
setClipboardText(text: string) {
postMessage({type: "emulator_set_clipboard_text", text});
}
getClipboardText(): string | undefined {
return this.#clipboard.clipboardText();
}
// EmulatorWorkerChunkedDiskDelegate implementation
willLoadChunk(chunkIndex: number) {
postMessage({type: "emulator_will_load_chunk", chunkIndex});
}
didLoadChunk(chunkIndex: number) {
postMessage({type: "emulator_did_load_chunk", chunkIndex});
}
didFailToLoadChunk(chunkIndex: number, chunkUrl: string, error: string) {
postMessage({
type: "emulator_did_have_error",
error: `Could not load disk chunk ${chunkUrl}: ${error})`,
});
}
async initDiskSavers() {
const errorTuple = await initDiskSavers(this.#diskSavers);
if (errorTuple) {
const [error, errorRaw] = errorTuple;
postMessage({type: "emulator_did_have_error", error, errorRaw});
}
}
#closeDiskSavers() {
for (const saver of this.#diskSavers) {
saver.close();
}
}
}
export class EmulatorFallbackEndpoint {
#commandQueue: EmulatorFallbackCommand[] = [];
#fetchFailures = 0;
#loggedMaxFetchFailures = false;
idleWait(timeout: number) {
this.#fetchSync(`./worker-idlewait?timeout=${timeout}&t=${Date.now()}`);
}
consumeInputEvents(): EmulatorInputEvent[] {
return this.#consumeCommands(
(c): c is EmulatorFallbackInputCommand => c.type === "input",
c => c.event
);
}
consumeFileUploads(): EmulatorFileUpload[] {
return this.#consumeCommands(
(c): c is EmulatorFallbackUploadFileCommand =>
c.type === "upload_file",
c => c.upload
);
}
consumeCDROMs(): EmulatorCDROM[] {
return this.#consumeCommands(
(c): c is EmulatorFallbackLoadCDROMCommand =>
c.type === "load_cdrom",
c => c.cdrom
);
}
consumeEthernetReceives(): Uint8Array[] {
return this.#consumeCommands(
(c): c is EmulatorFallbackEthernetReceiveCommand =>
c.type === "ethernet_receive",
c => new Uint8Array(c.packetArray)
);
}
consumeSetClipboardData(): EmulatorClipboardData | undefined {
const datas = this.#consumeCommands(
(c): c is EmulatorFallbackSetClipboardDataCommand =>
c.type === "set_clipboard_data",
c => c.data
);
return datas[datas.length - 1];
}
#consumeCommands<T extends EmulatorFallbackCommand, V>(
filter: (command: EmulatorFallbackCommand) => command is T,
extractor: (command: T) => V
): V[] {
this.#fetchCommands();
const remainingCommands: EmulatorFallbackCommand[] = [];
const result: V[] = [];
for (const command of this.#commandQueue) {
if (filter(command)) {
result.push(extractor(command as T));
} else {
remainingCommands.push(command);
}
}
this.#commandQueue = remainingCommands;
return result;
}
#fetchCommands() {
const commands = this.#fetchSync(`./worker-commands?t=${Date.now()}`);
if (commands) {
this.#commandQueue = this.#commandQueue.concat(commands);
}
}
#fetchSync(url: string): any | undefined {
// Don't keep trying to fetch if it's failing. That likely means that
// the service worker was not registered or activated, and these
// requests are hitting the server directly, which we don't want.
if (this.#fetchFailures > 100) {
if (!this.#loggedMaxFetchFailures) {
console.error(
"Too many fetch failures, disabling fallback endpoint"
);
this.#loggedMaxFetchFailures = true;
}
return undefined;
}
const xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send(null);
if (xhr.status !== 200) {
console.warn(
"Could not do fetchSync",
url,
xhr.status,
xhr.statusText
);
this.#fetchFailures++;
return undefined;
}
try {
const value = JSON.parse(xhr.responseText);
this.#fetchFailures = 0;
return value;
} catch (err) {
console.warn("Could not parse JSON", err, xhr.responseText);
this.#fetchFailures++;
}
}
}
async function startEmulator(config: EmulatorWorkerConfig) {
const moduleOverrides: Partial<EmscriptenModule> & {
workerApi?: EmulatorWorkerApi;
} = {
arguments: config.arguments,
instantiateWasm(imports, successCallback) {
const {dateOffset} = config;
if (dateOffset) {
let overrodeDateFunctions = false;
for (const moduleImports of Object.values(imports)) {
// Emscripten 3.1.73 and earlier end up accessing the current
// time through an exported emscripten_date_now function,
// so we can just override that.
if ("emscripten_date_now" in moduleImports) {
moduleImports["emscripten_date_now"] = () => {
return Date.now() + dateOffset;
};
overrodeDateFunctions = true;
}
// From Emscripten 3.1.74 onwards, emscripten_date_now is
// still used but it's not exposed via moduleImports, so we
// need to override the slightly higher level clock_time_get
// function. It writes the return value into the heap, which
// we can't easily access from here, so it's easier to
// monkey-patch Date.now while it runs.
const originalClockTimeGet =
moduleImports["clock_time_get"];
if (originalClockTimeGet instanceof Function) {
const originalDateNow = Date.now;
moduleImports["clock_time_get"] = (...args: any[]) => {
const offsetNow = originalDateNow() + dateOffset;
Date.now = () => offsetNow;
const rv = originalClockTimeGet(...args);
Date.now = originalDateNow;
return rv;
};
overrodeDateFunctions = true;
}
}
if (!overrodeDateFunctions) {
console.warn(
"Neither emscripten_date_now or clock_time_get were found to override, time may be incorrect"
);
}
}
WebAssembly.instantiateStreaming(
new Response(config.wasm, {
headers: {"Content-Type": "application/wasm"},
}),
imports
)
.then(output => successCallback(output.instance))
.catch(error => {
console.error(error);
postMessage({
type: "emulator_did_have_error",
error: `Cannot instantiate WebAssembly: ${error.toString()}\n${
error.stack
}`,
});
});
return {};
},
preRun: [
function () {
// Manually export FS to the global scope since this matches the
// type definitions that Emscripten has (and there's no ESM
// export of the FS module).
globalThis["FS"] = moduleOverrides.FS!;
FS.mkdir("/Shared");
FS.mkdir("/Shared/Downloads");
initializeExtractor();
for (const [name, buffer] of Object.entries(
config.autoloadFiles
)) {
const path = name.split("/");
const fileName = path[path.length - 1];
const parentPath = [];
let parent = "/";
for (const dir of path.slice(0, path.length - 1)) {
parentPath.push(dir);
parent = "/" + parentPath.join("/");
if (!FS.analyzePath(parent).exists) {
FS.mkdir(parent);
}
}
FS.createDataFile(
parent,
fileName,
new Uint8Array(buffer),
true,
true,
true
);
}
},
],
onRuntimeInitialized() {
postMessage({type: "emulator_ready"});
},
print(...args: any[]) {
// Chrome and Safari's console.groupCollapsed works like a log
// statement, so we can use it to wrap the current stack trace,
// which is handy for debugging.
if (
(navigator.userAgent.includes("Chrome") ||
navigator.userAgent.includes("Safari")) &&
args.length === 1 &&
typeof args[0] === "string"
) {
console.groupCollapsed(`%c${args[0]}`, "font-weight:normal");
console.trace();
console.groupEnd();
} else {
console.log(...args);
}
},
printErr: console.warn.bind(console),
quit(status: number, toThrow?: Error) {
console.log("Emulator quit with status", status);
if (status === 0) {
moduleOverrides.workerApi?.exit();
} else {
moduleOverrides.workerApi?.emulatorDidHaveError(
status,
toThrow
);
}
},
};
async function runEmulatorModule(emulatorModule: {
default: (module: EmscriptenModule) => void;
}) {
// The module overrides get populated with the complete Emscripten
// module API once it is loaded.
const emscriptenModule: EmscriptenModule =
moduleOverrides as EmscriptenModule;
const workerApi = new EmulatorWorkerApi(config, emscriptenModule);
moduleOverrides.workerApi = workerApi;
await workerApi.initDiskSavers();
// Inject into global scope as `workerApi` so that the Emscripten
// code can call into it.
const globalScope = globalThis as any;
globalScope.workerApi = workerApi;
emulatorModule.default(emscriptenModule);
}
try {
runEmulatorModule(await importEmulator(config));
} catch (error) {
console.log("Could not import emulator, will try fallback mode", error);
try {
runEmulatorModule(await importEmulatorFallback(config));
} catch (error) {
postMessage({type: "emulator_did_have_error", error});
}
}
}