-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmavka_web.js
More file actions
232 lines (183 loc) · 5.51 KB
/
Copy pathmavka_web.js
File metadata and controls
232 lines (183 loc) · 5.51 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
window.MAVKA_WEB_URL = "https://веб.мавка.укр";
class Mavka {
version;
options;
worker;
loading;
ready;
readyListener;
eid;
listeners;
static getSupportError() {
if (typeof WebAssembly === "undefined") {
return "WebAssembly недоступний у цьому браузері.";
}
if (typeof WebAssembly.Memory !== "function") {
return "WebAssembly Memory API не доступне; вимагається підтримка wasm64.";
}
try {
new WebAssembly.Memory({ initial: 1, maximum: 1, memory64: true });
return null;
} catch (e) {
return "Вимагається підтримка WebAssembly 64-bit. Ваш браузер або середовище не підтримує wasm64.";
}
}
constructor(version, options) {
this.version = version;
this.options = options || {};
this.reset();
}
reset() {
this.worker = null;
this.loading = false;
this.ready = false;
this.readyListener = null;
this.eid = 0;
this.listeners = new Map();
}
load() {
return new Promise(async (res, rej) => {
if (this.ready) {
rej(new Error("Mavka already loaded!"));
return;
}
if (this.loading) {
rej(new Error("Mavka is loading!"));
return;
}
async function createCrossOriginWorker(url) {
const response = await fetch(url);
const scriptText = await response.text();
const blob = new Blob([scriptText], { type: 'application/javascript' });
const blobURL = URL.createObjectURL(blob);
return new Worker(blobURL);
}
this.worker = await createCrossOriginWorker(`${window.MAVKA_WEB_URL}/версії/${this.version}/mavka_worker.js`);
this.readyListener = { res, rej };
this.worker.postMessage({ type: "INIT", mavkaWebUrl: window.MAVKA_WEB_URL });
this.worker.onmessage = (event) => {
const eventData = event.data;
if (eventData && typeof eventData === "object") {
const type = eventData.type;
const id = eventData.id;
if (type === "MAVKA_READY") {
this.ready = true;
this.readyListener.res();
this.readyListener = null;
return;
}
if (type === "WRITE_RESULT") {
const l = this.listeners.get(id);
this.listeners.delete(id);
if (eventData.error) {
l.rej(eventData.error);
} else {
l.res();
}
}
if (type === "READ_RESULT") {
const l = this.listeners.get(id);
this.listeners.delete(id);
if (eventData.error) {
l.rej(eventData.error);
} else {
l.res(eventData.result);
}
}
if (type === "RUN_RESULT") {
const l = this.listeners.get(id);
this.listeners.delete(id);
if (eventData.resultCode === 0) {
l.res(eventData.resultCode);
} else {
l.rej({ error: eventData.error, resultCode: eventData.resultCode });
}
}
if (type === "PRINT") {
const value = eventData.value;
const color = eventData.color;
if (this.options.print) {
this.options.print(value, color);
} else {
alert(value);
}
}
if (type === "READLINE") {
const prefix = eventData.prefix;
if (this.options.readline) {
this.options.readline((value) => {
this.worker.postMessage({ type: "READLINE_RESULT", id, value });
}, prefix);
} else {
const value = prompt(prefix);
this.worker.postMessage({ type: "READLINE_RESULT", id, value });
}
}
}
};
})
}
write(path, value) {
return new Promise((res, rej) => {
if (!this.ready) {
rej(new Error("Mavka not ready!"));
return;
}
const id = this.eid++;
this.worker.postMessage({ type: "WRITE", id, path, value });
this.listeners.set(id, { res, rej });
});
}
read(path, resultType) {
return new Promise((res, rej) => {
if (!this.ready) {
rej(new Error("Mavka not ready!"));
return;
}
const id = this.eid++;
this.worker.postMessage({ type: "READ", id, path, resultType });
this.listeners.set(id, { res, rej });
});
}
readBytes(path) {
return this.read(path, "bytes");
}
readString(path) {
return this.read(path, "string");
}
run(args) {
if (!args) {
args = [];
}
if (typeof args === "string") {
args = [args];
}
return new Promise((res, rej) => {
if (!this.ready) {
rej(new Error("Mavka not ready!"));
return;
}
const id = this.eid++;
this.worker.postMessage({ type: "RUN", id, args });
this.listeners.set(id, { res, rej });
});
}
terminate() {
if (!this.worker) {
throw new Error("No worker!");
}
this.worker.terminate();
this.reset();
}
static async fetchAvailableVersions() {
const url = `${window.MAVKA_WEB_URL}/версії/версії.txt`;
const response = await fetch(url, {
cache: 'no-store'
});
const text = await response.text();
const lines = text.trim().split("\n");
return lines
.map((line) => line.split(":"))
.map(([pkg, mavka]) => ({ pkg, mavka }))
}
}