@qds.dev/code 0.8.5 → 0.9.7

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 (43) hide show
  1. package/README.md +102 -0
  2. package/lib-types/src/debug.d.ts +1 -0
  3. package/lib-types/src/index.d.ts +2 -0
  4. package/lib-types/src/preview/bundler/bundled-config.d.ts +33 -0
  5. package/lib-types/src/preview/bundler/bundled.d.ts +3 -0
  6. package/lib-types/src/preview/bundler/bundled.unit.d.ts +1 -0
  7. package/lib-types/src/preview/bundler/client-events-listener.d.ts +1 -0
  8. package/lib-types/src/preview/bundler/index.d.ts +34 -0
  9. package/lib-types/src/preview/bundler/repl-bundler-worker.d.ts +30 -0
  10. package/lib-types/src/preview/bundler/repl-ssr-worker.d.ts +27 -0
  11. package/lib-types/src/preview/bundler/rollup-plugins.d.ts +5 -0
  12. package/lib-types/src/preview/bundler/rollup-plugins.unit.d.ts +1 -0
  13. package/lib-types/src/preview/constants.d.ts +5 -0
  14. package/lib-types/src/preview/index.d.ts +19 -0
  15. package/lib-types/src/preview/mocks/browser.d.ts +2 -0
  16. package/lib-types/src/preview/preview-sw.d.ts +20 -0
  17. package/lib-types/src/preview/preview-sw.unit.d.ts +1 -0
  18. package/lib-types/src/preview/register-preview-sw.d.ts +2 -0
  19. package/lib-types/src/preview/register-preview-sw.unit.d.ts +1 -0
  20. package/lib-types/src/preview/repl-editor.d.ts +1 -0
  21. package/lib-types/src/preview/repl-helpers-node.d.ts +4 -0
  22. package/lib-types/src/preview/repl-helpers.d.ts +16 -0
  23. package/lib-types/src/preview/repl-helpers.unit.d.ts +1 -0
  24. package/lib-types/src/preview/test-extend.d.ts +9 -0
  25. package/lib-types/src/preview/types.d.ts +70 -0
  26. package/lib-types/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +13 -8
  28. package/LICENSE +0 -21
  29. package/lib/node_modules/.pnpm/@emnapi_core@1.7.1/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.qwik.mjs +0 -5533
  30. package/lib/node_modules/.pnpm/@emnapi_runtime@1.8.1/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.qwik.mjs +0 -1225
  31. package/lib/node_modules/.pnpm/@emnapi_wasi-threads@1.1.0/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.qwik.mjs +0 -619
  32. package/lib/node_modules/.pnpm/@napi-rs_wasm-runtime@1.1.1/node_modules/@napi-rs/wasm-runtime/dist/fs.qwik.mjs +0 -14854
  33. package/lib/node_modules/.pnpm/@napi-rs_wasm-runtime@1.1.1/node_modules/@napi-rs/wasm-runtime/fs-proxy.qwik.mjs +0 -109
  34. package/lib/node_modules/.pnpm/@napi-rs_wasm-runtime@1.1.1/node_modules/@napi-rs/wasm-runtime/runtime.qwik.mjs +0 -4
  35. package/lib/node_modules/.pnpm/@qwik.dev_core@2.0.0-beta.15_vite@7.1.5_@types_node@24.9.0_jiti@2.6.1_lightningcss@1.30.1_ter_sl453hnfnrysgqsj6ikt4cdmnq/node_modules/@qwik.dev/core/dist/qwikloader.qwik.mjs +0 -152
  36. package/lib/node_modules/.pnpm/@rolldown_browser@1.0.0-beta.59/node_modules/@rolldown/browser/dist/index.browser.qwik.mjs +0 -13
  37. package/lib/node_modules/.pnpm/@rolldown_browser@1.0.0-beta.59/node_modules/@rolldown/browser/dist/normalize-string-or-regex-BB1FWNyl.qwik.mjs +0 -789
  38. package/lib/node_modules/.pnpm/@rolldown_browser@1.0.0-beta.59/node_modules/@rolldown/browser/dist/rolldown-binding.wasi-browser.qwik.mjs +0 -107
  39. package/lib/node_modules/.pnpm/@rolldown_browser@1.0.0-beta.59/node_modules/@rolldown/browser/dist/rolldown-build-Bfw5c2c3.qwik.mjs +0 -4157
  40. package/lib/node_modules/.pnpm/@tybys_wasm-util@0.10.1/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.qwik.mjs +0 -1968
  41. package/lib/node_modules/.pnpm/magic-regexp@0.10.0/node_modules/magic-regexp/dist/index.qwik.mjs +0 -10
  42. package/lib/node_modules/.pnpm/magic-regexp@0.10.0/node_modules/magic-regexp/dist/shared/magic-regexp.DKp_q_HX.qwik.mjs +0 -95
  43. package/lib/node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.qwik.mjs +0 -34
