2024-02-18 00:29:58 +01:00
|
|
|
let outputIndex = 0;
|
|
|
|
|
|
|
|
export function getOutputIndex() {
|
|
|
|
return outputIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function withTemporaryGlobalScope(callback) {
|
|
|
|
let state = {
|
|
|
|
oldValues: {},
|
|
|
|
set(key, value) {
|
|
|
|
this.oldValues[key] = globalThis[key];
|
|
|
|
globalThis[key] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await callback(state);
|
|
|
|
for (let key in state.oldValues) {
|
|
|
|
globalThis[key] = state.oldValues[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 12:10:02 +01:00
|
|
|
let evaluationComplete = null;
|
|
|
|
|
|
|
|
export async function evaluate(commands, { start, success, error }) {
|
|
|
|
if (evaluationComplete != null) {
|
|
|
|
await evaluationComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start != null) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
let signalEvaluationComplete;
|
|
|
|
evaluationComplete = new Promise((resolve, _reject) => {
|
|
|
|
signalEvaluationComplete = resolve;
|
|
|
|
})
|
|
|
|
|
2024-02-18 00:29:58 +01:00
|
|
|
outputIndex = 0;
|
|
|
|
try {
|
|
|
|
await withTemporaryGlobalScope(async scope => {
|
|
|
|
for (let command of commands) {
|
|
|
|
if (command.kind == "module") {
|
|
|
|
let blobUrl = URL.createObjectURL(new Blob([command.source], { type: "text/javascript" }));
|
|
|
|
let module = await import(blobUrl);
|
|
|
|
for (let exportedKey in module) {
|
|
|
|
scope.set(exportedKey, module[exportedKey]);
|
|
|
|
}
|
|
|
|
} else if (command.kind == "output") {
|
|
|
|
++outputIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-02-18 12:10:02 +01:00
|
|
|
if (success != null) {
|
|
|
|
success();
|
|
|
|
}
|
|
|
|
postMessage({
|
|
|
|
kind: "evalComplete",
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2024-02-18 00:29:58 +01:00
|
|
|
postMessage({
|
|
|
|
kind: "output",
|
|
|
|
output: {
|
|
|
|
kind: "error",
|
2024-02-18 12:10:02 +01:00
|
|
|
message: [err.toString()],
|
2024-02-18 00:29:58 +01:00
|
|
|
},
|
|
|
|
outputIndex,
|
|
|
|
});
|
2024-02-18 12:10:02 +01:00
|
|
|
if (error != null) {
|
|
|
|
error();
|
|
|
|
}
|
2024-02-18 00:29:58 +01:00
|
|
|
}
|
2024-02-18 12:10:02 +01:00
|
|
|
signalEvaluationComplete();
|
2024-02-18 00:29:58 +01:00
|
|
|
}
|
|
|
|
|