@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.00ef99d

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 (115) 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 +7746 -0
  5. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +428 -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 +428 -0
  8. package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +430 -0
  9. package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +8741 -0
  10. package/node_modules/@emnapi/core/dist/emnapi-core.js +9777 -0
  11. package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +428 -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 +7735 -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 +1398 -0
  20. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +671 -0
  21. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -0
  22. package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +671 -0
  23. package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +673 -0
  24. package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +1457 -0
  25. package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +426 -0
  26. package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +1528 -0
  27. package/node_modules/@emnapi/runtime/dist/emnapi.js +1529 -0
  28. package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +671 -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 +1367 -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 +915 -0
  37. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +271 -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 +271 -0
  40. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +273 -0
  41. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +975 -0
  42. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +987 -0
  43. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +271 -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 +910 -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/dist/emnapi-plugins.cjs +1826 -0
  50. package/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.js +1823 -0
  51. package/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +321 -0
  52. package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +25524 -0
  53. package/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +316 -0
  54. package/node_modules/@napi-rs/wasm-runtime/package.json +74 -0
  55. package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +39 -0
  56. package/node_modules/@napi-rs/wasm-runtime/runtime.js +36 -0
  57. package/node_modules/@tybys/wasm-util/README.md +193 -0
  58. package/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +11 -0
  59. package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +423 -0
  60. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +2932 -0
  61. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +2932 -0
  62. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -0
  63. package/node_modules/@tybys/wasm-util/dist/wasm-util.js +2954 -0
  64. package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -0
  65. package/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +158 -0
  66. package/node_modules/@tybys/wasm-util/lib/cjs/index.js +13 -0
  67. package/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +46 -0
  68. package/node_modules/@tybys/wasm-util/lib/cjs/load.js +97 -0
  69. package/node_modules/@tybys/wasm-util/lib/cjs/memory.js +35 -0
  70. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +103 -0
  71. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +268 -0
  72. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +3 -0
  73. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +192 -0
  74. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +316 -0
  75. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +1599 -0
  76. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +141 -0
  77. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +220 -0
  78. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +128 -0
  79. package/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +13 -0
  80. package/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +153 -0
  81. package/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +9 -0
  82. package/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +39 -0
  83. package/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +89 -0
  84. package/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +28 -0
  85. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +97 -0
  86. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +256 -0
  87. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +1 -0
  88. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +186 -0
  89. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +310 -0
  90. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +1593 -0
  91. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +135 -0
  92. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +216 -0
  93. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +113 -0
  94. package/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +9 -0
  95. package/node_modules/@tybys/wasm-util/package.json +58 -0
  96. package/node_modules/tslib/CopyrightNotice.txt +15 -0
  97. package/node_modules/tslib/LICENSE.txt +12 -0
  98. package/node_modules/tslib/README.md +164 -0
  99. package/node_modules/tslib/SECURITY.md +41 -0
  100. package/node_modules/tslib/modules/index.d.ts +38 -0
  101. package/node_modules/tslib/modules/index.js +70 -0
  102. package/node_modules/tslib/modules/package.json +3 -0
  103. package/node_modules/tslib/package.json +47 -0
  104. package/node_modules/tslib/tslib.d.ts +460 -0
  105. package/node_modules/tslib/tslib.es6.html +1 -0
  106. package/node_modules/tslib/tslib.es6.js +402 -0
  107. package/node_modules/tslib/tslib.es6.mjs +401 -0
  108. package/node_modules/tslib/tslib.html +1 -0
  109. package/node_modules/tslib/tslib.js +484 -0
  110. package/package.json +42 -0
  111. package/tailwindcss-oxide.wasi-browser.js +67 -0
  112. package/tailwindcss-oxide.wasi.cjs +123 -0
  113. package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
  114. package/wasi-worker-browser.mjs +41 -0
  115. package/wasi-worker.mjs +72 -0
