-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.ts
215 lines (179 loc) · 5.42 KB
/
worker.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) Thorsten Beier
// Copyright (c) JupyterLite Contributors
// Distributed under the terms of the Modified BSD License.
import { expose } from 'comlink';
import {
DriveFS,
DriveFSEmscriptenNodeOps,
IEmscriptenFSNode,
IStats
} from '@jupyterlite/contents';
declare function createXeusModule(options: any): any;
globalThis.Module = {};
const WASM_KERNEL_FILE = XEUS_KERNEL_FILE;
const DATA_FILE = LANGUAGE_DATA_FILE;
// TODO Remove this. This is to ensure we always perform node ops on Nodes and
// not Streams, but why is it needed??? Why do we get Streams and not Nodes from
// emscripten in the case of xeus-python???
class StreamNodeOps extends DriveFSEmscriptenNodeOps {
private getNode(nodeOrStream: any) {
if (nodeOrStream['node']) {
return nodeOrStream['node'];
}
return nodeOrStream;
}
lookup(parent: IEmscriptenFSNode, name: string): IEmscriptenFSNode {
return super.lookup(this.getNode(parent), name);
}
getattr(node: IEmscriptenFSNode): IStats {
return super.getattr(this.getNode(node));
}
setattr(node: IEmscriptenFSNode, attr: IStats): void {
super.setattr(this.getNode(node), attr);
}
mknod(
parent: IEmscriptenFSNode,
name: string,
mode: number,
dev: any
): IEmscriptenFSNode {
return super.mknod(this.getNode(parent), name, mode, dev);
}
rename(
oldNode: IEmscriptenFSNode,
newDir: IEmscriptenFSNode,
newName: string
): void {
super.rename(this.getNode(oldNode), this.getNode(newDir), newName);
}
rmdir(parent: IEmscriptenFSNode, name: string): void {
super.rmdir(this.getNode(parent), name);
}
readdir(node: IEmscriptenFSNode): string[] {
return super.readdir(this.getNode(node));
}
}
// TODO Remove this when we don't need StreamNodeOps anymore
class LoggingDrive extends DriveFS {
constructor(options: DriveFS.IOptions) {
super(options);
this.node_ops = new StreamNodeOps(this);
}
}
// when a toplevel cell uses an await, the cell is implicitly
// wrapped in a async function. Since the webloop - eventloop
// implementation does not support `eventloop.run_until_complete(f)`
// we need to convert the toplevel future in a javascript Promise
// this `toplevel` promise is then awaited before we
// execute the next cell. After the promise is awaited we need
// to do some cleanup and delete the python proxy
// (ie a js-wrapped python object) to avoid memory leaks
globalThis.toplevel_promise = null;
globalThis.toplevel_promise_py_proxy = null;
let resolveInputReply: any;
async function get_stdin() {
const replyPromise = new Promise(resolve => {
resolveInputReply = resolve;
});
return replyPromise;
}
(self as any).get_stdin = get_stdin;
class XeusKernel {
constructor() {
this._ready = new Promise(resolve => {
this.initialize(resolve);
});
}
async ready(): Promise<void> {
return await this._ready;
}
mount(driveName: string, mountpoint: string, baseUrl: string): void {
const { FS, PATH, ERRNO_CODES } = globalThis.Module;
if (!FS) {
return;
}
this._drive = new LoggingDrive({
FS,
PATH,
ERRNO_CODES,
baseUrl,
driveName,
mountpoint
});
FS.mkdir(mountpoint);
FS.mount(this._drive, {}, mountpoint);
FS.chdir(mountpoint);
}
cd(path: string) {
if (!path || !globalThis.Module.FS) {
return;
}
globalThis.Module.FS.chdir(path);
}
async processMessage(event: any): Promise<void> {
await this._ready;
if (
globalThis.toplevel_promise !== null &&
globalThis.toplevel_promise_py_proxy !== null
) {
await globalThis.toplevel_promise;
globalThis.toplevel_promise_py_proxy.delete();
globalThis.toplevel_promise_py_proxy = null;
globalThis.toplevel_promise = null;
}
const msg_type = event.msg.header.msg_type;
if (msg_type === 'input_reply') {
resolveInputReply(event.msg);
} else {
this._raw_xserver.notify_listener(event.msg);
}
}
private async initialize(resolve: () => void) {
importScripts(WASM_KERNEL_FILE);
globalThis.Module = await createXeusModule({});
try {
if (DATA_FILE.length !== 0) {
importScripts(DATA_FILE);
await this.waitRunDependency();
}
this._raw_xkernel = new globalThis.Module.xkernel();
this._raw_xserver = this._raw_xkernel.get_server();
if (!this._raw_xkernel) {
console.error('Failed to start kernel!');
}
this._raw_xkernel.start();
}
catch (e) {
if( typeof e === 'number' ) {
const msg = globalThis.Module.get_exception_message(e);
console.error(msg);
throw new Error(msg);
}
else {
console.error(e);
throw(e);
}
}
resolve();
}
private async waitRunDependency() {
const promise = new Promise<void>(resolve => {
globalThis.Module.monitorRunDependencies = (n: number) => {
if (n === 0) {
resolve();
}
};
});
// If there are no pending dependencies left, monitorRunDependencies will
// never be called. Since we can't check the number of dependencies,
// manually trigger a call.
globalThis.Module.addRunDependency('dummy');
globalThis.Module.removeRunDependency('dummy');
return promise;
}
private _raw_xkernel: any;
private _raw_xserver: any;
private _drive: DriveFS | null = null;
private _ready: PromiseLike<void>;
}
expose(new XeusKernel());