mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-11 16:46:16 +00:00
81 lines
1.5 KiB
JavaScript
81 lines
1.5 KiB
JavaScript
var logLines = [];
|
|
var lastFlush = new Date().getTime();
|
|
|
|
var msgQueue = [];
|
|
|
|
onmessage = async (event) => {
|
|
const data = event.data;
|
|
const payload = data.data;
|
|
|
|
switch (data.message) {
|
|
case "run":
|
|
runEmulation(payload.file, payload.options);
|
|
break;
|
|
case "event":
|
|
msgQueue.push(payload);
|
|
break;
|
|
}
|
|
};
|
|
|
|
function sendMessage(message, data) {
|
|
postMessage({ message, data });
|
|
}
|
|
|
|
function flushLines() {
|
|
const lines = logLines;
|
|
logLines = [];
|
|
lastFlush = new Date().getTime();
|
|
sendMessage("log", lines);
|
|
}
|
|
|
|
function logLine(text) {
|
|
logLines.push(text);
|
|
|
|
const now = new Date().getTime();
|
|
|
|
if (lastFlush + 15 < now) {
|
|
flushLines();
|
|
}
|
|
}
|
|
|
|
function notifyExit(code) {
|
|
flushLines();
|
|
sendMessage("end", code);
|
|
self.close();
|
|
}
|
|
|
|
function handleMessage(message) {
|
|
sendMessage("event", message);
|
|
}
|
|
|
|
function getMessageFromQueue() {
|
|
if (msgQueue.length == 0) {
|
|
return "";
|
|
}
|
|
|
|
return msgQueue.shift();
|
|
}
|
|
|
|
function runEmulation(file, options) {
|
|
const mainArguments = [...options, "-e", "./root", file];
|
|
|
|
globalThis.Module = {
|
|
arguments: mainArguments,
|
|
noInitialRun: true,
|
|
onRuntimeInitialized: function () {
|
|
FS.mkdir("/root");
|
|
FS.mount(IDBFS, {}, "/root");
|
|
FS.syncfs(true, function (_) {
|
|
Module.callMain(mainArguments);
|
|
});
|
|
},
|
|
print: logLine,
|
|
printErr: logLine,
|
|
onAbort: () => notifyExit(null),
|
|
onExit: notifyExit,
|
|
postRun: flushLines,
|
|
};
|
|
|
|
importScripts("./analyzer.js");
|
|
}
|