-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03f7738
commit fd1e376
Showing
8 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { LanguageSupport } from "@codemirror/language"; | ||
|
||
export type OutputLine = { | ||
type: string; | ||
type: 'log' | 'error' | 'warn'; | ||
line: string; | ||
}; | ||
|
||
export interface Kernel { | ||
run(code: string, onOutput: (line: OutputLine) => void, onDone: () => void); | ||
getSyntaxHighlighter(): LanguageSupport; | ||
export abstract class Kernel { | ||
requestID: number = 0; | ||
getRequestID() { | ||
return this.requestID++; | ||
} | ||
|
||
abstract run(code: string, onOutput: (line: OutputLine) => void, onDone: () => void); | ||
abstract getSyntaxHighlighter(): LanguageSupport; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @ts-ignore - Load worker source code. | ||
import WORKER_SRC from "!raw-loader!ts-loader!./worker.ts"; | ||
const blob = new Blob([WORKER_SRC], { type: "application/javascript" }); | ||
|
||
import { python } from "@codemirror/lang-python"; | ||
import { Kernel, OutputLine } from "../kernel"; | ||
|
||
export class PythonKernel extends Kernel { | ||
worker: Worker = new Worker(URL.createObjectURL(blob)); | ||
|
||
run(code: string, onOutput: (line: OutputLine) => void, onDone: () => void) { | ||
// Generate a unique ID to track this execution request. | ||
const requestID = this.getRequestID(); | ||
|
||
const messageHandler = (e: MessageEvent) => { | ||
if (e.data.requestID != requestID) return; | ||
|
||
if (e.data.kind === "run-code-output") { | ||
onOutput(e.data.output); | ||
} else if (e.data.kind === "run-code-done") { | ||
this.worker.removeEventListener("message", messageHandler); | ||
onDone(); | ||
} | ||
}; | ||
|
||
this.worker.addEventListener("message", messageHandler); | ||
|
||
// Post the code to the worker. | ||
this.worker.postMessage({ | ||
kind: "run-code", | ||
code: code, | ||
requestID: requestID, | ||
}); | ||
} | ||
|
||
|
||
getSyntaxHighlighter() { | ||
return python(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
importScripts("https://cdn.jsdelivr.net/npm/[email protected]/pyodide.min.js"); | ||
declare var loadPyodide; | ||
|
||
let onStdout: ((str) => void) | null = null; | ||
|
||
// Start loading Pyodide asynchronously. | ||
const loadPython = (async () => { | ||
return await loadPyodide({ | ||
stdout: (msg) => { | ||
if (msg === "Python initialization complete") return; | ||
if (onStdout) onStdout(msg); | ||
}, | ||
}); | ||
})(); | ||
|
||
self.onmessage = async (e: MessageEvent) => { | ||
console.log("Worker received message: %o", e); | ||
const requestID = e.data.requestID; | ||
|
||
if (e.data.kind === "run-code") { | ||
// Register stdout callback. | ||
onStdout = (msg) => { | ||
self.postMessage({ | ||
kind: "run-code-output", | ||
requestID: requestID, | ||
output: {type: "log", line: msg}, | ||
}); | ||
}; | ||
|
||
try { | ||
// Wait for Pyodide to load. | ||
const pyodide = await loadPython; | ||
|
||
// Run code in a new namespace. | ||
pyodide.runPython(e.data.code); | ||
} catch (error) { | ||
self.postMessage({ | ||
kind: "run-code-output", | ||
requestID: requestID, | ||
output: {type: "error", line: error.toString()}, | ||
}); | ||
} finally { | ||
// Unregister stdout callback. | ||
onStdout = null; | ||
|
||
self.postMessage({ | ||
kind: "run-code-done", | ||
requestID: requestID, | ||
}); | ||
} | ||
} | ||
}; |