Repository navigation
Expand file tree
/
Copy pathtar.wasi-browser.js
More file actions
1449 lines (1388 loc) · 54.2 KB
/
Copy pathtar.wasi-browser.js
File metadata and controls
1449 lines (1388 loc) · 54.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
emnapiAsyncWorkPlugin as __emnapiAsyncWorkPlugin,
emnapiTSFNPlugin as __emnapiTSFNPlugin,
createOnMessage as __wasmCreateOnMessageForFsProxy,
instantiateNapiModule as __emnapiInstantiateNapiModule,
WASI as __WASI,
} from '@napi-rs/wasm-runtime'
import { createContext as __emnapiCreateContext } from '@emnapi/runtime'
export const __napiBindingTarget = 'wasm32-wasi'
function __napiStampBindingTarget(exportsObject, target) {
if (
Object.prototype.hasOwnProperty.call(exportsObject, '__napiBindingTarget')
) {
if (exportsObject.__napiBindingTarget === target) {
// Already ours: the root entry aliases the object it loaded, so a WASI
// fallback candidate — or a `NAPI_RS_NATIVE_LIBRARY_PATH` override that
// is a generated loader — arrives already stamped with this same value.
return target
}
const error = new Error(
'`__napiBindingTarget` is reserved by the generated binding loader, but the loaded binding already exports it. Rename the export, e.g. #[napi(js_name = "...")].',
)
error.code = 'ERR_NAPI_BINDING_TARGET_CONFLICT'
throw error
}
if (!Object.isExtensible(exportsObject)) {
// A `#[napi(module_exports)]` hook may seal or freeze this object
// (`Object::seal` / `Object::freeze`). Reporting the artifact is metadata,
// never a reason to fail an otherwise successful load, so the stamp is
// skipped. What a consumer still sees then follows the entry point: the
// browser and deferred loaders declare `__napiBindingTarget` at module
// level and go on reporting it, while the CommonJS entries hand back this
// very object as `module.exports`, so there the value is absent.
return target
}
try {
// [[Define]], not [[Set]]: an ordinary assignment walks the prototype
// chain, so an inherited accessor could swallow the value or throw and
// fail an otherwise successful load. The descriptor is what a successful
// assignment would have produced.
Object.defineProperty(exportsObject, '__napiBindingTarget', {
configurable: true,
enumerable: true,
value: target,
writable: true,
})
} catch {
// Same rule as the non-extensible skip above: reporting the artifact is
// metadata, never a reason to fail an otherwise successful load. An exotic
// object (a Proxy whose defineProperty trap refuses) is skipped, not
// thrown over.
}
// The CommonJS loaders assign this return value so `cjs-module-lexer` — and
// therefore Node's CJS -> ESM named export detection — can see
// `__napiBindingTarget` statically.
return target
}
const __wasi = new __WASI({
version: 'preview1',
})
const __wasmUrl = new URL('./tar.wasm32-wasi.wasm', import.meta.url).href
const __wasmResponse = await globalThis.fetch(__wasmUrl)
if (!__wasmResponse.ok) {
throw new Error(
'Failed to fetch WASI module ' +
__wasmUrl +
': ' +
__wasmResponse.status +
' ' +
(__wasmResponse.statusText || 'Unknown Status'),
)
}
const __wasmFile = await __wasmResponse.arrayBuffer()
const __sharedMemory = new WebAssembly.Memory({
initial: 4000,
maximum: 65536,
shared: true,
})
const __asyncWorkPoolSize = 4
const __workerPoolSize = Math.max(
2,
globalThis.navigator?.hardwareConcurrency ?? 4,
)
let __emnapiContext
const __wasiDisposeSymbol = Symbol.for('napi.rs.wasi.dispose')
const __wasiWorkers = new Set()
// The thread manager has to be reachable *before* anything that can throw
// during load or registration. Initialization can fail after the pool has
// already spawned workers, and the rollback still has to mark their
// terminations as expected — but `__napiModule` is assigned only when
// instantiation RETURNS, so on exactly that path it is still undefined. A
// plugin factory runs while the emnapi module is being created, before the
// wasm is loaded and before any registration function runs, and its context
// carries the very same manager instance.
let __wasiThreadManager
function __captureWasiThreadManager(context) {
if (context && context.PThread) {
__wasiThreadManager = context.PThread
}
return {}
}
function __getWasiThreadManager() {
const manager =
__wasiThreadManager !== undefined
? __wasiThreadManager
: __napiModule
? __napiModule.PThread
: undefined
if (manager && typeof manager.terminateWorker === 'function') {
return manager
}
return undefined
}
let __napiInstance
let __emnapiContextDestroyed = false
let __emnapiContextDestroyPromise
let __emnapiWasmEnvCleanupPrepared = false
let __emnapiWasmEnvCleanupPreparing = false
// The closer for a barrier that is parked between `…_begin` and `…_finish`,
// set only while that window is open. `__emnapiWasmEnvCleanupPreparing` cannot
// tell those two apart on its own: it is raised both for a purely synchronous
// frame — which must not be re-entered, and which nothing outside it can
// finish — and across this window, which spans real event-loop turns, so a
// caller that cannot yield can land in the middle of one. That caller can close
// this window, because `…_finish` is idempotent and joins, which is exactly
// what the single call does. See `__prepareWasmEnvCleanup`.
let __finishParkedWasmEnvCleanup
// Raised while a caller that can still yield is driving the barrier, so the
// queue it leaves behind is expected rather than lost. See
// `__reportUnreachedWasmEnvSettlements`.
let __emnapiWasmEnvCleanupYielding = false
let __emnapiWasmEnvSettlementLossReported = false
let __emnapiWasmEnvCleanupRan = false
let __emnapiWasmEnvCleanupDrained = false
let __emnapiWasmEnvCleanupDrainPromise
let __wasiDisposed = false
let __wasiAsyncWorkDrainPromise
let __wasiDisposePromise
let __completeWasiDisposal = function () {}
// Overridden by loader flavors that have a last-resort reclaim for a rollback
// that stopped short of destroying the context. See
// `__rollbackWasiInitialization`.
let __retainWasiRollbackForRetry = function () {}
function __isThenable(value) {
return (
value !== null &&
(typeof value === 'object' || typeof value === 'function') &&
typeof value.then === 'function'
)
}
function __createCleanupError(errors, message) {
if (errors.length === 1) {
return errors[0]
}
const __AggregateError = globalThis.AggregateError
if (typeof __AggregateError === 'function') {
return new __AggregateError(errors, message)
}
const error = new Error(message)
error.errors = errors
return error
}
function __attachCleanupErrors(error, cleanupErrors) {
if (cleanupErrors.length === 0) {
return error
}
const cleanupError = __createCleanupError(
cleanupErrors,
'WASI binding cleanup failed',
)
try {
if (
error &&
(typeof error === 'object' || typeof error === 'function')
) {
if (error.cause === undefined) {
error.cause = cleanupError
if (error.cause === cleanupError) {
return error
}
}
if (Array.isArray(error.cleanupErrors)) {
error.cleanupErrors.push(cleanupError)
return error
} else {
const attachedCleanupErrors = [cleanupError]
error.cleanupErrors = attachedCleanupErrors
if (error.cleanupErrors === attachedCleanupErrors) {
return error
}
}
}
} catch {}
const aggregate = __createCleanupError(
[error, cleanupError],
'WASI binding initialization and cleanup failed',
)
try {
aggregate.cause = error
} catch {}
return aggregate
}
function __wrapEmnapiContextDestroyForSettlement(
context,
prepareEnvCleanup,
isPreparingEnvCleanup,
) {
let destroy
try {
destroy = context.destroy
} catch {
return context
}
if (typeof destroy !== 'function') {
return context
}
try {
Object.defineProperty(context, 'destroy', {
configurable: true,
enumerable: false,
writable: true,
value: function () {
// Reentered from a promise hook that fired inside the barrier: the
// frame running it destroys as soon as it returns.
if (isPreparingEnvCleanup?.()) {
return
}
prepareEnvCleanup?.()
return Reflect.apply(destroy, this, arguments)
},
})
} catch {}
return context
}
function __isPreparingWasmEnvCleanup() {
return __emnapiWasmEnvCleanupPreparing
}
function __prepareWasmEnvCleanup() {
if (__emnapiWasmEnvCleanupPrepared) {
return
}
// A handshake parked between its two halves is one this frame can close, and
// must: every caller of this function is about to destroy the context, and
// the turns the poll is waiting for will not come — an 'exit' teardown is
// the last thing the process runs, and `Context.destroy()` takes the
// environment away. Closing it here runs `…_finish`, which is the call that joins, so
// this degrades to exactly the single call below. Leaving it open instead
// destroys the context with the barrier still raised, the runtime never
// joined and the workers never drained.
const finishParked = __finishParkedWasmEnvCleanup
if (finishParked !== undefined) {
finishParked()
__reportUnreachedWasmEnvSettlements()
return
}
if (__emnapiWasmEnvCleanupPreparing) {
return
}
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
if (typeof prepare === 'function') {
// The addon settles the promises it cancels synchronously, under a
// non-reentrant lifecycle mutex: anything a promise hook calls from in
// here must not reach this export again.
__emnapiWasmEnvCleanupPreparing = true
try {
prepare()
} finally {
__emnapiWasmEnvCleanupPreparing = false
}
__emnapiWasmEnvCleanupRan = true
__reportUnreachedWasmEnvSettlements()
}
__emnapiWasmEnvCleanupPrepared = true
}
/**
* Say so when the barrier leaves settlements queued and nothing is left that
* could deliver them.
*
* Only the disposal chain yields the event-loop turns @emnapi/core needs to
* dispatch its queue. Every other caller of the barrier destroys in the same
* turn — a raw `Context.destroy()`, the 'exit' teardown — and
* `Context.destroy()` runs the threadsafe function's cleanup hook, which drains
* that queue with a null env and discards it. The promises those settlements
* were for then hang forever, silently.
*
* Loud, once, and never throwing: this runs from inside `Context.destroy()`,
* emnapi's own beforeExit destroy included, where throwing would take the whole
* teardown down with it. Destroying anyway is still the right trade — the queue
* is already unreachable by then.
*/
function __reportUnreachedWasmEnvSettlements() {
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {
return
}
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
if (typeof pending !== 'function') {
return
}
let queued
try {
queued = pending()
} catch {
return
}
if (!queued) {
return
}
__emnapiWasmEnvSettlementLossReported = true
try {
const consoleHost = globalThis.console
if (consoleHost && typeof consoleHost.error === 'function') {
consoleHost.error(
"napi-rs: the wasm environment is being destroyed with " +
queued +
" 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.",
)
}
} catch {}
}
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
// on, so the drain turns below interleave with that dispatch instead of racing
// ahead of it on a faster queue.
const __scheduleMacrotask = (function () {
if (typeof setImmediate === 'function') {
return function (callback) {
setImmediate(callback)
}
}
const __MessageChannel = globalThis.MessageChannel
if (typeof __MessageChannel === 'function') {
return function (callback) {
const channel = new __MessageChannel()
channel.port1.onmessage = function () {
channel.port1.onmessage = null
try {
channel.port1.close()
} catch {}
try {
channel.port2.close()
} catch {}
callback()
}
channel.port2.postMessage(null)
}
}
return function (callback) {
setTimeout(callback, 0)
}
})()
// A real, *referenced* timer, for waits that must let the whole host make
// progress between looks — the async-work drain polls the addon rather than
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there
// would spin the loop instead of yielding it. Falls back to the macrotask
// scheduler on a host without timers.
function __scheduleTimer(callback, delay) {
const setTimer = globalThis.setTimeout
if (typeof setTimer !== 'function') {
__scheduleMacrotask(callback)
return
}
try {
setTimer(callback, delay)
} catch {
__scheduleMacrotask(callback)
}
}
// A real, referenced timer rather than a zero-delay macrotask, for the same
// reason the async-work drain uses one: this polls the addon instead of
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
// the loop instead of yielding it.
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
// Arrivals it takes before the poll paces on the host's timers alone. One
// proves nothing: a timer armed before the host's timers stopped still fires.
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
// How long a parked turn's own timer must already have been due before a
// backup that runs calls it dropped. Slack, not a deadline: a timer is due
// against the event loop's clock, which is read once per iteration, while
// these are `Date.now()` readings taken part-way through one, so the two
// drift apart by however long the loop has been inside the current iteration.
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
// How long a backup itself waits. What is left of it after the slack and one
// interval — 149 ms — has to cover the *two* poll turns that can separate a
// parked turn from the last backup armed while the host's timers still
// worked, so the ceiling on a single turn is half of it. See the invariant on
// `__armWasmRuntimePollStallBackup`.
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
/**
* Pacing state for one runtime-work poll.
*
* Per poll, never per module: whether the host's timers arrive is not a
* property of the module. A host can lose its timers between two disposals,
* and in the deferred shape every instance shares this module — one healthy
* instance must not disarm the fallback for the next one.
*/
function __createWasmRuntimePollPace() {
return {
// Timers armed by *this* poll that have actually arrived.
arrivals: 0,
// The turn waiting on a timer alone *right now* — undefined whenever no
// turn is parked — and when that turn's own timer came due.
settleTurn: undefined,
turnTimerDueAt: 0,
}
}
/**
* The backup that ends a turn whose timer is never going to arrive.
*
* Once the poll paces on the timer alone it has nothing left to fall back on
* if the host's timers stop mid-poll: the turn that armed the dead timer is
* the turn that parks, and a parked poll schedules nothing that could notice.
* So every turn arms one of these before it yields, and each one compares due
* times instead of measuring how long the parked turn has been waiting.
*
* Invariant: a parked turn is ended by the newest backup that was armed while
* the host's timers still worked, and a backup ends a turn only when that
* turn's own timer was already due a whole window before the backup itself.
* Neither half turns on how far apart the arms happen to fall — what bounds
* the rescue is how far back that newest live backup is:
*
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a
* turn due a whole window earlier is still parked proves that turn's timer
* was dropped rather than merely late. That same comparison is what leaves a
* healthy host alone: there the turn's timer has already run and cleared
* `settleTurn` before any backup due after it can look.
* - *Two turns back, not one.* A turn that ended does not prove its own timer
* arrived: until `…_TRUSTED_ARRIVALS` is reached every turn arms both
* primitives and the macrotask wins, so such a turn can end with its own
* timer — and the backup armed one line before it — already dead. The
* arrival that then flips the poll onto the timer alone can itself be a
* timer armed before the host's timers died. So the turn that parks can sit
* two turns past the last live arm, and the newest live backup is due
* `…_BACKUP_MS` less *two* turn lengths after that turn's own timer.
* Arming on every turn is what holds it to two, rather than however far back
* a throttle last let one through.
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
* inside `…_BACKUP_MS` less the slack and one interval: 149 ms, so 74 ms
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
* Past that the turn stays parked and the disposal promise never settles.
* The bound is deliberate: reaching it takes a host that drops timers
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
* Node nor WebContainer — the hosts that run the threaded artifact — does
* the second.
*
* The poll then goes back to arming both primitives until two fresh arrivals
* prove the timers again. A host that stops running the timers it has
* *already* accepted leaves nothing to fire, and the disposal promise stays
* pending rather than wedging the thread — the same outcome as a blocking
* closure that never returns. Unreferenced wherever the host allows it: the
* poll's own turn timers are what keep the loop alive, never these.
*/
function __armWasmRuntimePollStallBackup(pace) {
const setTimer = globalThis.setTimeout
if (typeof setTimer !== 'function') {
// Nothing to back up: `__scheduleTimer` is on the macrotask channel
// already, and that one cannot park.
return
}
// Read before arming, so this never claims to be due earlier than the timer
// actually is: a backup ends a turn only when it is provably due after it.
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
let handle
try {
handle = setTimer(() => {
const settleTurn = pace.settleTurn
if (
!settleTurn ||
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
) {
// No turn is parked, or the parked one's timer came due too close to
// this backup to call it dropped — it may still arrive, and the turn
// that armed it armed a backup due a whole window after *that*.
return
}
pace.arrivals = 0
pace.settleTurn = undefined
settleTurn()
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
} catch {
return
}
if (handle && typeof handle.unref === 'function') {
try {
handle.unref()
} catch {}
}
}
/**
* One turn of the runtime-work poll.
*
* `__scheduleTimer` falls back to the macrotask scheduler when `setTimeout` is
* missing or throws, but not when it is present, returns a handle and never
* fires — fake timers in a test suite that disposes from an `afterEach`, or a
* host whose timers belong to an IO context that is already gone. That host
* would park this poll forever, and the poll is unbounded, so nothing would
* ever call `…_finish`.
*
* Arm both primitives until timers armed by this poll have arrived twice, and
* let whichever lands first end the turn; the loser resolves nothing. A host
* with working timers therefore pays the double arming for the first turn or
* two — the macrotask wins the race, but the timers behind it still arrive and
* are counted — and paces on the timer alone from then on, instead of spinning
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,
* and the macrotask is what keeps the poll moving. A host whose timers stop
* after proving themselves is caught by `__armWasmRuntimePollStallBackup`,
* which ends the parked turn and puts this poll back on both.
*/
function __yieldWasmRuntimePollTurn(pace) {
// Armed before the turn yields, and by every turn: what rescues a parked
// turn has to have been armed while the host's timers still worked, and the
// turn that parks is the one whose own timer is already dead.
__armWasmRuntimePollStallBackup(pace)
return new Promise((resolve) => {
let settled = false
const settle = () => {
if (settled) {
return
}
settled = true
if (pace.settleTurn === settle) {
// Nothing is parked any more: a backup running later must not read a
// due time this turn has already answered.
pace.settleTurn = undefined
}
resolve()
}
__scheduleTimer(() => {
pace.arrivals++
settle()
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
// Read next to the arming it describes; see
// `__armWasmRuntimePollStallBackup` for what the two due times mean.
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
__scheduleMacrotask(settle)
return
}
// Paced by the timer alone from here; the backup is what ends this turn if
// the timer never arrives.
pace.settleTurn = settle
pace.turnTimerDueAt = turnTimerDueAt
})
}
/**
* The barrier for callers that can yield: `__prepareWasmEnvCleanup` with real
* event-loop turns in the middle.
*
* `napi_prepare_wasm_env_cleanup` waits — it returns only once the addon's
* async runtime has quiesced, and on `wasm32-wasip1-threads` the thread it
* waits on is this one, the only thread that can give a running blocking
* closure the JavaScript turn *it* is waiting for. A single call there can wait
* for work that can never finish. The addon's two-phase form splits that:
* `…_begin` stops the runtime without joining and reports whether anything is
* still live, `napi_wasm_runtime_work_pending` answers that question again
* without blocking, and `…_finish` joins. The turns yielded in between are the
* entire point.
*
* The poll has no deadline, for the same reason the async-work drain below has
* none: giving up means calling `…_finish`, which joins on this thread, and the
* work it would join is the work that is waiting for a turn from this thread —
* so a bound does not end the wait, it only moves it somewhere the JavaScript
* thread can no longer be reached. A blocking closure that never returns keeps
* the disposal promise pending instead, exactly as a task whose `execute` never
* returns already keeps an *undisposed* process alive. The host contract is in
* `crates/async-runtime/README.md`: a blocking closure must never wait on a
* JavaScript turn. The process-exit path still blocks in `…_finish`, because it
* has no turns left to give (see `__prepareWasmEnvCleanup`).
*
* Feature-detected like every other export in this teardown, so an addon built
* against a napi crate that predates the split keeps the single blocking call.
* Returns nothing whenever the handshake finished without yielding, which keeps
* an idle disposal synchronous.
*/
function __prepareWasmEnvCleanupWithTurns() {
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
return
}
const exports = __napiInstance?.exports
const begin = exports?.napi_prepare_wasm_env_cleanup_begin
const finish = exports?.napi_prepare_wasm_env_cleanup_finish
if (typeof begin !== 'function' || typeof finish !== 'function') {
// No split to use. The settlement drain still follows this, so the queue
// the single call leaves behind is expected rather than lost.
__emnapiWasmEnvCleanupYielding = true
try {
__prepareWasmEnvCleanup()
} finally {
__emnapiWasmEnvCleanupYielding = false
}
return
}
const workPending = exports?.napi_wasm_runtime_work_pending
// The in-flight flag stays raised across the turns below, so a `destroy()`
// from one of the JavaScript handlers they run is the same no-op it is inside
// the single call: the barrier is up and the runtime is mid-teardown, and
// destroying between the halves would strand exactly what this delivers.
__emnapiWasmEnvCleanupPreparing = true
let live
try {
live = begin()
} catch (error) {
__emnapiWasmEnvCleanupPreparing = false
throw error
}
__emnapiWasmEnvCleanupRan = true
const finishCleanup = () => {
if (__emnapiWasmEnvCleanupPrepared) {
// Already closed by a caller that could not yield — the 'exit' teardown
// reached `__prepareWasmEnvCleanup` while this poll was parked. `…_finish`
// is idempotent, but the flags it lowers are not: running it again here
// would clear a `preparing` some later barrier had raised.
return
}
__finishParkedWasmEnvCleanup = undefined
try {
finish()
} finally {
__emnapiWasmEnvCleanupPreparing = false
}
__emnapiWasmEnvCleanupPrepared = true
}
if (!live || typeof workPending !== 'function') {
finishCleanup()
return
}
// Publish the closer before yielding: from here until `finishCleanup` runs,
// a caller that cannot yield is entitled to end this handshake itself.
__finishParkedWasmEnvCleanup = finishCleanup
return (async () => {
// Unbounded, exactly like the async-work drain below. The wait ends when
// the addon reports its runtime work finished; the turns spent here are
// what let that happen at all.
const pace = __createWasmRuntimePollPace()
for (;;) {
await __yieldWasmRuntimePollTurn(pace)
try {
if (!workPending()) {
return
}
} catch {
// A trap is the only way this fails, and a trapped instance has no
// reachable work left. Stop polling and finish.
return
}
}
})().then(finishCleanup, finishCleanup)
}
// Turns to wait for while the addon still reports queued settlements. Reaching
// zero is the only success. A counter still nonzero at this bound rejects the
// disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
// destroying the context over a still-queued settlement — the wait stays
// bounded either way.
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128
// Without `napi_wasm_env_cleanup_pending` the queue is not observable. Fall
// back to the number of turns @emnapi/core needs to coalesce and dispatch a
// call made on this thread (two), plus a margin.
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4
/**
* `napi_prepare_wasm_env_cleanup` only *queues* the promise settlements of the
* tasks it cancelled: `napi_call_threadsafe_function` appends to the
* threadsafe-function queue, and @emnapi/core dispatches that queue from a
* macrotask — two coalescing turns later, even for a call made on this very
* thread. `Context.destroy()` then runs the threadsafe function's cleanup hook,
* which drains the queue with a null env and *discards* whatever is still in it.
*
* So destroying without yielding first strands exactly the promises the barrier
* exists to settle. Yield real event-loop turns until the addon reports the
* queue empty; microtask checkpoints cannot help, no number of them lets a
* macrotask run.
*
* Returns nothing when there is nothing to wait for, which keeps disposal
* synchronous in the common case.
*
* The "already drained" flag is set only once a wait has actually finished.
* Scheduling a macrotask can fail — a host-provided or patched `setImmediate`
* that throws is enough — and a disposal that rejects stays retryable, so
* marking the drain complete up front would make the retry skip it and destroy
* the context with the barrier's settlements still queued.
*
* A wait that runs out of turns with the counter still nonzero rejects with
* `ERR_NAPI_WASI_CLEANUP_PENDING` for the same reason: at that point
* "finished" is indistinguishable from the stranding above, and destroying
* would discard the very settlement the wait was for. The rejection leaves the
* flag unset and disposal retryable.
*/
function __drainWasmEnvCleanup() {
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {
return
}
if (__emnapiWasmEnvCleanupDrainPromise) {
return __emnapiWasmEnvCleanupDrainPromise
}
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
const observable = typeof pending === 'function'
if (observable) {
let queued
try {
queued = pending()
} catch {
__emnapiWasmEnvCleanupDrained = true
return
}
if (!queued) {
__emnapiWasmEnvCleanupDrained = true
return
}
}
const limit = observable
? __WASM_ENV_CLEANUP_DRAIN_TURNS
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS
const drainPromise = (async () => {
let queued = 0
for (let turn = 0; turn < limit; turn++) {
await new Promise((resolve) => {
__scheduleMacrotask(resolve)
})
if (!observable) {
continue
}
try {
queued = pending()
} catch {
return
}
if (!queued) {
return
}
}
if (!observable) {
// Blind wait: without `napi_wasm_env_cleanup_pending` the bound IS the
// contract — there is nothing to consult, so finishing the turns is
// finishing the drain.
return
}
// The counter is still nonzero after every turn the bound allows. The wait
// stays bounded — but claiming success here would be indistinguishable from
// the stranding this drain exists to prevent: disposal would go on to
// destroy the context, whose cleanup hook discards the still-queued
// settlement with a null env, and the promise it was for hangs forever.
// Reject instead, as a retryable cleanup failure: the drained flag stays
// unset, dispose() (and the rollback) decline to destroy, and a later
// dispose() runs the drain again — by which time the queue has usually been
// delivered. A counter that is somehow stuck nonzero therefore costs each
// attempt at most another bounded wait and a rejection, never a stranded
// promise; the process-exit teardown still reclaims the context.
const drainError = new Error(
'the wasm environment still reports ' +
queued +
' queued settlement(s) after ' +
limit +
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',
)
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'
throw drainError
})().then(
(value) => {
// Set only when the wait actually finished AND the queue was seen empty
// (or is unobservable): a drain that timed out with settlements still
// queued rejects above and must stay repeatable.
__emnapiWasmEnvCleanupDrained = true
__emnapiWasmEnvCleanupDrainPromise = undefined
return value
},
(error) => {
__emnapiWasmEnvCleanupDrainPromise = undefined
throw error
},
)
__emnapiWasmEnvCleanupDrainPromise = drainPromise
return drainPromise
}
function __destroyEmnapiContext() {
if (__emnapiContextDestroyed || __emnapiContext === undefined) {
__emnapiContextDestroyed = true
return
}
if (__emnapiContextDestroyPromise) {
return __emnapiContextDestroyPromise
}
__prepareWasmEnvCleanup()
if (__isPreparingWasmEnvCleanup()) {
// Reached from inside the synchronous barrier — a promise hook one of the
// settlements above ran, which is the reentrancy the destroy wrapper
// exists for. `Context.destroy()` below would hit that wrapper's in-flight
// no-op and answer `undefined`, and recording that as a completed destroy
// is what makes the frame that *did* start the barrier skip the real one
// afterwards, leaving the context retained with its cleanup hooks unrun.
// Refuse instead: nothing is flagged, and that frame destroys for real the
// moment it returns. The deferred loader carries the same backstop. A
// parked handshake cannot get here — `__prepareWasmEnvCleanup` closes one
// rather than skipping it.
return
}
const result = __emnapiContext.destroy()
if (!__isThenable(result)) {
__emnapiContextDestroyed = true
return
}
const destroyPromise = Promise.resolve(result).then(
(value) => {
__emnapiContextDestroyed = true
return value
},
(error) => {
__emnapiContextDestroyPromise = undefined
throw error
},
)
__emnapiContextDestroyPromise = destroyPromise
return destroyPromise
}
/**
* Holds the event loop open until `work` settles.
*
* Nothing else can: the pool workers are deliberately unreferenced so an idle
* binding cannot keep a process alive, and referencing them again for the
* termination does not hold either — emnapi unreferences a worker the moment it
* reports `async-thread-ready`, which for a worker that was still starting
* lands *after* the termination began. Without a handle of its own, an
* `await dispose()` with nothing else pending exits the process with its
* promise unsettled, and everything after the `await` is skipped.
*
* The timer is cleared as soon as the work settles, so this never outlives the
* disposal that asked for it.
*/
function __keepEventLoopAliveUntil(work) {
const setTimer = globalThis.setInterval
const clearTimer = globalThis.clearInterval
if (typeof setTimer !== 'function' || typeof clearTimer !== 'function') {
return work
}
let timer
try {
timer = setTimer(function () {}, 50)
} catch {
return work
}
const release = function () {
try {
clearTimer(timer)
} catch {}
}
return work.then(
(value) => {
release()
return value
},
(error) => {
release()
throw error
},
)
}
// How often to re-read `napi_wasm_async_work_pending` while waiting. The wait
// ends when the addon reports zero, so this only decides how promptly disposal
// notices — not how long it waits.
const __WASI_ASYNC_WORK_POLL_INTERVAL_MS = 1
/**
* Settles this addon's outstanding `napi_async_work` before the teardown that
* would strand it.
*
* `napi_prepare_wasm_env_cleanup` does not cover async work, and nothing about
* it is observable from JavaScript: the threadless archive resolves
* `napi_*_async_work` through the `@emnapi/core` plugins, but the threaded one
* links the C `async_work.c` on the uv threadpool, so there the wasm neither
* imports nor exports those symbols and the only brackets a loader could watch
* (`_emnapi_ctx_*_waiting_request_counter`) are shared with threadsafe
* functions. The addon is the one place both flavors go through, so it answers
* for both, through the same kind of handshake the settlement drain uses:
*
* - `napi_wasm_cancel_pending_async_work()` cancels what no thread has
* started. Those completion callbacks run with `napi_cancelled`, which
* napi-rs turns into a promise rejected with an `AbortError`.
* - `napi_wasm_async_work_pending()` counts what is still owed a completion
* callback. Work already executing refuses cancellation and stays counted
* until it finishes normally — which it can, because this runs before the
* barrier, before `Context.destroy()` and before anything is terminated.
*
* Both exports are optional: an addon built against a napi crate that predates
* them drains nothing and keeps the previous behavior, exactly as the
* `napi_wasm_env_cleanup_pending` handshake degrades.
*
* Returns nothing when there is nothing outstanding, which keeps disposal
* synchronous in the common case. The promise it returns otherwise never
* rejects.
*
* The wait has no deadline, and that is the point: giving up would destroy the
* environment with a completion callback still owed, which is the stranding
* this exists to prevent. A task whose `execute` never returns already keeps an
* *undisposed* process alive in exactly the same way, so disposal inherits that
* rather than inventing a bound it cannot honor.
*
* Safe to call from inside a completion callback, which is reachable: settling
* a task runs addon code that can re-enter JavaScript — a setter on the value
* being handed back, a threadsafe-function callback — and that JavaScript can
* call `dispose()`. Two things make it terminate rather than wait on itself:
*
* - The addon keeps a work registered until its completion callback
* *finishes*, so the count read here is at least one and this takes the
* polling path instead of declaring the environment drained and tearing it
* down from inside the frame that is still settling a promise.
* - The poll is a timer, so it cannot run until the callback has returned to
* the host — by which time that work has left the registry. The count the
* next poll reads is the one taken after the callback finished.
*
* `__disposeWasiBinding` hands every caller the same in-flight promise, so the
* nested call joins this disposal rather than starting a second one.
*/
function __drainWasiAsyncWork() {
if (__wasiAsyncWorkDrainPromise !== undefined) {
return __wasiAsyncWorkDrainPromise
}
const exports = __napiInstance?.exports
const pending = exports?.napi_wasm_async_work_pending
const cancelPending = exports?.napi_wasm_cancel_pending_async_work
if (typeof pending !== 'function' || typeof cancelPending !== 'function') {
return
}
const readPending = () => {
try {
return pending()
} catch (error) {
// A trap is the only way this call fails: it reads a counter and cannot
// allocate or call back into JavaScript. A trapped instance can no longer
// run anything, so its outstanding work is unreachable by definition —
// there is nothing left to wait for, and refusing to dispose would only
// keep a dead instance and its stuck counter alive. Best-effort here is
// the honest answer, and it is what disposal did before this drain
// existed.
//
// Only a trap. Anything else means the export is not what this loader
// thinks it is, which is a defect worth surfacing rather than disposing
// over.
if (error instanceof globalThis.WebAssembly.RuntimeError) {
return 0
}
throw error
}
}
if (!readPending()) {
return
}
try {
cancelPending()
} catch {
// Cancellation is an optimization: it bounds the wait by the work already
// executing. Failing it only means waiting for the whole queue instead.
}
if (!readPending()) {
return
}
const drainPromise = __keepEventLoopAliveUntil(
(async () => {
while (readPending()) {
await new Promise((resolve) => {
__scheduleTimer(resolve, __WASI_ASYNC_WORK_POLL_INTERVAL_MS)
})
}
})(),
).then(
() => {
__wasiAsyncWorkDrainPromise = undefined
},
(error) => {
// A wait that could not run is not a wait that finished. The only way
// here is a host whose timers and macrotask primitives all refuse, and
// the work is still outstanding — reporting success would destroy the