@@ -0,0 +1,1826 @@
1
+ 'use strict';
2
+
3
+ //#region src/emnapi/async-work.js
4
+ function asyncWork (emnapiPluginCtx) {
5
+ const { emnapiCtx, emnapiNodeBinding, emnapiAsyncWorkPoolSize } = emnapiPluginCtx;
6
+ var mod = (function (exports, emnapi_shared, emscripten_runtime) {
7
+
8
+ //#region src/async-work.ts
9
+ /**
10
+ * @__deps $emnapiCtx
11
+ * @__deps $emnapiEnv
12
+ * @__deps $emnapiNodeBinding
13
+ * @__deps $emnapiAsyncWorkPoolSize
14
+ * @__postset
15
+ * ```
16
+ * emnapiAWST.init();
17
+ * ```
18
+ */
19
+ var emnapiAWST = {
20
+ idGen: {},
21
+ values: [undefined],
22
+ queued: new Set(),
23
+ pending: [],
24
+ init: function () {
25
+ const idGen = {
26
+ nextId: 1,
27
+ list: [],
28
+ generate: function () {
29
+ let id;
30
+ if (idGen.list.length) {
31
+ id = idGen.list.shift();
32
+ }
33
+ else {
34
+ id = idGen.nextId;
35
+ idGen.nextId++;
36
+ }
37
+ return id;
38
+ },
39
+ reuse: function (id) {
40
+ idGen.list.push(id);
41
+ }
42
+ };
43
+ emnapiAWST.idGen = idGen;
44
+ emnapiAWST.values = [undefined];
45
+ emnapiAWST.queued = new Set();
46
+ emnapiAWST.pending = [];
47
+ },
48
+ create: function (env, resource, resourceName, execute, complete, data) {
49
+ let asyncId = 0;
50
+ let triggerAsyncId = 0;
51
+ if (emnapiNodeBinding) {
52
+ const asyncContext = emnapiNodeBinding.node.emitAsyncInit(resource, resourceName, -1);
53
+ asyncId = asyncContext.asyncId;
54
+ triggerAsyncId = asyncContext.triggerAsyncId;
55
+ }
56
+ const id = emnapiAWST.idGen.generate();
57
+ emnapiAWST.values[id] = {
58
+ env,
59
+ id,
60
+ resource,
61
+ asyncId,
62
+ triggerAsyncId,
63
+ status: 0,
64
+ execute,
65
+ complete,
66
+ data
67
+ };
68
+ return id;
69
+ },
70
+ callComplete: function (work, status) {
71
+ const complete = work.complete;
72
+ const env = work.env;
73
+ const data = work.data;
74
+ const callback = () => {
75
+ if (!complete)
76
+ return;
77
+ const envObject = emnapi_shared.emnapiEnv;
78
+ const scope = emnapiCtx.openScope(envObject);
79
+ try {
80
+ envObject.callbackIntoModule(true, () => {
81
+ (emnapiPluginCtx.wasmTable.get(complete))(env, status, data);
82
+ });
83
+ }
84
+ finally {
85
+ emnapiCtx.closeScope(envObject, scope);
86
+ }
87
+ };
88
+ if (emnapiNodeBinding) {
89
+ emnapiNodeBinding.node.makeCallback(work.resource, callback, [], {
90
+ asyncId: work.asyncId,
91
+ triggerAsyncId: work.triggerAsyncId
92
+ });
93
+ }
94
+ else {
95
+ callback();
96
+ }
97
+ },
98
+ queue: function (id) {
99
+ const work = emnapiAWST.values[id];
100
+ if (!work)
101
+ return;
102
+ if (work.status === 0) {
103
+ work.status = 1;
104
+ if (emnapiAWST.queued.size >= (Math.abs(emnapiAsyncWorkPoolSize) || 4)) {
105
+ emnapiAWST.pending.push(id);
106
+ return;
107
+ }
108
+ emnapiAWST.queued.add(id);
109
+ const env = work.env;
110
+ const data = work.data;
111
+ const execute = work.execute;
112
+ work.status = 2;
113
+ emnapiCtx.features.setImmediate(() => {
114
+ (emnapiPluginCtx.wasmTable.get(execute))(env, data);
115
+ emnapiAWST.queued.delete(id);
116
+ work.status = 3;
117
+ emnapiCtx.features.setImmediate(() => {
118
+ emnapiAWST.callComplete(work, 0 /* napi_status.napi_ok */);
119
+ });
120
+ if (emnapiAWST.pending.length > 0) {
121
+ const nextWorkId = emnapiAWST.pending.shift();
122
+ emnapiAWST.values[nextWorkId].status = 0;
123
+ emnapiAWST.queue(nextWorkId);
124
+ }
125
+ });
126
+ }
127
+ },
128
+ cancel: function (id) {
129
+ const index = emnapiAWST.pending.indexOf(id);
130
+ if (index !== -1) {
131
+ const work = emnapiAWST.values[id];
132
+ if (work && (work.status === 1)) {
133
+ work.status = 4;
134
+ emnapiAWST.pending.splice(index, 1);
135
+ emnapiCtx.features.setImmediate(() => {
136
+ emnapiAWST.callComplete(work, 11 /* napi_status.napi_cancelled */);
137
+ });
138
+ return 0 /* napi_status.napi_ok */;
139
+ }
140
+ else {
141
+ return 9 /* napi_status.napi_generic_failure */;
142
+ }
143
+ }
144
+ return 9 /* napi_status.napi_generic_failure */;
145
+ },
146
+ remove: function (id) {
147
+ const work = emnapiAWST.values[id];
148
+ if (!work)
149
+ return;
150
+ if (emnapiNodeBinding) {
151
+ emnapiNodeBinding.node.emitAsyncDestroy({
152
+ asyncId: work.asyncId,
153
+ triggerAsyncId: work.triggerAsyncId
154
+ });
155
+ }
156
+ emnapiAWST.values[id] = undefined;
157
+ emnapiAWST.idGen.reuse(id);
158
+ }
159
+ };
160
+
161
+ //#endregion src/async-work.ts
162
+
163
+ //#region src/macro.ts
164
+
165
+ //#endregion src/macro.ts
166
+
167
+ //#region src/core/async-work.ts
168
+ const {
169
+ // onCreateWorker, napiModule, singleThreadAsyncWork, _emnapi_async_work_pool_size,
170
+ // PThread, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_PTHREAD, wasmInstance, _free, wasmMemory, _malloc,
171
+ _emnapi_node_emit_async_init, _emnapi_node_emit_async_destroy, _emnapi_runtime_keepalive_pop, _emnapi_runtime_keepalive_push } = emnapiPluginCtx;
172
+ var emnapiAWMT = {
173
+ pool: [],
174
+ workerReady: null,
175
+ globalAddress: 0,
176
+ globalOffset: {
177
+ idle_threads: 0,
178
+ q: 1 * 4,
179
+ next: 1 * 4,
180
+ prev: 2 * 4,
181
+ mutex: 3 * 4,
182
+ cond: 4 * 4,
183
+ exit_message: 5 * 4,
184
+ end: 7 * 4
185
+ },
186
+ offset: {
187
+ /* napi_ref */ resource: 0,
188
+ /* double */ async_id: 8,
189
+ /* double */ trigger_async_id: 16,
190
+ /* napi_env */ env: 24,
191
+ /* int32_t */ status: 1 * 4 + 24, // 0 for pending, 1 for cancelled, 2 for completed
192
+ queue: 2 * 4 + 24,
193
+ queue_next: 2 * 4 + 24,
194
+ queue_prev: 3 * 4 + 24,
195
+ /* void* */ data: 4 * 4 + 24,
196
+ /* napi_async_execute_callback */ execute: 5 * 4 + 24,
197
+ /* napi_async_complete_callback */ complete: 6 * 4 + 24,
198
+ end: 7 * 4 + 24
199
+ },
200
+ /**
201
+ * When another thread grows the shared WebAssembly.Memory, this agent's
202
+ * cached `wasmMemory.buffer` may still have the old shorter length
203
+ * (V8 refreshes it lazily). If a pointer derived from shared memory lies
204
+ * beyond the cached length, `wasmMemory.grow(0)` forces the agent to
205
+ * observe the current memory size and refreshes the buffer.
206
+ */
207
+ ensureBufferFor(end) {
208
+ let buffer = emscripten_runtime.wasmMemory.buffer;
209
+ if (end > buffer.byteLength) {
210
+ emscripten_runtime.wasmMemory.grow(0);
211
+ buffer = emscripten_runtime.wasmMemory.buffer;
212
+ }
213
+ return buffer;
214
+ },
215
+ init() {
216
+ emnapiAWMT.pool = [];
217
+ emnapiAWMT.workerReady = null;
218
+ if (typeof emscripten_runtime.PThread !== 'undefined') {
219
+ emscripten_runtime.PThread.unusedWorkers.forEach(emnapiAWMT.addListener);
220
+ Object.values(emscripten_runtime.PThread.pthreads).forEach(emnapiAWMT.addListener);
221
+ const __original_getNewWorker = emscripten_runtime.PThread.getNewWorker;
222
+ emscripten_runtime.PThread.getNewWorker = function () {
223
+ const r = __original_getNewWorker.apply(this, arguments);
224
+ emnapiAWMT.addListener(r);
225
+ return r;
226
+ };
227
+ }
228
+ },
229
+ addListener(worker) {
230
+ if (!worker)
231
+ return false;
232
+ if (worker._emnapiAWMTListener)
233
+ return true;
234
+ const handler = function (e) {
235
+ const data = emscripten_runtime.ENVIRONMENT_IS_NODE ? e : e.data;
236
+ const __emnapi__ = data.__emnapi__;
237
+ if (__emnapi__) {
238
+ const type = __emnapi__.type;
239
+ const payload = __emnapi__.payload;
240
+ if (type === 'async-work-complete') {
241
+ emnapiAWMT.callComplete(payload.work, 0 /* napi_status.napi_ok */);
242
+ }
243
+ }
244
+ };
245
+ const dispose = function () {
246
+ if (emscripten_runtime.ENVIRONMENT_IS_NODE) {
247
+ worker.off('message', handler);
248
+ }
249
+ else {
250
+ worker.removeEventListener('message', handler, false);
251
+ }
252
+ delete worker._emnapiAWMTListener;
253
+ };
254
+ worker._emnapiAWMTListener = { handler, dispose };
255
+ if (emscripten_runtime.ENVIRONMENT_IS_NODE) {
256
+ worker.on('message', handler);
257
+ }
258
+ else {
259
+ worker.addEventListener('message', handler, false);
260
+ }
261
+ return true;
262
+ },
263
+ initGlobal() {
264
+ if (!emnapiAWMT.globalAddress) {
265
+ emnapiAWMT.globalAddress = emscripten_runtime._malloc(emnapiAWMT.globalOffset.end);
266
+ emnapiAWMT.globalAddress >>>= 0;
267
+ const size = emnapiAWMT.globalOffset.end;
268
+ const addr = emnapiAWMT.globalAddress;
269
+ new Uint8Array(emnapiAWMT.ensureBufferFor(addr + size), addr, size).fill(0);
270
+ emnapiAWMT.queueInit(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.q);
271
+ emnapiAWMT.queueInit(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.exit_message);
272
+ }
273
+ },
274
+ terminateWorkers() {
275
+ emnapiAWMT.pool.forEach(w => {
276
+ w._emnapiAWMTListener?.dispose();
277
+ w._emnapiTSFNListener?.dispose();
278
+ w.terminate();
279
+ });
280
+ emnapiAWMT.pool.length = 0;
281
+ },
282
+ initWorkers(n) {
283
+ if (emscripten_runtime.ENVIRONMENT_IS_PTHREAD) {
284
+ return emnapiAWMT.workerReady || (emnapiAWMT.workerReady = Promise.resolve());
285
+ }
286
+ if (emnapiAWMT.workerReady)
287
+ return emnapiAWMT.workerReady;
288
+ if (!('emnapi_async_worker_create' in emscripten_runtime.wasmInstance.exports)) {
289
+ throw new TypeError('`emnapi_async_worker_create` is not exported, please try to add `--export=emnapi_async_worker_create` to linker flags');
290
+ }
291
+ const emnapi_async_worker_create = emscripten_runtime.wasmInstance.exports.emnapi_async_worker_create;
292
+ const args = [];
293
+ emnapiAWMT.initGlobal();
294
+ for (let i = 0; i < n; ++i) {
295
+ args.push(emnapi_async_worker_create(1, emnapiAWMT.globalAddress));
296
+ }
297
+ const promises = args.map(index => {
298
+ if (index === 0) {
299
+ return Promise.reject(new Error('Failed to create async worker'));
300
+ }
301
+ let worker;
302
+ if (index < 0) {
303
+ worker = emnapiAWMT.pool[-index - 1];
304
+ if (worker)
305
+ return worker.whenLoaded;
306
+ }
307
+ index >>>= 0;
308
+ const tidOffset = 20;
309
+ const view = new DataView(emnapiAWMT.ensureBufferFor(index + tidOffset + 4));
310
+ const tid = view.getInt32(index + tidOffset, true);
311
+ worker = emscripten_runtime.PThread.pthreads[tid];
312
+ return worker.whenLoaded;
313
+ });
314
+ emnapiAWMT.workerReady = Promise.all(promises);
315
+ return emnapiAWMT.workerReady;
316
+ },
317
+ getResource(work) {
318
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.resource + 4);
319
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
320
+ return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.resource, true);
321
+ },
322
+ getExecute(work) {
323
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.execute + 4);
324
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
325
+ return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.execute, true);
326
+ },
327
+ getComplete(work) {
328
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.complete + 4);
329
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
330
+ return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.complete, true);
331
+ },
332
+ getEnv(work) {
333
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.env + 4);
334
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
335
+ return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.env, true);
336
+ },
337
+ getData(work) {
338
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.data + 4);
339
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
340
+ return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.data, true);
341
+ },
342
+ getMutex() {
343
+ const index = emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.mutex;
344
+ const mutex = {
345
+ lock() {
346
+ const isBrowserMain = typeof window !== 'undefined' && typeof document !== 'undefined' && !emscripten_runtime.ENVIRONMENT_IS_NODE;
347
+ const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
348
+ if (isBrowserMain) {
349
+ while (true) {
350
+ const oldValue = Atomics.compareExchange(i32a, 0, 0, 10);
351
+ if (oldValue === 0) {
352
+ return;
353
+ }
354
+ }
355
+ }
356
+ else {
357
+ while (true) {
358
+ const oldValue = Atomics.compareExchange(i32a, 0, 0, 10);
359
+ if (oldValue === 0) {
360
+ return;
361
+ }
362
+ Atomics.wait(i32a, 0, 10);
363
+ }
364
+ }
365
+ },
366
+ unlock() {
367
+ const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
368
+ const oldValue = Atomics.compareExchange(i32a, 0, 10, 0);
369
+ if (oldValue !== 10) {
370
+ throw new Error('Tried to unlock while not holding the mutex');
371
+ }
372
+ Atomics.notify(i32a, 0, 1);
373
+ },
374
+ execute(fn) {
375
+ mutex.lock();
376
+ try {
377
+ return fn();
378
+ }
379
+ finally {
380
+ mutex.unlock();
381
+ }
382
+ }
383
+ };
384
+ return mutex;
385
+ },
386
+ getCond() {
387
+ const index = emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.cond;
388
+ const mutex = emnapiAWMT.getMutex();
389
+ const cond = {
390
+ wait() {
391
+ const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
392
+ const value = Atomics.load(i32a, 0);
393
+ mutex.unlock();
394
+ Atomics.wait(i32a, 0, value);
395
+ mutex.lock();
396
+ },
397
+ signal() {
398
+ const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
399
+ Atomics.add(i32a, 0, 1);
400
+ Atomics.notify(i32a, 0, 1);
401
+ }
402
+ };
403
+ return cond;
404
+ },
405
+ queueInit(q) {
406
+ emnapiAWMT.ensureBufferFor(q + 4 + 4);
407
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
408
+ GET_HEAP_DATA_VIEW().setUint32(q, q, true);
409
+ GET_HEAP_DATA_VIEW().setUint32(q + 4, q, true);
410
+ },
411
+ queueInsertTail(h, q) {
412
+ emnapiAWMT.ensureBufferFor(h + 4 + 4);
413
+ emnapiAWMT.ensureBufferFor(q + 4 + 4);
414
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
415
+ GET_HEAP_DATA_VIEW().setUint32(q, h, true);
416
+ const tempValue = GET_HEAP_DATA_VIEW().getUint32(h + 4, true);
417
+ GET_HEAP_DATA_VIEW().setUint32(q + 4, tempValue, true);
418
+ const qprev = GET_HEAP_DATA_VIEW().getUint32(q + 4, true);
419
+ GET_HEAP_DATA_VIEW().setUint32(qprev, q, true);
420
+ GET_HEAP_DATA_VIEW().setUint32(h + 4, q, true);
421
+ },
422
+ queueRemove(q) {
423
+ emnapiAWMT.ensureBufferFor(q + 4 + 4);
424
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
425
+ const qprev = GET_HEAP_DATA_VIEW().getUint32(q + 4, true);
426
+ const qnext = GET_HEAP_DATA_VIEW().getUint32(q, true);
427
+ GET_HEAP_DATA_VIEW().setUint32(qprev, qnext, true);
428
+ GET_HEAP_DATA_VIEW().setUint32(qnext + 4, qprev, true);
429
+ },
430
+ queueEmpty(q) {
431
+ emnapiAWMT.ensureBufferFor(q + 4);
432
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
433
+ // eslint-disable-next-line eqeqeq
434
+ return q == GET_HEAP_DATA_VIEW().getUint32(q, true);
435
+ },
436
+ scheduleWork: function (work) {
437
+ if (!emnapiAWMT.workerReady?.ready) {
438
+ emnapiAWMT.initWorkers(emnapi_shared._emnapi_async_work_pool_size()).then(() => {
439
+ emnapiAWMT.workerReady.ready = true;
440
+ }).catch((err) => {
441
+ emnapiAWMT.workerReady = null;
442
+ throw err;
443
+ });
444
+ }
445
+ _emnapi_runtime_keepalive_push();
446
+ emnapiCtx.increaseWaitingRequestCounter();
447
+ const statusBuffer = new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1);
448
+ Atomics.store(statusBuffer, 0, 0 /* AsyncWorkStatus.Pending */);
449
+ const mutex = emnapiAWMT.getMutex();
450
+ const cond = emnapiAWMT.getCond();
451
+ mutex.lock();
452
+ try {
453
+ emnapiAWMT.queueInsertTail(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.q, work + emnapiAWMT.offset.queue);
454
+ }
455
+ catch (err) {
456
+ _emnapi_runtime_keepalive_pop();
457
+ emnapiCtx.decreaseWaitingRequestCounter();
458
+ mutex.unlock();
459
+ throw err;
460
+ }
461
+ emnapiAWMT.ensureBufferFor(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.idle_threads + 4);
462
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
463
+ if (GET_HEAP_DATA_VIEW().getUint32(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.idle_threads, true) > 0) {
464
+ cond.signal();
465
+ }
466
+ mutex.unlock();
467
+ },
468
+ cancelWork(work) {
469
+ let cancelled = false;
470
+ emnapiAWMT.getMutex().execute(() => {
471
+ emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4);
472
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
473
+ cancelled = !emnapiAWMT.queueEmpty(work + emnapiAWMT.offset.queue) && GET_HEAP_DATA_VIEW().getInt32(work + emnapiAWMT.offset.status, true) !== 2 /* AsyncWorkStatus.Completed */;
474
+ if (cancelled) {
475
+ emnapiAWMT.queueRemove(work + emnapiAWMT.offset.queue);
476
+ }
477
+ });
478
+ if (!cancelled) {
479
+ return 9 /* napi_status.napi_generic_failure */;
480
+ }
481
+ if (Atomics.compareExchange(new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1), 0, 0 /* AsyncWorkStatus.Pending */, 1 /* AsyncWorkStatus.Cancelled */) !== 0 /* AsyncWorkStatus.Pending */) {
482
+ return 9 /* napi_status.napi_generic_failure */;
483
+ }
484
+ emnapiCtx.features.setImmediate(() => {
485
+ emnapiAWMT.callComplete(work, 11 /* napi_status.napi_cancelled */);
486
+ });
487
+ return 0 /* napi_status.napi_ok */;
488
+ },
489
+ callComplete: function (work, status) {
490
+ _emnapi_runtime_keepalive_pop();
491
+ emnapiCtx.decreaseWaitingRequestCounter();
492
+ const complete = emnapiAWMT.getComplete(work);
493
+ const env = emnapiAWMT.getEnv(work);
494
+ const data = emnapiAWMT.getData(work);
495
+ const envObject = emnapi_shared.emnapiEnv;
496
+ const scope = emnapiCtx.openScope(envObject);
497
+ const callback = () => {
498
+ if (!complete)
499
+ return;
500
+ envObject.callbackIntoModule(true, () => {
501
+ (emnapiPluginCtx.wasmTable.get(complete))(env, status, data);
502
+ });
503
+ };
504
+ try {
505
+ if (emnapiNodeBinding) {
506
+ const resource = emnapiAWMT.getResource(work);
507
+ const resource_value = emnapiCtx.getRef(resource).get();
508
+ const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
509
+ const view = new DataView(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.trigger_async_id + 8));
510
+ const asyncId = view.getFloat64(work + emnapiAWMT.offset.async_id, true);
511
+ const triggerAsyncId = view.getFloat64(work + emnapiAWMT.offset.trigger_async_id, true);
512
+ emnapiNodeBinding.node.makeCallback(resourceObject, callback, [], {
513
+ asyncId,
514
+ triggerAsyncId
515
+ });
516
+ }
517
+ else {
518
+ callback();
519
+ }
520
+ }
521
+ finally {
522
+ emnapiCtx.closeScope(envObject, scope);
523
+ }
524
+ }
525
+ };
526
+ emnapiAWST.init();
527
+ emnapiAWMT.init();
528
+ /** @__sig ippppppp */
529
+ var napi_create_async_work = emnapi_shared.singleThreadAsyncWork
530
+ ? function (env, resource, resource_name, execute, complete, data, result) {
531
+ if (!env)
532
+ return 1 /* napi_status.napi_invalid_arg */;
533
+ // @ts-expect-error
534
+ const envObject = emnapi_shared.emnapiEnv;
535
+ envObject.checkGCAccess();
536
+ if (!execute)
537
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
538
+ if (!result)
539
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
540
+ let resourceObject;
541
+ if (resource) {
542
+ resourceObject = Object(emnapiCtx.jsValueFromNapiValue(resource));
543
+ }
544
+ else {
545
+ resourceObject = {};
546
+ }
547
+ if (!resource_name)
548
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
549
+ const resourceName = String(emnapiCtx.jsValueFromNapiValue(resource_name));
550
+ const id = emnapiAWST.create(env, resourceObject, resourceName, execute, complete, data);
551
+ result >>>= 0;
552
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
553
+ GET_HEAP_DATA_VIEW().setUint32(result, id, true);
554
+ return envObject.clearLastError();
555
+ }
556
+ : function (env, resource, resource_name, execute, complete, data, result) {
557
+ if (!env)
558
+ return 1 /* napi_status.napi_invalid_arg */;
559
+ // @ts-expect-error
560
+ const envObject = emnapi_shared.emnapiEnv;
561
+ envObject.checkGCAccess();
562
+ if (!execute)
563
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
564
+ if (!result)
565
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
566
+ let resourceObject;
567
+ if (resource) {
568
+ resourceObject = Object(emnapiCtx.jsValueFromNapiValue(resource));
569
+ }
570
+ else {
571
+ resourceObject = {};
572
+ }
573
+ if (!resource_name)
574
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
575
+ const sizeofAW = emnapiAWMT.offset.end;
576
+ let aw = emscripten_runtime._malloc(sizeofAW);
577
+ if (!aw)
578
+ return envObject.setLastError(9 /* napi_status.napi_generic_failure */);
579
+ aw >>>= 0;
580
+ new Uint8Array(emnapiAWMT.ensureBufferFor(aw + sizeofAW)).subarray(aw, aw + sizeofAW).fill(0);
581
+ const s = emnapiCtx.napiValueFromJsValue(resourceObject);
582
+ const resourceRef = emnapiCtx.createReference(envObject, s, 1, 1 /* ReferenceOwnership.kUserland */);
583
+ const resource_ = resourceRef.id;
584
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
585
+ GET_HEAP_DATA_VIEW().setUint32(aw, resource_, true);
586
+ _emnapi_node_emit_async_init(s, resource_name, -1, aw + emnapiAWMT.offset.async_id);
587
+ GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.env, env, true);
588
+ GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.execute, execute, true);
589
+ GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.complete, complete, true);
590
+ GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.data, data, true);
591
+ emnapiAWMT.queueInit(aw + emnapiAWMT.offset.queue);
592
+ result >>>= 0;
593
+ GET_HEAP_DATA_VIEW().setUint32(result, aw, true);
594
+ return envObject.clearLastError();
595
+ };
596
+ /** @__sig ipp */
597
+ var napi_delete_async_work = emnapi_shared.singleThreadAsyncWork
598
+ ? function (env, work) {
599
+ if (!env)
600
+ return 1 /* napi_status.napi_invalid_arg */;
601
+ // @ts-expect-error
602
+ const envObject = emnapi_shared.emnapiEnv;
603
+ envObject.checkGCAccess();
604
+ if (!work)
605
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
606
+ work >>>= 0;
607
+ emnapiAWST.remove(work);
608
+ return envObject.clearLastError();
609
+ }
610
+ : function (env, work) {
611
+ if (!env)
612
+ return 1 /* napi_status.napi_invalid_arg */;
613
+ // @ts-expect-error
614
+ const envObject = emnapi_shared.emnapiEnv;
615
+ envObject.checkGCAccess();
616
+ if (!work)
617
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
618
+ work >>>= 0;
619
+ const resource = emnapiAWMT.getResource(work);
620
+ emnapiCtx.getRef(resource).dispose();
621
+ if (emnapiNodeBinding) {
622
+ const view = new DataView(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.trigger_async_id + 8));
623
+ const asyncId = view.getFloat64(work + emnapiAWMT.offset.async_id, true);
624
+ const triggerAsyncId = view.getFloat64(work + emnapiAWMT.offset.trigger_async_id, true);
625
+ _emnapi_node_emit_async_destroy(asyncId, triggerAsyncId);
626
+ }
627
+ emscripten_runtime._free(work);
628
+ return envObject.clearLastError();
629
+ };
630
+ /** @__sig ipp */
631
+ var napi_queue_async_work = emnapi_shared.singleThreadAsyncWork
632
+ ? function (env, work) {
633
+ if (!env)
634
+ return 1 /* napi_status.napi_invalid_arg */;
635
+ const envObject = emnapi_shared.emnapiEnv;
636
+ if (!work)
637
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
638
+ work >>>= 0;
639
+ emnapiAWST.queue(work);
640
+ return envObject.clearLastError();
641
+ }
642
+ : function (env, work) {
643
+ if (!env)
644
+ return 1 /* napi_status.napi_invalid_arg */;
645
+ const envObject = emnapi_shared.emnapiEnv;
646
+ if (!work)
647
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
648
+ work >>>= 0;
649
+ emnapiAWMT.scheduleWork(work);
650
+ return envObject.clearLastError();
651
+ };
652
+ /** @__sig ipp */
653
+ var napi_cancel_async_work = emnapi_shared.singleThreadAsyncWork
654
+ ? function (env, work) {
655
+ if (!env)
656
+ return 1 /* napi_status.napi_invalid_arg */;
657
+ const envObject = emnapi_shared.emnapiEnv;
658
+ if (!work)
659
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
660
+ work >>>= 0;
661
+ const status = emnapiAWST.cancel(work);
662
+ if (status === 0 /* napi_status.napi_ok */)
663
+ return envObject.clearLastError();
664
+ return envObject.setLastError(status);
665
+ }
666
+ : function (env, work) {
667
+ if (!env)
668
+ return 1 /* napi_status.napi_invalid_arg */;
669
+ const envObject = emnapi_shared.emnapiEnv;
670
+ if (!work)
671
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
672
+ work >>>= 0;
673
+ const status = emnapiAWMT.cancelWork(work);
674
+ if (status === 0 /* napi_status.napi_ok */)
675
+ return envObject.clearLastError();
676
+ return envObject.setLastError(status);
677
+ };
678
+ /** @__sig pp */
679
+ function _emnapi_async_worker(globalAddress) {
680
+ globalAddress >>>= 0;
681
+ emnapiAWMT.globalAddress = globalAddress;
682
+ const mutex = emnapiAWMT.getMutex();
683
+ const cond = emnapiAWMT.getCond();
684
+ mutex.lock();
685
+ const exitMessageAddr = globalAddress + emnapiAWMT.globalOffset.exit_message;
686
+ const idleThreadsAddr = globalAddress + emnapiAWMT.globalOffset.idle_threads;
687
+ const workerQueueAddr = globalAddress + emnapiAWMT.globalOffset.q;
688
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
689
+ for (;;) {
690
+ emnapiAWMT.ensureBufferFor(workerQueueAddr + 4);
691
+ while (emnapiAWMT.queueEmpty(workerQueueAddr)) {
692
+ Atomics.add(new Int32Array(emnapiAWMT.ensureBufferFor(idleThreadsAddr + 4), idleThreadsAddr, 1), 0, 1);
693
+ cond.wait();
694
+ Atomics.sub(new Int32Array(emnapiAWMT.ensureBufferFor(idleThreadsAddr + 4), idleThreadsAddr, 1), 0, 1);
695
+ }
696
+ const q = GET_HEAP_DATA_VIEW().getUint32(workerQueueAddr, true);
697
+ if (q === exitMessageAddr) {
698
+ cond.signal();
699
+ mutex.unlock();
700
+ break;
701
+ }
702
+ const work = q - emnapiAWMT.offset.queue;
703
+ emnapiAWMT.queueRemove(q);
704
+ emnapiAWMT.queueInit(q);
705
+ mutex.unlock();
706
+ const statusBuffer = new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1);
707
+ if (Atomics.load(statusBuffer, 0) === 1 /* AsyncWorkStatus.Cancelled */) {
708
+ emscripten_runtime.abort('unreachable');
709
+ }
710
+ const execute = emnapiAWMT.getExecute(work);
711
+ const env = emnapiAWMT.getEnv(work);
712
+ const data = emnapiAWMT.getData(work);
713
+ (emnapiPluginCtx.wasmTable.get(execute))(env, data);
714
+ Atomics.store(statusBuffer, 0, 2 /* AsyncWorkStatus.Completed */);
715
+ const postMessage = emnapi_shared.napiModule.postMessage;
716
+ postMessage({
717
+ __emnapi__: {
718
+ type: 'async-work-complete',
719
+ payload: { work }
720
+ }
721
+ });
722
+ mutex.lock();
723
+ }
724
+ return 0;
725
+ }
726
+ /** @__sig ipp */
727
+ function _emnapi_spawn_worker(f, globalAddress) {
728
+ if (typeof emnapi_shared.onCreateWorker !== 'function') {
729
+ throw new TypeError('`options.onCreateWorker` is not a function');
730
+ }
731
+ const promises = [];
732
+ const args = [];
733
+ if (!('emnapi_async_worker_create' in emscripten_runtime.wasmInstance.exports)) {
734
+ throw new TypeError('`emnapi_async_worker_create` is not exported, please try to add `--export=emnapi_async_worker_create` to linker flags');
735
+ }
736
+ args.push(emscripten_runtime.wasmInstance.exports.emnapi_async_worker_create(0, 0));
737
+ const handleError = (e) => {
738
+ if ('message' in e && (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1)) {
739
+ emnapiAWMT.terminateWorkers();
740
+ }
741
+ };
742
+ let ret;
743
+ try {
744
+ const worker = emnapi_shared.onCreateWorker({ type: 'async-work', name: 'emnapi-async-worker' });
745
+ const p = emscripten_runtime.PThread.loadWasmModuleToWorker(worker);
746
+ if (emscripten_runtime.ENVIRONMENT_IS_NODE) {
747
+ worker.on('error', handleError);
748
+ }
749
+ else {
750
+ worker.addEventListener('error', handleError, false);
751
+ }
752
+ emnapiAWMT.addListener(worker);
753
+ if (typeof emnapiPluginCtx.emnapiTSFN !== 'undefined') {
754
+ emnapiPluginCtx.emnapiTSFN.addListener(worker);
755
+ }
756
+ promises.push(p.then(() => {
757
+ if (typeof worker.unref === 'function') {
758
+ worker.unref();
759
+ }
760
+ }));
761
+ ret = emnapiAWMT.pool.push(worker) - 1;
762
+ const arg = args[0];
763
+ worker.threadBlockBase = arg;
764
+ worker.postMessage({
765
+ __emnapi__: {
766
+ type: 'async-worker-init',
767
+ payload: { arg, func: [f, globalAddress] }
768
+ }
769
+ });
770
+ }
771
+ catch (err) {
772
+ const arg = args[0];
773
+ emscripten_runtime._free(arg);
774
+ throw err;
775
+ }
776
+ return ret;
777
+ }
778
+ function initWorker(startArg, func) {
779
+ if (emnapi_shared.napiModule.childThread) {
780
+ if (typeof emscripten_runtime.wasmInstance.exports.emnapi_async_worker_init !== 'function') {
781
+ throw new TypeError('`emnapi_async_worker_init` is not exported, please try to add `--export=emnapi_async_worker_init` to linker flags');
782
+ }
783
+ emscripten_runtime.wasmInstance.exports.emnapi_async_worker_init(startArg);
784
+ (emnapiPluginCtx.wasmTable.get(func[0]))(func[1]);
785
+ }
786
+ else {
787
+ throw new Error('startThread is only available in child threads');
788
+ }
789
+ }
790
+ emnapi_shared.napiModule.initWorker = initWorker;
791
+
792
+ //#endregion src/core/async-work.ts
793
+
794
+ exports._emnapi_async_worker = _emnapi_async_worker;
795
+ exports._emnapi_spawn_worker = _emnapi_spawn_worker;
796
+ exports.napi_cancel_async_work = napi_cancel_async_work;
797
+ exports.napi_create_async_work = napi_create_async_work;
798
+ exports.napi_delete_async_work = napi_delete_async_work;
799
+ exports.napi_queue_async_work = napi_queue_async_work;
800
+
801
+ return exports;
802
+
803
+ })({}, emnapiPluginCtx, emnapiPluginCtx);
804
+ return {
805
+ importObject: (original) => {
806
+
807
+ Object.keys(mod).forEach(key => {
808
+ if (key.startsWith('napi_') || key.startsWith('node_api_')) {
809
+ original.napi[key] = mod[key];
810
+ } else {
811
+ original.env[key] = mod[key];
812
+ }
813
+ });
814
+ }
815
+ };
816
+ }
817
+
818
+ //#region src/emnapi/threadsafe-function.js
819
+ function threadsafeFunction (emnapiPluginCtx) {
820
+ const { emnapiCtx, emnapiNodeBinding, _emnapi_node_emit_async_destroy: __emnapi_node_emit_async_destroy, _emnapi_node_emit_async_init: __emnapi_node_emit_async_init, _emnapi_runtime_keepalive_pop: __emnapi_runtime_keepalive_pop, _emnapi_runtime_keepalive_push: __emnapi_runtime_keepalive_push } = emnapiPluginCtx;
821
+ var mod = (function (exports, emnapi_shared, emscripten_runtime) {
822
+
823
+ //#region src/macro.ts
824
+
825
+ //#endregion src/macro.ts
826
+
827
+ //#region src/threadsafe-function.ts
828
+ /* eslint-disable @stylistic/indent */
829
+ /**
830
+ * @__deps malloc
831
+ * @__deps free
832
+ * @__deps $emnapiCtx
833
+ * @__deps $emnapiEnv
834
+ * @__deps $emnapiNodeBinding
835
+ * @__deps _emnapi_node_emit_async_destroy
836
+ * @__deps _emnapi_runtime_keepalive_pop
837
+ * @__postset
838
+ * ```
839
+ * emnapiTSFN.init();
840
+ * ```
841
+ */
842
+ const emnapiTSFN = {
843
+ _liveSet: {},
844
+ offset: {
845
+ __size__: 0,
846
+ /* napi_ref */ resource: 0,
847
+ /* double */ async_id: 0,
848
+ /* double */ trigger_async_id: 0,
849
+ /* size_t */ queue_size: 0,
850
+ /* bool */ is_some: 0,
851
+ /* void* */ queue: 0,
852
+ // Reuse uv_async_t storage as JS-side wakeup state: pending event + scheduled drain.
853
+ async_pending: 0,
854
+ async_u_fd: 0,
855
+ /* size_t */ thread_count: 0,
856
+ /* int32_t */ state: 0,
857
+ /* atomic_uchar */ dispatch_state: 0,
858
+ /* void* */ context: 0,
859
+ /* size_t */ max_queue_size: 0,
860
+ /* napi_ref */ ref: 0,
861
+ /* napi_env */ env: 0,
862
+ /* void* */ finalize_data: 0,
863
+ /* napi_finalize */ finalize_cb: 0,
864
+ /* napi_threadsafe_function_call_js */ call_js_cb: 0,
865
+ /* bool */ handles_closing: 0,
866
+ /* bool */ async_ref: 0,
867
+ /* int32_t */ mutex: 0,
868
+ /* int32_t */ cond: 0,
869
+ },
870
+ init() {
871
+ emnapiTSFN._liveSet = new Set();
872
+ emnapiTSFN.offset.__size__ = 184 /* NapiTSFNOffset32.__size__ */;
873
+ emnapiTSFN.offset.resource = 0 /* NapiTSFNOffset32.async_resource_resource */;
874
+ emnapiTSFN.offset.async_id = 8 /* NapiTSFNOffset32.async_resource_async_context_async_id */;
875
+ emnapiTSFN.offset.trigger_async_id = 16 /* NapiTSFNOffset32.async_resource_async_context_trigger_async_id */;
876
+ emnapiTSFN.offset.queue_size = 60 /* NapiTSFNOffset32.queue_size */;
877
+ emnapiTSFN.offset.is_some = 24 /* NapiTSFNOffset32.async_resource_is_some */;
878
+ emnapiTSFN.offset.queue = 64 /* NapiTSFNOffset32.queue */;
879
+ emnapiTSFN.offset.async_pending = 132 /* NapiTSFNOffset32.async_pending */;
880
+ emnapiTSFN.offset.async_u_fd = 96 /* NapiTSFNOffset32.async_u_fd */;
881
+ emnapiTSFN.offset.thread_count = 136 /* NapiTSFNOffset32.thread_count */;
882
+ emnapiTSFN.offset.state = 140 /* NapiTSFNOffset32.state */;
883
+ emnapiTSFN.offset.dispatch_state = 144 /* NapiTSFNOffset32.dispatch_state */;
884
+ emnapiTSFN.offset.context = 148 /* NapiTSFNOffset32.context */;
885
+ emnapiTSFN.offset.max_queue_size = 152 /* NapiTSFNOffset32.max_queue_size */;
886
+ emnapiTSFN.offset.ref = 156 /* NapiTSFNOffset32.ref */;
887
+ emnapiTSFN.offset.env = 160 /* NapiTSFNOffset32.env */;
888
+ emnapiTSFN.offset.finalize_data = 164 /* NapiTSFNOffset32.finalize_data */;
889
+ emnapiTSFN.offset.finalize_cb = 168 /* NapiTSFNOffset32.finalize_cb */;
890
+ emnapiTSFN.offset.call_js_cb = 172 /* NapiTSFNOffset32.call_js_cb */;
891
+ emnapiTSFN.offset.handles_closing = 176 /* NapiTSFNOffset32.handles_closing */;
892
+ emnapiTSFN.offset.async_ref = 180 /* NapiTSFNOffset32.async_ref */;
893
+ emnapiTSFN.offset.mutex = 32 /* NapiTSFNOffset32.mutex */;
894
+ emnapiTSFN.offset.cond = 56 /* NapiTSFNOffset32.cond */;
895
+ emnapiTSFN.offset.mutex = emnapiTSFN.offset.mutex + 4;
896
+ if (typeof emscripten_runtime.PThread !== 'undefined') {
897
+ emscripten_runtime.PThread.unusedWorkers.forEach(emnapiTSFN.addListener);
898
+ Object.values(emscripten_runtime.PThread.pthreads).forEach(emnapiTSFN.addListener);
899
+ const __original_getNewWorker = emscripten_runtime.PThread.getNewWorker;
900
+ emscripten_runtime.PThread.getNewWorker = function () {
901
+ const r = __original_getNewWorker.apply(this, arguments);
902
+ emnapiTSFN.addListener(r);
903
+ return r;
904
+ };
905
+ }
906
+ },
907
+ addListener(worker) {
908
+ if (!worker)
909
+ return false;
910
+ if (worker._emnapiTSFNListener)
911
+ return true;
912
+ const handler = function (e) {
913
+ const data = emscripten_runtime.ENVIRONMENT_IS_NODE ? e : e.data;
914
+ const __emnapi__ = data.__emnapi__;
915
+ if (__emnapi__) {
916
+ const type = __emnapi__.type;
917
+ const payload = __emnapi__.payload;
918
+ if (type === 'tsfn-send') {
919
+ const pendng = payload.tsfn + emnapiTSFN.offset.async_pending;
920
+ if (Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2) !== 0) {
921
+ emnapiTSFN.enqueue(payload.tsfn);
922
+ }
923
+ }
924
+ }
925
+ };
926
+ const dispose = function () {
927
+ if (emscripten_runtime.ENVIRONMENT_IS_NODE) {
928
+ worker.off('message', handler);
929
+ }
930
+ else {
931
+ worker.removeEventListener('message', handler, false);
932
+ }
933
+ delete worker._emnapiTSFNListener;
934
+ };
935
+ worker._emnapiTSFNListener = { handler, dispose };
936
+ if (emscripten_runtime.ENVIRONMENT_IS_NODE) {
937
+ worker.on('message', handler);
938
+ }
939
+ else {
940
+ worker.addEventListener('message', handler, false);
941
+ }
942
+ return true;
943
+ },
944
+ /**
945
+ * When another thread grows the shared WebAssembly.Memory, this agent's
946
+ * cached `wasmMemory.buffer` may still have the old shorter length
947
+ * (V8 refreshes it lazily). If a pointer derived from shared memory lies
948
+ * beyond the cached length, `wasmMemory.grow(0)` forces the agent to
949
+ * observe the current memory size and refreshes the buffer.
950
+ */
951
+ ensureBufferFor(end) {
952
+ let buffer = emscripten_runtime.wasmMemory.buffer;
953
+ if (end > buffer.byteLength) {
954
+ emscripten_runtime.wasmMemory.grow(0);
955
+ buffer = emscripten_runtime.wasmMemory.buffer;
956
+ }
957
+ return buffer;
958
+ },
959
+ initQueue(func) {
960
+ const size = 2 * 4;
961
+ let queue = emscripten_runtime._malloc(size);
962
+ if (!queue)
963
+ return false;
964
+ queue >>>= 0;
965
+ new Uint8Array(emnapiTSFN.ensureBufferFor(queue + size), queue, size).fill(0);
966
+ emnapiTSFN.storeSizeTypeValue(func + emnapiTSFN.offset.queue, queue, false);
967
+ return true;
968
+ },
969
+ destroyQueue(func) {
970
+ const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
971
+ if (queue) {
972
+ let node = emnapiTSFN.loadSizeTypeValue(queue, false);
973
+ while (node !== 0) {
974
+ const next = emnapiTSFN.loadSizeTypeValue(node + 4, false);
975
+ emscripten_runtime._free(node);
976
+ node = next;
977
+ }
978
+ emscripten_runtime._free(queue);
979
+ }
980
+ },
981
+ pushQueue(func, data) {
982
+ const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
983
+ const head = emnapiTSFN.loadSizeTypeValue(queue, false);
984
+ const tail = emnapiTSFN.loadSizeTypeValue(queue + 4, false);
985
+ const size = 2 * 4;
986
+ let node = emscripten_runtime._malloc(size);
987
+ if (!node)
988
+ throw new Error('OOM');
989
+ node >>>= 0;
990
+ emnapiTSFN.storeSizeTypeValue(node, data, false);
991
+ emnapiTSFN.storeSizeTypeValue(node + 4, 0, false);
992
+ if (head === 0 && tail === 0) {
993
+ emnapiTSFN.storeSizeTypeValue(queue, node, false);
994
+ emnapiTSFN.storeSizeTypeValue(queue + 4, node, false);
995
+ }
996
+ else {
997
+ emnapiTSFN.storeSizeTypeValue(tail + 4, node, false);
998
+ emnapiTSFN.storeSizeTypeValue(queue + 4, node, false);
999
+ }
1000
+ emnapiTSFN.addQueueSize(func);
1001
+ },
1002
+ shiftQueue(func) {
1003
+ const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
1004
+ const head = emnapiTSFN.loadSizeTypeValue(queue, false);
1005
+ if (head === 0)
1006
+ return 0;
1007
+ const node = head;
1008
+ const next = emnapiTSFN.loadSizeTypeValue(head + 4, false);
1009
+ emnapiTSFN.storeSizeTypeValue(queue, next, false);
1010
+ if (next === 0) {
1011
+ emnapiTSFN.storeSizeTypeValue(queue + 4, 0, false);
1012
+ }
1013
+ emnapiTSFN.storeSizeTypeValue(node + 4, 0, false);
1014
+ const value = emnapiTSFN.loadSizeTypeValue(node, false);
1015
+ emscripten_runtime._free(node);
1016
+ emnapiTSFN.subQueueSize(func);
1017
+ return value;
1018
+ },
1019
+ push(func, data, mode) {
1020
+ const mutex = emnapiTSFN.getMutex(func);
1021
+ const cond = emnapiTSFN.getCond(func);
1022
+ const waitCondition = () => {
1023
+ const queueSize = emnapiTSFN.getQueueSize(func);
1024
+ const maxSize = emnapiTSFN.getMaxQueueSize(func);
1025
+ return queueSize >= maxSize && maxSize > 0 && emnapiTSFN.getState(func) === 0 /* State.kOpen */;
1026
+ };
1027
+ const isBrowserMain = typeof window !== 'undefined' && typeof document !== 'undefined' && !emscripten_runtime.ENVIRONMENT_IS_NODE;
1028
+ let shouldDelete = false;
1029
+ const ret = mutex.execute(() => {
1030
+ while (waitCondition()) {
1031
+ if (mode === 0 /* napi_threadsafe_function_call_mode.napi_tsfn_nonblocking */) {
1032
+ return 15 /* napi_status.napi_queue_full */;
1033
+ }
1034
+ /**
1035
+ * Browser JS main thread can not use `Atomics.wait`
1036
+ *
1037
+ * Related:
1038
+ * https://github.com/nodejs/node/pull/32689
1039
+ * https://github.com/nodejs/node/pull/33453
1040
+ */
1041
+ if (isBrowserMain) {
1042
+ return 21 /* napi_status.napi_would_deadlock */;
1043
+ }
1044
+ cond.wait();
1045
+ }
1046
+ if (emnapiTSFN.getState(func) === 0 /* State.kOpen */) {
1047
+ emnapiTSFN.pushQueue(func, data);
1048
+ emnapiTSFN.send(func);
1049
+ return 0 /* napi_status.napi_ok */;
1050
+ }
1051
+ if (emnapiTSFN.getThreadCount(func) === 0) {
1052
+ return 1 /* napi_status.napi_invalid_arg */;
1053
+ }
1054
+ emnapiTSFN.subThreadCount(func);
1055
+ if (!(emnapiTSFN.getState(func) === 2 /* State.kClosed */ && emnapiTSFN.getThreadCount(func) === 0)) {
1056
+ return 16 /* napi_status.napi_closing */;
1057
+ }
1058
+ shouldDelete = true;
1059
+ return 16 /* napi_status.napi_closing */;
1060
+ });
1061
+ if (shouldDelete) {
1062
+ emnapiTSFN.destroy(func);
1063
+ }
1064
+ return ret;
1065
+ },
1066
+ getMutex(func) {
1067
+ const index = func + emnapiTSFN.offset.mutex;
1068
+ const mutex = {
1069
+ lock() {
1070
+ const isBrowserMain = typeof window !== 'undefined' && typeof document !== 'undefined' && !emscripten_runtime.ENVIRONMENT_IS_NODE;
1071
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
1072
+ if (isBrowserMain) {
1073
+ while (true) {
1074
+ const oldValue = Atomics.compareExchange(i32a, 0, 0, 10);
1075
+ if (oldValue === 0) {
1076
+ return;
1077
+ }
1078
+ }
1079
+ }
1080
+ else {
1081
+ while (true) {
1082
+ const oldValue = Atomics.compareExchange(i32a, 0, 0, 10);
1083
+ if (oldValue === 0) {
1084
+ return;
1085
+ }
1086
+ Atomics.wait(i32a, 0, 10);
1087
+ }
1088
+ }
1089
+ },
1090
+ /* lockAsync () {
1091
+ return new Promise<void>(resolve => {
1092
+ const again = (): void => { fn() }
1093
+ const fn = (): void => {
1094
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1)
1095
+ const oldValue = Atomics.compareExchange(i32a, 0, 0, 10)
1096
+ if (oldValue === 0) {
1097
+ resolve()
1098
+ return
1099
+ }
1100
+ (Atomics as any).waitAsync(i32a, 0, 10).value.then(again)
1101
+ }
1102
+ fn()
1103
+ })
1104
+ }, */
1105
+ unlock() {
1106
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
1107
+ const oldValue = Atomics.compareExchange(i32a, 0, 10, 0);
1108
+ if (oldValue !== 10) {
1109
+ throw new Error('Tried to unlock while not holding the mutex');
1110
+ }
1111
+ Atomics.notify(i32a, 0, 1);
1112
+ },
1113
+ execute(fn) {
1114
+ mutex.lock();
1115
+ try {
1116
+ return fn();
1117
+ }
1118
+ finally {
1119
+ mutex.unlock();
1120
+ }
1121
+ } /* ,
1122
+ executeAsync<T> (fn: () => Promise<T>): Promise<T> {
1123
+ return mutex.lockAsync().then(() => {
1124
+ const r = fn()
1125
+ mutex.unlock()
1126
+ return r
1127
+ }, (err) => {
1128
+ mutex.unlock()
1129
+ throw err
1130
+ })
1131
+ } */
1132
+ };
1133
+ return mutex;
1134
+ },
1135
+ getCond(func) {
1136
+ const index = func + emnapiTSFN.offset.cond;
1137
+ const mutex = emnapiTSFN.getMutex(func);
1138
+ const cond = {
1139
+ wait() {
1140
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
1141
+ const value = Atomics.load(i32a, 0);
1142
+ mutex.unlock();
1143
+ Atomics.wait(i32a, 0, value);
1144
+ mutex.lock();
1145
+ },
1146
+ /* waitAsync () {
1147
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1)
1148
+ const value = Atomics.load(i32a, 0)
1149
+ mutex.unlock()
1150
+ const lock = (): Promise<void> => mutex.lockAsync()
1151
+ try {
1152
+ return (Atomics as any).waitAsync(i32a, 0, value).value.then(lock, lock)
1153
+ } catch (err) {
1154
+ return lock()
1155
+ }
1156
+ }, */
1157
+ signal() {
1158
+ const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
1159
+ Atomics.add(i32a, 0, 1);
1160
+ Atomics.notify(i32a, 0, 1);
1161
+ }
1162
+ };
1163
+ return cond;
1164
+ },
1165
+ getQueueSize(func) {
1166
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue_size, true);
1167
+ },
1168
+ addQueueSize(func) {
1169
+ const offset = emnapiTSFN.offset.queue_size;
1170
+ let arr, index;
1171
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
1172
+ index = (func + offset) >>> 2;
1173
+ Atomics.add(arr, index, 1);
1174
+ },
1175
+ subQueueSize(func) {
1176
+ const offset = emnapiTSFN.offset.queue_size;
1177
+ let arr, index;
1178
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
1179
+ index = (func + offset) >>> 2;
1180
+ Atomics.sub(arr, index, 1);
1181
+ },
1182
+ getThreadCount(func) {
1183
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.thread_count, true);
1184
+ },
1185
+ addThreadCount(func) {
1186
+ const offset = emnapiTSFN.offset.thread_count;
1187
+ let arr, index;
1188
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
1189
+ index = (func + offset) >>> 2;
1190
+ Atomics.add(arr, index, 1);
1191
+ },
1192
+ subThreadCount(func) {
1193
+ const offset = emnapiTSFN.offset.thread_count;
1194
+ let arr, index;
1195
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
1196
+ index = (func + offset) >>> 2;
1197
+ Atomics.sub(arr, index, 1);
1198
+ },
1199
+ getState(func) {
1200
+ return Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.state + 4)), (func + emnapiTSFN.offset.state) >>> 2);
1201
+ },
1202
+ setState(func, value) {
1203
+ Atomics.store(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.state + 4)), (func + emnapiTSFN.offset.state) >>> 2, value);
1204
+ },
1205
+ getHandlesClosing(func) {
1206
+ return Atomics.load(new Int8Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.handles_closing + 1)), (func + emnapiTSFN.offset.handles_closing));
1207
+ },
1208
+ setHandlesClosing(func, value) {
1209
+ Atomics.store(new Int8Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.handles_closing + 1)), (func + emnapiTSFN.offset.handles_closing), value);
1210
+ },
1211
+ getDispatchState(func) {
1212
+ return Atomics.load(new Uint32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.dispatch_state + 4)), (func + emnapiTSFN.offset.dispatch_state) >>> 2);
1213
+ },
1214
+ getContext(func) {
1215
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.context, false);
1216
+ },
1217
+ getMaxQueueSize(func) {
1218
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.max_queue_size, true);
1219
+ },
1220
+ getEnv(func) {
1221
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.env, false);
1222
+ },
1223
+ getCallJSCb(func) {
1224
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.call_js_cb, false);
1225
+ },
1226
+ getRef(func) {
1227
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.ref, false);
1228
+ },
1229
+ getResource(func) {
1230
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.resource, false);
1231
+ },
1232
+ getFinalizeCb(func) {
1233
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.finalize_cb, false);
1234
+ },
1235
+ getFinalizeData(func) {
1236
+ return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.finalize_data, false);
1237
+ },
1238
+ loadSizeTypeValue(offset, unsigned) {
1239
+ let ret;
1240
+ let arr;
1241
+ if (unsigned) {
1242
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(offset + 4));
1243
+ ret = Atomics.load(arr, offset >>> 2);
1244
+ return ret;
1245
+ }
1246
+ else {
1247
+ arr = new Int32Array(emnapiTSFN.ensureBufferFor(offset + 4));
1248
+ ret = Atomics.load(arr, offset >>> 2);
1249
+ return ret;
1250
+ }
1251
+ },
1252
+ storeSizeTypeValue(offset, value, unsigned) {
1253
+ let arr;
1254
+ if (unsigned) {
1255
+ arr = new Uint32Array(emnapiTSFN.ensureBufferFor(offset + 4));
1256
+ Atomics.store(arr, offset >>> 2, value);
1257
+ return undefined;
1258
+ }
1259
+ else {
1260
+ arr = new Int32Array(emnapiTSFN.ensureBufferFor(offset + 4));
1261
+ Atomics.store(arr, offset >>> 2, value >>> 0);
1262
+ return undefined;
1263
+ }
1264
+ },
1265
+ releaseResources(func) {
1266
+ if (emnapiTSFN.getState(func) !== 2 /* State.kClosed */) {
1267
+ emnapiTSFN.setState(func, 2 /* State.kClosed */);
1268
+ emnapiTSFN.getEnv(func);
1269
+ const envObject = emnapi_shared.emnapiEnv;
1270
+ const ref = emnapiTSFN.getRef(func);
1271
+ if (ref) {
1272
+ emnapiCtx.getRef(ref).dispose();
1273
+ }
1274
+ const resource = emnapiTSFN.getResource(func);
1275
+ emnapiCtx.getRef(resource).dispose();
1276
+ emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.is_some + 1);
1277
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
1278
+ GET_HEAP_DATA_VIEW().setInt8(func + emnapiTSFN.offset.is_some, 0, true);
1279
+ emnapiCtx.removeCleanupHook(envObject, emnapiTSFN.cleanup, func);
1280
+ envObject.unref();
1281
+ const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
1282
+ const asyncRefOffset = asyncRefAddress >>> 2;
1283
+ const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
1284
+ if (Atomics.load(arr, asyncRefOffset) > 0) {
1285
+ Atomics.store(arr, asyncRefOffset, 0);
1286
+ __emnapi_runtime_keepalive_pop();
1287
+ emnapiCtx.decreaseWaitingRequestCounter();
1288
+ }
1289
+ if (emnapiNodeBinding) {
1290
+ const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
1291
+ const asyncId = view.getFloat64(func + emnapiTSFN.offset.async_id, true);
1292
+ const triggerAsyncId = view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true);
1293
+ __emnapi_node_emit_async_destroy(asyncId, triggerAsyncId);
1294
+ }
1295
+ }
1296
+ },
1297
+ destroy(func) {
1298
+ emnapiTSFN._liveSet.delete(func);
1299
+ emnapiTSFN.destroyQueue(func);
1300
+ emnapiTSFN.releaseResources(func);
1301
+ emscripten_runtime._free(func);
1302
+ },
1303
+ emptyQueue(func) {
1304
+ const drainQueue = [];
1305
+ emnapiTSFN.getMutex(func).execute(() => {
1306
+ while (emnapiTSFN.getQueueSize(func) > 0) {
1307
+ drainQueue.push(emnapiTSFN.shiftQueue(func));
1308
+ }
1309
+ });
1310
+ const callJsCb = emnapiTSFN.getCallJSCb(func);
1311
+ const context = emnapiTSFN.getContext(func);
1312
+ let data;
1313
+ for (let i = 0; i < drainQueue.length; i++) {
1314
+ data = drainQueue[i];
1315
+ if (callJsCb) {
1316
+ (emnapiPluginCtx.wasmTable.get(callJsCb))(0, 0, context, data);
1317
+ }
1318
+ }
1319
+ },
1320
+ maybeDelete(func) {
1321
+ let shouldDelete = false;
1322
+ emnapiTSFN.getMutex(func).execute(() => {
1323
+ if (emnapiTSFN.getThreadCount(func) > 0) {
1324
+ emnapiTSFN.releaseResources(func);
1325
+ }
1326
+ else {
1327
+ shouldDelete = true;
1328
+ }
1329
+ });
1330
+ if (shouldDelete) {
1331
+ emnapiTSFN.destroy(func);
1332
+ }
1333
+ },
1334
+ finalize(func) {
1335
+ emnapiTSFN.getEnv(func);
1336
+ const envObject = emnapi_shared.emnapiEnv;
1337
+ emnapiCtx.openScope(envObject);
1338
+ const finalize = emnapiTSFN.getFinalizeCb(func);
1339
+ const data = emnapiTSFN.getFinalizeData(func);
1340
+ const context = emnapiTSFN.getContext(func);
1341
+ const f = () => {
1342
+ envObject.callFinalizerInternal(0, finalize, data, context);
1343
+ };
1344
+ try {
1345
+ emnapiTSFN.emptyQueue(func);
1346
+ if (finalize) {
1347
+ if (emnapiNodeBinding) {
1348
+ const resource = emnapiTSFN.getResource(func);
1349
+ const resource_value = emnapiCtx.getRef(resource).get();
1350
+ const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
1351
+ const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
1352
+ const asyncId = view.getFloat64(func + emnapiTSFN.offset.async_id, true);
1353
+ const triggerAsyncId = view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true);
1354
+ emnapiNodeBinding.node.makeCallback(resourceObject, f, [], {
1355
+ asyncId,
1356
+ triggerAsyncId
1357
+ });
1358
+ }
1359
+ else {
1360
+ f();
1361
+ }
1362
+ }
1363
+ emnapiTSFN.maybeDelete(func);
1364
+ }
1365
+ finally {
1366
+ emnapiCtx.closeScope(envObject);
1367
+ }
1368
+ },
1369
+ cleanup(func) {
1370
+ emnapiTSFN.closeHandlesAndMaybeDelete(func, 1);
1371
+ },
1372
+ closeHandlesAndMaybeDelete(func, set_closing) {
1373
+ emnapiTSFN.getEnv(func);
1374
+ const envObject = emnapi_shared.emnapiEnv;
1375
+ emnapiCtx.openScope(envObject);
1376
+ try {
1377
+ if (set_closing) {
1378
+ emnapiTSFN.getMutex(func).execute(() => {
1379
+ emnapiTSFN.setState(func, 1 /* State.kClosing */);
1380
+ if (emnapiTSFN.getMaxQueueSize(func) > 0) {
1381
+ emnapiTSFN.getCond(func).signal();
1382
+ }
1383
+ });
1384
+ }
1385
+ if (emnapiTSFN.getHandlesClosing(func)) {
1386
+ return;
1387
+ }
1388
+ emnapiTSFN.setHandlesClosing(func, 1);
1389
+ Atomics.store(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.async_pending + 4)), (func + emnapiTSFN.offset.async_pending) >>> 2, 1);
1390
+ emnapiCtx.features.setImmediate(() => {
1391
+ emnapiTSFN.finalize(func);
1392
+ });
1393
+ }
1394
+ finally {
1395
+ emnapiCtx.closeScope(envObject);
1396
+ }
1397
+ },
1398
+ dispatchOne(func) {
1399
+ let data = 0;
1400
+ let popped_value = false;
1401
+ let has_more = false;
1402
+ const mutex = emnapiTSFN.getMutex(func);
1403
+ const cond = emnapiTSFN.getCond(func);
1404
+ mutex.execute(() => {
1405
+ if (emnapiTSFN.getState(func) === 0 /* State.kOpen */) {
1406
+ let size = emnapiTSFN.getQueueSize(func);
1407
+ if (size > 0) {
1408
+ data = emnapiTSFN.shiftQueue(func);
1409
+ popped_value = true;
1410
+ const maxQueueSize = emnapiTSFN.getMaxQueueSize(func);
1411
+ if (size === maxQueueSize && maxQueueSize > 0) {
1412
+ cond.signal();
1413
+ }
1414
+ size--;
1415
+ }
1416
+ if (size === 0) {
1417
+ if (emnapiTSFN.getThreadCount(func) === 0) {
1418
+ emnapiTSFN.setState(func, 1 /* State.kClosing */);
1419
+ if (emnapiTSFN.getMaxQueueSize(func) > 0) {
1420
+ cond.signal();
1421
+ }
1422
+ emnapiTSFN.closeHandlesAndMaybeDelete(func, 0);
1423
+ }
1424
+ }
1425
+ else {
1426
+ has_more = true;
1427
+ }
1428
+ }
1429
+ else {
1430
+ emnapiTSFN.closeHandlesAndMaybeDelete(func, 0);
1431
+ }
1432
+ });
1433
+ if (popped_value) {
1434
+ const env = emnapiTSFN.getEnv(func);
1435
+ const envObject = emnapi_shared.emnapiEnv;
1436
+ emnapiCtx.openScope(envObject);
1437
+ const f = () => {
1438
+ envObject.callbackIntoModule(false, () => {
1439
+ const callJsCb = emnapiTSFN.getCallJSCb(func);
1440
+ const ref = emnapiTSFN.getRef(func);
1441
+ const js_callback = ref ? emnapiCtx.getRef(ref).get() : 0;
1442
+ if (callJsCb) {
1443
+ const context = emnapiTSFN.getContext(func);
1444
+ (emnapiPluginCtx.wasmTable.get(callJsCb))(env, js_callback, context, data);
1445
+ }
1446
+ else {
1447
+ const jsCallback = js_callback ? emnapiCtx.jsValueFromNapiValue(js_callback) : null;
1448
+ if (typeof jsCallback === 'function') {
1449
+ jsCallback();
1450
+ }
1451
+ }
1452
+ });
1453
+ };
1454
+ try {
1455
+ if (emnapiNodeBinding) {
1456
+ const resource = emnapiTSFN.getResource(func);
1457
+ const resource_value = emnapiCtx.getRef(resource).get();
1458
+ const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
1459
+ const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
1460
+ emnapiNodeBinding.node.makeCallback(resourceObject, f, [], {
1461
+ asyncId: view.getFloat64(func + emnapiTSFN.offset.async_id, true),
1462
+ triggerAsyncId: view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true)
1463
+ });
1464
+ }
1465
+ else {
1466
+ f();
1467
+ }
1468
+ }
1469
+ finally {
1470
+ emnapiCtx.closeScope(envObject);
1471
+ }
1472
+ }
1473
+ return has_more;
1474
+ },
1475
+ dispatch(func) {
1476
+ let has_more = true;
1477
+ let iterations_left = 1000;
1478
+ const dispatchStateAddress = func + emnapiTSFN.offset.dispatch_state;
1479
+ const index = dispatchStateAddress >>> 2;
1480
+ while (has_more && --iterations_left !== 0) {
1481
+ Atomics.store(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 1);
1482
+ try {
1483
+ has_more = emnapiTSFN.dispatchOne(func);
1484
+ }
1485
+ catch (err) {
1486
+ // A throwing callback must not leave dispatch_state marked as
1487
+ // dispatching, or every later send() would be swallowed. Re-arm
1488
+ // the state and schedule another drain before rethrowing.
1489
+ if (emnapiTSFN._liveSet.has(func)) {
1490
+ Atomics.exchange(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 0);
1491
+ emnapiTSFN.send(func);
1492
+ }
1493
+ throw err;
1494
+ }
1495
+ if (Atomics.exchange(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 0) !== 1) {
1496
+ has_more = true;
1497
+ }
1498
+ }
1499
+ if (has_more) {
1500
+ emnapiTSFN.send(func);
1501
+ }
1502
+ },
1503
+ enqueue(func) {
1504
+ // `pending` means a worker thread has requested a wakeup that has not
1505
+ // been drained on the main thread yet.
1506
+ const pending = func + emnapiTSFN.offset.async_pending;
1507
+ // `scheduled` prevents queueing the same main-thread drain chain more than
1508
+ // once while a previous wakeup is still in flight.
1509
+ const scheduled = func + emnapiTSFN.offset.async_u_fd;
1510
+ const end = Math.max(pending, scheduled) + 4;
1511
+ const state = () => new Int32Array(emnapiTSFN.ensureBufferFor(end));
1512
+ if (Atomics.exchange(state(), scheduled >>> 2, 1) !== 0) {
1513
+ return;
1514
+ }
1515
+ // Match uv_async_send-style coalescing in JS: the first turn represents
1516
+ // the wakeup reaching the main thread, and the second turn performs the
1517
+ // actual TSFN drain after nearby Send/Signal calls have had a chance to
1518
+ // collapse into the shared AsyncProgressWorker state.
1519
+ emnapiCtx.features.setImmediate(() => {
1520
+ if (!emnapiTSFN._liveSet.has(func)) {
1521
+ return;
1522
+ }
1523
+ if (Atomics.load(state(), pending >>> 2) === 0) {
1524
+ Atomics.store(state(), scheduled >>> 2, 0);
1525
+ return;
1526
+ }
1527
+ emnapiCtx.features.setImmediate(() => {
1528
+ // After destroy(), the func address is freed. Skip the atomics
1529
+ // on that address entirely to avoid use-after-free (JS-side
1530
+ // lifecycle check must run before touching the state words).
1531
+ if (!emnapiTSFN._liveSet.has(func)) {
1532
+ return;
1533
+ }
1534
+ try {
1535
+ // Consume the coalesced wakeup once, then let dispatch() observe any
1536
+ // queue mutations through dispatch_state like the C implementation.
1537
+ if (Atomics.exchange(state(), pending >>> 2, 0) === 0) {
1538
+ return;
1539
+ }
1540
+ emnapiTSFN.dispatch(func);
1541
+ }
1542
+ finally {
1543
+ // Allow a later wakeup to schedule a new drain chain. If another
1544
+ // worker-thread send raced with this drain, enqueue one more turn.
1545
+ if (emnapiTSFN._liveSet.has(func)) {
1546
+ Atomics.store(state(), scheduled >>> 2, 0);
1547
+ if (Atomics.load(state(), pending >>> 2) !== 0) {
1548
+ emnapiTSFN.enqueue(func);
1549
+ }
1550
+ }
1551
+ }
1552
+ });
1553
+ });
1554
+ },
1555
+ send(func) {
1556
+ const dispatchStateAddress = func + emnapiTSFN.offset.dispatch_state;
1557
+ const current_state = Atomics.or(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), dispatchStateAddress >>> 2, 1 << 1);
1558
+ if ((current_state & 1) === 1) {
1559
+ return;
1560
+ }
1561
+ const pendng = func + emnapiTSFN.offset.async_pending;
1562
+ // A wakeup is already pending, so this send only needs to leave the queued
1563
+ // work in the TSFN queue and let the existing drain pick it up.
1564
+ if (Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2) !== 0) {
1565
+ return;
1566
+ }
1567
+ if (Atomics.exchange(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2, 1) === 0) {
1568
+ if ((typeof emscripten_runtime.ENVIRONMENT_IS_PTHREAD !== 'undefined') && emscripten_runtime.ENVIRONMENT_IS_PTHREAD) {
1569
+ // Worker threads only post a wakeup token. Main-thread draining is
1570
+ // serialized by enqueue() once the message is received.
1571
+ postMessage({
1572
+ __emnapi__: {
1573
+ type: 'tsfn-send',
1574
+ payload: {
1575
+ tsfn: func
1576
+ }
1577
+ }
1578
+ });
1579
+ }
1580
+ else {
1581
+ // On the main thread we can skip the cross-thread hop and schedule the
1582
+ // coalesced drain chain directly.
1583
+ emnapiTSFN.enqueue(func);
1584
+ }
1585
+ }
1586
+ }
1587
+ };
1588
+ emnapiTSFN.init();
1589
+ emnapiPluginCtx.emnapiTSFN = emnapiTSFN;
1590
+ /**
1591
+ * @__deps _emnapi_node_emit_async_init
1592
+ * @__deps _emnapi_runtime_keepalive_push
1593
+ * @__sig ippppppppppp
1594
+ */
1595
+ function napi_create_threadsafe_function(env, func, async_resource, async_resource_name, max_queue_size, initial_thread_count, thread_finalize_data, thread_finalize_cb, context, call_js_cb, result) {
1596
+ if (!env)
1597
+ return 1 /* napi_status.napi_invalid_arg */;
1598
+ // @ts-expect-error
1599
+ const envObject = emnapi_shared.emnapiEnv;
1600
+ envObject.checkGCAccess();
1601
+ if (!async_resource_name)
1602
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1603
+ max_queue_size >>>= 0;
1604
+ initial_thread_count >>>= 0;
1605
+ env >>>= 0;
1606
+ thread_finalize_data >>>= 0;
1607
+ thread_finalize_cb >>>= 0;
1608
+ context >>>= 0;
1609
+ call_js_cb >>>= 0;
1610
+ max_queue_size = max_queue_size >>> 0;
1611
+ initial_thread_count = initial_thread_count >>> 0;
1612
+ if (initial_thread_count === 0) {
1613
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1614
+ }
1615
+ if (!result)
1616
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1617
+ let ref = 0;
1618
+ func >>>= 0;
1619
+ if (!func) {
1620
+ if (!call_js_cb)
1621
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1622
+ }
1623
+ else {
1624
+ const funcValue = emnapiCtx.jsValueFromNapiValue(func);
1625
+ if (typeof funcValue !== 'function') {
1626
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1627
+ }
1628
+ ref = emnapiCtx.createReference(envObject, func, 1, 1 /* ReferenceOwnership.kUserland */).id;
1629
+ }
1630
+ let asyncResourceObject;
1631
+ if (async_resource) {
1632
+ asyncResourceObject = emnapiCtx.jsValueFromNapiValue(async_resource);
1633
+ if (asyncResourceObject == null) {
1634
+ return envObject.setLastError(2 /* napi_status.napi_object_expected */);
1635
+ }
1636
+ asyncResourceObject = Object(asyncResourceObject);
1637
+ }
1638
+ else {
1639
+ asyncResourceObject = {};
1640
+ }
1641
+ const resource = emnapiCtx.napiValueFromJsValue(asyncResourceObject);
1642
+ let asyncResourceName = emnapiCtx.jsValueFromNapiValue(async_resource_name);
1643
+ if (typeof asyncResourceName === 'symbol') {
1644
+ return envObject.setLastError(3 /* napi_status.napi_string_expected */);
1645
+ }
1646
+ asyncResourceName = String(asyncResourceName);
1647
+ const resource_name = emnapiCtx.napiValueFromJsValue(asyncResourceName);
1648
+ // tsfn create
1649
+ const sizeofTSFN = emnapiTSFN.offset.__size__;
1650
+ let tsfn = emscripten_runtime._malloc(sizeofTSFN);
1651
+ if (!tsfn)
1652
+ return envObject.setLastError(9 /* napi_status.napi_generic_failure */);
1653
+ tsfn >>>= 0;
1654
+ new Uint8Array(emnapiTSFN.ensureBufferFor(tsfn + sizeofTSFN)).subarray(tsfn, tsfn + sizeofTSFN).fill(0);
1655
+ const resourceRef = emnapiCtx.createReference(envObject, resource, 1, 1 /* ReferenceOwnership.kUserland */);
1656
+ const resource_ = resourceRef.id;
1657
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
1658
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.resource, resource_, true);
1659
+ if (!emnapiTSFN.initQueue(tsfn)) {
1660
+ emscripten_runtime._free(tsfn);
1661
+ resourceRef.dispose();
1662
+ return envObject.setLastError(9 /* napi_status.napi_generic_failure */);
1663
+ }
1664
+ __emnapi_node_emit_async_init(resource, resource_name, -1, tsfn + emnapiTSFN.offset.async_id);
1665
+ GET_HEAP_DATA_VIEW().setInt8(tsfn + emnapiTSFN.offset.is_some, 1, true);
1666
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.thread_count, initial_thread_count, true);
1667
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.context, context, true);
1668
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.max_queue_size, max_queue_size, true);
1669
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.ref, ref, true);
1670
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.env, env, true);
1671
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.finalize_data, thread_finalize_data, true);
1672
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.finalize_cb, thread_finalize_cb, true);
1673
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.call_js_cb, call_js_cb, true);
1674
+ emnapiCtx.addCleanupHook(envObject, emnapiTSFN.cleanup, tsfn);
1675
+ emnapiTSFN._liveSet.add(tsfn);
1676
+ envObject.ref();
1677
+ __emnapi_runtime_keepalive_push();
1678
+ emnapiCtx.increaseWaitingRequestCounter();
1679
+ GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.async_ref, 1, true);
1680
+ result >>>= 0;
1681
+ GET_HEAP_DATA_VIEW().setUint32(result, tsfn, true);
1682
+ return envObject.clearLastError();
1683
+ }
1684
+ /** @__sig ipp */
1685
+ function napi_get_threadsafe_function_context(func, result) {
1686
+ if (!func || !result) {
1687
+ emscripten_runtime.abort();
1688
+ return 1 /* napi_status.napi_invalid_arg */;
1689
+ }
1690
+ func >>>= 0;
1691
+ result >>>= 0;
1692
+ const context = emnapiTSFN.getContext(func);
1693
+ var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : (HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer));
1694
+ GET_HEAP_DATA_VIEW().setUint32(result, context, true);
1695
+ return 0 /* napi_status.napi_ok */;
1696
+ }
1697
+ /** @__sig ippi */
1698
+ function napi_call_threadsafe_function(func, data, mode) {
1699
+ if (!func) {
1700
+ emscripten_runtime.abort();
1701
+ return 1 /* napi_status.napi_invalid_arg */;
1702
+ }
1703
+ func >>>= 0;
1704
+ data >>>= 0;
1705
+ return emnapiTSFN.push(func, data, mode);
1706
+ }
1707
+ /** @__sig ip */
1708
+ function napi_acquire_threadsafe_function(func) {
1709
+ if (!func) {
1710
+ emscripten_runtime.abort();
1711
+ return 1 /* napi_status.napi_invalid_arg */;
1712
+ }
1713
+ func >>>= 0;
1714
+ const mutex = emnapiTSFN.getMutex(func);
1715
+ return mutex.execute(() => {
1716
+ if (emnapiTSFN.getState(func) === 0 /* State.kOpen */) {
1717
+ emnapiTSFN.addThreadCount(func);
1718
+ return 0 /* napi_status.napi_ok */;
1719
+ }
1720
+ return 16 /* napi_status.napi_closing */;
1721
+ });
1722
+ }
1723
+ /** @__sig ipi */
1724
+ function napi_release_threadsafe_function(func, mode) {
1725
+ if (!func) {
1726
+ emscripten_runtime.abort();
1727
+ return 1 /* napi_status.napi_invalid_arg */;
1728
+ }
1729
+ func >>>= 0;
1730
+ const mutex = emnapiTSFN.getMutex(func);
1731
+ const cond = emnapiTSFN.getCond(func);
1732
+ let shouldDelete = false;
1733
+ const ret = mutex.execute(() => {
1734
+ if (emnapiTSFN.getThreadCount(func) === 0) {
1735
+ return 1 /* napi_status.napi_invalid_arg */;
1736
+ }
1737
+ emnapiTSFN.subThreadCount(func);
1738
+ if (emnapiTSFN.getThreadCount(func) === 0 || mode === 1 /* napi_threadsafe_function_release_mode.napi_tsfn_abort */) {
1739
+ if (emnapiTSFN.getState(func) === 0 /* State.kOpen */) {
1740
+ if (mode === 1 /* napi_threadsafe_function_release_mode.napi_tsfn_abort */) {
1741
+ emnapiTSFN.setState(func, 1 /* State.kClosing */);
1742
+ }
1743
+ if (emnapiTSFN.getState(func) === 1 /* State.kClosing */ && emnapiTSFN.getMaxQueueSize(func) > 0) {
1744
+ cond.signal();
1745
+ }
1746
+ emnapiTSFN.send(func);
1747
+ }
1748
+ }
1749
+ if (!(emnapiTSFN.getState(func) === 2 /* State.kClosed */ && emnapiTSFN.getThreadCount(func) === 0)) {
1750
+ return 0 /* napi_status.napi_ok */;
1751
+ }
1752
+ shouldDelete = true;
1753
+ return 0 /* napi_status.napi_ok */;
1754
+ });
1755
+ if (shouldDelete) {
1756
+ emnapiTSFN.destroy(func);
1757
+ }
1758
+ return ret;
1759
+ }
1760
+ /**
1761
+ * @__deps _emnapi_runtime_keepalive_pop
1762
+ * @__sig ipp
1763
+ */
1764
+ function napi_unref_threadsafe_function(env, func) {
1765
+ if (!func) {
1766
+ emscripten_runtime.abort();
1767
+ return 1 /* napi_status.napi_invalid_arg */;
1768
+ }
1769
+ func >>>= 0;
1770
+ const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
1771
+ const asyncRefOffset = asyncRefAddress >>> 2;
1772
+ const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
1773
+ const currentValue = Atomics.load(arr, asyncRefOffset);
1774
+ if (currentValue > 0) {
1775
+ Atomics.store(arr, asyncRefOffset, currentValue - 1);
1776
+ if (currentValue === 1) {
1777
+ __emnapi_runtime_keepalive_pop();
1778
+ emnapiCtx.decreaseWaitingRequestCounter();
1779
+ }
1780
+ }
1781
+ return 0 /* napi_status.napi_ok */;
1782
+ }
1783
+ /**
1784
+ * @__deps _emnapi_runtime_keepalive_push
1785
+ * @__sig ipp
1786
+ */
1787
+ function napi_ref_threadsafe_function(env, func) {
1788
+ if (!func) {
1789
+ emscripten_runtime.abort();
1790
+ return 1 /* napi_status.napi_invalid_arg */;
1791
+ }
1792
+ func >>>= 0;
1793
+ const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
1794
+ const asyncRefOffset = asyncRefAddress >>> 2;
1795
+ const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
1796
+ const currentValue = Atomics.load(arr, asyncRefOffset);
1797
+ if (!currentValue) {
1798
+ __emnapi_runtime_keepalive_push();
1799
+ emnapiCtx.increaseWaitingRequestCounter();
1800
+ }
1801
+ Atomics.store(arr, asyncRefOffset, currentValue + 1);
1802
+ return 0 /* napi_status.napi_ok */;
1803
+ }
1804
+
1805
+ //#endregion src/threadsafe-function.ts
1806
+
1807
+ exports.napi_acquire_threadsafe_function = napi_acquire_threadsafe_function;
1808
+ exports.napi_call_threadsafe_function = napi_call_threadsafe_function;
1809
+ exports.napi_create_threadsafe_function = napi_create_threadsafe_function;
1810
+ exports.napi_get_threadsafe_function_context = napi_get_threadsafe_function_context;
1811
+ exports.napi_ref_threadsafe_function = napi_ref_threadsafe_function;
1812
+ exports.napi_release_threadsafe_function = napi_release_threadsafe_function;
1813
+ exports.napi_unref_threadsafe_function = napi_unref_threadsafe_function;
1814
+
1815
+ return exports;
1816
+
1817
+ })({}, emnapiPluginCtx, emnapiPluginCtx);
1818
+ return {
1819
+ importObject: (original) => {
1820
+ Object.assign(original.napi, mod);
1821
+ }
1822
+ };
1823
+ }
1824
+
1825
+ exports.emnapiAsyncWorkPlugin = asyncWork;
1826
+ exports.emnapiTSFNPlugin = threadsafeFunction;