@oxc-minify/binding-wasm32-wasi 0.151.0 → 0.152.0

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.
@@ -125,6 +125,20 @@ let __emnapiContextDestroyed = false
125
125
  let __emnapiContextDestroyPromise
126
126
  let __emnapiWasmEnvCleanupPrepared = false
127
127
  let __emnapiWasmEnvCleanupPreparing = false
128
+ // The closer for a barrier that is parked between `…_begin` and `…_finish`,
129
+ // set only while that window is open. `__emnapiWasmEnvCleanupPreparing` cannot
130
+ // tell those two apart on its own: it is raised both for a purely synchronous
131
+ // frame — which must not be re-entered, and which nothing outside it can
132
+ // finish — and across this window, which spans real event-loop turns, so a
133
+ // caller that cannot yield can land in the middle of one. That caller can close
134
+ // this window, because `…_finish` is idempotent and joins, which is exactly
135
+ // what the single call does. See `__prepareWasmEnvCleanup`.
136
+ let __finishParkedWasmEnvCleanup
137
+ // Raised while a caller that can still yield is driving the barrier, so the
138
+ // queue it leaves behind is expected rather than lost. See
139
+ // `__reportUnreachedWasmEnvSettlements`.
140
+ let __emnapiWasmEnvCleanupYielding = false
141
+ let __emnapiWasmEnvSettlementLossReported = false
128
142
  let __emnapiWasmEnvCleanupRan = false
129
143
  let __emnapiWasmEnvCleanupDrained = false
130
144
  let __emnapiWasmEnvCleanupDrainPromise
@@ -237,7 +251,24 @@ function __isPreparingWasmEnvCleanup() {
237
251
  }
238
252
 
239
253
  function __prepareWasmEnvCleanup() {
240
- if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
254
+ if (__emnapiWasmEnvCleanupPrepared) {
255
+ return
256
+ }
257
+ // A handshake parked between its two halves is one this frame can close, and
258
+ // must: every caller of this function is about to destroy the context, and
259
+ // the turns the poll is waiting for will not come — an 'exit' teardown is
260
+ // the last thing the process runs, and `Context.destroy()` takes the
261
+ // environment away. Closing it here runs `…_finish`, which is the call that joins, so
262
+ // this degrades to exactly the single call below. Leaving it open instead
263
+ // destroys the context with the barrier still raised, the runtime never
264
+ // joined and the workers never drained.
265
+ const finishParked = __finishParkedWasmEnvCleanup
266
+ if (finishParked !== undefined) {
267
+ finishParked()
268
+ __reportUnreachedWasmEnvSettlements()
269
+ return
270
+ }
271
+ if (__emnapiWasmEnvCleanupPreparing) {
241
272
  return
242
273
  }
243
274
  const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
@@ -252,10 +283,57 @@ function __prepareWasmEnvCleanup() {
252
283
  __emnapiWasmEnvCleanupPreparing = false
253
284
  }
254
285
  __emnapiWasmEnvCleanupRan = true
286
+ __reportUnreachedWasmEnvSettlements()
255
287
  }
256
288
  __emnapiWasmEnvCleanupPrepared = true
257
289
  }
258
290
 
291
+ /**
292
+ * Say so when the barrier leaves settlements queued and nothing is left that
293
+ * could deliver them.
294
+ *
295
+ * Only the disposal chain yields the event-loop turns @emnapi/core needs to
296
+ * dispatch its queue. Every other caller of the barrier destroys in the same
297
+ * turn — a raw `Context.destroy()`, the 'exit' teardown — and
298
+ * `Context.destroy()` runs the threadsafe function's cleanup hook, which drains
299
+ * that queue with a null env and discards it. The promises those settlements
300
+ * were for then hang forever, silently.
301
+ *
302
+ * Loud, once, and never throwing: this runs from inside `Context.destroy()`,
303
+ * emnapi's own beforeExit destroy included, where throwing would take the whole
304
+ * teardown down with it. Destroying anyway is still the right trade — the queue
305
+ * is already unreachable by then.
306
+ */
307
+ function __reportUnreachedWasmEnvSettlements() {
308
+ if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {
309
+ return
310
+ }
311
+ const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
312
+ if (typeof pending !== 'function') {
313
+ return
314
+ }
315
+ let queued
316
+ try {
317
+ queued = pending()
318
+ } catch {
319
+ return
320
+ }
321
+ if (!queued) {
322
+ return
323
+ }
324
+ __emnapiWasmEnvSettlementLossReported = true
325
+ try {
326
+ const consoleHost = globalThis.console
327
+ if (consoleHost && typeof consoleHost.error === 'function') {
328
+ consoleHost.error(
329
+ "napi-rs: the wasm environment is being destroyed with " +
330
+ queued +
331
+ " queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",
332
+ )
333
+ }
334
+ } catch {}
335
+ }
336
+
259
337
  // Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
260
338
  // on, so the drain turns below interleave with that dispatch instead of racing
261
339
  // ahead of it on a faster queue.
@@ -305,6 +383,291 @@ function __scheduleTimer(callback, delay) {
305
383
  }
306
384
  }
307
385
 
