@napi-rs/cli 3.8.2 → 3.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/dist/cli.js +527 -25
- package/dist/index.cjs +527 -25
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +527 -25
- package/docs/wasi.md +14 -0
- package/package.json +2 -2
- package/src/api/__tests__/__snapshots__/templates.spec.ts.md +795 -18
- package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
- package/src/api/__tests__/pre-publish.spec.ts +36 -1
- package/src/api/__tests__/templates.spec.ts +133 -1
- package/src/api/pre-publish.ts +18 -12
- package/src/api/templates/load-wasi-template.ts +546 -17
|
@@ -12,7 +12,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
12
12
|
emnapiAsyncWorkPlugin as __emnapiAsyncWorkPlugin,␊
|
|
13
13
|
emnapiTSFNPlugin as __emnapiTSFNPlugin,␊
|
|
14
14
|
createOnMessage as __wasmCreateOnMessageForFsProxy,␊
|
|
15
|
-
|
|
15
|
+
instantiateNapiModule as __emnapiInstantiateNapiModule,␊
|
|
16
16
|
WASI as __WASI,␊
|
|
17
17
|
} from '@napi-rs/wasm-runtime'␊
|
|
18
18
|
import { createContext as __emnapiCreateContext } from '@emnapi/runtime'␊
|
|
@@ -42,6 +42,11 @@ Generated by [AVA](https://avajs.dev).
|
|
|
42
42
|
maximum: 65536,␊
|
|
43
43
|
shared: true,␊
|
|
44
44
|
})␊
|
|
45
|
+
const __asyncWorkPoolSize = 4␊
|
|
46
|
+
const __workerPoolSize = Math.max(␊
|
|
47
|
+
2,␊
|
|
48
|
+
globalThis.navigator?.hardwareConcurrency ?? 4,␊
|
|
49
|
+
)␊
|
|
45
50
|
␊
|
|
46
51
|
let __emnapiContext␊
|
|
47
52
|
␊
|
|
@@ -51,9 +56,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
51
56
|
let __emnapiContextDestroyed = false␊
|
|
52
57
|
let __emnapiContextDestroyPromise␊
|
|
53
58
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
59
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
60
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
61
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
54
62
|
let __wasiDisposed = false␊
|
|
55
63
|
let __wasiDisposePromise␊
|
|
56
64
|
let __completeWasiDisposal = function() {}␊
|
|
65
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
66
|
+
// that stopped short of destroying the context. See␊
|
|
67
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
68
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
57
69
|
␊
|
|
58
70
|
function __isThenable(value) {␊
|
|
59
71
|
return (␊
|
|
@@ -124,10 +136,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
124
136
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
125
137
|
if (typeof prepare === 'function') {␊
|
|
126
138
|
prepare()␊
|
|
139
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
127
140
|
}␊
|
|
128
141
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
129
142
|
}␊
|
|
130
143
|
␊
|
|
144
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
145
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
146
|
+
// ahead of it on a faster queue.␊
|
|
147
|
+
const __scheduleMacrotask = (function () {␊
|
|
148
|
+
if (typeof setImmediate === 'function') {␊
|
|
149
|
+
return function (callback) {␊
|
|
150
|
+
setImmediate(callback)␊
|
|
151
|
+
}␊
|
|
152
|
+
}␊
|
|
153
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
154
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
155
|
+
return function (callback) {␊
|
|
156
|
+
const channel = new __MessageChannel()␊
|
|
157
|
+
channel.port1.onmessage = function () {␊
|
|
158
|
+
channel.port1.onmessage = null␊
|
|
159
|
+
try {␊
|
|
160
|
+
channel.port1.close()␊
|
|
161
|
+
} catch {}␊
|
|
162
|
+
try {␊
|
|
163
|
+
channel.port2.close()␊
|
|
164
|
+
} catch {}␊
|
|
165
|
+
callback()␊
|
|
166
|
+
}␊
|
|
167
|
+
channel.port2.postMessage(null)␊
|
|
168
|
+
}␊
|
|
169
|
+
}␊
|
|
170
|
+
return function (callback) {␊
|
|
171
|
+
setTimeout(callback, 0)␊
|
|
172
|
+
}␊
|
|
173
|
+
})()␊
|
|
174
|
+
␊
|
|
175
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
176
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
177
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
178
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
179
|
+
// bounded either way.␊
|
|
180
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
181
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
182
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
183
|
+
// call made on this thread (two), plus a margin.␊
|
|
184
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
185
|
+
␊
|
|
186
|
+
/**␊
|
|
187
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
188
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
189
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
190
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
191
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
192
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
193
|
+
*␊
|
|
194
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
195
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
196
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
197
|
+
* macrotask run.␊
|
|
198
|
+
*␊
|
|
199
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
200
|
+
* synchronous in the common case.␊
|
|
201
|
+
*␊
|
|
202
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
203
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
204
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
205
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
206
|
+
* the context with the barrier's settlements still queued.␊
|
|
207
|
+
*␊
|
|
208
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
209
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
210
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
211
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
212
|
+
* flag unset and disposal retryable.␊
|
|
213
|
+
*/␊
|
|
214
|
+
function __drainWasmEnvCleanup() {␊
|
|
215
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
216
|
+
return␊
|
|
217
|
+
}␊
|
|
218
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
219
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
220
|
+
}␊
|
|
221
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
222
|
+
const observable = typeof pending === 'function'␊
|
|
223
|
+
if (observable) {␊
|
|
224
|
+
let queued␊
|
|
225
|
+
try {␊
|
|
226
|
+
queued = pending()␊
|
|
227
|
+
} catch {␊
|
|
228
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
229
|
+
return␊
|
|
230
|
+
}␊
|
|
231
|
+
if (!queued) {␊
|
|
232
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
233
|
+
return␊
|
|
234
|
+
}␊
|
|
235
|
+
}␊
|
|
236
|
+
const limit = observable␊
|
|
237
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
238
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
239
|
+
const drainPromise = (async () => {␊
|
|
240
|
+
let queued = 0␊
|
|
241
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
242
|
+
await new Promise((resolve) => {␊
|
|
243
|
+
__scheduleMacrotask(resolve)␊
|
|
244
|
+
})␊
|
|
245
|
+
if (!observable) {␊
|
|
246
|
+
continue␊
|
|
247
|
+
}␊
|
|
248
|
+
try {␊
|
|
249
|
+
queued = pending()␊
|
|
250
|
+
} catch {␊
|
|
251
|
+
return␊
|
|
252
|
+
}␊
|
|
253
|
+
if (!queued) {␊
|
|
254
|
+
return␊
|
|
255
|
+
}␊
|
|
256
|
+
}␊
|
|
257
|
+
if (!observable) {␊
|
|
258
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
259
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
260
|
+
// finishing the drain.␊
|
|
261
|
+
return␊
|
|
262
|
+
}␊
|
|
263
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
264
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
265
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
266
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
267
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
268
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
269
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
270
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
271
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
272
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
273
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
274
|
+
const drainError = new Error(␊
|
|
275
|
+
'the wasm environment still reports ' +␊
|
|
276
|
+
queued +␊
|
|
277
|
+
' queued settlement(s) after ' +␊
|
|
278
|
+
limit +␊
|
|
279
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
280
|
+
)␊
|
|
281
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
282
|
+
throw drainError␊
|
|
283
|
+
})().then(␊
|
|
284
|
+
(value) => {␊
|
|
285
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
286
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
287
|
+
// queued rejects above and must stay repeatable.␊
|
|
288
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
289
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
290
|
+
return value␊
|
|
291
|
+
},␊
|
|
292
|
+
(error) => {␊
|
|
293
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
294
|
+
throw error␊
|
|
295
|
+
},␊
|
|
296
|
+
)␊
|
|
297
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
298
|
+
return drainPromise␊
|
|
299
|
+
}␊
|
|
300
|
+
␊
|
|
131
301
|
function __destroyEmnapiContext() {␊
|
|
132
302
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
133
303
|
__emnapiContextDestroyed = true␊
|
|
@@ -205,7 +375,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
205
375
|
return __completeWasiDisposal()␊
|
|
206
376
|
}␊
|
|
207
377
|
␊
|
|
208
|
-
function
|
|
378
|
+
function __continueWasiDisposal() {␊
|
|
209
379
|
const destroyResult = __destroyEmnapiContext()␊
|
|
210
380
|
if (__isThenable(destroyResult)) {␊
|
|
211
381
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -213,6 +383,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
213
383
|
return __finishWasiDisposal()␊
|
|
214
384
|
}␊
|
|
215
385
|
␊
|
|
386
|
+
function __startWasiDisposal() {␊
|
|
387
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
388
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
389
|
+
// back to back is what strands them.␊
|
|
390
|
+
__prepareWasmEnvCleanup()␊
|
|
391
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
392
|
+
if (__isThenable(drainResult)) {␊
|
|
393
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
394
|
+
}␊
|
|
395
|
+
return __continueWasiDisposal()␊
|
|
396
|
+
}␊
|
|
397
|
+
␊
|
|
216
398
|
/**␊
|
|
217
399
|
* Disposes this generated WASI binding.␊
|
|
218
400
|
*␊
|
|
@@ -284,8 +466,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
284
466
|
return cleanupErrors␊
|
|
285
467
|
}␊
|
|
286
468
|
␊
|
|
287
|
-
function
|
|
288
|
-
const cleanupErrors = []␊
|
|
469
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
289
470
|
let destroyResult␊
|
|
290
471
|
try {␊
|
|
291
472
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -303,6 +484,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
303
484
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
304
485
|
}␊
|
|
305
486
|
␊
|
|
487
|
+
/**␊
|
|
488
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
489
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
490
|
+
*/␊
|
|
491
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
492
|
+
try {␊
|
|
493
|
+
__retainWasiRollbackForRetry()␊
|
|
494
|
+
} catch (cleanupError) {␊
|
|
495
|
+
cleanupErrors.push(cleanupError)␊
|
|
496
|
+
}␊
|
|
497
|
+
return cleanupErrors␊
|
|
498
|
+
}␊
|
|
499
|
+
␊
|
|
500
|
+
/**␊
|
|
501
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
502
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
503
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
504
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
505
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
506
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
507
|
+
*␊
|
|
508
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
509
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
510
|
+
*␊
|
|
511
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
512
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
513
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
514
|
+
* two trades, and not because of what it saves:␊
|
|
515
|
+
*␊
|
|
516
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
517
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
518
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
519
|
+
* forever with nothing left that could ever settle it.␊
|
|
520
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
521
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
522
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
523
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
524
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
525
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
526
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
527
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
528
|
+
* what makes that retained record useless.␊
|
|
529
|
+
*␊
|
|
530
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
531
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
532
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
533
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
534
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
535
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
536
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
537
|
+
*/␊
|
|
538
|
+
function __rollbackWasiInitialization() {␊
|
|
539
|
+
const cleanupErrors = []␊
|
|
540
|
+
let drainResult␊
|
|
541
|
+
let settlementsUnreached = false␊
|
|
542
|
+
try {␊
|
|
543
|
+
__prepareWasmEnvCleanup()␊
|
|
544
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
545
|
+
} catch (cleanupError) {␊
|
|
546
|
+
cleanupErrors.push(cleanupError)␊
|
|
547
|
+
settlementsUnreached = true␊
|
|
548
|
+
}␊
|
|
549
|
+
if (__isThenable(drainResult)) {␊
|
|
550
|
+
return Promise.resolve(drainResult).then(␊
|
|
551
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
552
|
+
(cleanupError) => {␊
|
|
553
|
+
cleanupErrors.push(cleanupError)␊
|
|
554
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
555
|
+
},␊
|
|
556
|
+
)␊
|
|
557
|
+
}␊
|
|
558
|
+
if (settlementsUnreached) {␊
|
|
559
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
560
|
+
}␊
|
|
561
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
562
|
+
}␊
|
|
563
|
+
␊
|
|
306
564
|
let __wasiModule␊
|
|
307
565
|
let __napiModule␊
|
|
308
566
|
␊
|
|
@@ -314,9 +572,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
314
572
|
instance: __napiInstance,␊
|
|
315
573
|
module: __wasiModule,␊
|
|
316
574
|
napiModule: __napiModule,␊
|
|
317
|
-
} =
|
|
575
|
+
} = await __emnapiInstantiateNapiModule(__wasmFile, {␊
|
|
318
576
|
context: __emnapiContext,␊
|
|
319
|
-
asyncWorkPoolSize:
|
|
577
|
+
asyncWorkPoolSize: __asyncWorkPoolSize,␊
|
|
578
|
+
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },␊
|
|
320
579
|
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],␊
|
|
321
580
|
wasi: __wasi,␊
|
|
322
581
|
onCreateWorker() {␊
|
|
@@ -361,7 +620,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
361
620
|
emnapiAsyncWorkPlugin as __emnapiAsyncWorkPlugin,␊
|
|
362
621
|
emnapiTSFNPlugin as __emnapiTSFNPlugin,␊
|
|
363
622
|
createOnMessage as __wasmCreateOnMessageForFsProxy,␊
|
|
364
|
-
|
|
623
|
+
instantiateNapiModule as __emnapiInstantiateNapiModule,␊
|
|
365
624
|
WASI as __WASI,␊
|
|
366
625
|
} from '@napi-rs/wasm-runtime'␊
|
|
367
626
|
import { createContext as __emnapiCreateContext } from '@emnapi/runtime'␊
|
|
@@ -391,6 +650,11 @@ Generated by [AVA](https://avajs.dev).
|
|
|
391
650
|
maximum: 65536,␊
|
|
392
651
|
shared: true,␊
|
|
393
652
|
})␊
|
|
653
|
+
const __asyncWorkPoolSize = 4␊
|
|
654
|
+
const __workerPoolSize = Math.max(␊
|
|
655
|
+
2,␊
|
|
656
|
+
globalThis.navigator?.hardwareConcurrency ?? 4,␊
|
|
657
|
+
)␊
|
|
394
658
|
␊
|
|
395
659
|
let __emnapiContext␊
|
|
396
660
|
␊
|
|
@@ -400,9 +664,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
400
664
|
let __emnapiContextDestroyed = false␊
|
|
401
665
|
let __emnapiContextDestroyPromise␊
|
|
402
666
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
667
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
668
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
669
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
403
670
|
let __wasiDisposed = false␊
|
|
404
671
|
let __wasiDisposePromise␊
|
|
405
672
|
let __completeWasiDisposal = function() {}␊
|
|
673
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
674
|
+
// that stopped short of destroying the context. See␊
|
|
675
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
676
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
406
677
|
␊
|
|
407
678
|
function __isThenable(value) {␊
|
|
408
679
|
return (␊
|
|
@@ -473,10 +744,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
473
744
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
474
745
|
if (typeof prepare === 'function') {␊
|
|
475
746
|
prepare()␊
|
|
747
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
476
748
|
}␊
|
|
477
749
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
478
750
|
}␊
|
|
479
751
|
␊
|
|
752
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
753
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
754
|
+
// ahead of it on a faster queue.␊
|
|
755
|
+
const __scheduleMacrotask = (function () {␊
|
|
756
|
+
if (typeof setImmediate === 'function') {␊
|
|
757
|
+
return function (callback) {␊
|
|
758
|
+
setImmediate(callback)␊
|
|
759
|
+
}␊
|
|
760
|
+
}␊
|
|
761
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
762
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
763
|
+
return function (callback) {␊
|
|
764
|
+
const channel = new __MessageChannel()␊
|
|
765
|
+
channel.port1.onmessage = function () {␊
|
|
766
|
+
channel.port1.onmessage = null␊
|
|
767
|
+
try {␊
|
|
768
|
+
channel.port1.close()␊
|
|
769
|
+
} catch {}␊
|
|
770
|
+
try {␊
|
|
771
|
+
channel.port2.close()␊
|
|
772
|
+
} catch {}␊
|
|
773
|
+
callback()␊
|
|
774
|
+
}␊
|
|
775
|
+
channel.port2.postMessage(null)␊
|
|
776
|
+
}␊
|
|
777
|
+
}␊
|
|
778
|
+
return function (callback) {␊
|
|
779
|
+
setTimeout(callback, 0)␊
|
|
780
|
+
}␊
|
|
781
|
+
})()␊
|
|
782
|
+
␊
|
|
783
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
784
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
785
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
786
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
787
|
+
// bounded either way.␊
|
|
788
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
789
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
790
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
791
|
+
// call made on this thread (two), plus a margin.␊
|
|
792
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
793
|
+
␊
|
|
794
|
+
/**␊
|
|
795
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
796
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
797
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
798
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
799
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
800
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
801
|
+
*␊
|
|
802
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
803
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
804
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
805
|
+
* macrotask run.␊
|
|
806
|
+
*␊
|
|
807
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
808
|
+
* synchronous in the common case.␊
|
|
809
|
+
*␊
|
|
810
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
811
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
812
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
813
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
814
|
+
* the context with the barrier's settlements still queued.␊
|
|
815
|
+
*␊
|
|
816
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
817
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
818
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
819
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
820
|
+
* flag unset and disposal retryable.␊
|
|
821
|
+
*/␊
|
|
822
|
+
function __drainWasmEnvCleanup() {␊
|
|
823
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
824
|
+
return␊
|
|
825
|
+
}␊
|
|
826
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
827
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
828
|
+
}␊
|
|
829
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
830
|
+
const observable = typeof pending === 'function'␊
|
|
831
|
+
if (observable) {␊
|
|
832
|
+
let queued␊
|
|
833
|
+
try {␊
|
|
834
|
+
queued = pending()␊
|
|
835
|
+
} catch {␊
|
|
836
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
837
|
+
return␊
|
|
838
|
+
}␊
|
|
839
|
+
if (!queued) {␊
|
|
840
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
841
|
+
return␊
|
|
842
|
+
}␊
|
|
843
|
+
}␊
|
|
844
|
+
const limit = observable␊
|
|
845
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
846
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
847
|
+
const drainPromise = (async () => {␊
|
|
848
|
+
let queued = 0␊
|
|
849
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
850
|
+
await new Promise((resolve) => {␊
|
|
851
|
+
__scheduleMacrotask(resolve)␊
|
|
852
|
+
})␊
|
|
853
|
+
if (!observable) {␊
|
|
854
|
+
continue␊
|
|
855
|
+
}␊
|
|
856
|
+
try {␊
|
|
857
|
+
queued = pending()␊
|
|
858
|
+
} catch {␊
|
|
859
|
+
return␊
|
|
860
|
+
}␊
|
|
861
|
+
if (!queued) {␊
|
|
862
|
+
return␊
|
|
863
|
+
}␊
|
|
864
|
+
}␊
|
|
865
|
+
if (!observable) {␊
|
|
866
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
867
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
868
|
+
// finishing the drain.␊
|
|
869
|
+
return␊
|
|
870
|
+
}␊
|
|
871
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
872
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
873
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
874
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
875
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
876
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
877
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
878
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
879
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
880
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
881
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
882
|
+
const drainError = new Error(␊
|
|
883
|
+
'the wasm environment still reports ' +␊
|
|
884
|
+
queued +␊
|
|
885
|
+
' queued settlement(s) after ' +␊
|
|
886
|
+
limit +␊
|
|
887
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
888
|
+
)␊
|
|
889
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
890
|
+
throw drainError␊
|
|
891
|
+
})().then(␊
|
|
892
|
+
(value) => {␊
|
|
893
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
894
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
895
|
+
// queued rejects above and must stay repeatable.␊
|
|
896
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
897
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
898
|
+
return value␊
|
|
899
|
+
},␊
|
|
900
|
+
(error) => {␊
|
|
901
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
902
|
+
throw error␊
|
|
903
|
+
},␊
|
|
904
|
+
)␊
|
|
905
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
906
|
+
return drainPromise␊
|
|
907
|
+
}␊
|
|
908
|
+
␊
|
|
480
909
|
function __destroyEmnapiContext() {␊
|
|
481
910
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
482
911
|
__emnapiContextDestroyed = true␊
|
|
@@ -554,7 +983,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
554
983
|
return __completeWasiDisposal()␊
|
|
555
984
|
}␊
|
|
556
985
|
␊
|
|
557
|
-
function
|
|
986
|
+
function __continueWasiDisposal() {␊
|
|
558
987
|
const destroyResult = __destroyEmnapiContext()␊
|
|
559
988
|
if (__isThenable(destroyResult)) {␊
|
|
560
989
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -562,6 +991,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
562
991
|
return __finishWasiDisposal()␊
|
|
563
992
|
}␊
|
|
564
993
|
␊
|
|
994
|
+
function __startWasiDisposal() {␊
|
|
995
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
996
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
997
|
+
// back to back is what strands them.␊
|
|
998
|
+
__prepareWasmEnvCleanup()␊
|
|
999
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
1000
|
+
if (__isThenable(drainResult)) {␊
|
|
1001
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
1002
|
+
}␊
|
|
1003
|
+
return __continueWasiDisposal()␊
|
|
1004
|
+
}␊
|
|
1005
|
+
␊
|
|
565
1006
|
/**␊
|
|
566
1007
|
* Disposes this generated WASI binding.␊
|
|
567
1008
|
*␊
|
|
@@ -633,8 +1074,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
633
1074
|
return cleanupErrors␊
|
|
634
1075
|
}␊
|
|
635
1076
|
␊
|
|
636
|
-
function
|
|
637
|
-
const cleanupErrors = []␊
|
|
1077
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
638
1078
|
let destroyResult␊
|
|
639
1079
|
try {␊
|
|
640
1080
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -652,6 +1092,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
652
1092
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
653
1093
|
}␊
|
|
654
1094
|
␊
|
|
1095
|
+
/**␊
|
|
1096
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
1097
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
1098
|
+
*/␊
|
|
1099
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
1100
|
+
try {␊
|
|
1101
|
+
__retainWasiRollbackForRetry()␊
|
|
1102
|
+
} catch (cleanupError) {␊
|
|
1103
|
+
cleanupErrors.push(cleanupError)␊
|
|
1104
|
+
}␊
|
|
1105
|
+
return cleanupErrors␊
|
|
1106
|
+
}␊
|
|
1107
|
+
␊
|
|
1108
|
+
/**␊
|
|
1109
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
1110
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
1111
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
1112
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
1113
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
1114
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
1115
|
+
*␊
|
|
1116
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
1117
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
1118
|
+
*␊
|
|
1119
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
1120
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
1121
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
1122
|
+
* two trades, and not because of what it saves:␊
|
|
1123
|
+
*␊
|
|
1124
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
1125
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
1126
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
1127
|
+
* forever with nothing left that could ever settle it.␊
|
|
1128
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
1129
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
1130
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
1131
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
1132
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
1133
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
1134
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
1135
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
1136
|
+
* what makes that retained record useless.␊
|
|
1137
|
+
*␊
|
|
1138
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
1139
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
1140
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
1141
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
1142
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
1143
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
1144
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
1145
|
+
*/␊
|
|
1146
|
+
function __rollbackWasiInitialization() {␊
|
|
1147
|
+
const cleanupErrors = []␊
|
|
1148
|
+
let drainResult␊
|
|
1149
|
+
let settlementsUnreached = false␊
|
|
1150
|
+
try {␊
|
|
1151
|
+
__prepareWasmEnvCleanup()␊
|
|
1152
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
1153
|
+
} catch (cleanupError) {␊
|
|
1154
|
+
cleanupErrors.push(cleanupError)␊
|
|
1155
|
+
settlementsUnreached = true␊
|
|
1156
|
+
}␊
|
|
1157
|
+
if (__isThenable(drainResult)) {␊
|
|
1158
|
+
return Promise.resolve(drainResult).then(␊
|
|
1159
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
1160
|
+
(cleanupError) => {␊
|
|
1161
|
+
cleanupErrors.push(cleanupError)␊
|
|
1162
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1163
|
+
},␊
|
|
1164
|
+
)␊
|
|
1165
|
+
}␊
|
|
1166
|
+
if (settlementsUnreached) {␊
|
|
1167
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1168
|
+
}␊
|
|
1169
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1170
|
+
}␊
|
|
1171
|
+
␊
|
|
655
1172
|
let __wasiModule␊
|
|
656
1173
|
let __napiModule␊
|
|
657
1174
|
␊
|
|
@@ -663,9 +1180,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
663
1180
|
instance: __napiInstance,␊
|
|
664
1181
|
module: __wasiModule,␊
|
|
665
1182
|
napiModule: __napiModule,␊
|
|
666
|
-
} =
|
|
1183
|
+
} = await __emnapiInstantiateNapiModule(__wasmFile, {␊
|
|
667
1184
|
context: __emnapiContext,␊
|
|
668
|
-
asyncWorkPoolSize:
|
|
1185
|
+
asyncWorkPoolSize: __asyncWorkPoolSize,␊
|
|
1186
|
+
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },␊
|
|
669
1187
|
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],␊
|
|
670
1188
|
wasi: __wasi,␊
|
|
671
1189
|
onCreateWorker() {␊
|
|
@@ -723,7 +1241,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
723
1241
|
emnapiAsyncWorkPlugin as __emnapiAsyncWorkPlugin,␊
|
|
724
1242
|
emnapiTSFNPlugin as __emnapiTSFNPlugin,␊
|
|
725
1243
|
createOnMessage as __wasmCreateOnMessageForFsProxy,␊
|
|
726
|
-
|
|
1244
|
+
instantiateNapiModule as __emnapiInstantiateNapiModule,␊
|
|
727
1245
|
WASI as __WASI,␊
|
|
728
1246
|
} from '@napi-rs/wasm-runtime'␊
|
|
729
1247
|
import { createContext as __emnapiCreateContext } from '@emnapi/runtime'␊
|
|
@@ -759,6 +1277,11 @@ Generated by [AVA](https://avajs.dev).
|
|
|
759
1277
|
maximum: 65536,␊
|
|
760
1278
|
shared: true,␊
|
|
761
1279
|
})␊
|
|
1280
|
+
const __asyncWorkPoolSize = 4␊
|
|
1281
|
+
const __workerPoolSize = Math.max(␊
|
|
1282
|
+
2,␊
|
|
1283
|
+
globalThis.navigator?.hardwareConcurrency ?? 4,␊
|
|
1284
|
+
)␊
|
|
762
1285
|
␊
|
|
763
1286
|
let __emnapiContext␊
|
|
764
1287
|
␊
|
|
@@ -768,9 +1291,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
768
1291
|
let __emnapiContextDestroyed = false␊
|
|
769
1292
|
let __emnapiContextDestroyPromise␊
|
|
770
1293
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
1294
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
1295
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
1296
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
771
1297
|
let __wasiDisposed = false␊
|
|
772
1298
|
let __wasiDisposePromise␊
|
|
773
1299
|
let __completeWasiDisposal = function() {}␊
|
|
1300
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
1301
|
+
// that stopped short of destroying the context. See␊
|
|
1302
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
1303
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
774
1304
|
␊
|
|
775
1305
|
function __isThenable(value) {␊
|
|
776
1306
|
return (␊
|
|
@@ -841,10 +1371,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
841
1371
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
842
1372
|
if (typeof prepare === 'function') {␊
|
|
843
1373
|
prepare()␊
|
|
1374
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
844
1375
|
}␊
|
|
845
1376
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
846
1377
|
}␊
|
|
847
1378
|
␊
|
|
1379
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
1380
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
1381
|
+
// ahead of it on a faster queue.␊
|
|
1382
|
+
const __scheduleMacrotask = (function () {␊
|
|
1383
|
+
if (typeof setImmediate === 'function') {␊
|
|
1384
|
+
return function (callback) {␊
|
|
1385
|
+
setImmediate(callback)␊
|
|
1386
|
+
}␊
|
|
1387
|
+
}␊
|
|
1388
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
1389
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
1390
|
+
return function (callback) {␊
|
|
1391
|
+
const channel = new __MessageChannel()␊
|
|
1392
|
+
channel.port1.onmessage = function () {␊
|
|
1393
|
+
channel.port1.onmessage = null␊
|
|
1394
|
+
try {␊
|
|
1395
|
+
channel.port1.close()␊
|
|
1396
|
+
} catch {}␊
|
|
1397
|
+
try {␊
|
|
1398
|
+
channel.port2.close()␊
|
|
1399
|
+
} catch {}␊
|
|
1400
|
+
callback()␊
|
|
1401
|
+
}␊
|
|
1402
|
+
channel.port2.postMessage(null)␊
|
|
1403
|
+
}␊
|
|
1404
|
+
}␊
|
|
1405
|
+
return function (callback) {␊
|
|
1406
|
+
setTimeout(callback, 0)␊
|
|
1407
|
+
}␊
|
|
1408
|
+
})()␊
|
|
1409
|
+
␊
|
|
1410
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
1411
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
1412
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
1413
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
1414
|
+
// bounded either way.␊
|
|
1415
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
1416
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
1417
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
1418
|
+
// call made on this thread (two), plus a margin.␊
|
|
1419
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
1420
|
+
␊
|
|
1421
|
+
/**␊
|
|
1422
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
1423
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
1424
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
1425
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
1426
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
1427
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
1428
|
+
*␊
|
|
1429
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
1430
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
1431
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
1432
|
+
* macrotask run.␊
|
|
1433
|
+
*␊
|
|
1434
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
1435
|
+
* synchronous in the common case.␊
|
|
1436
|
+
*␊
|
|
1437
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
1438
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
1439
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
1440
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
1441
|
+
* the context with the barrier's settlements still queued.␊
|
|
1442
|
+
*␊
|
|
1443
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
1444
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
1445
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
1446
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
1447
|
+
* flag unset and disposal retryable.␊
|
|
1448
|
+
*/␊
|
|
1449
|
+
function __drainWasmEnvCleanup() {␊
|
|
1450
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
1451
|
+
return␊
|
|
1452
|
+
}␊
|
|
1453
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
1454
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
1455
|
+
}␊
|
|
1456
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
1457
|
+
const observable = typeof pending === 'function'␊
|
|
1458
|
+
if (observable) {␊
|
|
1459
|
+
let queued␊
|
|
1460
|
+
try {␊
|
|
1461
|
+
queued = pending()␊
|
|
1462
|
+
} catch {␊
|
|
1463
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1464
|
+
return␊
|
|
1465
|
+
}␊
|
|
1466
|
+
if (!queued) {␊
|
|
1467
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1468
|
+
return␊
|
|
1469
|
+
}␊
|
|
1470
|
+
}␊
|
|
1471
|
+
const limit = observable␊
|
|
1472
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
1473
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
1474
|
+
const drainPromise = (async () => {␊
|
|
1475
|
+
let queued = 0␊
|
|
1476
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
1477
|
+
await new Promise((resolve) => {␊
|
|
1478
|
+
__scheduleMacrotask(resolve)␊
|
|
1479
|
+
})␊
|
|
1480
|
+
if (!observable) {␊
|
|
1481
|
+
continue␊
|
|
1482
|
+
}␊
|
|
1483
|
+
try {␊
|
|
1484
|
+
queued = pending()␊
|
|
1485
|
+
} catch {␊
|
|
1486
|
+
return␊
|
|
1487
|
+
}␊
|
|
1488
|
+
if (!queued) {␊
|
|
1489
|
+
return␊
|
|
1490
|
+
}␊
|
|
1491
|
+
}␊
|
|
1492
|
+
if (!observable) {␊
|
|
1493
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
1494
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
1495
|
+
// finishing the drain.␊
|
|
1496
|
+
return␊
|
|
1497
|
+
}␊
|
|
1498
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
1499
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
1500
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
1501
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
1502
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
1503
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
1504
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
1505
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
1506
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
1507
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
1508
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
1509
|
+
const drainError = new Error(␊
|
|
1510
|
+
'the wasm environment still reports ' +␊
|
|
1511
|
+
queued +␊
|
|
1512
|
+
' queued settlement(s) after ' +␊
|
|
1513
|
+
limit +␊
|
|
1514
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
1515
|
+
)␊
|
|
1516
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
1517
|
+
throw drainError␊
|
|
1518
|
+
})().then(␊
|
|
1519
|
+
(value) => {␊
|
|
1520
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
1521
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
1522
|
+
// queued rejects above and must stay repeatable.␊
|
|
1523
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1524
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
1525
|
+
return value␊
|
|
1526
|
+
},␊
|
|
1527
|
+
(error) => {␊
|
|
1528
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
1529
|
+
throw error␊
|
|
1530
|
+
},␊
|
|
1531
|
+
)␊
|
|
1532
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
1533
|
+
return drainPromise␊
|
|
1534
|
+
}␊
|
|
1535
|
+
␊
|
|
848
1536
|
function __destroyEmnapiContext() {␊
|
|
849
1537
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
850
1538
|
__emnapiContextDestroyed = true␊
|
|
@@ -922,7 +1610,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
922
1610
|
return __completeWasiDisposal()␊
|
|
923
1611
|
}␊
|
|
924
1612
|
␊
|
|
925
|
-
function
|
|
1613
|
+
function __continueWasiDisposal() {␊
|
|
926
1614
|
const destroyResult = __destroyEmnapiContext()␊
|
|
927
1615
|
if (__isThenable(destroyResult)) {␊
|
|
928
1616
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -930,6 +1618,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
930
1618
|
return __finishWasiDisposal()␊
|
|
931
1619
|
}␊
|
|
932
1620
|
␊
|
|
1621
|
+
function __startWasiDisposal() {␊
|
|
1622
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
1623
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
1624
|
+
// back to back is what strands them.␊
|
|
1625
|
+
__prepareWasmEnvCleanup()␊
|
|
1626
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
1627
|
+
if (__isThenable(drainResult)) {␊
|
|
1628
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
1629
|
+
}␊
|
|
1630
|
+
return __continueWasiDisposal()␊
|
|
1631
|
+
}␊
|
|
1632
|
+
␊
|
|
933
1633
|
/**␊
|
|
934
1634
|
* Disposes this generated WASI binding.␊
|
|
935
1635
|
*␊
|
|
@@ -1001,8 +1701,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1001
1701
|
return cleanupErrors␊
|
|
1002
1702
|
}␊
|
|
1003
1703
|
␊
|
|
1004
|
-
function
|
|
1005
|
-
const cleanupErrors = []␊
|
|
1704
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
1006
1705
|
let destroyResult␊
|
|
1007
1706
|
try {␊
|
|
1008
1707
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -1020,6 +1719,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1020
1719
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
1021
1720
|
}␊
|
|
1022
1721
|
␊
|
|
1722
|
+
/**␊
|
|
1723
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
1724
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
1725
|
+
*/␊
|
|
1726
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
1727
|
+
try {␊
|
|
1728
|
+
__retainWasiRollbackForRetry()␊
|
|
1729
|
+
} catch (cleanupError) {␊
|
|
1730
|
+
cleanupErrors.push(cleanupError)␊
|
|
1731
|
+
}␊
|
|
1732
|
+
return cleanupErrors␊
|
|
1733
|
+
}␊
|
|
1734
|
+
␊
|
|
1735
|
+
/**␊
|
|
1736
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
1737
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
1738
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
1739
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
1740
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
1741
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
1742
|
+
*␊
|
|
1743
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
1744
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
1745
|
+
*␊
|
|
1746
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
1747
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
1748
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
1749
|
+
* two trades, and not because of what it saves:␊
|
|
1750
|
+
*␊
|
|
1751
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
1752
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
1753
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
1754
|
+
* forever with nothing left that could ever settle it.␊
|
|
1755
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
1756
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
1757
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
1758
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
1759
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
1760
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
1761
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
1762
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
1763
|
+
* what makes that retained record useless.␊
|
|
1764
|
+
*␊
|
|
1765
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
1766
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
1767
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
1768
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
1769
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
1770
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
1771
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
1772
|
+
*/␊
|
|
1773
|
+
function __rollbackWasiInitialization() {␊
|
|
1774
|
+
const cleanupErrors = []␊
|
|
1775
|
+
let drainResult␊
|
|
1776
|
+
let settlementsUnreached = false␊
|
|
1777
|
+
try {␊
|
|
1778
|
+
__prepareWasmEnvCleanup()␊
|
|
1779
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
1780
|
+
} catch (cleanupError) {␊
|
|
1781
|
+
cleanupErrors.push(cleanupError)␊
|
|
1782
|
+
settlementsUnreached = true␊
|
|
1783
|
+
}␊
|
|
1784
|
+
if (__isThenable(drainResult)) {␊
|
|
1785
|
+
return Promise.resolve(drainResult).then(␊
|
|
1786
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
1787
|
+
(cleanupError) => {␊
|
|
1788
|
+
cleanupErrors.push(cleanupError)␊
|
|
1789
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1790
|
+
},␊
|
|
1791
|
+
)␊
|
|
1792
|
+
}␊
|
|
1793
|
+
if (settlementsUnreached) {␊
|
|
1794
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1795
|
+
}␊
|
|
1796
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1797
|
+
}␊
|
|
1798
|
+
␊
|
|
1023
1799
|
let __wasiModule␊
|
|
1024
1800
|
let __napiModule␊
|
|
1025
1801
|
␊
|
|
@@ -1031,9 +1807,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1031
1807
|
instance: __napiInstance,␊
|
|
1032
1808
|
module: __wasiModule,␊
|
|
1033
1809
|
napiModule: __napiModule,␊
|
|
1034
|
-
} =
|
|
1810
|
+
} = await __emnapiInstantiateNapiModule(__wasmFile, {␊
|
|
1035
1811
|
context: __emnapiContext,␊
|
|
1036
|
-
asyncWorkPoolSize:
|
|
1812
|
+
asyncWorkPoolSize: __asyncWorkPoolSize,␊
|
|
1813
|
+
reuseWorker: { size: __asyncWorkPoolSize + __workerPoolSize },␊
|
|
1037
1814
|
plugins: [__emnapiAsyncWorkPlugin, __emnapiTSFNPlugin],␊
|
|
1038
1815
|
wasi: __wasi,␊
|
|
1039
1816
|
onCreateWorker() {␊
|