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