treehouse/static/js/components/literate-programming/eval.js

101 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-02-18 00:29:58 +01:00
let outputIndex = 0;
2024-02-18 23:37:31 +01:00
export const jsConsole = console;
2024-07-26 23:21:29 +02:00
const loggingEnabled = false;
function log(...message) {
if (loggingEnabled) {
jsConsole.log("[eval]", ...message);
}
}
2024-02-18 23:37:31 +01:00
// Overwrite globalThis.console with domConsole to redirect output to the DOM console.
// To always output to the JavaScript console regardless, use jsConsole.
export const domConsole = {
log(...message) {
postMessage({
kind: "output",
output: {
kind: "console.log",
message: [...message],
},
outputIndex,
});
},
};
2024-07-26 23:21:29 +02:00
export async function defaultEvalModule(_state, source, language, _params) {
if (language == "javascript") {
let blobUrl = URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
let module = await import(blobUrl);
for (let exportedKey in module) {
globalThis[exportedKey] = module[exportedKey];
}
2024-07-26 23:21:29 +02:00
return _state;
} else {
return null;
}
}
let kernel = {
evalModule: defaultEvalModule,
2024-02-18 23:37:31 +01:00
};
2024-02-18 00:29:58 +01:00
export function getKernel() {
return kernel;
}
2024-02-18 12:10:02 +01:00
let evaluationComplete = null;
2024-02-18 23:37:31 +01:00
export async function evaluate(commands, { error, newOutput }) {
2024-02-18 12:10:02 +01:00
if (evaluationComplete != null) {
await evaluationComplete;
}
let signalEvaluationComplete;
evaluationComplete = new Promise((resolve, _reject) => {
signalEvaluationComplete = resolve;
});
2024-02-18 00:29:58 +01:00
outputIndex = 0;
try {
2024-07-26 23:21:29 +02:00
let kernelState = {};
2024-02-18 23:37:31 +01:00
for (let command of commands) {
2024-07-26 23:21:29 +02:00
log(`frame ${treehouseSandboxInternals.outputIndex} module`, command);
2024-02-18 23:37:31 +01:00
if (command.kind == "module") {
await kernel.evalModule(
kernelState,
command.source,
command.language,
command.kernelParameters,
);
2024-02-18 23:37:31 +01:00
} else if (command.kind == "output") {
if (newOutput != null) {
newOutput(outputIndex);
2024-02-18 00:29:58 +01:00
}
2024-02-18 23:37:31 +01:00
++outputIndex;
2024-02-18 00:29:58 +01:00
}
2024-02-18 12:10:02 +01:00
}
2024-07-26 23:21:29 +02:00
log(`frame ${treehouseSandboxInternals.outputIndex} evalComplete`);
2024-02-18 12:10:02 +01:00
postMessage({
kind: "evalComplete",
});
} catch (err) {
2024-07-26 23:21:29 +02:00
log(`frame ${treehouseSandboxInternals.outputIndex} error`, err);
2024-02-18 00:29:58 +01:00
postMessage({
kind: "output",
output: {
kind: "error",
2024-07-25 23:36:50 +02:00
message: [
err.stack.length > 0 ? err.toString() + "\n\n" + err.stack : 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
}