mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-11 16:46:16 +00:00
66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
var logLines = [];
|
|
var lastFlush = new Date().getTime();
|
|
|
|
onmessage = async (event) => {
|
|
const data = event.data;
|
|
if (data.message == "run") {
|
|
const payload = data.data;
|
|
runEmulation(payload.filesystem, payload.file, payload.options);
|
|
}
|
|
};
|
|
|
|
function flushLines() {
|
|
const lines = logLines;
|
|
logLines = [];
|
|
lastFlush = new Date().getTime();
|
|
postMessage({ message: "log", data: lines });
|
|
}
|
|
|
|
function logLine(text) {
|
|
logLines.push(text);
|
|
|
|
const now = new Date().getTime();
|
|
|
|
if (lastFlush + 15 < now) {
|
|
flushLines();
|
|
}
|
|
}
|
|
|
|
function notifyExit(code) {
|
|
flushLines();
|
|
postMessage({ message: "end", data: code });
|
|
self.close();
|
|
}
|
|
|
|
function getMessageFromQueue() {
|
|
return "";
|
|
}
|
|
|
|
function runEmulation(filesystem, file, options) {
|
|
globalThis.Module = {
|
|
arguments: [...options, "-e", "./root", file],
|
|
onRuntimeInitialized: function () {
|
|
filesystem.forEach((e) => {
|
|
if (e.name.endsWith("/")) {
|
|
FS.mkdir(e.name.slice(0, -1));
|
|
} else {
|
|
const dirs = e.name.split("/");
|
|
const file = dirs.pop();
|
|
const buffer = new Uint8Array(e.data);
|
|
if (FS.analyzePath(e.name).exists) {
|
|
FS.unlink(e.name);
|
|
}
|
|
FS.createDataFile("/" + dirs.join("/"), file, buffer, true, true);
|
|
}
|
|
});
|
|
},
|
|
print: logLine,
|
|
printErr: logLine,
|
|
onAbort: () => notifyExit(null),
|
|
onExit: notifyExit,
|
|
postRun: flushLines,
|
|
};
|
|
|
|
importScripts("./analyzer.js");
|
|
}
|