@oxc-minify/binding-wasm32-wasi 0.150.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.
- package/minify.wasi-browser.js +831 -27
- package/minify.wasi.cjs +850 -28
- package/minify.wasi.d.cts +37 -1
- package/minify.wasm32-wasi.wasm +0 -0
- package/package.json +1 -1
package/minify.wasi-browser.js
CHANGED
|
@@ -8,6 +8,55 @@ import {
|
|
|
8
8
|
import { createContext as __emnapiCreateContext } from '@emnapi/runtime'
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
export const __napiBindingTarget = 'wasm32-wasi'
|
|
12
|
+
function __napiStampBindingTarget(exportsObject, target) {
|
|
13
|
+
if (
|
|
14
|
+
Object.prototype.hasOwnProperty.call(exportsObject, '__napiBindingTarget')
|
|
15
|
+
) {
|
|
16
|
+
if (exportsObject.__napiBindingTarget === target) {
|
|
17
|
+
// Already ours: the root entry aliases the object it loaded, so a WASI
|
|
18
|
+
// fallback candidate — or a `NAPI_RS_NATIVE_LIBRARY_PATH` override that
|
|
19
|
+
// is a generated loader — arrives already stamped with this same value.
|
|
20
|
+
return target
|
|
21
|
+
}
|
|
22
|
+
const error = new Error(
|
|
23
|
+
'`__napiBindingTarget` is reserved by the generated binding loader, but the loaded binding already exports it. Rename the export, e.g. #[napi(js_name = "...")].',
|
|
24
|
+
)
|
|
25
|
+
error.code = 'ERR_NAPI_BINDING_TARGET_CONFLICT'
|
|
26
|
+
throw error
|
|
27
|
+
}
|
|
28
|
+
if (!Object.isExtensible(exportsObject)) {
|
|
29
|
+
// A `#[napi(module_exports)]` hook may seal or freeze this object
|
|
30
|
+
// (`Object::seal` / `Object::freeze`). Reporting the artifact is metadata,
|
|
31
|
+
// never a reason to fail an otherwise successful load, so the stamp is
|
|
32
|
+
// skipped. What a consumer still sees then follows the entry point: the
|
|
33
|
+
// browser and deferred loaders declare `__napiBindingTarget` at module
|
|
34
|
+
// level and go on reporting it, while the CommonJS entries hand back this
|
|
35
|
+
// very object as `module.exports`, so there the value is absent.
|
|
36
|
+
return target
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
// [[Define]], not [[Set]]: an ordinary assignment walks the prototype
|
|
40
|
+
// chain, so an inherited accessor could swallow the value or throw and
|
|
41
|
+
// fail an otherwise successful load. The descriptor is what a successful
|
|
42
|
+
// assignment would have produced.
|
|
43
|
+
Object.defineProperty(exportsObject, '__napiBindingTarget', {
|
|
44
|
+
configurable: true,
|
|
45
|
+
enumerable: true,
|
|
46
|
+
value: target,
|
|
47
|
+
writable: true,
|
|
48
|
+
})
|
|
49
|
+
} catch {
|
|
50
|
+
// Same rule as the non-extensible skip above: reporting the artifact is
|
|
51
|
+
// metadata, never a reason to fail an otherwise successful load. An exotic
|
|
52
|
+
// object (a Proxy whose defineProperty trap refuses) is skipped, not
|
|
53
|
+
// thrown over.
|
|
54
|
+
}
|
|
55
|
+
// The CommonJS loaders assign this return value so `cjs-module-lexer` — and
|
|
56
|
+
// therefore Node's CJS -> ESM named export detection — can see
|
|
57
|
+
// `__napiBindingTarget` statically.
|
|
58
|
+
return target
|
|
59
|
+
}
|
|
11
60
|
|
|
12
61
|
const __wasi = new __WASI({
|
|
13
62
|
version: 'preview1',
|
|
@@ -42,14 +91,59 @@ let __emnapiContext
|
|
|
42
91
|
|
|
43
92
|
const __wasiDisposeSymbol = Symbol.for('napi.rs.wasi.dispose')
|
|
44
93
|
const __wasiWorkers = new Set()
|
|
94
|
+
// The thread manager has to be reachable *before* anything that can throw
|
|
95
|
+
// during load or registration. Initialization can fail after the pool has
|
|
96
|
+
// already spawned workers, and the rollback still has to mark their
|
|
97
|
+
// terminations as expected — but `__napiModule` is assigned only when
|
|
98
|
+
// instantiation RETURNS, so on exactly that path it is still undefined. A
|
|
99
|
+
// plugin factory runs while the emnapi module is being created, before the
|
|
100
|
+
// wasm is loaded and before any registration function runs, and its context
|
|
101
|
+
// carries the very same manager instance.
|
|
102
|
+
let __wasiThreadManager
|
|
103
|
+
|
|
104
|
+
function __captureWasiThreadManager(context) {
|
|
105
|
+
if (context && context.PThread) {
|
|
106
|
+
__wasiThreadManager = context.PThread
|
|
107
|
+
}
|
|
108
|
+
return {}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function __getWasiThreadManager() {
|
|
112
|
+
const manager =
|
|
113
|
+
__wasiThreadManager !== undefined
|
|
114
|
+
? __wasiThreadManager
|
|
115
|
+
: __napiModule
|
|
116
|
+
? __napiModule.PThread
|
|
117
|
+
: undefined
|
|
118
|
+
if (manager && typeof manager.terminateWorker === 'function') {
|
|
119
|
+
return manager
|
|
120
|
+
}
|
|
121
|
+
return undefined
|
|
122
|
+
}
|
|
45
123
|
let __napiInstance
|
|
46
124
|
let __emnapiContextDestroyed = false
|
|
47
125
|
let __emnapiContextDestroyPromise
|
|
48
126
|
let __emnapiWasmEnvCleanupPrepared = false
|
|
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
|
|
49
142
|
let __emnapiWasmEnvCleanupRan = false
|
|
50
143
|
let __emnapiWasmEnvCleanupDrained = false
|
|
51
144
|
let __emnapiWasmEnvCleanupDrainPromise
|
|
52
145
|
let __wasiDisposed = false
|
|
146
|
+
let __wasiAsyncWorkDrainPromise
|
|
53
147
|
let __wasiDisposePromise
|
|
54
148
|
let __completeWasiDisposal = function () {}
|
|
55
149
|
// Overridden by loader flavors that have a last-resort reclaim for a rollback
|
|
@@ -119,18 +213,127 @@ function __attachCleanupErrors(error, cleanupErrors) {
|
|
|
119
213
|
return aggregate
|
|
120
214
|
}
|
|
121
215
|
|
|
216
|
+
function __wrapEmnapiContextDestroyForSettlement(
|
|
217
|
+
context,
|
|
218
|
+
prepareEnvCleanup,
|
|
219
|
+
isPreparingEnvCleanup,
|
|
220
|
+
) {
|
|
221
|
+
let destroy
|
|
222
|
+
try {
|
|
223
|
+
destroy = context.destroy
|
|
224
|
+
} catch {
|
|
225
|
+
return context
|
|
226
|
+
}
|
|
227
|
+
if (typeof destroy !== 'function') {
|
|
228
|
+
return context
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
Object.defineProperty(context, 'destroy', {
|
|
232
|
+
configurable: true,
|
|
233
|
+
enumerable: false,
|
|
234
|
+
writable: true,
|
|
235
|
+
value: function () {
|
|
236
|
+
// Reentered from a promise hook that fired inside the barrier: the
|
|
237
|
+
// frame running it destroys as soon as it returns.
|
|
238
|
+
if (isPreparingEnvCleanup?.()) {
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
prepareEnvCleanup?.()
|
|
242
|
+
return Reflect.apply(destroy, this, arguments)
|
|
243
|
+
},
|
|
244
|
+
})
|
|
245
|
+
} catch {}
|
|
246
|
+
return context
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function __isPreparingWasmEnvCleanup() {
|
|
250
|
+
return __emnapiWasmEnvCleanupPreparing
|
|
251
|
+
}
|
|
252
|
+
|
|
122
253
|
function __prepareWasmEnvCleanup() {
|
|
123
254
|
if (__emnapiWasmEnvCleanupPrepared) {
|
|
124
255
|
return
|
|
125
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) {
|
|
272
|
+
return
|
|
273
|
+
}
|
|
126
274
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
|
|
127
275
|
if (typeof prepare === 'function') {
|
|
128
|
-
|
|
276
|
+
// The addon settles the promises it cancels synchronously, under a
|
|
277
|
+
// non-reentrant lifecycle mutex: anything a promise hook calls from in
|
|
278
|
+
// here must not reach this export again.
|
|
279
|
+
__emnapiWasmEnvCleanupPreparing = true
|
|
280
|
+
try {
|
|
281
|
+
prepare()
|
|
282
|
+
} finally {
|
|
283
|
+
__emnapiWasmEnvCleanupPreparing = false
|
|
284
|
+
}
|
|
129
285
|
__emnapiWasmEnvCleanupRan = true
|
|
286
|
+
__reportUnreachedWasmEnvSettlements()
|
|
130
287
|
}
|
|
131
288
|
__emnapiWasmEnvCleanupPrepared = true
|
|
132
289
|
}
|
|
133
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
|
+
|
|
134
337
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
|
|
135
338
|
// on, so the drain turns below interleave with that dispatch instead of racing
|
|
136
339
|
// ahead of it on a faster queue.
|
|
@@ -162,6 +365,309 @@ const __scheduleMacrotask = (function () {
|
|
|
162
365
|
}
|
|
163
366
|
})()
|
|
164
367
|
|
|
368
|
+
// A real, *referenced* timer, for waits that must let the whole host make
|
|
369
|
+
// progress between looks — the async-work drain polls the addon rather than
|
|
370
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there
|
|
371
|
+
// would spin the loop instead of yielding it. Falls back to the macrotask
|
|
372
|
+
// scheduler on a host without timers.
|
|
373
|
+
function __scheduleTimer(callback, delay) {
|
|
374
|
+
const setTimer = globalThis.setTimeout
|
|
375
|
+
if (typeof setTimer !== 'function') {
|
|
376
|
+
__scheduleMacrotask(callback)
|
|
377
|
+
return
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
setTimer(callback, delay)
|
|
381
|
+
} catch {
|
|
382
|
+
__scheduleMacrotask(callback)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
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
|
+
|
|
165
671
|
// Turns to wait for while the addon still reports queued settlements. Reaching
|
|
166
672
|
// zero is the only success. A counter still nonzero at this bound rejects the
|
|
167
673
|
// disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
|
|
@@ -298,6 +804,19 @@ function __destroyEmnapiContext() {
|
|
|
298
804
|
}
|
|
299
805
|
|
|
300
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
|
+
}
|
|
301
820
|
const result = __emnapiContext.destroy()
|
|
302
821
|
if (!__isThenable(result)) {
|
|
303
822
|
__emnapiContextDestroyed = true
|
|
@@ -318,13 +837,209 @@ function __destroyEmnapiContext() {
|
|
|
318
837
|
return destroyPromise
|
|
319
838
|
}
|
|
320
839
|
|
|
840
|
+
/**
|
|
841
|
+
* Holds the event loop open until `work` settles.
|
|
842
|
+
*
|
|
843
|
+
* Nothing else can: the pool workers are deliberately unreferenced so an idle
|
|
844
|
+
* binding cannot keep a process alive, and referencing them again for the
|
|
845
|
+
* termination does not hold either — emnapi unreferences a worker the moment it
|
|
846
|
+
* reports `async-thread-ready`, which for a worker that was still starting
|
|
847
|
+
* lands *after* the termination began. Without a handle of its own, an
|
|
848
|
+
* `await dispose()` with nothing else pending exits the process with its
|
|
849
|
+
* promise unsettled, and everything after the `await` is skipped.
|
|
850
|
+
*
|
|
851
|
+
* The timer is cleared as soon as the work settles, so this never outlives the
|
|
852
|
+
* disposal that asked for it.
|
|
853
|
+
*/
|
|
854
|
+
function __keepEventLoopAliveUntil(work) {
|
|
855
|
+
const setTimer = globalThis.setInterval
|
|
856
|
+
const clearTimer = globalThis.clearInterval
|
|
857
|
+
if (typeof setTimer !== 'function' || typeof clearTimer !== 'function') {
|
|
858
|
+
return work
|
|
859
|
+
}
|
|
860
|
+
let timer
|
|
861
|
+
try {
|
|
862
|
+
timer = setTimer(function () {}, 50)
|
|
863
|
+
} catch {
|
|
864
|
+
return work
|
|
865
|
+
}
|
|
866
|
+
const release = function () {
|
|
867
|
+
try {
|
|
868
|
+
clearTimer(timer)
|
|
869
|
+
} catch {}
|
|
870
|
+
}
|
|
871
|
+
return work.then(
|
|
872
|
+
(value) => {
|
|
873
|
+
release()
|
|
874
|
+
return value
|
|
875
|
+
},
|
|
876
|
+
(error) => {
|
|
877
|
+
release()
|
|
878
|
+
throw error
|
|
879
|
+
},
|
|
880
|
+
)
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// How often to re-read `napi_wasm_async_work_pending` while waiting. The wait
|
|
884
|
+
// ends when the addon reports zero, so this only decides how promptly disposal
|
|
885
|
+
// notices — not how long it waits.
|
|
886
|
+
const __WASI_ASYNC_WORK_POLL_INTERVAL_MS = 1
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Settles this addon's outstanding `napi_async_work` before the teardown that
|
|
890
|
+
* would strand it.
|
|
891
|
+
*
|
|
892
|
+
* `napi_prepare_wasm_env_cleanup` does not cover async work, and nothing about
|
|
893
|
+
* it is observable from JavaScript: the threadless archive resolves
|
|
894
|
+
* `napi_*_async_work` through the `@emnapi/core` plugins, but the threaded one
|
|
895
|
+
* links the C `async_work.c` on the uv threadpool, so there the wasm neither
|
|
896
|
+
* imports nor exports those symbols and the only brackets a loader could watch
|
|
897
|
+
* (`_emnapi_ctx_*_waiting_request_counter`) are shared with threadsafe
|
|
898
|
+
* functions. The addon is the one place both flavors go through, so it answers
|
|
899
|
+
* for both, through the same kind of handshake the settlement drain uses:
|
|
900
|
+
*
|
|
901
|
+
* - `napi_wasm_cancel_pending_async_work()` cancels what no thread has
|
|
902
|
+
* started. Those completion callbacks run with `napi_cancelled`, which
|
|
903
|
+
* napi-rs turns into a promise rejected with an `AbortError`.
|
|
904
|
+
* - `napi_wasm_async_work_pending()` counts what is still owed a completion
|
|
905
|
+
* callback. Work already executing refuses cancellation and stays counted
|
|
906
|
+
* until it finishes normally — which it can, because this runs before the
|
|
907
|
+
* barrier, before `Context.destroy()` and before anything is terminated.
|
|
908
|
+
*
|
|
909
|
+
* Both exports are optional: an addon built against a napi crate that predates
|
|
910
|
+
* them drains nothing and keeps the previous behavior, exactly as the
|
|
911
|
+
* `napi_wasm_env_cleanup_pending` handshake degrades.
|
|
912
|
+
*
|
|
913
|
+
* Returns nothing when there is nothing outstanding, which keeps disposal
|
|
914
|
+
* synchronous in the common case. The promise it returns otherwise never
|
|
915
|
+
* rejects.
|
|
916
|
+
*
|
|
917
|
+
* The wait has no deadline, and that is the point: giving up would destroy the
|
|
918
|
+
* environment with a completion callback still owed, which is the stranding
|
|
919
|
+
* this exists to prevent. A task whose `execute` never returns already keeps an
|
|
920
|
+
* *undisposed* process alive in exactly the same way, so disposal inherits that
|
|
921
|
+
* rather than inventing a bound it cannot honor.
|
|
922
|
+
*
|
|
923
|
+
* Safe to call from inside a completion callback, which is reachable: settling
|
|
924
|
+
* a task runs addon code that can re-enter JavaScript — a setter on the value
|
|
925
|
+
* being handed back, a threadsafe-function callback — and that JavaScript can
|
|
926
|
+
* call `dispose()`. Two things make it terminate rather than wait on itself:
|
|
927
|
+
*
|
|
928
|
+
* - The addon keeps a work registered until its completion callback
|
|
929
|
+
* *finishes*, so the count read here is at least one and this takes the
|
|
930
|
+
* polling path instead of declaring the environment drained and tearing it
|
|
931
|
+
* down from inside the frame that is still settling a promise.
|
|
932
|
+
* - The poll is a timer, so it cannot run until the callback has returned to
|
|
933
|
+
* the host — by which time that work has left the registry. The count the
|
|
934
|
+
* next poll reads is the one taken after the callback finished.
|
|
935
|
+
*
|
|
936
|
+
* `__disposeWasiBinding` hands every caller the same in-flight promise, so the
|
|
937
|
+
* nested call joins this disposal rather than starting a second one.
|
|
938
|
+
*/
|
|
939
|
+
function __drainWasiAsyncWork() {
|
|
940
|
+
if (__wasiAsyncWorkDrainPromise !== undefined) {
|
|
941
|
+
return __wasiAsyncWorkDrainPromise
|
|
942
|
+
}
|
|
943
|
+
const exports = __napiInstance?.exports
|
|
944
|
+
const pending = exports?.napi_wasm_async_work_pending
|
|
945
|
+
const cancelPending = exports?.napi_wasm_cancel_pending_async_work
|
|
946
|
+
if (typeof pending !== 'function' || typeof cancelPending !== 'function') {
|
|
947
|
+
return
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const readPending = () => {
|
|
951
|
+
try {
|
|
952
|
+
return pending()
|
|
953
|
+
} catch (error) {
|
|
954
|
+
// A trap is the only way this call fails: it reads a counter and cannot
|
|
955
|
+
// allocate or call back into JavaScript. A trapped instance can no longer
|
|
956
|
+
// run anything, so its outstanding work is unreachable by definition —
|
|
957
|
+
// there is nothing left to wait for, and refusing to dispose would only
|
|
958
|
+
// keep a dead instance and its stuck counter alive. Best-effort here is
|
|
959
|
+
// the honest answer, and it is what disposal did before this drain
|
|
960
|
+
// existed.
|
|
961
|
+
//
|
|
962
|
+
// Only a trap. Anything else means the export is not what this loader
|
|
963
|
+
// thinks it is, which is a defect worth surfacing rather than disposing
|
|
964
|
+
// over.
|
|
965
|
+
if (error instanceof globalThis.WebAssembly.RuntimeError) {
|
|
966
|
+
return 0
|
|
967
|
+
}
|
|
968
|
+
throw error
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
if (!readPending()) {
|
|
973
|
+
return
|
|
974
|
+
}
|
|
975
|
+
try {
|
|
976
|
+
cancelPending()
|
|
977
|
+
} catch {
|
|
978
|
+
// Cancellation is an optimization: it bounds the wait by the work already
|
|
979
|
+
// executing. Failing it only means waiting for the whole queue instead.
|
|
980
|
+
}
|
|
981
|
+
if (!readPending()) {
|
|
982
|
+
return
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
const drainPromise = __keepEventLoopAliveUntil(
|
|
986
|
+
(async () => {
|
|
987
|
+
while (readPending()) {
|
|
988
|
+
await new Promise((resolve) => {
|
|
989
|
+
__scheduleTimer(resolve, __WASI_ASYNC_WORK_POLL_INTERVAL_MS)
|
|
990
|
+
})
|
|
991
|
+
}
|
|
992
|
+
})(),
|
|
993
|
+
).then(
|
|
994
|
+
() => {
|
|
995
|
+
__wasiAsyncWorkDrainPromise = undefined
|
|
996
|
+
},
|
|
997
|
+
(error) => {
|
|
998
|
+
// A wait that could not run is not a wait that finished. The only way
|
|
999
|
+
// here is a host whose timers and macrotask primitives all refuse, and
|
|
1000
|
+
// the work is still outstanding — reporting success would destroy the
|
|
1001
|
+
// environment over it, which is the stranding this exists to prevent.
|
|
1002
|
+
// Reject instead: disposal stays retryable, and the context is not
|
|
1003
|
+
// destroyed. Clearing the memo first is what makes the retry re-run this.
|
|
1004
|
+
__wasiAsyncWorkDrainPromise = undefined
|
|
1005
|
+
throw error
|
|
1006
|
+
},
|
|
1007
|
+
)
|
|
1008
|
+
__wasiAsyncWorkDrainPromise = drainPromise
|
|
1009
|
+
return drainPromise
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* `@emnapi/wasi-threads` counts a worker exit as expected only when its own
|
|
1014
|
+
* thread manager performed the termination. A bare `worker.terminate()` reaches
|
|
1015
|
+
* the manager's `exit` listener instead, which reports
|
|
1016
|
+
* `worker (tid = N) sent an error! ... stopped with exit code 1` and rethrows
|
|
1017
|
+
* inside the emit — aborting the `once('exit')` that backs the terminate
|
|
1018
|
+
* promise, so disposal never settles and the process dies with an uncaught
|
|
1019
|
+
* exception. Mark the termination through the manager first.
|
|
1020
|
+
*
|
|
1021
|
+
* The manager comes from `__getWasiThreadManager`, not from `__napiModule`:
|
|
1022
|
+
* the initialization rollback runs on the one path where instantiation never
|
|
1023
|
+
* returned, so `__napiModule` is still undefined there while the workers it
|
|
1024
|
+
* spawned are already registered and loaded.
|
|
1025
|
+
*
|
|
1026
|
+
* Not `terminateAllThreads()`: that one recreates the pool it just shut down.
|
|
1027
|
+
*/
|
|
321
1028
|
function __terminateWasiWorkers() {
|
|
322
1029
|
const cleanupErrors = []
|
|
323
1030
|
const pending = []
|
|
1031
|
+
const threadManager = __getWasiThreadManager()
|
|
324
1032
|
|
|
325
1033
|
for (const worker of __wasiWorkers) {
|
|
326
1034
|
let result
|
|
327
1035
|
try {
|
|
1036
|
+
if (threadManager) {
|
|
1037
|
+
threadManager.terminateWorker(worker)
|
|
1038
|
+
// `terminateWorker` leaves behind a reporter that logs every message
|
|
1039
|
+
// still queued on the port, which Node flushes on exit. Nothing is
|
|
1040
|
+
// listening for those any more.
|
|
1041
|
+
worker.onmessage = undefined
|
|
1042
|
+
}
|
|
328
1043
|
result = worker.terminate()
|
|
329
1044
|
} catch (error) {
|
|
330
1045
|
cleanupErrors.push(error)
|
|
@@ -354,7 +1069,9 @@ function __terminateWasiWorkers() {
|
|
|
354
1069
|
)
|
|
355
1070
|
}
|
|
356
1071
|
}
|
|
357
|
-
return pending.length > 0
|
|
1072
|
+
return pending.length > 0
|
|
1073
|
+
? __keepEventLoopAliveUntil(Promise.all(pending)).then(finish)
|
|
1074
|
+
: finish()
|
|
358
1075
|
}
|
|
359
1076
|
|
|
360
1077
|
function __finishWasiDisposal() {
|
|
@@ -373,11 +1090,7 @@ function __continueWasiDisposal() {
|
|
|
373
1090
|
return __finishWasiDisposal()
|
|
374
1091
|
}
|
|
375
1092
|
|
|
376
|
-
function
|
|
377
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually
|
|
378
|
-
// reach JavaScript, and only then destroy the environment. Doing these two
|
|
379
|
-
// back to back is what strands them.
|
|
380
|
-
__prepareWasmEnvCleanup()
|
|
1093
|
+
function __drainWasmEnvForWasiDisposal() {
|
|
381
1094
|
const drainResult = __drainWasmEnvCleanup()
|
|
382
1095
|
if (__isThenable(drainResult)) {
|
|
383
1096
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)
|
|
@@ -385,6 +1098,33 @@ function __startWasiDisposal() {
|
|
|
385
1098
|
return __continueWasiDisposal()
|
|
386
1099
|
}
|
|
387
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
|
+
|
|
1113
|
+
function __startWasiDisposal() {
|
|
1114
|
+
// Outstanding `napi_async_work` goes first, while the environment is still
|
|
1115
|
+
// completely live: the completion callbacks run addon code, and everything
|
|
1116
|
+
// after this point takes that away from them — the barrier shuts the async
|
|
1117
|
+
// runtime down, `Context.destroy()` stops JavaScript calls, and terminating
|
|
1118
|
+
// the pool threads removes what would have reported the work finished.
|
|
1119
|
+
const asyncWorkResult = __drainWasiAsyncWork()
|
|
1120
|
+
if (__isThenable(asyncWorkResult)) {
|
|
1121
|
+
return Promise.resolve(asyncWorkResult).then(
|
|
1122
|
+
__cleanUpWasmEnvForWasiDisposal,
|
|
1123
|
+
)
|
|
1124
|
+
}
|
|
1125
|
+
return __cleanUpWasmEnvForWasiDisposal()
|
|
1126
|
+
}
|
|
1127
|
+
|
|
388
1128
|
/**
|
|
389
1129
|
* Disposes this generated WASI binding.
|
|
390
1130
|
*
|
|
@@ -526,36 +1266,90 @@ function __retainFailedWasiRollback(cleanupErrors) {
|
|
|
526
1266
|
* bug with no upper bound, while the retained bookkeeping is bounded by the page.
|
|
527
1267
|
*/
|
|
528
1268
|
function __rollbackWasiInitialization() {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
1269
|
+
// The environment teardown this rollback performs, kept nested so it cannot
|
|
1270
|
+
// be reached without the async-work drain below running first.
|
|
1271
|
+
function __rollbackWasmEnvForWasiInitialization() {
|
|
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) {
|
|
1297
|
+
let drainResult
|
|
1298
|
+
try {
|
|
1299
|
+
drainResult = __drainWasmEnvCleanup()
|
|
1300
|
+
} catch (cleanupError) {
|
|
1301
|
+
cleanupErrors.push(cleanupError)
|
|
1302
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1303
|
+
}
|
|
1304
|
+
if (__isThenable(drainResult)) {
|
|
1305
|
+
return Promise.resolve(drainResult).then(
|
|
1306
|
+
() => __destroyContextForWasiRollback(cleanupErrors),
|
|
1307
|
+
(cleanupError) => {
|
|
1308
|
+
cleanupErrors.push(cleanupError)
|
|
1309
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1310
|
+
},
|
|
1311
|
+
)
|
|
1312
|
+
}
|
|
1313
|
+
return __destroyContextForWasiRollback(cleanupErrors)
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// Same reason as `__startWasiDisposal`: a module-init hook can start async
|
|
1317
|
+
// work before the load goes on to fail, and this rollback tears down exactly
|
|
1318
|
+
// what those completions need. Settle them while everything is still live,
|
|
1319
|
+
// before the barrier and the teardown above take that away.
|
|
1320
|
+
//
|
|
1321
|
+
// A drain that could not finish leaves async work possibly outstanding, and
|
|
1322
|
+
// destroying the context over it would strand exactly what this rollback is
|
|
1323
|
+
// there to settle. Stop short and retain instead — the same trade
|
|
1324
|
+
// `__rollbackWasmEnvForWasiInitialization` makes for the settlement drain, so
|
|
1325
|
+
// the context stays reclaimable by a retry or by this flavor's own
|
|
1326
|
+
// last-resort teardown.
|
|
1327
|
+
const __retainAfterAsyncWorkDrainFailure = (cleanupError) =>
|
|
1328
|
+
__retainFailedWasiRollback([cleanupError])
|
|
1329
|
+
let asyncWorkResult
|
|
532
1330
|
try {
|
|
533
|
-
|
|
534
|
-
drainResult = __drainWasmEnvCleanup()
|
|
1331
|
+
asyncWorkResult = __drainWasiAsyncWork()
|
|
535
1332
|
} catch (cleanupError) {
|
|
536
|
-
|
|
537
|
-
settlementsUnreached = true
|
|
1333
|
+
return __retainAfterAsyncWorkDrainFailure(cleanupError)
|
|
538
1334
|
}
|
|
539
|
-
if (__isThenable(
|
|
540
|
-
return Promise.resolve(
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
cleanupErrors.push(cleanupError)
|
|
544
|
-
return __retainFailedWasiRollback(cleanupErrors)
|
|
545
|
-
},
|
|
1335
|
+
if (__isThenable(asyncWorkResult)) {
|
|
1336
|
+
return Promise.resolve(asyncWorkResult).then(
|
|
1337
|
+
__rollbackWasmEnvForWasiInitialization,
|
|
1338
|
+
__retainAfterAsyncWorkDrainFailure,
|
|
546
1339
|
)
|
|
547
1340
|
}
|
|
548
|
-
|
|
549
|
-
return __retainFailedWasiRollback(cleanupErrors)
|
|
550
|
-
}
|
|
551
|
-
return __destroyContextForWasiRollback(cleanupErrors)
|
|
1341
|
+
return __rollbackWasmEnvForWasiInitialization()
|
|
552
1342
|
}
|
|
553
1343
|
|
|
554
1344
|
let __wasiModule
|
|
555
1345
|
let __napiModule
|
|
556
1346
|
|
|
557
1347
|
try {
|
|
558
|
-
__emnapiContext =
|
|
1348
|
+
__emnapiContext = __wrapEmnapiContextDestroyForSettlement(
|
|
1349
|
+
__emnapiCreateContext({ autoDestroy: false }),
|
|
1350
|
+
__prepareWasmEnvCleanup,
|
|
1351
|
+
__isPreparingWasmEnvCleanup,
|
|
1352
|
+
)
|
|
559
1353
|
__emnapiContext.suppressDestroy()
|
|
560
1354
|
|
|
561
1355
|
;({
|
|
@@ -566,7 +1360,11 @@ try {
|
|
|
566
1360
|
context: __emnapiContext,
|
|
567
1361
|
asyncWorkPoolSize: __asyncWorkPoolSize,
|
|
568
1362
|
reuseWorker: false,
|
|
569
|
-
plugins: [
|
|
1363
|
+
plugins: [
|
|
1364
|
+
__captureWasiThreadManager,
|
|
1365
|
+
__emnapiAsyncWorkPlugin,
|
|
1366
|
+
__emnapiTSFNPlugin,
|
|
1367
|
+
],
|
|
570
1368
|
wasi: __wasi,
|
|
571
1369
|
onCreateWorker() {
|
|
572
1370
|
const worker = new Worker(new URL('@oxc-minify/binding-wasm32-wasi/wasi-worker-browser.mjs', import.meta.url), {
|
|
@@ -596,6 +1394,12 @@ try {
|
|
|
596
1394
|
},
|
|
597
1395
|
}))
|
|
598
1396
|
__publishWasiDispose(__napiModule.exports)
|
|
1397
|
+
// The default export hands out this object; a named module export does not
|
|
1398
|
+
// travel with it, so carry the marker on the binding itself too. After the
|
|
1399
|
+
// host install, which hands the same object to addon-provided registration
|
|
1400
|
+
// functions that may put anything on it, and inside this `try`, so a claimed
|
|
1401
|
+
// name fails the load through the rollback below rather than past it.
|
|
1402
|
+
__napiStampBindingTarget(__napiModule.exports, __napiBindingTarget)
|
|
599
1403
|
} catch (error) {
|
|
600
1404
|
const cleanupErrors = await __rollbackWasiInitialization()
|
|
601
1405
|
throw __attachCleanupErrors(error, cleanupErrors)
|