-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyodide-main.js
112 lines (97 loc) · 2.64 KB
/
pyodide-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as Synclink from "https://unpkg.com/[email protected]/dist/esm/synclink.mjs";
window.Synclink = Synclink;
let pyodide;
let InnerExecution;
let BANNER;
let complete;
function sleep(t) {
return new Promise((resolve) => setTimeout(resolve, t));
}
function promiseHandles() {
let result;
let promise = new Promise((resolve, reject) => {
result = { resolve, reject };
});
result.promise = promise;
return result;
}
let { resolve: resolveInitialized, promise: initialized } = promiseHandles();
async function initializePyodide() {
const worker = new Worker("pyodide-worker.js");
const wrapper = Synclink.wrap(worker);
const result = await wrapper(Synclink.proxy(window));
({ pyodide, InnerExecution, BANNER, complete } = result);
wrapper[Synclink.releaseProxy]();
BANNER = "Welcome to the Pyodide terminal emulator 🐍\n" + (await BANNER);
window.pyodide = pyodide;
resolveInitialized();
}
Synclink.transferHandlers.set("EVENT", {
canHandle: (obj) => obj instanceof Event,
serialize: (ev) => {
return [
{
target: {
id: ev.target.id,
classList: [...ev.target.classList],
clientX: ev.clientX,
clientY: ev.clientY,
},
},
[],
];
},
deserialize: (obj) => obj,
});
class Execution {
constructor(code) {
return (async () => {
await initialized;
this._inner = await new InnerExecution(code);
this._result = this._inner.result();
this._validate_syntax = this._inner.validate_syntax();
this._interrupt_buffer = await this._inner.interrupt_buffer();
this._started = false;
return this;
})();
}
start() {
this._started = true;
this._inner.start().schedule_async();
}
keyboardInterrupt() {
this._interrupt_buffer[0] = 2;
}
validate_syntax() {
return this._validate_syntax;
}
result() {
return this._result;
}
async onStdin(callback) {
if (this._started) {
throw new Error(
"Cannot set standard in callback after starting the execution."
);
}
await this._inner.setStdin(Synclink.proxy(callback));
}
async onStdout(callback) {
if (this._started) {
throw new Error(
"Cannot set standard out callback after starting the execution."
);
}
await this._inner.onStdout(Synclink.proxy(callback));
}
async onStderr(callback) {
if (this._started) {
throw new Error(
"Cannot set standard error callback after starting the execution."
);
}
await this._inner.onStderr(Synclink.proxy(callback));
}
}
window.Execution = Execution;
export { Execution, pyodide, BANNER, complete, initializePyodide };