-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_template.js
More file actions
247 lines (216 loc) · 9.66 KB
/
_template.js
File metadata and controls
247 lines (216 loc) · 9.66 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ⚠️ This file is used to generate xworker.js
// That means if any import is circular or brings in too much
// that would be a higher payload for every worker.
// Please check via `npm run size` that worker code is not much
// bigger than it used to be before any changes is applied to this file.
import * as JSON from '@ungap/structured-clone/json';
import coincident from 'coincident/window';
import { assign, create, createFunction, createOverload, createResolved, dispatch, registerJSModules } from '../utils.js';
import createJSModules from './js_modules.js';
import { configs, registry } from '../interpreters.js';
import { getRuntime, getRuntimeID } from '../loader.js';
import { patch, polluteJS, js as jsHooks, code as codeHooks } from '../hooks.js';
const SAB = 'SharedArrayBuffer';
let interpreter, runEvent, transform;
const add = (type, fn) => {
addEventListener(
type,
fn ||
(async (event) => {
try {
await interpreter;
runEvent(`xworker.on${type}`, event);
} catch (error) {
postMessage(error);
}
}),
!!fn && { once: true },
);
};
const { parse, stringify } = JSON;
const { proxy: sync, window, isWindowProxy } = coincident(self, {
parse,
stringify,
transform: value => transform ? transform(value) : value
});
const xworker = {
// allows synchronous utilities between this worker and the main thread
sync,
// allow access to the main thread world
window,
// allow introspection for foreign (main thread) references
isWindowProxy,
// standard worker related events / features
onmessage: console.info,
onerror: console.error,
onmessageerror: console.warn,
postMessage: postMessage.bind(self),
};
add('message', ({ data: { options, config: baseURL, configURL, code, hooks } }) => {
interpreter = (async () => {
try {
const { id, tag, type, custom, version, config, named, async: isAsync } = options;
const runtimeID = getRuntimeID(type, version);
const interpreter = await getRuntime(runtimeID, baseURL, configURL, config);
const { js_modules, sync_main_only } = configs.get(runtimeID);
const mainModules = js_modules?.main;
// this flag allows interacting with the xworker.sync exposed
// *only in the worker* and eventually invoked *only from main*.
// If that flag is `false` or not present, then SharedArrayBuffer
// must be available or not much can work in here.
let syncMainAndWorker = !sync_main_only;
// bails out out of the box with a native/meaningful error
// in case the SharedArrayBuffer is not available
try {
new SharedArrayBuffer(4);
// if this does not throw there's no reason to
// branch out of all the features ... but ...
syncMainAndWorker = true;
}
// eslint-disable-next-line no-unused-vars
catch (_) {
// if it does throw and `sync_main_only` was not `true`
// then there's no way to go further
if (syncMainAndWorker) {
const prefix = `Unable to use ${SAB}`;
if (!named || sync_main_only === false) {
throw new Error(
[
`${prefix} due insecure environment.`,
`Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/${SAB}#security_requirements`,
].join('\n'),
);
}
console.warn(`${prefix}. Fallback to async only interaction.`);
}
syncMainAndWorker = false;
}
const details = create(registry.get(type));
const resolved = createResolved(
details,
custom || type,
config || {},
interpreter
);
let name = 'run';
if (isAsync) name += 'Async';
if (hooks) {
let before = '';
let after = '';
for (const key of codeHooks) {
const value = hooks[key];
if (value) {
const asyncCode = key.endsWith('Async');
// either async hook and this worker is async
// or sync hook and this worker is sync
// other shared options possible cases are ignored
if ((asyncCode && isAsync) || (!asyncCode && !isAsync)) {
if (key.startsWith('codeBefore'))
before = value;
else
after = value;
}
}
}
if (before || after)
createOverload(details, name, before, after);
let beforeCB, afterCB;
// exclude onWorker and onReady
for (const key of jsHooks.slice(2)) {
const value = hooks[key];
if (value) {
const asyncCode = key.endsWith('Async');
if ((asyncCode && isAsync) || (!asyncCode && !isAsync)) {
const cb = createFunction(value);
if (key.startsWith('onBefore'))
beforeCB = cb;
else
afterCB = cb;
}
}
}
polluteJS(details, resolved, xworker, isAsync, beforeCB, afterCB);
}
// there's no way to query the DOM, use foreign CustomEvent and so on
// in case there's no SharedArrayBuffer around.
let CustomEvent, document, notify, currentScript = null, target = '';
if (syncMainAndWorker) {
({ CustomEvent, document } = window);
currentScript = id && document.getElementById(id) || null;
notify = kind => dispatch(currentScript, custom || type, kind, true, CustomEvent);
}
// TODO: even this is problematic without SharedArrayBuffer
// but let's see if we can manage to make it work somehow.
const JSModules = createJSModules(window, sync, mainModules, baseURL);
registerJSModules(type, details, interpreter, JSModules);
details.registerJSModule(interpreter, 'polyscript', {
xworker,
currentScript,
config: resolved.config,
js_modules: JSModules,
get target() {
if (!target && currentScript) {
if (tag === 'SCRIPT') {
currentScript.after(assign(
document.createElement(`script-${custom || type}`),
{ id: (target = `${id}-target`) }
));
}
else {
target = id;
currentScript.replaceChildren();
currentScript.style.display = 'block';
}
}
return target;
}
});
// simplify runEvent calls
runEvent = details.runEvent.bind(details, interpreter);
// allows transforming arguments with sync
transform = details.transform.bind(details, interpreter);
// notify worker ready to execute
if (currentScript) notify('ready');
// evaluate the optional `onReady` callback
if (hooks?.onReady) {
createFunction(hooks?.onReady).call(
details,
patch.call(details, resolved, interpreter),
xworker,
);
}
// run either sync or async code in the worker
await details[name](interpreter, code);
if (['micropython', 'pyodide'].includes(details.type)) {
// this dance is required due Pyodide issues with runtime sync exports
// or MicroPython issue with `runPython` not returning values
const polyscript = 'polyscript';
const workers = `__${polyscript}_workers__`;
const exports = '__export__';
interpreter.runPython([
`import js as ${workers}`,
`${workers}.${workers} = "${exports}" in locals() and ${exports} or []`,
`del ${workers}`,
].join('\n'));
const list = [...globalThis[workers]];
delete globalThis[workers];
if (list.length) {
interpreter.runPython([
`from ${polyscript} import xworker as ${workers}`,
...list.map(util => `${workers}.sync.${util} = ${util}`),
`del ${workers}`,
].join('\n'));
}
}
// notify worker done executing
if (currentScript) notify('done');
postMessage('polyscript:done');
return interpreter;
} catch (error) {
postMessage(error);
}
})();
add('error');
add('message');
add('messageerror');
});