386
+ // A real, referenced timer rather than a zero-delay macrotask, for the same
387
+ // reason the async-work drain uses one: this polls the addon instead of
388
+ // interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
389
+ // the loop instead of yielding it.
390
+ const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
391
+ // Arrivals it takes before the poll paces on the host's timers alone. One
392
+ // proves nothing: a timer armed before the host's timers stopped still fires.
393
+ const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
394
+ // How long a parked turn's own timer must already have been due before a
395
+ // backup that runs calls it dropped. Slack, not a deadline: a timer is due
396
+ // against the event loop's clock, which is read once per iteration, while
397
+ // these are `Date.now()` readings taken part-way through one, so the two
398
+ // drift apart by however long the loop has been inside the current iteration.
399
+ const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
400
+ // How long a backup itself waits. What is left of it after the slack and one
401
+ // interval — 149 ms — has to cover the *two* poll turns that can separate a
402
+ // parked turn from the last backup armed while the host's timers still
403
+ // worked, so the ceiling on a single turn is half of it. See the invariant on
404
+ // `__armWasmRuntimePollStallBackup`.
405
+ const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
406
+
407
+ /**
408
+ * Pacing state for one runtime-work poll.
409
+ *
410
+ * Per poll, never per module: whether the host's timers arrive is not a
411
+ * property of the module. A host can lose its timers between two disposals,
412
+ * and in the deferred shape every instance shares this module — one healthy
413
+ * instance must not disarm the fallback for the next one.
414
+ */
415
+ function __createWasmRuntimePollPace() {
416
+ return {
417
+ // Timers armed by *this* poll that have actually arrived.
418
+ arrivals: 0,
419
+ // The turn waiting on a timer alone *right now* — undefined whenever no
420
+ // turn is parked — and when that turn's own timer came due.
421
+ settleTurn: undefined,
422
+ turnTimerDueAt: 0,
423
+ }
424
+ }
425
+
426
+ /**
427
+ * The backup that ends a turn whose timer is never going to arrive.
428
+ *
429
+ * Once the poll paces on the timer alone it has nothing left to fall back on
430
+ * if the host's timers stop mid-poll: the turn that armed the dead timer is
431
+ * the turn that parks, and a parked poll schedules nothing that could notice.
432
+ * So every turn arms one of these before it yields, and each one compares due
433
+ * times instead of measuring how long the parked turn has been waiting.
434
+ *
435
+ * Invariant: a parked turn is ended by the newest backup that was armed while
436
+ * the host's timers still worked, and a backup ends a turn only when that
437
+ * turn's own timer was already due a whole window before the backup itself.
438
+ * Neither half turns on how far apart the arms happen to fall — what bounds
439
+ * the rescue is how far back that newest live backup is:
440
+ *
441
+ * - *Ends it.* Hosts run timers in due order, so a backup that runs while a
442
+ * turn due a whole window earlier is still parked proves that turn's timer
443
+ * was dropped rather than merely late. That same comparison is what leaves a
444
+ * healthy host alone: there the turn's timer has already run and cleared
445
+ * `settleTurn` before any backup due after it can look.
446
+ * - *Two turns back, not one.* A turn that ended does not prove its own timer
447
+ * arrived: until `…_TRUSTED_ARRIVALS` is reached every turn arms both
448
+ * primitives and the macrotask wins, so such a turn can end with its own
449
+ * timer — and the backup armed one line before it — already dead. The
450
+ * arrival that then flips the poll onto the timer alone can itself be a
451
+ * timer armed before the host's timers died. So the turn that parks can sit
452
+ * two turns past the last live arm, and the newest live backup is due
453
+ * `…_BACKUP_MS` less *two* turn lengths after that turn's own timer.
454
+ * Arming on every turn is what holds it to two, rather than however far back
455
+ * a throttle last let one through.
456
+ * - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
457
+ * inside `…_BACKUP_MS` less the slack and one interval: 149 ms, so 74 ms
458
+ * per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
459
+ * Past that the turn stays parked and the disposal promise never settles.
460
+ * The bound is deliberate: reaching it takes a host that drops timers
461
+ * mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
462
+ * Node nor WebContainer — the hosts that run the threaded artifact — does
463
+ * the second.
464
+ *
465
+ * The poll then goes back to arming both primitives until two fresh arrivals
466
+ * prove the timers again. A host that stops running the timers it has
467
+ * *already* accepted leaves nothing to fire, and the disposal promise stays
468
+ * pending rather than wedging the thread — the same outcome as a blocking
469
+ * closure that never returns. Unreferenced wherever the host allows it: the
470
+ * poll's own turn timers are what keep the loop alive, never these.
471
+ */
472
+ function __armWasmRuntimePollStallBackup(pace) {
473
+ const setTimer = globalThis.setTimeout
474
+ if (typeof setTimer !== 'function') {
475
+ // Nothing to back up: `__scheduleTimer` is on the macrotask channel
476
+ // already, and that one cannot park.
477
+ return
478
+ }
479
+ // Read before arming, so this never claims to be due earlier than the timer
480
+ // actually is: a backup ends a turn only when it is provably due after it.
481
+ const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
482
+ let handle
483
+ try {
484
+ handle = setTimer(() => {
485
+ const settleTurn = pace.settleTurn
486
+ if (
487
+ !settleTurn ||
488
+ pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
489
+ ) {
490
+ // No turn is parked, or the parked one's timer came due too close to
491
+ // this backup to call it dropped — it may still arrive, and the turn
492
+ // that armed it armed a backup due a whole window after *that*.
493
+ return
494
+ }
495
+ pace.arrivals = 0
496
+ pace.settleTurn = undefined
497
+ settleTurn()
498
+ }, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
499
+ } catch {
500
+ return
501
+ }
502
+ if (handle && typeof handle.unref === 'function') {
503
+ try {
504
+ handle.unref()
505
+ } catch {}
506
+ }
507
+ }
508
+
509
+ /**
510
+ * One turn of the runtime-work poll.
511
+ *
512
+ * `__scheduleTimer` falls back to the macrotask scheduler when `setTimeout` is
513
+ * missing or throws, but not when it is present, returns a handle and never
514
+ * fires — fake timers in a test suite that disposes from an `afterEach`, or a
515
+ * host whose timers belong to an IO context that is already gone. That host
516
+ * would park this poll forever, and the poll is unbounded, so nothing would
517
+ * ever call `…_finish`.
518
+ *
519
+ * Arm both primitives until timers armed by this poll have arrived twice, and
520
+ * let whichever lands first end the turn; the loser resolves nothing. A host
521
+ * with working timers therefore pays the double arming for the first turn or
522
+ * two — the macrotask wins the race, but the timers behind it still arrive and
523
+ * are counted — and paces on the timer alone from then on, instead of spinning
524
+ * the loop on a zero-delay queue. A host whose timers never arrive keeps both,
525
+ * and the macrotask is what keeps the poll moving. A host whose timers stop
526
+ * after proving themselves is caught by `__armWasmRuntimePollStallBackup`,
527
+ * which ends the parked turn and puts this poll back on both.
528
+ */
529
+ function __yieldWasmRuntimePollTurn(pace) {
530
+ // Armed before the turn yields, and by every turn: what rescues a parked
531
+ // turn has to have been armed while the host's timers still worked, and the
532
+ // turn that parks is the one whose own timer is already dead.
533
+ __armWasmRuntimePollStallBackup(pace)
534
+ return new Promise((resolve) => {
535
+ let settled = false
536
+ const settle = () => {
537
+ if (settled) {
538
+ return
539
+ }
540
+ settled = true
541
+ if (pace.settleTurn === settle) {
542
+ // Nothing is parked any more: a backup running later must not read a
543
+ // due time this turn has already answered.
544
+ pace.settleTurn = undefined
545
+ }
546
+ resolve()
547
+ }
548
+ __scheduleTimer(() => {
549
+ pace.arrivals++
550
+ settle()
551
+ }, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
552
+ // Read next to the arming it describes; see
553
+ // `__armWasmRuntimePollStallBackup` for what the two due times mean.
554
+ const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
555
+ if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
556
+ __scheduleMacrotask(settle)
557
+ return
558
+ }
559
+ // Paced by the timer alone from here; the backup is what ends this turn if
560
+ // the timer never arrives.
561
+ pace.settleTurn = settle
562
+ pace.turnTimerDueAt = turnTimerDueAt
563
+ })
564
+ }
565
+
566
+ /**
567
+ * The barrier for callers that can yield: `__prepareWasmEnvCleanup` with real
568
+ * event-loop turns in the middle.
569
+ *
570
+ * `napi_prepare_wasm_env_cleanup` waits — it returns only once the addon's
571
+ * async runtime has quiesced, and on `wasm32-wasip1-threads` the thread it
572
+ * waits on is this one, the only thread that can give a running blocking
573
+ * closure the JavaScript turn *it* is waiting for. A single call there can wait
574
+ * for work that can never finish. The addon's two-phase form splits that:
575
+ * `…_begin` stops the runtime without joining and reports whether anything is
576
+ * still live, `napi_wasm_runtime_work_pending` answers that question again
577
+ * without blocking, and `…_finish` joins. The turns yielded in between are the
578
+ * entire point.
579
+ *
580
+ * The poll has no deadline, for the same reason the async-work drain below has
581
+ * none: giving up means calling `…_finish`, which joins on this thread, and the
582
+ * work it would join is the work that is waiting for a turn from this thread —
583
+ * so a bound does not end the wait, it only moves it somewhere the JavaScript
584
+ * thread can no longer be reached. A blocking closure that never returns keeps
585
+ * the disposal promise pending instead, exactly as a task whose `execute` never
586
+ * returns already keeps an *undisposed* process alive. The host contract is in
587
+ * `crates/async-runtime/README.md`: a blocking closure must never wait on a
588
+ * JavaScript turn. The process-exit path still blocks in `…_finish`, because it
589
+ * has no turns left to give (see `__prepareWasmEnvCleanup`).
590
+ *
591
+ * Feature-detected like every other export in this teardown, so an addon built
592
+ * against a napi crate that predates the split keeps the single blocking call.
593
+ * Returns nothing whenever the handshake finished without yielding, which keeps
594
+ * an idle disposal synchronous.
595
+ */
596
+ function __prepareWasmEnvCleanupWithTurns() {
597
+ if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
598
+ return
599
+ }
600
+ const exports = __napiInstance?.exports
601
+ const begin = exports?.napi_prepare_wasm_env_cleanup_begin
602
+ const finish = exports?.napi_prepare_wasm_env_cleanup_finish
603
+ if (typeof begin !== 'function' || typeof finish !== 'function') {
604
+ // No split to use. The settlement drain still follows this, so the queue
605
+ // the single call leaves behind is expected rather than lost.
606
+ __emnapiWasmEnvCleanupYielding = true
607
+ try {
608
+ __prepareWasmEnvCleanup()
609
+ } finally {
610
+ __emnapiWasmEnvCleanupYielding = false
611
+ }
612
+ return
613
+ }
614
+ const workPending = exports?.napi_wasm_runtime_work_pending
615
+ // The in-flight flag stays raised across the turns below, so a `destroy()`
616
+ // from one of the JavaScript handlers they run is the same no-op it is inside
617
+ // the single call: the barrier is up and the runtime is mid-teardown, and
618
+ // destroying between the halves would strand exactly what this delivers.
619
+ __emnapiWasmEnvCleanupPreparing = true
620
+ let live
621
+ try {
622
+ live = begin()
623
+ } catch (error) {
624
+ __emnapiWasmEnvCleanupPreparing = false
625
+ throw error
626
+ }
627
+ __emnapiWasmEnvCleanupRan = true
628
+ const finishCleanup = () => {
629
+ if (__emnapiWasmEnvCleanupPrepared) {
630
+ // Already closed by a caller that could not yield — the 'exit' teardown
631
+ // reached `__prepareWasmEnvCleanup` while this poll was parked. `…_finish`
632
+ // is idempotent, but the flags it lowers are not: running it again here
633
+ // would clear a `preparing` some later barrier had raised.
634
+ return
635
+ }
636
+ __finishParkedWasmEnvCleanup = undefined
637
+ try {
638
+ finish()
639
+ } finally {
640
+ __emnapiWasmEnvCleanupPreparing = false
641
+ }
642
+ __emnapiWasmEnvCleanupPrepared = true
643
+ }
644
+ if (!live || typeof workPending !== 'function') {
645
+ finishCleanup()
646
+ return
647
+ }
648
+ // Publish the closer before yielding: from here until `finishCleanup` runs,
649
+ // a caller that cannot yield is entitled to end this handshake itself.
650
+ __finishParkedWasmEnvCleanup = finishCleanup
651
+ return (async () => {
652
+ // Unbounded, exactly like the async-work drain below. The wait ends when
653
+ // the addon reports its runtime work finished; the turns spent here are
654
+ // what let that happen at all.
655
+ const pace = __createWasmRuntimePollPace()
656
+ for (;;) {
657
+ await __yieldWasmRuntimePollTurn(pace)
658
+ try {
659
+ if (!workPending()) {
660
+ return
661
+ }
662
+ } catch {
663
+ // A trap is the only way this fails, and a trapped instance has no
664
+ // reachable work left. Stop polling and finish.
665
+ return
666
+ }
667
+ }
668
+ })().then(finishCleanup, finishCleanup)
669
+ }
670
+
308
671
  // Turns to wait for while the addon still reports queued settlements. Reaching
