big commit
This commit is contained in:
parent
aff885cf17
commit
b506f5a219
22 changed files with 692 additions and 556 deletions
|
@ -1,8 +1,21 @@
|
|||
let outputIndex = 0;
|
||||
|
||||
export function getOutputIndex() {
|
||||
return outputIndex;
|
||||
}
|
||||
export const jsConsole = console;
|
||||
|
||||
// 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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async function withTemporaryGlobalScope(callback) {
|
||||
let state = {
|
||||
|
@ -13,6 +26,7 @@ async function withTemporaryGlobalScope(callback) {
|
|||
}
|
||||
};
|
||||
await callback(state);
|
||||
jsConsole.trace(state.oldValues, "bringing back old state");
|
||||
for (let key in state.oldValues) {
|
||||
globalThis[key] = state.oldValues[key];
|
||||
}
|
||||
|
@ -20,15 +34,11 @@ async function withTemporaryGlobalScope(callback) {
|
|||
|
||||
let evaluationComplete = null;
|
||||
|
||||
export async function evaluate(commands, { start, success, error }) {
|
||||
export async function evaluate(commands, { error, newOutput }) {
|
||||
if (evaluationComplete != null) {
|
||||
await evaluationComplete;
|
||||
}
|
||||
|
||||
if (start != null) {
|
||||
start();
|
||||
}
|
||||
|
||||
let signalEvaluationComplete;
|
||||
evaluationComplete = new Promise((resolve, _reject) => {
|
||||
signalEvaluationComplete = resolve;
|
||||
|
@ -36,21 +46,19 @@ export async function evaluate(commands, { start, success, error }) {
|
|||
|
||||
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;
|
||||
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) {
|
||||
globalThis[exportedKey] = module[exportedKey];
|
||||
}
|
||||
} else if (command.kind == "output") {
|
||||
if (newOutput != null) {
|
||||
newOutput(outputIndex);
|
||||
}
|
||||
++outputIndex;
|
||||
}
|
||||
});
|
||||
if (success != null) {
|
||||
success();
|
||||
}
|
||||
postMessage({
|
||||
kind: "evalComplete",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue