@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.d801d8d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/node_modules/@emnapi/core/LICENSE +21 -0
- package/node_modules/@emnapi/core/README.md +1 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +6661 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +363 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +363 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +365 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +7411 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.js +8390 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +363 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -0
- package/node_modules/@emnapi/core/dist/emnapi-core.mjs +6650 -0
- package/node_modules/@emnapi/core/index.js +5 -0
- package/node_modules/@emnapi/core/package.json +49 -0
- package/node_modules/@emnapi/runtime/LICENSE +21 -0
- package/node_modules/@emnapi/runtime/README.md +1 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +1347 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +663 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +663 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +665 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +1403 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +420 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +1474 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.js +1475 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +663 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -0
- package/node_modules/@emnapi/runtime/dist/emnapi.mjs +1316 -0
- package/node_modules/@emnapi/runtime/index.js +5 -0
- package/node_modules/@emnapi/runtime/package.json +48 -0
- package/node_modules/@emnapi/wasi-threads/LICENSE +21 -0
- package/node_modules/@emnapi/wasi-threads/README.md +203 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +881 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +214 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +214 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +216 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +929 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +941 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +214 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -0
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +876 -0
- package/node_modules/@emnapi/wasi-threads/index.js +5 -0
- package/node_modules/@emnapi/wasi-threads/package.json +50 -0
- package/node_modules/@napi-rs/wasm-runtime/LICENSE +43 -0
- package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +23712 -0
- package/node_modules/@napi-rs/wasm-runtime/dist/runtime.js +12269 -0
- package/node_modules/@napi-rs/wasm-runtime/fs-proxy.cjs +193 -0
- package/node_modules/@napi-rs/wasm-runtime/package.json +60 -0
- package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +15 -0
- package/node_modules/@tybys/wasm-util/README.md +193 -0
- package/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +11 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +417 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +2738 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +2738 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.js +2760 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +158 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/index.js +13 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +46 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/load.js +97 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/memory.js +35 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +103 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +268 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +3 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +194 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +174 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +1544 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +141 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +220 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +128 -0
- package/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +13 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +153 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +9 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +39 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +89 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +28 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +97 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +256 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +1 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +188 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +168 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +1539 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +135 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +216 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +113 -0
- package/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +9 -0
- package/node_modules/@tybys/wasm-util/package.json +58 -0
- package/node_modules/tslib/CopyrightNotice.txt +15 -0
- package/node_modules/tslib/LICENSE.txt +12 -0
- package/node_modules/tslib/README.md +164 -0
- package/node_modules/tslib/SECURITY.md +41 -0
- package/node_modules/tslib/modules/index.d.ts +38 -0
- package/node_modules/tslib/modules/index.js +70 -0
- package/node_modules/tslib/modules/package.json +3 -0
- package/node_modules/tslib/package.json +47 -0
- package/node_modules/tslib/tslib.d.ts +460 -0
- package/node_modules/tslib/tslib.es6.html +1 -0
- package/node_modules/tslib/tslib.es6.js +393 -0
- package/node_modules/tslib/tslib.es6.mjs +392 -0
- package/node_modules/tslib/tslib.html +1 -0
- package/node_modules/tslib/tslib.js +475 -0
- package/package.json +45 -0
- package/tailwindcss-oxide.wasi-browser.js +63 -0
- package/tailwindcss-oxide.wasi.cjs +88 -0
- package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
- package/wasi-worker-browser.mjs +39 -0
- package/wasi-worker.mjs +63 -0
|
@@ -0,0 +1,941 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.wasiThreads = {}));
|
|
5
|
+
})(this, (function (exports) {
|
|
6
|
+
var _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
7
|
+
? WebAssembly
|
|
8
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
9
|
+
? WXWebAssembly
|
|
10
|
+
: undefined;
|
|
11
|
+
var ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null &&
|
|
12
|
+
typeof process.versions === 'object' && process.versions !== null &&
|
|
13
|
+
typeof process.versions.node === 'string';
|
|
14
|
+
function getPostMessage(options) {
|
|
15
|
+
return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === 'function'
|
|
16
|
+
? options.postMessage
|
|
17
|
+
: typeof postMessage === 'function'
|
|
18
|
+
? postMessage
|
|
19
|
+
: undefined;
|
|
20
|
+
}
|
|
21
|
+
function serizeErrorToBuffer(sab, code, error) {
|
|
22
|
+
var i32array = new Int32Array(sab);
|
|
23
|
+
Atomics.store(i32array, 0, code);
|
|
24
|
+
if (code > 1 && error) {
|
|
25
|
+
var name_1 = error.name;
|
|
26
|
+
var message = error.message;
|
|
27
|
+
var stack = error.stack;
|
|
28
|
+
var nameBuffer = new TextEncoder().encode(name_1);
|
|
29
|
+
var messageBuffer = new TextEncoder().encode(message);
|
|
30
|
+
var stackBuffer = new TextEncoder().encode(stack);
|
|
31
|
+
Atomics.store(i32array, 1, nameBuffer.length);
|
|
32
|
+
Atomics.store(i32array, 2, messageBuffer.length);
|
|
33
|
+
Atomics.store(i32array, 3, stackBuffer.length);
|
|
34
|
+
var buffer = new Uint8Array(sab);
|
|
35
|
+
buffer.set(nameBuffer, 16);
|
|
36
|
+
buffer.set(messageBuffer, 16 + nameBuffer.length);
|
|
37
|
+
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function deserizeErrorFromBuffer(sab) {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
var i32array = new Int32Array(sab);
|
|
43
|
+
var status = Atomics.load(i32array, 0);
|
|
44
|
+
if (status <= 1) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
var nameLength = Atomics.load(i32array, 1);
|
|
48
|
+
var messageLength = Atomics.load(i32array, 2);
|
|
49
|
+
var stackLength = Atomics.load(i32array, 3);
|
|
50
|
+
var buffer = new Uint8Array(sab);
|
|
51
|
+
var nameBuffer = buffer.slice(16, 16 + nameLength);
|
|
52
|
+
var messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
|
|
53
|
+
var stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
|
|
54
|
+
var name = new TextDecoder().decode(nameBuffer);
|
|
55
|
+
var message = new TextDecoder().decode(messageBuffer);
|
|
56
|
+
var stack = new TextDecoder().decode(stackBuffer);
|
|
57
|
+
var ErrorConstructor = (_a = globalThis[name]) !== null && _a !== void 0 ? _a : (name === 'RuntimeError' ? ((_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error) : Error);
|
|
58
|
+
var error = new ErrorConstructor(message);
|
|
59
|
+
Object.defineProperty(error, 'stack', {
|
|
60
|
+
value: stack,
|
|
61
|
+
writable: true,
|
|
62
|
+
enumerable: false,
|
|
63
|
+
configurable: true
|
|
64
|
+
});
|
|
65
|
+
return error;
|
|
66
|
+
}
|
|
67
|
+
/** @public */
|
|
68
|
+
function isSharedArrayBuffer(value) {
|
|
69
|
+
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
|
70
|
+
(Object.prototype.toString.call(value.constructor) === '[object SharedArrayBuffer]'));
|
|
71
|
+
}
|
|
72
|
+
/** @public */
|
|
73
|
+
function isTrapError(e) {
|
|
74
|
+
try {
|
|
75
|
+
return e instanceof _WebAssembly.RuntimeError;
|
|
76
|
+
}
|
|
77
|
+
catch (_) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function createMessage(type, payload) {
|
|
83
|
+
return {
|
|
84
|
+
__emnapi__: {
|
|
85
|
+
type: type,
|
|
86
|
+
payload: payload
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
var WASI_THREADS_MAX_TID = 0x1FFFFFFF;
|
|
92
|
+
function checkSharedWasmMemory(wasmMemory) {
|
|
93
|
+
if (wasmMemory) {
|
|
94
|
+
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
|
|
95
|
+
throw new Error('Multithread features require shared wasm memory. ' +
|
|
96
|
+
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
|
|
97
|
+
'then create WebAssembly.Memory with `shared: true` option');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
if (typeof SharedArrayBuffer === 'undefined') {
|
|
102
|
+
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function getReuseWorker(value) {
|
|
107
|
+
var _a;
|
|
108
|
+
if (typeof value === 'boolean') {
|
|
109
|
+
return value ? { size: 0, strict: false } : false;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === 'number') {
|
|
112
|
+
if (!(value >= 0)) {
|
|
113
|
+
throw new RangeError('reuseWorker: size must be a non-negative integer');
|
|
114
|
+
}
|
|
115
|
+
return { size: value, strict: false };
|
|
116
|
+
}
|
|
117
|
+
if (!value) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
var size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
|
|
121
|
+
var strict = Boolean(value.strict);
|
|
122
|
+
if (!(size > 0) && strict) {
|
|
123
|
+
throw new RangeError('reuseWorker: size must be set to positive integer if strict is set to true');
|
|
124
|
+
}
|
|
125
|
+
return { size: size, strict: strict };
|
|
126
|
+
}
|
|
127
|
+
var nextWorkerID = 0;
|
|
128
|
+
/** @public */
|
|
129
|
+
var ThreadManager = /*#__PURE__*/ (function () {
|
|
130
|
+
function ThreadManager(options) {
|
|
131
|
+
var _a;
|
|
132
|
+
this.unusedWorkers = [];
|
|
133
|
+
this.runningWorkers = [];
|
|
134
|
+
this.pthreads = Object.create(null);
|
|
135
|
+
this.wasmModule = null;
|
|
136
|
+
this.wasmMemory = null;
|
|
137
|
+
this.messageEvents = new WeakMap();
|
|
138
|
+
if (!options) {
|
|
139
|
+
throw new TypeError('ThreadManager(): options is not provided');
|
|
140
|
+
}
|
|
141
|
+
if ('childThread' in options) {
|
|
142
|
+
this._childThread = Boolean(options.childThread);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
this._childThread = false;
|
|
146
|
+
}
|
|
147
|
+
if (this._childThread) {
|
|
148
|
+
this._onCreateWorker = undefined;
|
|
149
|
+
this._reuseWorker = false;
|
|
150
|
+
this._beforeLoad = undefined;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this._onCreateWorker = options.onCreateWorker;
|
|
154
|
+
this._reuseWorker = getReuseWorker(options.reuseWorker);
|
|
155
|
+
this._beforeLoad = options.beforeLoad;
|
|
156
|
+
}
|
|
157
|
+
this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
|
|
158
|
+
}
|
|
159
|
+
Object.defineProperty(ThreadManager.prototype, "nextWorkerID", {
|
|
160
|
+
get: function () { return nextWorkerID; },
|
|
161
|
+
enumerable: false,
|
|
162
|
+
configurable: true
|
|
163
|
+
});
|
|
164
|
+
ThreadManager.prototype.init = function () {
|
|
165
|
+
if (!this._childThread) {
|
|
166
|
+
this.initMainThread();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
ThreadManager.prototype.initMainThread = function () {
|
|
170
|
+
this.preparePool();
|
|
171
|
+
};
|
|
172
|
+
ThreadManager.prototype.preparePool = function () {
|
|
173
|
+
if (this._reuseWorker) {
|
|
174
|
+
if (this._reuseWorker.size) {
|
|
175
|
+
var pthreadPoolSize = this._reuseWorker.size;
|
|
176
|
+
while (pthreadPoolSize--) {
|
|
177
|
+
var worker = this.allocateUnusedWorker();
|
|
178
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
179
|
+
// https://github.com/nodejs/node/issues/53036
|
|
180
|
+
worker.once('message', function () { });
|
|
181
|
+
worker.unref();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
ThreadManager.prototype.shouldPreloadWorkers = function () {
|
|
188
|
+
return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
|
|
189
|
+
};
|
|
190
|
+
ThreadManager.prototype.loadWasmModuleToAllWorkers = function () {
|
|
191
|
+
var _this_1 = this;
|
|
192
|
+
var promises = Array(this.unusedWorkers.length);
|
|
193
|
+
var _loop_1 = function (i) {
|
|
194
|
+
var worker = this_1.unusedWorkers[i];
|
|
195
|
+
if (ENVIRONMENT_IS_NODE)
|
|
196
|
+
worker.ref();
|
|
197
|
+
promises[i] = this_1.loadWasmModuleToWorker(worker).then(function (w) {
|
|
198
|
+
if (ENVIRONMENT_IS_NODE)
|
|
199
|
+
worker.unref();
|
|
200
|
+
return w;
|
|
201
|
+
}, function (e) {
|
|
202
|
+
if (ENVIRONMENT_IS_NODE)
|
|
203
|
+
worker.unref();
|
|
204
|
+
throw e;
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
var this_1 = this;
|
|
208
|
+
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
|
209
|
+
_loop_1(i);
|
|
210
|
+
}
|
|
211
|
+
return Promise.all(promises).catch(function (err) {
|
|
212
|
+
_this_1.terminateAllThreads();
|
|
213
|
+
throw err;
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
ThreadManager.prototype.preloadWorkers = function () {
|
|
217
|
+
if (this.shouldPreloadWorkers()) {
|
|
218
|
+
return this.loadWasmModuleToAllWorkers();
|
|
219
|
+
}
|
|
220
|
+
return Promise.resolve([]);
|
|
221
|
+
};
|
|
222
|
+
ThreadManager.prototype.setup = function (wasmModule, wasmMemory) {
|
|
223
|
+
this.wasmModule = wasmModule;
|
|
224
|
+
this.wasmMemory = wasmMemory;
|
|
225
|
+
};
|
|
226
|
+
ThreadManager.prototype.markId = function (worker) {
|
|
227
|
+
if (worker.__emnapi_tid)
|
|
228
|
+
return worker.__emnapi_tid;
|
|
229
|
+
var tid = nextWorkerID + 43;
|
|
230
|
+
nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
|
|
231
|
+
this.pthreads[tid] = worker;
|
|
232
|
+
worker.__emnapi_tid = tid;
|
|
233
|
+
return tid;
|
|
234
|
+
};
|
|
235
|
+
ThreadManager.prototype.returnWorkerToPool = function (worker) {
|
|
236
|
+
var tid = worker.__emnapi_tid;
|
|
237
|
+
if (tid !== undefined) {
|
|
238
|
+
delete this.pthreads[tid];
|
|
239
|
+
}
|
|
240
|
+
this.unusedWorkers.push(worker);
|
|
241
|
+
this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
|
|
242
|
+
delete worker.__emnapi_tid;
|
|
243
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
244
|
+
worker.unref();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
ThreadManager.prototype.loadWasmModuleToWorker = function (worker, sab) {
|
|
248
|
+
var _this_1 = this;
|
|
249
|
+
if (worker.whenLoaded)
|
|
250
|
+
return worker.whenLoaded;
|
|
251
|
+
var err = this.printErr;
|
|
252
|
+
var beforeLoad = this._beforeLoad;
|
|
253
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
254
|
+
var _this = this;
|
|
255
|
+
worker.whenLoaded = new Promise(function (resolve, reject) {
|
|
256
|
+
var handleError = function (e) {
|
|
257
|
+
var message = 'worker sent an error!';
|
|
258
|
+
if (worker.__emnapi_tid !== undefined) {
|
|
259
|
+
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
|
260
|
+
}
|
|
261
|
+
err(message + ' ' + e.message);
|
|
262
|
+
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
|
263
|
+
try {
|
|
264
|
+
_this.terminateAllThreads();
|
|
265
|
+
}
|
|
266
|
+
catch (_) { }
|
|
267
|
+
}
|
|
268
|
+
reject(e);
|
|
269
|
+
throw e;
|
|
270
|
+
};
|
|
271
|
+
var handleMessage = function (data) {
|
|
272
|
+
if (data.__emnapi__) {
|
|
273
|
+
var type = data.__emnapi__.type;
|
|
274
|
+
var payload = data.__emnapi__.payload;
|
|
275
|
+
if (type === 'loaded') {
|
|
276
|
+
worker.loaded = true;
|
|
277
|
+
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
|
278
|
+
worker.unref();
|
|
279
|
+
}
|
|
280
|
+
resolve(worker);
|
|
281
|
+
// if (payload.err) {
|
|
282
|
+
// err('failed to load in child thread: ' + (payload.err.message || payload.err))
|
|
283
|
+
// }
|
|
284
|
+
}
|
|
285
|
+
else if (type === 'cleanup-thread') {
|
|
286
|
+
if (payload.tid in _this_1.pthreads) {
|
|
287
|
+
_this_1.cleanThread(worker, payload.tid);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
worker.onmessage = function (e) {
|
|
293
|
+
handleMessage(e.data);
|
|
294
|
+
_this_1.fireMessageEvent(worker, e);
|
|
295
|
+
};
|
|
296
|
+
worker.onerror = handleError;
|
|
297
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
298
|
+
worker.on('message', function (data) {
|
|
299
|
+
var _a, _b;
|
|
300
|
+
(_b = (_a = worker).onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
301
|
+
data: data
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
worker.on('error', function (e) {
|
|
305
|
+
var _a, _b;
|
|
306
|
+
(_b = (_a = worker).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
|
307
|
+
});
|
|
308
|
+
worker.on('detachedExit', function () { });
|
|
309
|
+
}
|
|
310
|
+
if (typeof beforeLoad === 'function') {
|
|
311
|
+
beforeLoad(worker);
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
worker.postMessage(createMessage('load', {
|
|
315
|
+
wasmModule: _this_1.wasmModule,
|
|
316
|
+
wasmMemory: _this_1.wasmMemory,
|
|
317
|
+
sab: sab
|
|
318
|
+
}));
|
|
319
|
+
}
|
|
320
|
+
catch (err) {
|
|
321
|
+
checkSharedWasmMemory(_this_1.wasmMemory);
|
|
322
|
+
throw err;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
return worker.whenLoaded;
|
|
326
|
+
};
|
|
327
|
+
ThreadManager.prototype.allocateUnusedWorker = function () {
|
|
328
|
+
var _onCreateWorker = this._onCreateWorker;
|
|
329
|
+
if (typeof _onCreateWorker !== 'function') {
|
|
330
|
+
throw new TypeError('`options.onCreateWorker` is not provided');
|
|
331
|
+
}
|
|
332
|
+
var worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
|
333
|
+
this.unusedWorkers.push(worker);
|
|
334
|
+
return worker;
|
|
335
|
+
};
|
|
336
|
+
ThreadManager.prototype.getNewWorker = function (sab) {
|
|
337
|
+
if (this._reuseWorker) {
|
|
338
|
+
if (this.unusedWorkers.length === 0) {
|
|
339
|
+
if (this._reuseWorker.strict) {
|
|
340
|
+
if (!ENVIRONMENT_IS_NODE) {
|
|
341
|
+
var err = this.printErr;
|
|
342
|
+
err('Tried to spawn a new thread, but the thread pool is exhausted.\n' +
|
|
343
|
+
'This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.');
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
var worker_1 = this.allocateUnusedWorker();
|
|
348
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
349
|
+
this.loadWasmModuleToWorker(worker_1, sab);
|
|
350
|
+
}
|
|
351
|
+
return this.unusedWorkers.pop();
|
|
352
|
+
}
|
|
353
|
+
var worker = this.allocateUnusedWorker();
|
|
354
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
355
|
+
this.loadWasmModuleToWorker(worker, sab);
|
|
356
|
+
return this.unusedWorkers.pop();
|
|
357
|
+
};
|
|
358
|
+
ThreadManager.prototype.cleanThread = function (worker, tid, force) {
|
|
359
|
+
if (!force && this._reuseWorker) {
|
|
360
|
+
this.returnWorkerToPool(worker);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
delete this.pthreads[tid];
|
|
364
|
+
var index = this.runningWorkers.indexOf(worker);
|
|
365
|
+
if (index !== -1) {
|
|
366
|
+
this.runningWorkers.splice(index, 1);
|
|
367
|
+
}
|
|
368
|
+
this.terminateWorker(worker);
|
|
369
|
+
delete worker.__emnapi_tid;
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
ThreadManager.prototype.terminateWorker = function (worker) {
|
|
373
|
+
var _this_1 = this;
|
|
374
|
+
var _a;
|
|
375
|
+
var tid = worker.__emnapi_tid;
|
|
376
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
377
|
+
worker.terminate();
|
|
378
|
+
(_a = this.messageEvents.get(worker)) === null || _a === void 0 ? void 0 : _a.clear();
|
|
379
|
+
this.messageEvents.delete(worker);
|
|
380
|
+
worker.onmessage = function (e) {
|
|
381
|
+
if (e.data.__emnapi__) {
|
|
382
|
+
var err = _this_1.printErr;
|
|
383
|
+
err('received "' + e.data.__emnapi__.type + '" command from terminated worker: ' + tid);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
ThreadManager.prototype.terminateAllThreads = function () {
|
|
388
|
+
for (var i = 0; i < this.runningWorkers.length; ++i) {
|
|
389
|
+
this.terminateWorker(this.runningWorkers[i]);
|
|
390
|
+
}
|
|
391
|
+
for (var i = 0; i < this.unusedWorkers.length; ++i) {
|
|
392
|
+
this.terminateWorker(this.unusedWorkers[i]);
|
|
393
|
+
}
|
|
394
|
+
this.unusedWorkers = [];
|
|
395
|
+
this.runningWorkers = [];
|
|
396
|
+
this.pthreads = Object.create(null);
|
|
397
|
+
this.preparePool();
|
|
398
|
+
};
|
|
399
|
+
ThreadManager.prototype.addMessageEventListener = function (worker, onMessage) {
|
|
400
|
+
var listeners = this.messageEvents.get(worker);
|
|
401
|
+
if (!listeners) {
|
|
402
|
+
listeners = new Set();
|
|
403
|
+
this.messageEvents.set(worker, listeners);
|
|
404
|
+
}
|
|
405
|
+
listeners.add(onMessage);
|
|
406
|
+
return function () {
|
|
407
|
+
listeners === null || listeners === void 0 ? void 0 : listeners.delete(onMessage);
|
|
408
|
+
};
|
|
409
|
+
};
|
|
410
|
+
ThreadManager.prototype.fireMessageEvent = function (worker, e) {
|
|
411
|
+
var listeners = this.messageEvents.get(worker);
|
|
412
|
+
if (!listeners)
|
|
413
|
+
return;
|
|
414
|
+
var err = this.printErr;
|
|
415
|
+
listeners.forEach(function (listener) {
|
|
416
|
+
try {
|
|
417
|
+
listener(e);
|
|
418
|
+
}
|
|
419
|
+
catch (e) {
|
|
420
|
+
err(e.stack);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
return ThreadManager;
|
|
425
|
+
}());
|
|
426
|
+
|
|
427
|
+
var kIsProxy = Symbol('kIsProxy');
|
|
428
|
+
/** @public */
|
|
429
|
+
function createInstanceProxy(instance, memory) {
|
|
430
|
+
if (instance[kIsProxy])
|
|
431
|
+
return instance;
|
|
432
|
+
// https://github.com/nodejs/help/issues/4102
|
|
433
|
+
var originalExports = instance.exports;
|
|
434
|
+
var createHandler = function (target) {
|
|
435
|
+
var handlers = [
|
|
436
|
+
'apply',
|
|
437
|
+
'construct',
|
|
438
|
+
'defineProperty',
|
|
439
|
+
'deleteProperty',
|
|
440
|
+
'get',
|
|
441
|
+
'getOwnPropertyDescriptor',
|
|
442
|
+
'getPrototypeOf',
|
|
443
|
+
'has',
|
|
444
|
+
'isExtensible',
|
|
445
|
+
'ownKeys',
|
|
446
|
+
'preventExtensions',
|
|
447
|
+
'set',
|
|
448
|
+
'setPrototypeOf'
|
|
449
|
+
];
|
|
450
|
+
var handler = {};
|
|
451
|
+
var _loop_1 = function (i) {
|
|
452
|
+
var name_1 = handlers[i];
|
|
453
|
+
handler[name_1] = function () {
|
|
454
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
|
455
|
+
args.unshift(target);
|
|
456
|
+
return Reflect[name_1].apply(Reflect, args);
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
for (var i = 0; i < handlers.length; i++) {
|
|
460
|
+
_loop_1(i);
|
|
461
|
+
}
|
|
462
|
+
return handler;
|
|
463
|
+
};
|
|
464
|
+
var handler = createHandler(originalExports);
|
|
465
|
+
var _initialize = function () { };
|
|
466
|
+
var _start = function () { return 0; };
|
|
467
|
+
handler.get = function (_target, p, receiver) {
|
|
468
|
+
var _a;
|
|
469
|
+
if (p === 'memory') {
|
|
470
|
+
return (_a = (typeof memory === 'function' ? memory() : memory)) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
|
|
471
|
+
}
|
|
472
|
+
if (p === '_initialize') {
|
|
473
|
+
return p in originalExports ? _initialize : undefined;
|
|
474
|
+
}
|
|
475
|
+
if (p === '_start') {
|
|
476
|
+
return p in originalExports ? _start : undefined;
|
|
477
|
+
}
|
|
478
|
+
return Reflect.get(originalExports, p, receiver);
|
|
479
|
+
};
|
|
480
|
+
handler.has = function (_target, p) {
|
|
481
|
+
if (p === 'memory')
|
|
482
|
+
return true;
|
|
483
|
+
return Reflect.has(originalExports, p);
|
|
484
|
+
};
|
|
485
|
+
var exportsProxy = new Proxy(Object.create(null), handler);
|
|
486
|
+
return new Proxy(instance, {
|
|
487
|
+
get: function (target, p, receiver) {
|
|
488
|
+
if (p === 'exports') {
|
|
489
|
+
return exportsProxy;
|
|
490
|
+
}
|
|
491
|
+
if (p === kIsProxy) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
return Reflect.get(target, p, receiver);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
var patchedWasiInstances = new WeakMap();
|
|
500
|
+
/** @public */
|
|
501
|
+
var WASIThreads = /*#__PURE__*/ (function () {
|
|
502
|
+
function WASIThreads(options) {
|
|
503
|
+
var _this_1 = this;
|
|
504
|
+
if (!options) {
|
|
505
|
+
throw new TypeError('WASIThreads(): options is not provided');
|
|
506
|
+
}
|
|
507
|
+
if (!options.wasi) {
|
|
508
|
+
throw new TypeError('WASIThreads(): options.wasi is not provided');
|
|
509
|
+
}
|
|
510
|
+
patchedWasiInstances.set(this, new WeakSet());
|
|
511
|
+
var wasi = options.wasi;
|
|
512
|
+
patchWasiInstance(this, wasi);
|
|
513
|
+
this.wasi = wasi;
|
|
514
|
+
if ('childThread' in options) {
|
|
515
|
+
this.childThread = Boolean(options.childThread);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
this.childThread = false;
|
|
519
|
+
}
|
|
520
|
+
this.PThread = undefined;
|
|
521
|
+
if ('threadManager' in options) {
|
|
522
|
+
if (typeof options.threadManager === 'function') {
|
|
523
|
+
this.PThread = options.threadManager();
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
this.PThread = options.threadManager;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
if (!this.childThread) {
|
|
531
|
+
this.PThread = new ThreadManager(options);
|
|
532
|
+
this.PThread.init();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
var waitThreadStart = false;
|
|
536
|
+
if ('waitThreadStart' in options) {
|
|
537
|
+
waitThreadStart = typeof options.waitThreadStart === 'number' ? options.waitThreadStart : Boolean(options.waitThreadStart);
|
|
538
|
+
}
|
|
539
|
+
var postMessage = getPostMessage(options);
|
|
540
|
+
if (this.childThread && typeof postMessage !== 'function') {
|
|
541
|
+
throw new TypeError('options.postMessage is not a function');
|
|
542
|
+
}
|
|
543
|
+
this.postMessage = postMessage;
|
|
544
|
+
var wasm64 = Boolean(options.wasm64);
|
|
545
|
+
var onMessage = function (e) {
|
|
546
|
+
if (e.data.__emnapi__) {
|
|
547
|
+
var type = e.data.__emnapi__.type;
|
|
548
|
+
var payload = e.data.__emnapi__.payload;
|
|
549
|
+
if (type === 'spawn-thread') {
|
|
550
|
+
threadSpawn(payload.startArg, payload.errorOrTid);
|
|
551
|
+
}
|
|
552
|
+
else if (type === 'terminate-all-threads') {
|
|
553
|
+
_this_1.terminateAllThreads();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
var threadSpawn = function (startArg, errorOrTid) {
|
|
558
|
+
var _a;
|
|
559
|
+
var EAGAIN = 6;
|
|
560
|
+
var isNewABI = errorOrTid !== undefined;
|
|
561
|
+
try {
|
|
562
|
+
checkSharedWasmMemory(_this_1.wasmMemory);
|
|
563
|
+
}
|
|
564
|
+
catch (err) {
|
|
565
|
+
(_a = _this_1.PThread) === null || _a === void 0 ? void 0 : _a.printErr(err.stack);
|
|
566
|
+
if (isNewABI) {
|
|
567
|
+
var struct_1 = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
|
568
|
+
Atomics.store(struct_1, 0, 1);
|
|
569
|
+
Atomics.store(struct_1, 1, EAGAIN);
|
|
570
|
+
Atomics.notify(struct_1, 1);
|
|
571
|
+
return 1;
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
return -EAGAIN;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (!isNewABI) {
|
|
578
|
+
var malloc = _this_1.wasmInstance.exports.malloc;
|
|
579
|
+
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
|
|
580
|
+
if (!errorOrTid) {
|
|
581
|
+
return -48; /* ENOMEM */
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
var _free = _this_1.wasmInstance.exports.free;
|
|
585
|
+
var free = wasm64 ? function (ptr) { _free(BigInt(ptr)); } : _free;
|
|
586
|
+
var struct = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
|
|
587
|
+
Atomics.store(struct, 0, 0);
|
|
588
|
+
Atomics.store(struct, 1, 0);
|
|
589
|
+
if (_this_1.childThread) {
|
|
590
|
+
postMessage(createMessage('spawn-thread', {
|
|
591
|
+
startArg: startArg,
|
|
592
|
+
errorOrTid: errorOrTid
|
|
593
|
+
}));
|
|
594
|
+
Atomics.wait(struct, 1, 0);
|
|
595
|
+
var isError = Atomics.load(struct, 0);
|
|
596
|
+
var result = Atomics.load(struct, 1);
|
|
597
|
+
if (isNewABI) {
|
|
598
|
+
return isError;
|
|
599
|
+
}
|
|
600
|
+
free(errorOrTid);
|
|
601
|
+
return isError ? -result : result;
|
|
602
|
+
}
|
|
603
|
+
var shouldWait = waitThreadStart || (waitThreadStart === 0);
|
|
604
|
+
var sab;
|
|
605
|
+
if (shouldWait) {
|
|
606
|
+
sab = new Int32Array(new SharedArrayBuffer(16 + 8192));
|
|
607
|
+
Atomics.store(sab, 0, 0);
|
|
608
|
+
}
|
|
609
|
+
var worker;
|
|
610
|
+
var tid;
|
|
611
|
+
var PThread = _this_1.PThread;
|
|
612
|
+
try {
|
|
613
|
+
worker = PThread.getNewWorker(sab);
|
|
614
|
+
if (!worker) {
|
|
615
|
+
throw new Error('failed to get new worker');
|
|
616
|
+
}
|
|
617
|
+
PThread.addMessageEventListener(worker, onMessage);
|
|
618
|
+
tid = PThread.markId(worker);
|
|
619
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
620
|
+
worker.ref();
|
|
621
|
+
}
|
|
622
|
+
worker.postMessage(createMessage('start', {
|
|
623
|
+
tid: tid,
|
|
624
|
+
arg: startArg,
|
|
625
|
+
sab: sab
|
|
626
|
+
}));
|
|
627
|
+
if (shouldWait) {
|
|
628
|
+
if (typeof waitThreadStart === 'number') {
|
|
629
|
+
var waitResult = Atomics.wait(sab, 0, 0, waitThreadStart);
|
|
630
|
+
if (waitResult === 'timed-out') {
|
|
631
|
+
try {
|
|
632
|
+
PThread.cleanThread(worker, tid, true);
|
|
633
|
+
}
|
|
634
|
+
catch (_) { }
|
|
635
|
+
throw new Error('Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.');
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
Atomics.wait(sab, 0, 0);
|
|
640
|
+
}
|
|
641
|
+
var r = Atomics.load(sab, 0);
|
|
642
|
+
if (r > 1) {
|
|
643
|
+
try {
|
|
644
|
+
PThread.cleanThread(worker, tid, true);
|
|
645
|
+
}
|
|
646
|
+
catch (_) { }
|
|
647
|
+
throw deserizeErrorFromBuffer(sab.buffer);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
catch (e) {
|
|
652
|
+
Atomics.store(struct, 0, 1);
|
|
653
|
+
Atomics.store(struct, 1, EAGAIN);
|
|
654
|
+
Atomics.notify(struct, 1);
|
|
655
|
+
PThread === null || PThread === void 0 ? void 0 : PThread.printErr(e.stack);
|
|
656
|
+
if (isNewABI) {
|
|
657
|
+
return 1;
|
|
658
|
+
}
|
|
659
|
+
free(errorOrTid);
|
|
660
|
+
return -EAGAIN;
|
|
661
|
+
}
|
|
662
|
+
Atomics.store(struct, 0, 0);
|
|
663
|
+
Atomics.store(struct, 1, tid);
|
|
664
|
+
Atomics.notify(struct, 1);
|
|
665
|
+
PThread.runningWorkers.push(worker);
|
|
666
|
+
if (!shouldWait) {
|
|
667
|
+
worker.whenLoaded.catch(function (err) {
|
|
668
|
+
delete worker.whenLoaded;
|
|
669
|
+
PThread.cleanThread(worker, tid, true);
|
|
670
|
+
throw err;
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
if (isNewABI) {
|
|
674
|
+
return 0;
|
|
675
|
+
}
|
|
676
|
+
free(errorOrTid);
|
|
677
|
+
return tid;
|
|
678
|
+
};
|
|
679
|
+
this.threadSpawn = threadSpawn;
|
|
680
|
+
}
|
|
681
|
+
WASIThreads.prototype.getImportObject = function () {
|
|
682
|
+
return {
|
|
683
|
+
wasi: {
|
|
684
|
+
'thread-spawn': this.threadSpawn
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
};
|
|
688
|
+
WASIThreads.prototype.setup = function (wasmInstance, wasmModule, wasmMemory) {
|
|
689
|
+
wasmMemory !== null && wasmMemory !== void 0 ? wasmMemory : (wasmMemory = wasmInstance.exports.memory);
|
|
690
|
+
this.wasmInstance = wasmInstance;
|
|
691
|
+
this.wasmMemory = wasmMemory;
|
|
692
|
+
if (this.PThread) {
|
|
693
|
+
this.PThread.setup(wasmModule, wasmMemory);
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
WASIThreads.prototype.preloadWorkers = function () {
|
|
697
|
+
if (this.PThread) {
|
|
698
|
+
return this.PThread.preloadWorkers();
|
|
699
|
+
}
|
|
700
|
+
return Promise.resolve([]);
|
|
701
|
+
};
|
|
702
|
+
/**
|
|
703
|
+
* It's ok to call this method to a WASI command module.
|
|
704
|
+
*
|
|
705
|
+
* in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
|
|
706
|
+
*
|
|
707
|
+
* @returns A proxied WebAssembly instance if in child thread, other wise the original instance
|
|
708
|
+
*/
|
|
709
|
+
WASIThreads.prototype.initialize = function (instance, module, memory) {
|
|
710
|
+
var exports = instance.exports;
|
|
711
|
+
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
|
712
|
+
if (this.childThread) {
|
|
713
|
+
instance = createInstanceProxy(instance, memory);
|
|
714
|
+
}
|
|
715
|
+
this.setup(instance, module, memory);
|
|
716
|
+
var wasi = this.wasi;
|
|
717
|
+
if (('_start' in exports) && (typeof exports._start === 'function')) {
|
|
718
|
+
if (this.childThread) {
|
|
719
|
+
wasi.start(instance);
|
|
720
|
+
try {
|
|
721
|
+
var kStarted = getWasiSymbol(wasi, 'kStarted');
|
|
722
|
+
wasi[kStarted] = false;
|
|
723
|
+
}
|
|
724
|
+
catch (_) { }
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
setupInstance(wasi, instance);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
else {
|
|
731
|
+
wasi.initialize(instance);
|
|
732
|
+
}
|
|
733
|
+
return instance;
|
|
734
|
+
};
|
|
735
|
+
/**
|
|
736
|
+
* Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
|
|
737
|
+
* ```js
|
|
738
|
+
* this.initialize(instance, module, memory)
|
|
739
|
+
* this.wasi.start(instance)
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
WASIThreads.prototype.start = function (instance, module, memory) {
|
|
743
|
+
var exports = instance.exports;
|
|
744
|
+
memory !== null && memory !== void 0 ? memory : (memory = exports.memory);
|
|
745
|
+
if (this.childThread) {
|
|
746
|
+
instance = createInstanceProxy(instance, memory);
|
|
747
|
+
}
|
|
748
|
+
this.setup(instance, module, memory);
|
|
749
|
+
var exitCode = this.wasi.start(instance);
|
|
750
|
+
return { exitCode: exitCode, instance: instance };
|
|
751
|
+
};
|
|
752
|
+
WASIThreads.prototype.terminateAllThreads = function () {
|
|
753
|
+
var _a;
|
|
754
|
+
if (!this.childThread) {
|
|
755
|
+
(_a = this.PThread) === null || _a === void 0 ? void 0 : _a.terminateAllThreads();
|
|
756
|
+
}
|
|
757
|
+
else {
|
|
758
|
+
this.postMessage(createMessage('terminate-all-threads', {}));
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
return WASIThreads;
|
|
762
|
+
}());
|
|
763
|
+
function patchWasiInstance(wasiThreads, wasi) {
|
|
764
|
+
var patched = patchedWasiInstances.get(wasiThreads);
|
|
765
|
+
if (patched.has(wasi)) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
var _this = wasiThreads;
|
|
769
|
+
var wasiImport = wasi.wasiImport;
|
|
770
|
+
if (wasiImport) {
|
|
771
|
+
var proc_exit_1 = wasiImport.proc_exit;
|
|
772
|
+
wasiImport.proc_exit = function (code) {
|
|
773
|
+
_this.terminateAllThreads();
|
|
774
|
+
return proc_exit_1.call(this, code);
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
var start = wasi.start;
|
|
778
|
+
if (typeof start === 'function') {
|
|
779
|
+
wasi.start = function (instance) {
|
|
780
|
+
try {
|
|
781
|
+
return start.call(this, instance);
|
|
782
|
+
}
|
|
783
|
+
catch (err) {
|
|
784
|
+
if (isTrapError(err)) {
|
|
785
|
+
_this.terminateAllThreads();
|
|
786
|
+
}
|
|
787
|
+
throw err;
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
patched.add(wasi);
|
|
792
|
+
}
|
|
793
|
+
function getWasiSymbol(wasi, description) {
|
|
794
|
+
var symbols = Object.getOwnPropertySymbols(wasi);
|
|
795
|
+
var selectDescription = function (description) { return function (s) {
|
|
796
|
+
if (s.description) {
|
|
797
|
+
return s.description === description;
|
|
798
|
+
}
|
|
799
|
+
return s.toString() === "Symbol(".concat(description, ")");
|
|
800
|
+
}; };
|
|
801
|
+
if (Array.isArray(description)) {
|
|
802
|
+
return description.map(function (d) { return symbols.filter(selectDescription(d))[0]; });
|
|
803
|
+
}
|
|
804
|
+
return symbols.filter(selectDescription(description))[0];
|
|
805
|
+
}
|
|
806
|
+
function setupInstance(wasi, instance) {
|
|
807
|
+
var _a = getWasiSymbol(wasi, ['kInstance', 'kSetMemory']), kInstance = _a[0], kSetMemory = _a[1];
|
|
808
|
+
wasi[kInstance] = instance;
|
|
809
|
+
wasi[kSetMemory](instance.exports.memory);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/** @public */
|
|
813
|
+
var ThreadMessageHandler = /*#__PURE__*/ (function () {
|
|
814
|
+
function ThreadMessageHandler(options) {
|
|
815
|
+
var postMsg = getPostMessage(options);
|
|
816
|
+
if (typeof postMsg !== 'function') {
|
|
817
|
+
throw new TypeError('options.postMessage is not a function');
|
|
818
|
+
}
|
|
819
|
+
this.postMessage = postMsg;
|
|
820
|
+
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
|
821
|
+
this.instance = undefined;
|
|
822
|
+
// this.module = undefined
|
|
823
|
+
this.messagesBeforeLoad = [];
|
|
824
|
+
}
|
|
825
|
+
/** @virtual */
|
|
826
|
+
ThreadMessageHandler.prototype.instantiate = function (data) {
|
|
827
|
+
if (typeof this.onLoad === 'function') {
|
|
828
|
+
return this.onLoad(data);
|
|
829
|
+
}
|
|
830
|
+
throw new Error('ThreadMessageHandler.prototype.instantiate is not implemented');
|
|
831
|
+
};
|
|
832
|
+
/** @virtual */
|
|
833
|
+
ThreadMessageHandler.prototype.handle = function (e) {
|
|
834
|
+
var _this = this;
|
|
835
|
+
var _a;
|
|
836
|
+
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
|
837
|
+
var type = e.data.__emnapi__.type;
|
|
838
|
+
var payload_1 = e.data.__emnapi__.payload;
|
|
839
|
+
if (type === 'load') {
|
|
840
|
+
this._load(payload_1);
|
|
841
|
+
}
|
|
842
|
+
else if (type === 'start') {
|
|
843
|
+
this.handleAfterLoad(e, function () {
|
|
844
|
+
_this._start(payload_1);
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
ThreadMessageHandler.prototype._load = function (payload) {
|
|
850
|
+
var _this = this;
|
|
851
|
+
if (this.instance !== undefined)
|
|
852
|
+
return;
|
|
853
|
+
var source;
|
|
854
|
+
try {
|
|
855
|
+
source = this.instantiate(payload);
|
|
856
|
+
}
|
|
857
|
+
catch (err) {
|
|
858
|
+
this._loaded(err, null, payload);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
var then = source && 'then' in source ? source.then : undefined;
|
|
862
|
+
if (typeof then === 'function') {
|
|
863
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
864
|
+
then.call(source, function (source) { _this._loaded(null, source, payload); }, function (err) { _this._loaded(err, null, payload); });
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
this._loaded(null, source, payload);
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
ThreadMessageHandler.prototype._start = function (payload) {
|
|
871
|
+
if (typeof this.instance.exports.wasi_thread_start !== 'function') {
|
|
872
|
+
var err = new TypeError('wasi_thread_start is not exported');
|
|
873
|
+
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
874
|
+
throw err;
|
|
875
|
+
}
|
|
876
|
+
var postMessage = this.postMessage;
|
|
877
|
+
var tid = payload.tid;
|
|
878
|
+
var startArg = payload.arg;
|
|
879
|
+
notifyPthreadCreateResult(payload.sab, 1);
|
|
880
|
+
try {
|
|
881
|
+
this.instance.exports.wasi_thread_start(tid, startArg);
|
|
882
|
+
}
|
|
883
|
+
catch (err) {
|
|
884
|
+
if (isTrapError(err)) {
|
|
885
|
+
postMessage(createMessage('terminate-all-threads', {}));
|
|
886
|
+
}
|
|
887
|
+
throw err;
|
|
888
|
+
}
|
|
889
|
+
postMessage(createMessage('cleanup-thread', { tid: tid }));
|
|
890
|
+
};
|
|
891
|
+
ThreadMessageHandler.prototype._loaded = function (err, source, payload) {
|
|
892
|
+
if (err) {
|
|
893
|
+
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
894
|
+
throw err;
|
|
895
|
+
}
|
|
896
|
+
if (source == null) {
|
|
897
|
+
var err_1 = new TypeError('onLoad should return an object');
|
|
898
|
+
notifyPthreadCreateResult(payload.sab, 2, err_1);
|
|
899
|
+
throw err_1;
|
|
900
|
+
}
|
|
901
|
+
var instance = source.instance;
|
|
902
|
+
if (!instance) {
|
|
903
|
+
var err_2 = new TypeError('onLoad should return an object which includes "instance"');
|
|
904
|
+
notifyPthreadCreateResult(payload.sab, 2, err_2);
|
|
905
|
+
throw err_2;
|
|
906
|
+
}
|
|
907
|
+
this.instance = instance;
|
|
908
|
+
var postMessage = this.postMessage;
|
|
909
|
+
postMessage(createMessage('loaded', {}));
|
|
910
|
+
var messages = this.messagesBeforeLoad;
|
|
911
|
+
this.messagesBeforeLoad = [];
|
|
912
|
+
for (var i = 0; i < messages.length; i++) {
|
|
913
|
+
var data = messages[i];
|
|
914
|
+
this.handle({ data: data });
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
ThreadMessageHandler.prototype.handleAfterLoad = function (e, f) {
|
|
918
|
+
if (this.instance !== undefined) {
|
|
919
|
+
f.call(this, e);
|
|
920
|
+
}
|
|
921
|
+
else {
|
|
922
|
+
this.messagesBeforeLoad.push(e.data);
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
return ThreadMessageHandler;
|
|
926
|
+
}());
|
|
927
|
+
function notifyPthreadCreateResult(sab, result, error) {
|
|
928
|
+
if (sab) {
|
|
929
|
+
serizeErrorToBuffer(sab.buffer, result, error);
|
|
930
|
+
Atomics.notify(sab, 0);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
exports.ThreadManager = ThreadManager;
|
|
935
|
+
exports.ThreadMessageHandler = ThreadMessageHandler;
|
|
936
|
+
exports.WASIThreads = WASIThreads;
|
|
937
|
+
exports.createInstanceProxy = createInstanceProxy;
|
|
938
|
+
exports.isSharedArrayBuffer = isSharedArrayBuffer;
|
|
939
|
+
exports.isTrapError = isTrapError;
|
|
940
|
+
|
|
941
|
+
}));
|