309
672
  // zero is the only success. A counter still nonzero at this bound rejects the
310
673
  // disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
@@ -441,6 +804,19 @@ function __destroyEmnapiContext() {
441
804
  }
442
805
 
443
806
  __prepareWasmEnvCleanup()
807
+ if (__isPreparingWasmEnvCleanup()) {
808
+ // Reached from inside the synchronous barrier — a promise hook one of the
809
+ // settlements above ran, which is the reentrancy the destroy wrapper
810
+ // exists for. `Context.destroy()` below would hit that wrapper's in-flight
811
+ // no-op and answer `undefined`, and recording that as a completed destroy
812
+ // is what makes the frame that *did* start the barrier skip the real one
813
+ // afterwards, leaving the context retained with its cleanup hooks unrun.
814
+ // Refuse instead: nothing is flagged, and that frame destroys for real the
815
+ // moment it returns. The deferred loader carries the same backstop. A
816
+ // parked handshake cannot get here — `__prepareWasmEnvCleanup` closes one
817
+ // rather than skipping it.
818
+ return
819
+ }
444
820
  const result = __emnapiContext.destroy()
445
821
  if (!__isThenable(result)) {
446
822
  __emnapiContextDestroyed = true
@@ -714,11 +1090,7 @@ function __continueWasiDisposal() {
714
1090
  return __finishWasiDisposal()
715
1091
  }
716
1092
 
717
- function __cleanUpWasmEnvForWasiDisposal() {
718
- // Run the pre-teardown barrier, then let the settlements it queued actually
719
- // reach JavaScript, and only then destroy the environment. Doing these two
720
- // back to back is what strands them.
721
- __prepareWasmEnvCleanup()
1093
+ function __drainWasmEnvForWasiDisposal() {
722
1094
  const drainResult = __drainWasmEnvCleanup()
723
1095
  if (__isThenable(drainResult)) {
724
1096
  return Promise.resolve(drainResult).then(__continueWasiDisposal)
@@ -726,6 +1098,18 @@ function __cleanUpWasmEnvForWasiDisposal() {
726
1098
  return __continueWasiDisposal()
727
1099
  }
728
1100
 
1101
+ function __cleanUpWasmEnvForWasiDisposal() {
1102
+ // Run the pre-teardown barrier — yielding the turns its two-phase form asks
1103
+ // for, when the addon has one — then let the settlements it queued actually
1104
+ // reach JavaScript, and only then destroy the environment. Doing any two of
1105
+ // these back to back is what strands them.
1106
+ const prepareResult = __prepareWasmEnvCleanupWithTurns()
1107
+ if (__isThenable(prepareResult)) {
1108
+ return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)
1109
+ }
1110
+ return __drainWasmEnvForWasiDisposal()
1111
+ }
1112
+
729
1113
  function __startWasiDisposal() {
730
1114
  // Outstanding `napi_async_work` goes first, while the environment is still
731
1115
  // completely live: the completion callbacks run addon code, and everything
@@ -886,14 +1270,36 @@ function __rollbackWasiInitialization() {
886
1270
  // be reached without the async-work drain below running first.
887
1271
  function __rollbackWasmEnvForWasiInitialization() {
888
1272
  const cleanupErrors = []
1273
+ let prepareResult
1274
+ try {
1275
+ prepareResult = __prepareWasmEnvCleanupWithTurns()
1276
+ } catch (cleanupError) {
1277
+ cleanupErrors.push(cleanupError)
1278
+ return __retainFailedWasiRollback(cleanupErrors)
1279
+ }
1280
+ if (__isThenable(prepareResult)) {
1281
+ return Promise.resolve(prepareResult).then(
1282
+ () => __drainWasmEnvForWasiRollback(cleanupErrors),
1283
+ (cleanupError) => {
1284
+ cleanupErrors.push(cleanupError)
1285
+ return __retainFailedWasiRollback(cleanupErrors)
1286
+ },
1287
+ )
1288
+ }
1289
+ return __drainWasmEnvForWasiRollback(cleanupErrors)
1290
+ }
1291
+
1292
+ // The settlement drain of the rollback above, reached either straight away or
1293
+ // after the barrier's two-phase form has yielded its turns. A barrier that
1294
+ // did not finish never gets here: it retains instead, exactly as a drain that
1295
+ // did not finish does.
1296
+ function __drainWasmEnvForWasiRollback(cleanupErrors) {
889
1297
  let drainResult
890
- let settlementsUnreached = false
891
1298
  try {
892
- __prepareWasmEnvCleanup()
893
1299
  drainResult = __drainWasmEnvCleanup()
894
1300
  } catch (cleanupError) {
895
1301
  cleanupErrors.push(cleanupError)
896
- settlementsUnreached = true
1302
+ return __retainFailedWasiRollback(cleanupErrors)
897
1303
  }
898
1304
  if (__isThenable(drainResult)) {
899
1305
  return Promise.resolve(drainResult).then(
@@ -904,9 +1310,6 @@ function __rollbackWasiInitialization() {
904
1310
  },
905
1311
  )
906
1312
  }
907
- if (settlementsUnreached) {
908
- return __retainFailedWasiRollback(cleanupErrors)
909
- }
910
1313
  return __destroyContextForWasiRollback(cleanupErrors)
911
1314
  }
912
1315
 
package/minify.wasi.cjs CHANGED
@@ -233,6 +233,20 @@ let __emnapiContextDestroyed = false
233
233
  let __emnapiContextDestroyPromise
234
234
  let __emnapiWasmEnvCleanupPrepared = false
235
235
  let __emnapiWasmEnvCleanupPreparing = false
236
+ // The closer for a barrier that is parked between `…_begin` and `…_finish`,
237
+ // set only while that window is open. `__emnapiWasmEnvCleanupPreparing` cannot
238
+ // tell those two apart on its own: it is raised both for a purely synchronous
239
+ // frame — which must not be re-entered, and which nothing outside it can
240
+ // finish — and across this window, which spans real event-loop turns, so a
241
+ // caller that cannot yield can land in the middle of one. That caller can close
242
+ // this window, because `…_finish` is idempotent and joins, which is exactly
243
+ // what the single call does. See `__prepareWasmEnvCleanup`.
244
+ let __finishParkedWasmEnvCleanup
245
+ // Raised while a caller that can still yield is driving the barrier, so the
246
+ // queue it leaves behind is expected rather than lost. See
247
+ // `__reportUnreachedWasmEnvSettlements`.
248
+ let __emnapiWasmEnvCleanupYielding = false
249
+ let __emnapiWasmEnvSettlementLossReported = false
236
250
  let __emnapiWasmEnvCleanupRan = false
237
251
  let __emnapiWasmEnvCleanupDrained = false
238
252
  let __emnapiWasmEnvCleanupDrainPromise
@@ -345,7 +359,24 @@ function __isPreparingWasmEnvCleanup() {
345
359
  }
346
360
 
347
361
  function __prepareWasmEnvCleanup() {
348
- if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
362
+ if (__emnapiWasmEnvCleanupPrepared) {
363
+ return
364
+ }
365
+ // A handshake parked between its two halves is one this frame can close, and
366
+ // must: every caller of this function is about to destroy the context, and
367
+ // the turns the poll is waiting for will not come — an 'exit' teardown is
368
+ // the last thing the process runs, and `Context.destroy()` takes the
369
+ // environment away. Closing it here runs `…_finish`, which is the call that joins, so
370
+ // this degrades to exactly the single call below. Leaving it open instead
371
+ // destroys the context with the barrier still raised, the runtime never
372
+ // joined and the workers never drained.
373
+ const finishParked = __finishParkedWasmEnvCleanup
374
+ if (finishParked !== undefined) {
375
+ finishParked()
376
+ __reportUnreachedWasmEnvSettlements()
377
+ return
378
+ }
379
+ if (__emnapiWasmEnvCleanupPreparing) {
349
380
  return
350
381
  }
351
382
  const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
@@ -360,10 +391,57 @@ function __prepareWasmEnvCleanup() {
360
391
  __emnapiWasmEnvCleanupPreparing = false
361
392
  }
362
393
  __emnapiWasmEnvCleanupRan = true
394
+ __reportUnreachedWasmEnvSettlements()
363
395
  }
364
396
  __emnapiWasmEnvCleanupPrepared = true
365
397
  }
366
398
 
399
+ /**
400
+ * Say so when the barrier leaves settlements queued and nothing is left that
401
+ * could deliver them.
402
+ *
403
+ * Only the disposal chain yields the event-loop turns @emnapi/core needs to
404
+ * dispatch its queue. Every other caller of the barrier destroys in the same
405
+ * turn — a raw `Context.destroy()`, the 'exit' teardown — and
406
+ * `Context.destroy()` runs the threadsafe function's cleanup hook, which drains
407
+ * that queue with a null env and discards it. The promises those settlements
408
+ * were for then hang forever, silently.
409
+ *
410
+ * Loud, once, and never throwing: this runs from inside `Context.destroy()`,
411
+ * emnapi's own beforeExit destroy included, where throwing would take the whole
412
+ * teardown down with it. Destroying anyway is still the right trade — the queue
413
+ * is already unreachable by then.
414
+ */
415
+ function __reportUnreachedWasmEnvSettlements() {
416
+ if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {
417
+ return
418
+ }
419
+ const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
420
+ if (typeof pending !== 'function') {
421
+ return
422
+ }
423
+ let queued
424
+ try {
425
+ queued = pending()
426
+ } catch {
427
+ return
428
+ }
429
+ if (!queued) {
430
+ return
431
+ }
432
+ __emnapiWasmEnvSettlementLossReported = true
433
+ try {
434
+ const consoleHost = globalThis.console
435
+ if (consoleHost && typeof consoleHost.error === 'function') {
436
+ consoleHost.error(
437
+ "napi-rs: the wasm environment is being destroyed with " +
438
+ queued +
439
+ " queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",
440
+ )
441
+ }
442
+ } catch {}
443
+ }
444
+
367
445
  // Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
368
446
  // on, so the drain turns below interleave with that dispatch instead of racing
369
447
  // ahead of it on a faster queue.
@@ -413,6 +491,291 @@ function __scheduleTimer(callback, delay) {
413
491
  }
414
492
  }
415
493
 
494
+ // A real, referenced timer rather than a zero-delay macrotask, for the same
495
+ // reason the async-work drain uses one: this polls the addon instead of
496
+ // interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
497
+ // the loop instead of yielding it.
498
+ const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
499
+ // Arrivals it takes before the poll paces on the host's timers alone. One
500
+ // proves nothing: a timer armed before the host's timers stopped still fires.
501
+ const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
502
+ // How long a parked turn's own timer must already have been due before a
503
+ // backup that runs calls it dropped. Slack, not a deadline: a timer is due
504
+ // against the event loop's clock, which is read once per iteration, while
505
+ // these are `Date.now()` readings taken part-way through one, so the two
506
+ // drift apart by however long the loop has been inside the current iteration.
507
+ const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
508
+ // How long a backup itself waits. What is left of it after the slack and one
509
+ // interval — 149 ms — has to cover the *two* poll turns that can separate a
510
+ // parked turn from the last backup armed while the host's timers still
511
+ // worked, so the ceiling on a single turn is half of it. See the invariant on
512
+ // `__armWasmRuntimePollStallBackup`.
513
+ const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
514
+
515
+ /**
516
+ * Pacing state for one runtime-work poll.
517
+ *
518
+ * Per poll, never per module: whether the host's timers arrive is not a
519
+ * property of the module. A host can lose its timers between two disposals,
520
+ * and in the deferred shape every instance shares this module — one healthy
521
+ * instance must not disarm the fallback for the next one.
522
+ */
523
+ function __createWasmRuntimePollPace() {
524
+ return {
525
+ // Timers armed by *this* poll that have actually arrived.
526
+ arrivals: 0,
527
+ // The turn waiting on a timer alone *right now* — undefined whenever no
528
+ // turn is parked — and when that turn's own timer came due.
529
+ settleTurn: undefined,
530
+ turnTimerDueAt: 0,
531
+ }
532
+ }
533
+
534
+ /**
535
+ * The backup that ends a turn whose timer is never going to arrive.
536
+ *
537
+ * Once the poll paces on the timer alone it has nothing left to fall back on
538
+ * if the host's timers stop mid-poll: the turn that armed the dead timer is
539
+ * the turn that parks, and a parked poll schedules nothing that could notice.
540
+ * So every turn arms one of these before it yields, and each one compares due
541
+ * times instead of measuring how long the parked turn has been waiting.
542
+ *
543
+ * Invariant: a parked turn is ended by the newest backup that was armed while
544
+ * the host's timers still worked, and a backup ends a turn only when that
545
+ * turn's own timer was already due a whole window before the backup itself.
546
+ * Neither half turns on how far apart the arms happen to fall — what bounds
547
+ * the rescue is how far back that newest live backup is:
548
+ *
549
+ * - *Ends it.* Hosts run timers in due order, so a backup that runs while a
550
+ * turn due a whole window earlier is still parked proves that turn's timer
551
+ * was dropped rather than merely late. That same comparison is what leaves a
552
+ * healthy host alone: there the turn's timer has already run and cleared
553
+ * `settleTurn` before any backup due after it can look.
554
+ * - *Two turns back, not one.* A turn that ended does not prove its own timer
555
+ * arrived: until `…_TRUSTED_ARRIVALS` is reached every turn arms both
556
+ * primitives and the macrotask wins, so such a turn can end with its own
557
+ * timer — and the backup armed one line before it — already dead. The
558
+ * arrival that then flips the poll onto the timer alone can itself be a
559
+ * timer armed before the host's timers died. So the turn that parks can sit
560
+ * two turns past the last live arm, and the newest live backup is due
561
+ * `…_BACKUP_MS` less *two* turn lengths after that turn's own timer.
562
+ * Arming on every turn is what holds it to two, rather than however far back
563
+ * a throttle last let one through.
564
+ * - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
565
+ * inside `…_BACKUP_MS` less the slack and one interval: 149 ms, so 74 ms
566
+ * per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
567
+ * Past that the turn stays parked and the disposal promise never settles.
568
+ * The bound is deliberate: reaching it takes a host that drops timers
569
+ * mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
570
+ * Node nor WebContainer — the hosts that run the threaded artifact — does
571
+ * the second.
572
+ *
573
+ * The poll then goes back to arming both primitives until two fresh arrivals
574
+ * prove the timers again. A host that stops running the timers it has
575
+ * *already* accepted leaves nothing to fire, and the disposal promise stays
576
+ * pending rather than wedging the thread — the same outcome as a blocking
577
+ * closure that never returns. Unreferenced wherever the host allows it: the
578
+ * poll's own turn timers are what keep the loop alive, never these.
579
+ */
580
+ function __armWasmRuntimePollStallBackup(pace) {
581
+ const setTimer = globalThis.setTimeout
582
+ if (typeof setTimer !== 'function') {
583
+ // Nothing to back up: `__scheduleTimer` is on the macrotask channel
584
+ // already, and that one cannot park.
585
+ return
586
+ }
587
+ // Read before arming, so this never claims to be due earlier than the timer
588
+ // actually is: a backup ends a turn only when it is provably due after it.
589
+ const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
590
+ let handle
591
+ try {
592
+ handle = setTimer(() => {
593
+ const settleTurn = pace.settleTurn
594
+ if (
595
+ !settleTurn ||
596
+ pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
597
+ ) {
598
+ // No turn is parked, or the parked one's timer came due too close to
599
+ // this backup to call it dropped — it may still arrive, and the turn
600
+ // that armed it armed a backup due a whole window after *that*.
601
+ return
602
+ }
603
+ pace.arrivals = 0
604
+ pace.settleTurn = undefined
605
+ settleTurn()
606
+ }, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
607
+ } catch {
608
+ return
609
+ }
610
+ if (handle && typeof handle.unref === 'function') {
611
+ try {
612
+ handle.unref()
613
+ } catch {}
614
+ }
615
+ }
616
+
617
+ /**
618
+ * One turn of the runtime-work poll.
619
+ *
620
+ * `__scheduleTimer` falls back to the macrotask scheduler when `setTimeout` is
621
+ * missing or throws, but not when it is present, returns a handle and never
622
+ * fires — fake timers in a test suite that disposes from an `afterEach`, or a
623
+ * host whose timers belong to an IO context that is already gone. That host
624
+ * would park this poll forever, and the poll is unbounded, so nothing would
625
+ * ever call `…_finish`.
626
+ *
627
+ * Arm both primitives until timers armed by this poll have arrived twice, and
628
+ * let whichever lands first end the turn; the loser resolves nothing. A host
629
+ * with working timers therefore pays the double arming for the first turn or
630
+ * two — the macrotask wins the race, but the timers behind it still arrive and
631
+ * are counted — and paces on the timer alone from then on, instead of spinning
632
+ * the loop on a zero-delay queue. A host whose timers never arrive keeps both,
633
+ * and the macrotask is what keeps the poll moving. A host whose timers stop
634
+ * after proving themselves is caught by `__armWasmRuntimePollStallBackup`,
635
+ * which ends the parked turn and puts this poll back on both.
636
+ */
637
+ function __yieldWasmRuntimePollTurn(pace) {
638
+ // Armed before the turn yields, and by every turn: what rescues a parked
639
+ // turn has to have been armed while the host's timers still worked, and the
640
+ // turn that parks is the one whose own timer is already dead.
641
+ __armWasmRuntimePollStallBackup(pace)
642
+ return new Promise((resolve) => {
643
+ let settled = false
644
+ const settle = () => {
645
+ if (settled) {
646
+ return
647
+ }
648
+ settled = true
649
+ if (pace.settleTurn === settle) {
650
+ // Nothing is parked any more: a backup running later must not read a
651
+ // due time this turn has already answered.
652
+ pace.settleTurn = undefined
653
+ }
654
+ resolve()
655
+ }
656
+ __scheduleTimer(() => {
657
+ pace.arrivals++
658
+ settle()
659
+ }, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
660
+ // Read next to the arming it describes; see
661
+ // `__armWasmRuntimePollStallBackup` for what the two due times mean.
662
+ const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
663
+ if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
664
+ __scheduleMacrotask(settle)
665
+ return
666
+ }
667
+ // Paced by the timer alone from here; the backup is what ends this turn if
668
+ // the timer never arrives.
669
+ pace.settleTurn = settle
670
+ pace.turnTimerDueAt = turnTimerDueAt
671
+ })
672
+ }
673
+
674
+ /**
675
+ * The barrier for callers that can yield: `__prepareWasmEnvCleanup` with real
676
+ * event-loop turns in the middle.
677
+ *
678
+ * `napi_prepare_wasm_env_cleanup` waits — it returns only once the addon's
679
+ * async runtime has quiesced, and on `wasm32-wasip1-threads` the thread it
680
+ * waits on is this one, the only thread that can give a running blocking
681
+ * closure the JavaScript turn *it* is waiting for. A single call there can wait
682
+ * for work that can never finish. The addon's two-phase form splits that:
683
+ * `…_begin` stops the runtime without joining and reports whether anything is
684
+ * still live, `napi_wasm_runtime_work_pending` answers that question again
685
+ * without blocking, and `…_finish` joins. The turns yielded in between are the
686
+ * entire point.
687
+ *
688
+ * The poll has no deadline, for the same reason the async-work drain below has
689
+ * none: giving up means calling `…_finish`, which joins on this thread, and the
690
+ * work it would join is the work that is waiting for a turn from this thread —
691
+ * so a bound does not end the wait, it only moves it somewhere the JavaScript
692
+ * thread can no longer be reached. A blocking closure that never returns keeps
693
+ * the disposal promise pending instead, exactly as a task whose `execute` never
694
+ * returns already keeps an *undisposed* process alive. The host contract is in
695
+ * `crates/async-runtime/README.md`: a blocking closure must never wait on a
696
+ * JavaScript turn. The process-exit path still blocks in `…_finish`, because it
697
+ * has no turns left to give (see `__prepareWasmEnvCleanup`).
698
+ *
699
+ * Feature-detected like every other export in this teardown, so an addon built
700
+ * against a napi crate that predates the split keeps the single blocking call.
701
+ * Returns nothing whenever the handshake finished without yielding, which keeps
702
+ * an idle disposal synchronous.
703
+ */
704
+ function __prepareWasmEnvCleanupWithTurns() {
705
+ if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
706
+ return
707
+ }
708
+ const exports = __napiInstance?.exports
709
+ const begin = exports?.napi_prepare_wasm_env_cleanup_begin
710
+ const finish = exports?.napi_prepare_wasm_env_cleanup_finish
711
+ if (typeof begin !== 'function' || typeof finish !== 'function') {
712
+ // No split to use. The settlement drain still follows this, so the queue
713
+ // the single call leaves behind is expected rather than lost.
714
+ __emnapiWasmEnvCleanupYielding = true
715
+ try {
716
+ __prepareWasmEnvCleanup()
717
+ } finally {
718
+ __emnapiWasmEnvCleanupYielding = false
719
+ }
720
+ return
721
+ }
722
+ const workPending = exports?.napi_wasm_runtime_work_pending
723
+ // The in-flight flag stays raised across the turns below, so a `destroy()`
724
+ // from one of the JavaScript handlers they run is the same no-op it is inside
725
+ // the single call: the barrier is up and the runtime is mid-teardown, and
726
+ // destroying between the halves would strand exactly what this delivers.
727
+ __emnapiWasmEnvCleanupPreparing = true
728
+ let live
729
+ try {
730
+ live = begin()
731
+ } catch (error) {
732
+ __emnapiWasmEnvCleanupPreparing = false
733
+ throw error
734
+ }
735
+ __emnapiWasmEnvCleanupRan = true
736
+ const finishCleanup = () => {
737
+ if (__emnapiWasmEnvCleanupPrepared) {
738
+ // Already closed by a caller that could not yield — the 'exit' teardown
739
+ // reached `__prepareWasmEnvCleanup` while this poll was parked. `…_finish`
740
+ // is idempotent, but the flags it lowers are not: running it again here
741
+ // would clear a `preparing` some later barrier had raised.
742
+ return
743
+ }
744
+ __finishParkedWasmEnvCleanup = undefined
745
+ try {
746
+ finish()
747
+ } finally {
748
+ __emnapiWasmEnvCleanupPreparing = false
749
+ }
750
+ __emnapiWasmEnvCleanupPrepared = true
751
+ }
752
+ if (!live || typeof workPending !== 'function') {
753
+ finishCleanup()
754
+ return
755
+ }
756
+ // Publish the closer before yielding: from here until `finishCleanup` runs,
757
+ // a caller that cannot yield is entitled to end this handshake itself.
758
+ __finishParkedWasmEnvCleanup = finishCleanup
759
+ return (async () => {
760
+ // Unbounded, exactly like the async-work drain below. The wait ends when
761
+ // the addon reports its runtime work finished; the turns spent here are
762
+ // what let that happen at all.
763
+ const pace = __createWasmRuntimePollPace()
764
+ for (;;) {
765
+ await __yieldWasmRuntimePollTurn(pace)
766
+ try {
767
+ if (!workPending()) {
768
+ return
769
+ }
770
+ } catch {
771
+ // A trap is the only way this fails, and a trapped instance has no
772
+ // reachable work left. Stop polling and finish.
773
+ return
774
+ }
775
+ }
776
+ })().then(finishCleanup, finishCleanup)
777
+ }
778
+
416
779
  // Turns to wait for while the addon still reports queued settlements. Reaching
417
780
  // zero is the only success. A counter still nonzero at this bound rejects the
418
781
  // disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
@@ -549,6 +912,19 @@ function __destroyEmnapiContext() {
549
912
  }
550
913
 
551
914
  __prepareWasmEnvCleanup()
915
+ if (__isPreparingWasmEnvCleanup()) {
916
+ // Reached from inside the synchronous barrier — a promise hook one of the
917
+ // settlements above ran, which is the reentrancy the destroy wrapper
918
+ // exists for. `Context.destroy()` below would hit that wrapper's in-flight
919
+ // no-op and answer `undefined`, and recording that as a completed destroy
920
+ // is what makes the frame that *did* start the barrier skip the real one
921
+ // afterwards, leaving the context retained with its cleanup hooks unrun.
922
+ // Refuse instead: nothing is flagged, and that frame destroys for real the
923
+ // moment it returns. The deferred loader carries the same backstop. A
924
+ // parked handshake cannot get here — `__prepareWasmEnvCleanup` closes one
925
+ // rather than skipping it.
926
+ return
927
+ }
552
928
  const result = __emnapiContext.destroy()
553
929
  if (!__isThenable(result)) {
554
930
  __emnapiContextDestroyed = true
@@ -822,11 +1198,7 @@ function __continueWasiDisposal() {
822
1198
  return __finishWasiDisposal()
823
1199
  }
824
1200
 
825
- function __cleanUpWasmEnvForWasiDisposal() {
826
- // Run the pre-teardown barrier, then let the settlements it queued actually
827
- // reach JavaScript, and only then destroy the environment. Doing these two
828
- // back to back is what strands them.
829
- __prepareWasmEnvCleanup()
1201
+ function __drainWasmEnvForWasiDisposal() {
830
1202
  const drainResult = __drainWasmEnvCleanup()
831
1203
  if (__isThenable(drainResult)) {
832
1204
  return Promise.resolve(drainResult).then(__continueWasiDisposal)
@@ -834,6 +1206,18 @@ function __cleanUpWasmEnvForWasiDisposal() {
834
1206
  return __continueWasiDisposal()
835
1207
  }
836
1208
 
1209
+ function __cleanUpWasmEnvForWasiDisposal() {
1210
+ // Run the pre-teardown barrier — yielding the turns its two-phase form asks
1211
+ // for, when the addon has one — then let the settlements it queued actually
1212
+ // reach JavaScript, and only then destroy the environment. Doing any two of
1213
+ // these back to back is what strands them.
1214
+ const prepareResult = __prepareWasmEnvCleanupWithTurns()
1215
+ if (__isThenable(prepareResult)) {
1216
+ return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)
1217
+ }
1218
+ return __drainWasmEnvForWasiDisposal()
1219
+ }
1220
+
837
1221
  function __startWasiDisposal() {
838
1222
  // Outstanding `napi_async_work` goes first, while the environment is still
839
1223
  // completely live: the completion callbacks run addon code, and everything
@@ -994,14 +1378,36 @@ function __rollbackWasiInitialization() {
994
1378
  // be reached without the async-work drain below running first.
995
1379
  function __rollbackWasmEnvForWasiInitialization() {
996
1380
  const cleanupErrors = []
1381
+ let prepareResult
1382
+ try {
1383
+ prepareResult = __prepareWasmEnvCleanupWithTurns()
1384
+ } catch (cleanupError) {
1385
+ cleanupErrors.push(cleanupError)
1386
+ return __retainFailedWasiRollback(cleanupErrors)
1387
+ }
1388
+ if (__isThenable(prepareResult)) {
1389
+ return Promise.resolve(prepareResult).then(
1390
+ () => __drainWasmEnvForWasiRollback(cleanupErrors),
1391
+ (cleanupError) => {
1392
+ cleanupErrors.push(cleanupError)
1393
+ return __retainFailedWasiRollback(cleanupErrors)
1394
+ },
1395
+ )
1396
+ }
1397
+ return __drainWasmEnvForWasiRollback(cleanupErrors)
1398
+ }
1399
+
1400
+ // The settlement drain of the rollback above, reached either straight away or
1401
+ // after the barrier's two-phase form has yielded its turns. A barrier that
1402
+ // did not finish never gets here: it retains instead, exactly as a drain that
1403
+ // did not finish does.
1404
+ function __drainWasmEnvForWasiRollback(cleanupErrors) {
997
1405
  let drainResult
998
- let settlementsUnreached = false
999
1406
  try {
1000
- __prepareWasmEnvCleanup()
1001
1407
  drainResult = __drainWasmEnvCleanup()
1002
1408
  } catch (cleanupError) {
1003
1409
  cleanupErrors.push(cleanupError)
1004
- settlementsUnreached = true
1410
+ return __retainFailedWasiRollback(cleanupErrors)
1005
1411
  }
1006
1412
  if (__isThenable(drainResult)) {
1007
1413
  return Promise.resolve(drainResult).then(
@@ -1012,9 +1418,6 @@ function __rollbackWasiInitialization() {
1012
1418
  },
1013
1419
  )
1014
1420
  }
1015
- if (settlementsUnreached) {
1016
- return __retainFailedWasiRollback(cleanupErrors)
1017
- }
1018
1421
  return __destroyContextForWasiRollback(cleanupErrors)
1019
1422
  }
1020
1423
 
@@ -1153,7 +1556,10 @@ function __disposeWasiBindingAtExit() {
1153
1556
  // settlements the way __startWasiDisposal does — the process is leaving and
1154
1557
  // those promises have no observer left anyway. Run the synchronous teardown
1155
1558
  // directly. Every step is idempotent, which also makes this the synchronous
1156
- // finish for a disposal that is still waiting for its drain.
1559
+ // finish for a disposal that is still waiting for its drain — and, through
1560
+ // __prepareWasmEnvCleanup, for one still parked between the two halves of
1561
+ // the environment cleanup barrier: there are no turns left to poll with, so
1562
+ // this closes that handshake with `…_finish`, which joins.
1157
1563
  try {
1158
1564
  __destroyEmnapiContext()
1159
1565
  } catch {}
package/minify.wasi.d.cts CHANGED
@@ -341,6 +341,39 @@ export declare const enum Severity {
341
341
  Advice = 'Advice',
342
342
  }
343
343
 
344
+ /** See <https://tc39.es/ecma426/#sec-source-map-format>. */
345
+ export interface JSONSourceMap {
346
+ /** The version field, must be 3. */
347
+ version: number
348
+ /** An optional name of the generated code that this source map is associated with. */
349
+ file?: string
350
+ /** A string with the encoded mapping data. */
351
+ mappings: string
352
+ /**
353
+ * An optional source root, useful for relocating source files on a server or removing repeated values in the "sources" entry.
354
+ * This value is prepended to the individual entries in the "source" field.
355
+ */
356
+ sourceRoot?: string
357
+ /** A list of original sources used by the "mappings" entry. */
358
+ sources: Array<string>
359
+ /**
360
+ * An optional list of source content, useful when the "source" can't be hosted.
361
+ * The contents are listed in the same order as the sources in line 5. "null" may be used if some original sources should be retrieved by name.
362
+ */
363
+ sourcesContent?: Array<string | undefined | null>
364
+ /** A list of symbol names used by the "mappings" entry. */
365
+ names: Array<string>
366
+ /** An optional field containing the debugId for this sourcemap. */
367
+ debugId?: string
368
+ /**
369
+ * Identifies third-party sources (such as framework code or bundler-generated code), allowing developers to avoid code that they don't want to see or step through, without having to configure this beforehand.
370
+ * The `ignoreList` field refers to the `sources` array, and lists the indices of all the known third-party sources in that source map.
371
+ * When parsing the source map, developer tools can use this to determine sections of the code that the browser loads and runs that could be automatically ignore-listed.
372
+ * JSON input falls back to the deprecated `x_google_ignoreList` when `ignoreList` is missing or null.
373
+ */
374
+ ignoreList?: Array<number>
375
+ }
376
+
344
377
  export interface SourceMap {
345
378
  file?: string
346
379
  mappings: string
@@ -349,5 +382,5 @@ export interface SourceMap {
349
382
  sources: Array<string>
350
383
  sourcesContent?: Array<string>
351
384
  version: number
352
- x_google_ignoreList?: Array<number>
385
+ ignoreList?: Array<number>
353
386
  }
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxc-minify/binding-wasm32-wasi",
3
- "version": "0.151.0",
3
+ "version": "0.152.0",
4
4
  "main": "minify.wasi.cjs",
5
5
  "files": [
6
6
  "minify.wasm32-wasi.wasm",