-
Notifications
You must be signed in to change notification settings - Fork 4
/
kernel.js
59 lines (53 loc) · 1.64 KB
/
kernel.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
const bs = new BroadcastChannel('rpc');
let rprc_id = 0;
const modules = new Proxy({}, {
get(target, namespace) {
return new Proxy({}, {
get(target, name) {
return (...args) => {
return new Promise((resolve, reject) => {
const id = ++rprc_id;
const method = name;
bs.addEventListener('message', function handler({data}) {
if (data.id === id) {
if (data.error) {
reject(data.error);
} else {
resolve(data.result);
}
bs.removeEventListener('message', handler);
}
});
bs.postMessage({
id,
namespace,
method,
args
});
});
};
}
});
}
});
let handler_id = 0;
const handlers = {};
function open(filename) {
return new Promise((resolve, reject) => {
fs.stat(filename).then(() => {
const id = ++handler_id;
handlers[id] = filename;
resolve(id);
}).catch(err => {
reject(err);
});
});
}
const kernel_bs = new BroadcastChannel('rpc');
const kernel = {
exec(program) {
return fs.readFile(program).then(code => {
eval(code);
});
}
};