@@ -1,619 +0,0 @@
1
- //#region ../../node_modules/.pnpm/@emnapi+wasi-threads@1.1.0/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js
2
- var _WebAssembly = typeof WebAssembly !== "undefined" ? WebAssembly : typeof WXWebAssembly !== "undefined" ? WXWebAssembly : void 0;
3
- var ENVIRONMENT_IS_NODE = typeof process === "object" && process !== null && typeof process.versions === "object" && process.versions !== null && typeof process.versions.node === "string";
4
- function getPostMessage(options) {
5
- return typeof (options === null || options === void 0 ? void 0 : options.postMessage) === "function" ? options.postMessage : typeof postMessage === "function" ? postMessage : void 0;
6
- }
7
- function deserizeErrorFromBuffer(sab) {
8
- var _a, _b;
9
- var i32array = new Int32Array(sab);
10
- if (Atomics.load(i32array, 0) <= 1) return null;
11
- var nameLength = Atomics.load(i32array, 1);
12
- var messageLength = Atomics.load(i32array, 2);
13
- var stackLength = Atomics.load(i32array, 3);
14
- var buffer = new Uint8Array(sab);
15
- var nameBuffer = buffer.slice(16, 16 + nameLength);
16
- var messageBuffer = buffer.slice(16 + nameLength, 16 + nameLength + messageLength);
17
- var stackBuffer = buffer.slice(16 + nameLength + messageLength, 16 + nameLength + messageLength + stackLength);
18
- var name = new TextDecoder().decode(nameBuffer);
19
- var message = new TextDecoder().decode(messageBuffer);
20
- var stack = new TextDecoder().decode(stackBuffer);
21
- var error = new ((_a = globalThis[name]) !== null && _a !== void 0 ? _a : name === "RuntimeError" ? (_b = _WebAssembly.RuntimeError) !== null && _b !== void 0 ? _b : Error : Error)(message);
22
- Object.defineProperty(error, "stack", {
23
- value: stack,
24
- writable: true,
25
- enumerable: false,
26
- configurable: true
27
- });
28
- return error;
29
- }
30
- /** @public */
31
- function isSharedArrayBuffer(value) {
32
- return typeof SharedArrayBuffer === "function" && value instanceof SharedArrayBuffer || Object.prototype.toString.call(value) === "[object SharedArrayBuffer]";
33
- }
34
- /** @public */
35
- function isTrapError(e) {
36
- try {
37
- return e instanceof _WebAssembly.RuntimeError;
38
- } catch (_) {
39
- return false;
40
- }
41
- }
42
- function createMessage(type, payload) {
43
- return { __emnapi__: {
44
- type,
45
- payload
46
- } };
47
- }
48
- var WASI_THREADS_MAX_TID = 536870911;
49
- function checkSharedWasmMemory(wasmMemory) {
50
- if (wasmMemory) {
51
- if (!isSharedArrayBuffer(wasmMemory.buffer)) throw new Error("Multithread features require shared wasm memory. Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, then create WebAssembly.Memory with `shared: true` option");
52
- } else if (typeof SharedArrayBuffer === "undefined") throw new Error("Current environment does not support SharedArrayBuffer, threads are not available!");
53
- }
54
- function getReuseWorker(value) {
55
- var _a;
56
- if (typeof value === "boolean") return value ? {
57
- size: 0,
58
- strict: false
59
- } : false;
60
- if (typeof value === "number") {
61
- if (!(value >= 0)) throw new RangeError("reuseWorker: size must be a non-negative integer");
62
- return {
63
- size: value,
64
- strict: false
65
- };
66
- }
67
- if (!value) return false;
68
- var size = (_a = Number(value.size)) !== null && _a !== void 0 ? _a : 0;
69
- var strict = Boolean(value.strict);
70
- if (!(size > 0) && strict) throw new RangeError("reuseWorker: size must be set to positive integer if strict is set to true");
71
- return {
72
- size,
73
- strict
74
- };
75
- }
76
- var nextWorkerID = 0;
77
- /** @public */
78
- var ThreadManager = /* @__PURE__ */ function() {
79
- function ThreadManager$1(options) {
80
- var _a;
81
- this.unusedWorkers = [];
82
- this.runningWorkers = [];
83
- this.pthreads = Object.create(null);
84
- this.wasmModule = null;
85
- this.wasmMemory = null;
86
- this.messageEvents = /* @__PURE__ */ new WeakMap();
87
- if (!options) throw new TypeError("ThreadManager(): options is not provided");
88
- if ("childThread" in options) this._childThread = Boolean(options.childThread);
89
- else this._childThread = false;
90
- if (this._childThread) {
91
- this._onCreateWorker = void 0;
92
- this._reuseWorker = false;
93
- this._beforeLoad = void 0;
94
- } else {
95
- this._onCreateWorker = options.onCreateWorker;
96
- this._reuseWorker = getReuseWorker(options.reuseWorker);
97
- this._beforeLoad = options.beforeLoad;
98
- }
99
- this.printErr = (_a = options.printErr) !== null && _a !== void 0 ? _a : console.error.bind(console);
100
- }
101
- Object.defineProperty(ThreadManager$1.prototype, "nextWorkerID", {
102
- get: function() {
103
- return nextWorkerID;
104
- },
105
- enumerable: false,
106
- configurable: true
107
- });
108
- ThreadManager$1.prototype.init = function() {
109
- if (!this._childThread) this.initMainThread();
110
- };
111
- ThreadManager$1.prototype.initMainThread = function() {
112
- this.preparePool();
113
- };
114
- ThreadManager$1.prototype.preparePool = function() {
115
- if (this._reuseWorker) {
116
- if (this._reuseWorker.size) {
117
- var pthreadPoolSize = this._reuseWorker.size;
118
- while (pthreadPoolSize--) {
119
- var worker = this.allocateUnusedWorker();
120
- if (ENVIRONMENT_IS_NODE) {
121
- worker.once("message", function() {});
122
- worker.unref();
123
- }
124
- }
125
- }
126
- }
127
- };
128
- ThreadManager$1.prototype.shouldPreloadWorkers = function() {
129
- return !this._childThread && this._reuseWorker && this._reuseWorker.size > 0;
130
- };
131
- ThreadManager$1.prototype.loadWasmModuleToAllWorkers = function() {
132
- var _this_1 = this;
133
- var promises = Array(this.unusedWorkers.length);
134
- var _loop_1 = function(i$1) {
135
- var worker = this_1.unusedWorkers[i$1];
136
- if (ENVIRONMENT_IS_NODE) worker.ref();
137
- promises[i$1] = this_1.loadWasmModuleToWorker(worker).then(function(w) {
138
- if (ENVIRONMENT_IS_NODE) worker.unref();
139
- return w;
140
- }, function(e) {
141
- if (ENVIRONMENT_IS_NODE) worker.unref();
142
- throw e;
143
- });
144
- };
145
- var this_1 = this;
146
- for (var i = 0; i < this.unusedWorkers.length; ++i) _loop_1(i);
147
- return Promise.all(promises).catch(function(err) {
148
- _this_1.terminateAllThreads();
149
- throw err;
150
- });
151
- };
152
- ThreadManager$1.prototype.preloadWorkers = function() {
153
- if (this.shouldPreloadWorkers()) return this.loadWasmModuleToAllWorkers();
154
- return Promise.resolve([]);
155
- };
156
- ThreadManager$1.prototype.setup = function(wasmModule, wasmMemory) {
157
- this.wasmModule = wasmModule;
158
- this.wasmMemory = wasmMemory;
159
- };
160
- ThreadManager$1.prototype.markId = function(worker) {
161
- if (worker.__emnapi_tid) return worker.__emnapi_tid;
162
- var tid = nextWorkerID + 43;
163
- nextWorkerID = (nextWorkerID + 1) % (WASI_THREADS_MAX_TID - 42);
164
- this.pthreads[tid] = worker;
165
- worker.__emnapi_tid = tid;
166
- return tid;
167
- };
168
- ThreadManager$1.prototype.returnWorkerToPool = function(worker) {
169
- var tid = worker.__emnapi_tid;
170
- if (tid !== void 0) delete this.pthreads[tid];
171
- this.unusedWorkers.push(worker);
172
- this.runningWorkers.splice(this.runningWorkers.indexOf(worker), 1);
173
- delete worker.__emnapi_tid;
174
- if (ENVIRONMENT_IS_NODE) worker.unref();
175
- };
176
- ThreadManager$1.prototype.loadWasmModuleToWorker = function(worker, sab) {
177
- var _this_1 = this;
178
- if (worker.whenLoaded) return worker.whenLoaded;
179
- var err = this.printErr;
180
- var beforeLoad = this._beforeLoad;
181
- var _this = this;
182
- worker.whenLoaded = new Promise(function(resolve, reject) {
183
- var handleError = function(e) {
184
- var message = "worker sent an error!";
185
- if (worker.__emnapi_tid !== void 0) message = "worker (tid = " + worker.__emnapi_tid + ") sent an error!";
186
- if ("message" in e) {
187
- err(message + " " + e.message);
188
- if (e.message.indexOf("RuntimeError") !== -1 || e.message.indexOf("unreachable") !== -1) try {
189
- _this.terminateAllThreads();
190
- } catch (_) {}
191
- } else err(message);
192
- reject(e);
193
- throw e;
194
- };
195
- var handleMessage = function(data) {
196
- if (data.__emnapi__) {
197
- var type = data.__emnapi__.type;
198
- var payload = data.__emnapi__.payload;
199
- if (type === "loaded") {
200
- worker.loaded = true;
201
- if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) worker.unref();
202
- resolve(worker);
203
- } else if (type === "cleanup-thread") {
204
- if (payload.tid in _this_1.pthreads) _this_1.cleanThread(worker, payload.tid);
205
- }
206
- }
207
- };
208
- worker.onmessage = function(e) {
209
- handleMessage(e.data);
210
- _this_1.fireMessageEvent(worker, e);
211
- };
212
- worker.onerror = handleError;
213
- if (ENVIRONMENT_IS_NODE) {
214
- worker.on("message", function(data) {
215
- var _a, _b;
216
- (_b = (_a = worker).onmessage) === null || _b === void 0 || _b.call(_a, { data });
217
- });
218
- worker.on("error", function(e) {
219
- var _a, _b;
220
- (_b = (_a = worker).onerror) === null || _b === void 0 || _b.call(_a, e);
221
- });
222
- worker.on("detachedExit", function() {});
223
- }
224
- if (typeof beforeLoad === "function") beforeLoad(worker);
225
- try {
226
- worker.postMessage(createMessage("load", {
227
- wasmModule: _this_1.wasmModule,
228
- wasmMemory: _this_1.wasmMemory,
229
- sab
230
- }));
231
- } catch (err$1) {
232
- checkSharedWasmMemory(_this_1.wasmMemory);
233
- throw err$1;
234
- }
235
- });
236
- return worker.whenLoaded;
237
- };
238
- ThreadManager$1.prototype.allocateUnusedWorker = function() {
239
- var _onCreateWorker = this._onCreateWorker;
240
- if (typeof _onCreateWorker !== "function") throw new TypeError("`options.onCreateWorker` is not provided");
241
- var worker = _onCreateWorker({
242
- type: "thread",
243
- name: "emnapi-pthread"
244
- });
245
- this.unusedWorkers.push(worker);
246
- return worker;
247
- };
248
- ThreadManager$1.prototype.getNewWorker = function(sab) {
249
- if (this._reuseWorker) {
250
- if (this.unusedWorkers.length === 0) {
251
- if (this._reuseWorker.strict) {
252
- if (!ENVIRONMENT_IS_NODE) {
253
- var err = this.printErr;
254
- err("Tried to spawn a new thread, but the thread pool is exhausted.\nThis might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.");
255
- return;
256
- }
257
- }
258
- var worker_1 = this.allocateUnusedWorker();
259
- this.loadWasmModuleToWorker(worker_1, sab);
260
- }
261
- return this.unusedWorkers.pop();
262
- }
263
- var worker = this.allocateUnusedWorker();
264
- this.loadWasmModuleToWorker(worker, sab);
265
- return this.unusedWorkers.pop();
266
- };
267
- ThreadManager$1.prototype.cleanThread = function(worker, tid, force) {
268
- if (!force && this._reuseWorker) this.returnWorkerToPool(worker);
269
- else {
270
- delete this.pthreads[tid];
271
- var index = this.runningWorkers.indexOf(worker);
272
- if (index !== -1) this.runningWorkers.splice(index, 1);
273
- this.terminateWorker(worker);
274
- delete worker.__emnapi_tid;
275
- }
276
- };
277
- ThreadManager$1.prototype.terminateWorker = function(worker) {
278
- var _this_1 = this;
279
- var _a;
280
- var tid = worker.__emnapi_tid;
281
- worker.terminate();
282
- (_a = this.messageEvents.get(worker)) === null || _a === void 0 || _a.clear();
283
- this.messageEvents.delete(worker);
284
- worker.onmessage = function(e) {
285
- if (e.data.__emnapi__) {
286
- var err = _this_1.printErr;
287
- err("received \"" + e.data.__emnapi__.type + "\" command from terminated worker: " + tid);
288
- }
289
- };
290
- };
291
- ThreadManager$1.prototype.terminateAllThreads = function() {
292
- for (var i = 0; i < this.runningWorkers.length; ++i) this.terminateWorker(this.runningWorkers[i]);
293
- for (var i = 0; i < this.unusedWorkers.length; ++i) this.terminateWorker(this.unusedWorkers[i]);
294
- this.unusedWorkers = [];
295
- this.runningWorkers = [];
296
- this.pthreads = Object.create(null);
297
- this.preparePool();
298
- };
299
- ThreadManager$1.prototype.addMessageEventListener = function(worker, onMessage) {
300
- var listeners = this.messageEvents.get(worker);
301
- if (!listeners) {
302
- listeners = /* @__PURE__ */ new Set();
303
- this.messageEvents.set(worker, listeners);
304
- }
305
- listeners.add(onMessage);
306
- return function() {
307
- listeners === null || listeners === void 0 || listeners.delete(onMessage);
308
- };
309
- };
310
- ThreadManager$1.prototype.fireMessageEvent = function(worker, e) {
311
- var listeners = this.messageEvents.get(worker);
312
- if (!listeners) return;
313
- var err = this.printErr;
314
- listeners.forEach(function(listener) {
315
- try {
316
- listener(e);
317
- } catch (e$1) {
318
- err(e$1.stack);
319
- }
320
- });
321
- };
322
- return ThreadManager$1;
323
- }();
324
- var kIsProxy = Symbol("kIsProxy");
325
- /** @public */
326
- function createInstanceProxy(instance, memory) {
327
- if (instance[kIsProxy]) return instance;
328
- var originalExports = instance.exports;
329
- var createHandler = function(target) {
330
- var handlers = [
331
- "apply",
332
- "construct",
333
- "defineProperty",
334
- "deleteProperty",
335
- "get",
336
- "getOwnPropertyDescriptor",
337
- "getPrototypeOf",
338
- "has",
339
- "isExtensible",
340
- "ownKeys",
341
- "preventExtensions",
342
- "set",
343
- "setPrototypeOf"
344
- ];
345
- var handler$1 = {};
346
- var _loop_1 = function(i$1) {
347
- var name_1 = handlers[i$1];
348
- handler$1[name_1] = function() {
349
- var args = Array.prototype.slice.call(arguments, 1);
350
- args.unshift(target);
351
- return Reflect[name_1].apply(Reflect, args);
352
- };
353
- };
354
- for (var i = 0; i < handlers.length; i++) _loop_1(i);
355
- return handler$1;
356
- };
357
- var handler = createHandler(originalExports);
358
- var _initialize = function() {};
359
- var _start = function() {
360
- return 0;
361
- };
362
- handler.get = function(_target, p, receiver) {
363
- var _a;
364
- if (p === "memory") return (_a = typeof memory === "function" ? memory() : memory) !== null && _a !== void 0 ? _a : Reflect.get(originalExports, p, receiver);
365
- if (p === "_initialize") return p in originalExports ? _initialize : void 0;
366
- if (p === "_start") return p in originalExports ? _start : void 0;
367
- return Reflect.get(originalExports, p, receiver);
368
- };
369
- handler.has = function(_target, p) {
370
- if (p === "memory") return true;
371
- return Reflect.has(originalExports, p);
372
- };
373
- var exportsProxy = new Proxy(Object.create(null), handler);
374
- return new Proxy(instance, { get: function(target, p, receiver) {
375
- if (p === "exports") return exportsProxy;
376
- if (p === kIsProxy) return true;
377
- return Reflect.get(target, p, receiver);
378
- } });
379
- }
380
- var patchedWasiInstances = /* @__PURE__ */ new WeakMap();
381
- /** @public */
382
- var WASIThreads = /* @__PURE__ */ function() {
383
- function WASIThreads$1(options) {
384
- var _this_1 = this;
385
- if (!options) throw new TypeError("WASIThreads(): options is not provided");
386
- if (!options.wasi) throw new TypeError("WASIThreads(): options.wasi is not provided");
387
- patchedWasiInstances.set(this, /* @__PURE__ */ new WeakSet());
388
- var wasi = options.wasi;
389
- patchWasiInstance(this, wasi);
390
- this.wasi = wasi;
391
- if ("childThread" in options) this.childThread = Boolean(options.childThread);
392
- else this.childThread = false;
393
- this.PThread = void 0;
394
- if ("threadManager" in options) if (typeof options.threadManager === "function") this.PThread = options.threadManager();
395
- else this.PThread = options.threadManager;
396
- else if (!this.childThread) {
397
- this.PThread = new ThreadManager(options);
398
- this.PThread.init();
399
- }
400
- var waitThreadStart = false;
401
- if ("waitThreadStart" in options) waitThreadStart = typeof options.waitThreadStart === "number" ? options.waitThreadStart : Boolean(options.waitThreadStart);
402
- var postMessage$1 = getPostMessage(options);
403
- if (this.childThread && typeof postMessage$1 !== "function") throw new TypeError("options.postMessage is not a function");
404
- this.postMessage = postMessage$1;
405
- var wasm64 = Boolean(options.wasm64);
406
- var onMessage = function(e) {
407
- if (e.data.__emnapi__) {
408
- var type = e.data.__emnapi__.type;
409
- var payload = e.data.__emnapi__.payload;
410
- if (type === "spawn-thread") threadSpawn(payload.startArg, payload.errorOrTid);
411
- else if (type === "terminate-all-threads") _this_1.terminateAllThreads();
412
- }
413
- };
414
- var threadSpawn = function(startArg, errorOrTid) {
415
- var _a;
416
- var EAGAIN = 6;
417
- var isNewABI = errorOrTid !== void 0;
418
- try {
419
- checkSharedWasmMemory(_this_1.wasmMemory);
420
- } catch (err) {
421
- (_a = _this_1.PThread) === null || _a === void 0 || _a.printErr(err.stack);
422
- if (isNewABI) {
423
- var struct_1 = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
424
- Atomics.store(struct_1, 0, 1);
425
- Atomics.store(struct_1, 1, EAGAIN);
426
- Atomics.notify(struct_1, 1);
427
- return 1;
428
- } else return -EAGAIN;
429
- }
430
- if (!isNewABI) {
431
- var malloc = _this_1.wasmInstance.exports.malloc;
432
- errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8);
433
- if (!errorOrTid) return -48;
434
- }
435
- var _free = _this_1.wasmInstance.exports.free;
436
- var free = wasm64 ? function(ptr) {
437
- _free(BigInt(ptr));
438
- } : _free;
439
- var struct = new Int32Array(_this_1.wasmMemory.buffer, errorOrTid, 2);
440
- Atomics.store(struct, 0, 0);
441
- Atomics.store(struct, 1, 0);
442
- if (_this_1.childThread) {
443
- postMessage$1(createMessage("spawn-thread", {
444
- startArg,
445
- errorOrTid
446
- }));
447
- Atomics.wait(struct, 1, 0);
448
- var isError = Atomics.load(struct, 0);
449
- var result = Atomics.load(struct, 1);
450
- if (isNewABI) return isError;
451
- free(errorOrTid);
452
- return isError ? -result : result;
453
- }
454
- var shouldWait = waitThreadStart || waitThreadStart === 0;
455
- var sab;
456
- if (shouldWait) {
457
- sab = new Int32Array(new SharedArrayBuffer(8208));
458
- Atomics.store(sab, 0, 0);
459
- }
460
- var worker;
461
- var tid;
462
- var PThread = _this_1.PThread;
463
- try {
464
- worker = PThread.getNewWorker(sab);
465
- if (!worker) throw new Error("failed to get new worker");
466
- PThread.addMessageEventListener(worker, onMessage);
467
- tid = PThread.markId(worker);
468
- if (ENVIRONMENT_IS_NODE) worker.ref();
469
- worker.postMessage(createMessage("start", {
470
- tid,
471
- arg: startArg,
472
- sab
473
- }));
474
- if (shouldWait) {
475
- if (typeof waitThreadStart === "number") {
476
- if (Atomics.wait(sab, 0, 0, waitThreadStart) === "timed-out") {
477
- try {
478
- PThread.cleanThread(worker, tid, true);
479
- } catch (_) {}
480
- throw new Error("Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.");
481
- }
482
- } else Atomics.wait(sab, 0, 0);
483
- if (Atomics.load(sab, 0) > 1) {
484
- try {
485
- PThread.cleanThread(worker, tid, true);
486
- } catch (_) {}
487
- throw deserizeErrorFromBuffer(sab.buffer);
488
- }
489
- }
490
- } catch (e) {
491
- Atomics.store(struct, 0, 1);
492
- Atomics.store(struct, 1, EAGAIN);
493
- Atomics.notify(struct, 1);
494
- PThread === null || PThread === void 0 || PThread.printErr(e.stack);
495
- if (isNewABI) return 1;
496
- free(errorOrTid);
497
- return -EAGAIN;
498
- }
499
- Atomics.store(struct, 0, 0);
500
- Atomics.store(struct, 1, tid);
501
- Atomics.notify(struct, 1);
502
- PThread.runningWorkers.push(worker);
503
- if (!shouldWait) worker.whenLoaded.catch(function(err) {
504
- delete worker.whenLoaded;
505
- PThread.cleanThread(worker, tid, true);
506
- throw err;
507
- });
508
- if (isNewABI) return 0;
509
- free(errorOrTid);
510
- return tid;
511
- };
512
- this.threadSpawn = threadSpawn;
513
- }
514
- WASIThreads$1.prototype.getImportObject = function() {
515
- return { wasi: { "thread-spawn": this.threadSpawn } };
516
- };
517
- WASIThreads$1.prototype.setup = function(wasmInstance, wasmModule, wasmMemory) {
518
- wasmMemory !== null && wasmMemory !== void 0 || (wasmMemory = wasmInstance.exports.memory);
519
- this.wasmInstance = wasmInstance;
520
- this.wasmMemory = wasmMemory;
521
- if (this.PThread) this.PThread.setup(wasmModule, wasmMemory);
522
- };
523
- WASIThreads$1.prototype.preloadWorkers = function() {
524
- if (this.PThread) return this.PThread.preloadWorkers();
525
- return Promise.resolve([]);
526
- };
527
- /**
528
- * It's ok to call this method to a WASI command module.
529
- *
530
- * in child thread, must call this method instead of {@link WASIThreads.start} even if it's a WASI command module
531
- *
532
- * @returns A proxied WebAssembly instance if in child thread, other wise the original instance
533
- */
534
- WASIThreads$1.prototype.initialize = function(instance, module, memory) {
535
- var exports = instance.exports;
536
- memory !== null && memory !== void 0 || (memory = exports.memory);
537
- if (this.childThread) instance = createInstanceProxy(instance, memory);
538
- this.setup(instance, module, memory);
539
- var wasi = this.wasi;
540
- if ("_start" in exports && typeof exports._start === "function") if (this.childThread) {
541
- wasi.start(instance);
542
- try {
543
- var kStarted = getWasiSymbol(wasi, "kStarted");
544
- wasi[kStarted] = false;
545
- } catch (_) {}
546
- } else setupInstance(wasi, instance);
547
- else wasi.initialize(instance);
548
- return instance;
549
- };
550
- /**
551
- * Equivalent to calling {@link WASIThreads.initialize} and then calling {@link WASIInstance.start}
552
- * ```js
553
- * this.initialize(instance, module, memory)
554
- * this.wasi.start(instance)
555
- * ```
556
- */
557
- WASIThreads$1.prototype.start = function(instance, module, memory) {
558
- var exports = instance.exports;
559
- memory !== null && memory !== void 0 || (memory = exports.memory);
560
- if (this.childThread) instance = createInstanceProxy(instance, memory);
561
- this.setup(instance, module, memory);
562
- return {
563
- exitCode: this.wasi.start(instance),
564
- instance
565
- };
566
- };
567
- WASIThreads$1.prototype.terminateAllThreads = function() {
568
- var _a;
569
- if (!this.childThread) (_a = this.PThread) === null || _a === void 0 || _a.terminateAllThreads();
570
- else this.postMessage(createMessage("terminate-all-threads", {}));
571
- };
572
- return WASIThreads$1;
573
- }();
574
- function patchWasiInstance(wasiThreads, wasi) {
575
- var patched = patchedWasiInstances.get(wasiThreads);
576
- if (patched.has(wasi)) return;
577
- var _this = wasiThreads;
578
- var wasiImport = wasi.wasiImport;
579
- if (wasiImport) {
580
- var proc_exit_1 = wasiImport.proc_exit;
581
- wasiImport.proc_exit = function(code) {
582
- _this.terminateAllThreads();
583
- return proc_exit_1.call(this, code);
584
- };
585
- }
586
- if (!_this.childThread) {
587
- var start_1 = wasi.start;
588
- if (typeof start_1 === "function") wasi.start = function(instance) {
589
- try {
590
- return start_1.call(this, instance);
591
- } catch (err) {
592
- if (isTrapError(err)) _this.terminateAllThreads();
593
- throw err;
594
- }
595
- };
596
- }
597
- patched.add(wasi);
598
- }
599
- function getWasiSymbol(wasi, description) {
600
- var symbols = Object.getOwnPropertySymbols(wasi);
601
- var selectDescription = function(description$1) {
602
- return function(s) {
603
- if (s.description) return s.description === description$1;
604
- return s.toString() === "Symbol(".concat(description$1, ")");
605
- };
606
- };
607
- if (Array.isArray(description)) return description.map(function(d) {
608
- return symbols.filter(selectDescription(d))[0];
609
- });
610
- return symbols.filter(selectDescription(description))[0];
611
- }
612
- function setupInstance(wasi, instance) {
613
- var _a = getWasiSymbol(wasi, ["kInstance", "kSetMemory"]), kInstance = _a[0], kSetMemory = _a[1];
614
- wasi[kInstance] = instance;
615
- wasi[kSetMemory](instance.exports.memory);
616
- }
617
-
618
- //#endregion
619
- export { ThreadManager, WASIThreads, createInstanceProxy, isSharedArrayBuffer, isTrapError };