@endge/computation-sandbox 0.1.1

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.
@@ -0,0 +1,1020 @@
1
+ var v = { JS_EVAL_TYPE_GLOBAL: 0, JS_EVAL_TYPE_MODULE: 1, JS_EVAL_FLAG_STRICT: 8, JS_EVAL_FLAG_STRIP: 16, JS_EVAL_FLAG_COMPILE_ONLY: 32, JS_EVAL_FLAG_BACKTRACE_BARRIER: 64 }, I = { BaseObjects: 1, Date: 2, Eval: 4, StringNormalize: 8, RegExp: 16, RegExpCompiler: 32, JSON: 64, Proxy: 128, MapSet: 256, TypedArrays: 512, Promise: 1024, BigInt: 2048, BigFloat: 4096, BigDecimal: 8192, OperatorOverloading: 16384, BignumExt: 32768 }, A = { Pending: 0, Fulfilled: 1, Rejected: 2 }, w = { JS_GPN_STRING_MASK: 1, JS_GPN_SYMBOL_MASK: 2, JS_GPN_PRIVATE_MASK: 4, JS_GPN_ENUM_ONLY: 16, QTS_GPN_NUMBER_MASK: 64, QTS_STANDARD_COMPLIANT_NUMBER: 128 }, _ = { IsStrictlyEqual: 0, IsSameValue: 1, IsSameValueZero: 2 }, ee = Object.defineProperty, te = (e, t) => {
2
+ for (var r in t) ee(e, r, { get: t[r], enumerable: !0 });
3
+ };
4
+ function b(...e) {
5
+ }
6
+ var re = {};
7
+ te(re, { QuickJSAsyncifyError: () => U, QuickJSAsyncifySuspended: () => D, QuickJSEmptyGetOwnPropertyNames: () => K, QuickJSEmscriptenModuleError: () => se, QuickJSHostRefInvalid: () => p, QuickJSHostRefRangeExceeded: () => q, QuickJSMemoryLeakDetected: () => ie, QuickJSNotImplemented: () => j, QuickJSPromisePending: () => z, QuickJSUnknownIntrinsic: () => $, QuickJSUnwrapError: () => H, QuickJSUseAfterFree: () => M, QuickJSWrongOwner: () => G });
8
+ var H = class extends Error {
9
+ constructor(e, t) {
10
+ let r = typeof e == "object" && e && "message" in e ? String(e.message) : String(e);
11
+ super(r), this.cause = e, this.context = t, this.name = "QuickJSUnwrapError";
12
+ }
13
+ }, G = class extends Error {
14
+ constructor() {
15
+ super(...arguments), this.name = "QuickJSWrongOwner";
16
+ }
17
+ }, M = class extends Error {
18
+ constructor() {
19
+ super(...arguments), this.name = "QuickJSUseAfterFree";
20
+ }
21
+ }, j = class extends Error {
22
+ constructor() {
23
+ super(...arguments), this.name = "QuickJSNotImplemented";
24
+ }
25
+ }, U = class extends Error {
26
+ constructor() {
27
+ super(...arguments), this.name = "QuickJSAsyncifyError";
28
+ }
29
+ }, D = class extends Error {
30
+ constructor() {
31
+ super(...arguments), this.name = "QuickJSAsyncifySuspended";
32
+ }
33
+ }, ie = class extends Error {
34
+ constructor() {
35
+ super(...arguments), this.name = "QuickJSMemoryLeakDetected";
36
+ }
37
+ }, se = class extends Error {
38
+ constructor() {
39
+ super(...arguments), this.name = "QuickJSEmscriptenModuleError";
40
+ }
41
+ }, $ = class extends TypeError {
42
+ constructor() {
43
+ super(...arguments), this.name = "QuickJSUnknownIntrinsic";
44
+ }
45
+ }, z = class extends Error {
46
+ constructor() {
47
+ super(...arguments), this.name = "QuickJSPromisePending";
48
+ }
49
+ }, K = class extends Error {
50
+ constructor() {
51
+ super(...arguments), this.name = "QuickJSEmptyGetOwnPropertyNames";
52
+ }
53
+ }, q = class extends Error {
54
+ constructor() {
55
+ super(...arguments), this.name = "QuickJSHostRefRangeExceeded";
56
+ }
57
+ }, p = class extends Error {
58
+ constructor() {
59
+ super(...arguments), this.name = "QuickJSHostRefInvalid";
60
+ }
61
+ };
62
+ function* Y(e) {
63
+ return yield e;
64
+ }
65
+ function ne(e) {
66
+ return Y(J(e));
67
+ }
68
+ var k = Y;
69
+ k.of = ne;
70
+ function L(e, t) {
71
+ return (...r) => {
72
+ let i = t.call(e, k, ...r);
73
+ return J(i);
74
+ };
75
+ }
76
+ function oe(e, t) {
77
+ let r = t.call(e, k);
78
+ return J(r);
79
+ }
80
+ function J(e) {
81
+ function t(r) {
82
+ return r.done ? r.value : r.value instanceof Promise ? r.value.then((i) => t(e.next(i)), (i) => t(e.throw(i))) : t(e.next(r.value));
83
+ }
84
+ return t(e.next());
85
+ }
86
+ var m = class {
87
+ [Symbol.dispose]() {
88
+ return this.dispose();
89
+ }
90
+ }, R = Symbol.dispose ?? Symbol.for("Symbol.dispose"), N = m.prototype;
91
+ N[R] || (N[R] = function() {
92
+ return this.dispose();
93
+ });
94
+ var c = class W extends m {
95
+ constructor(t, r, i, s) {
96
+ super(), this._value = t, this.copier = r, this.disposer = i, this._owner = s, this._alive = !0, this._constructorStack = void 0;
97
+ }
98
+ get alive() {
99
+ return this._alive;
100
+ }
101
+ get value() {
102
+ return this.assertAlive(), this._value;
103
+ }
104
+ get owner() {
105
+ return this._owner;
106
+ }
107
+ get dupable() {
108
+ return !!this.copier;
109
+ }
110
+ dup() {
111
+ if (this.assertAlive(), !this.copier) throw new Error("Non-dupable lifetime");
112
+ return new W(this.copier(this._value), this.copier, this.disposer, this._owner);
113
+ }
114
+ consume(t) {
115
+ this.assertAlive();
116
+ let r = t(this);
117
+ return this.dispose(), r;
118
+ }
119
+ map(t) {
120
+ return this.assertAlive(), t(this);
121
+ }
122
+ tap(t) {
123
+ return t(this), this;
124
+ }
125
+ dispose() {
126
+ this.assertAlive(), this.disposer && this.disposer(this._value), this._alive = !1;
127
+ }
128
+ assertAlive() {
129
+ if (!this.alive) throw this._constructorStack ? new M(`Lifetime not alive
130
+ ${this._constructorStack}
131
+ Lifetime used`) : new M("Lifetime not alive");
132
+ }
133
+ }, y = class extends c {
134
+ constructor(e, t) {
135
+ super(e, void 0, void 0, t);
136
+ }
137
+ get dupable() {
138
+ return !0;
139
+ }
140
+ dup() {
141
+ return this;
142
+ }
143
+ dispose() {
144
+ }
145
+ }, O = class extends c {
146
+ constructor(e, t, r, i) {
147
+ super(e, t, r, i);
148
+ }
149
+ dispose() {
150
+ this._alive = !1;
151
+ }
152
+ };
153
+ function T(e, t) {
154
+ let r;
155
+ try {
156
+ e.dispose();
157
+ } catch (i) {
158
+ r = i;
159
+ }
160
+ if (t && r) throw Object.assign(t, { message: `${t.message}
161
+ Then, failed to dispose scope: ${r.message}`, disposeError: r }), t;
162
+ if (t || r) throw t || r;
163
+ }
164
+ var d = class x extends m {
165
+ constructor() {
166
+ super(...arguments), this._disposables = new c(/* @__PURE__ */ new Set()), this.manage = (t) => (this._disposables.value.add(t), t);
167
+ }
168
+ static withScope(t) {
169
+ let r = new x(), i;
170
+ try {
171
+ return t(r);
172
+ } catch (s) {
173
+ throw i = s, s;
174
+ } finally {
175
+ T(r, i);
176
+ }
177
+ }
178
+ static withScopeMaybeAsync(t, r) {
179
+ return oe(void 0, function* (i) {
180
+ let s = new x(), n;
181
+ try {
182
+ return yield* i.of(r.call(t, i, s));
183
+ } catch (o) {
184
+ throw n = o, o;
185
+ } finally {
186
+ T(s, n);
187
+ }
188
+ });
189
+ }
190
+ static async withScopeAsync(t) {
191
+ let r = new x(), i;
192
+ try {
193
+ return await t(r);
194
+ } catch (s) {
195
+ throw i = s, s;
196
+ } finally {
197
+ T(r, i);
198
+ }
199
+ }
200
+ get alive() {
201
+ return this._disposables.alive;
202
+ }
203
+ dispose() {
204
+ let t = Array.from(this._disposables.value.values()).reverse();
205
+ for (let r of t) r.alive && r.dispose();
206
+ this._disposables.dispose();
207
+ }
208
+ };
209
+ function ue(e) {
210
+ let t = e ? Array.from(e) : [];
211
+ function r() {
212
+ return t.forEach((s) => s.alive ? s.dispose() : void 0);
213
+ }
214
+ function i() {
215
+ return t.some((s) => s.alive);
216
+ }
217
+ return Object.defineProperty(t, R, { configurable: !0, enumerable: !1, value: r }), Object.defineProperty(t, "dispose", { configurable: !0, enumerable: !1, value: r }), Object.defineProperty(t, "alive", { configurable: !0, enumerable: !1, get: i }), t;
218
+ }
219
+ function E(e) {
220
+ return !!(e && (typeof e == "object" || typeof e == "function") && "alive" in e && typeof e.alive == "boolean" && "dispose" in e && typeof e.dispose == "function");
221
+ }
222
+ var C = class Z extends m {
223
+ static success(t) {
224
+ return new ae(t);
225
+ }
226
+ static fail(t, r) {
227
+ return new le(t, r);
228
+ }
229
+ static is(t) {
230
+ return t instanceof Z;
231
+ }
232
+ }, ae = class extends C {
233
+ constructor(e) {
234
+ super(), this.value = e;
235
+ }
236
+ get alive() {
237
+ return E(this.value) ? this.value.alive : !0;
238
+ }
239
+ dispose() {
240
+ E(this.value) && this.value.dispose();
241
+ }
242
+ unwrap() {
243
+ return this.value;
244
+ }
245
+ unwrapOr(e) {
246
+ return this.value;
247
+ }
248
+ }, le = class extends C {
249
+ constructor(e, t) {
250
+ super(), this.error = e, this.onUnwrap = t;
251
+ }
252
+ get alive() {
253
+ return E(this.error) ? this.error.alive : !0;
254
+ }
255
+ dispose() {
256
+ E(this.error) && this.error.dispose();
257
+ }
258
+ unwrap() {
259
+ throw this.onUnwrap(this), this.error;
260
+ }
261
+ unwrapOr(e) {
262
+ return e;
263
+ }
264
+ }, S = C, he = class extends m {
265
+ constructor(e) {
266
+ super(), this.resolve = (t) => {
267
+ this.resolveHandle.alive && (this.context.unwrapResult(this.context.callFunction(this.resolveHandle, this.context.undefined, t || this.context.undefined)).dispose(), this.disposeResolvers(), this.onSettled());
268
+ }, this.reject = (t) => {
269
+ this.rejectHandle.alive && (this.context.unwrapResult(this.context.callFunction(this.rejectHandle, this.context.undefined, t || this.context.undefined)).dispose(), this.disposeResolvers(), this.onSettled());
270
+ }, this.dispose = () => {
271
+ this.handle.alive && this.handle.dispose(), this.disposeResolvers();
272
+ }, this.context = e.context, this.owner = e.context.runtime, this.handle = e.promiseHandle, this.settled = new Promise((t) => {
273
+ this.onSettled = t;
274
+ }), this.resolveHandle = e.resolveHandle, this.rejectHandle = e.rejectHandle;
275
+ }
276
+ get alive() {
277
+ return this.handle.alive || this.resolveHandle.alive || this.rejectHandle.alive;
278
+ }
279
+ disposeResolvers() {
280
+ this.resolveHandle.alive && this.resolveHandle.dispose(), this.rejectHandle.alive && this.rejectHandle.dispose();
281
+ }
282
+ }, X = class {
283
+ constructor(e) {
284
+ this.module = e;
285
+ }
286
+ toPointerArray(e) {
287
+ let t = new Int32Array(e.map((s) => s.value)), r = t.length * t.BYTES_PER_ELEMENT, i = this.module._malloc(r);
288
+ return new Uint8Array(this.module.HEAPU8.buffer, i, r).set(new Uint8Array(t.buffer)), new c(i, void 0, (s) => this.module._free(s));
289
+ }
290
+ newTypedArray(e, t) {
291
+ let r = new e(new Array(t).fill(0)), i = r.length * r.BYTES_PER_ELEMENT, s = this.module._malloc(i), n = new e(this.module.HEAPU8.buffer, s, t);
292
+ return n.set(r), new c({ typedArray: n, ptr: s }, void 0, (o) => this.module._free(o.ptr));
293
+ }
294
+ newMutablePointerArray(e) {
295
+ return this.newTypedArray(Int32Array, e);
296
+ }
297
+ newHeapCharPointer(e) {
298
+ let t = this.module.lengthBytesUTF8(e), r = t + 1, i = this.module._malloc(r);
299
+ return this.module.stringToUTF8(e, i, r), new c({ ptr: i, strlen: t }, void 0, (s) => this.module._free(s.ptr));
300
+ }
301
+ newHeapBufferPointer(e) {
302
+ let t = e.byteLength, r = this.module._malloc(t);
303
+ return this.module.HEAPU8.set(e, r), new c({ pointer: r, numBytes: t }, void 0, (i) => this.module._free(i.pointer));
304
+ }
305
+ consumeHeapCharPointer(e) {
306
+ let t = this.module.UTF8ToString(e);
307
+ return this.module._free(e), t;
308
+ }
309
+ };
310
+ function ce(e) {
311
+ if (!e) return 0;
312
+ let t = 0;
313
+ for (let [r, i] of Object.entries(e)) {
314
+ if (!(r in I)) throw new $(r);
315
+ i && (t |= I[r]);
316
+ }
317
+ return t;
318
+ }
319
+ function de(e) {
320
+ if (typeof e == "number") return e;
321
+ if (e === void 0) return 0;
322
+ let { type: t, strict: r, strip: i, compileOnly: s, backtraceBarrier: n } = e, o = 0;
323
+ return t === "global" && (o |= v.JS_EVAL_TYPE_GLOBAL), t === "module" && (o |= v.JS_EVAL_TYPE_MODULE), r && (o |= v.JS_EVAL_FLAG_STRICT), i && (o |= v.JS_EVAL_FLAG_STRIP), s && (o |= v.JS_EVAL_FLAG_COMPILE_ONLY), n && (o |= v.JS_EVAL_FLAG_BACKTRACE_BARRIER), o;
324
+ }
325
+ function me(e) {
326
+ if (typeof e == "number") return e;
327
+ if (e === void 0) return 0;
328
+ let { strings: t, symbols: r, quickjsPrivate: i, onlyEnumerable: s, numbers: n, numbersAsStrings: o } = e, u = 0;
329
+ return t && (u |= w.JS_GPN_STRING_MASK), r && (u |= w.JS_GPN_SYMBOL_MASK), i && (u |= w.JS_GPN_PRIVATE_MASK), s && (u |= w.JS_GPN_ENUM_ONLY), n && (u |= w.QTS_GPN_NUMBER_MASK), o && (u |= w.QTS_STANDARD_COMPLIANT_NUMBER), u;
330
+ }
331
+ function fe(...e) {
332
+ let t = [];
333
+ for (let r of e) r !== void 0 && (t = t.concat(r));
334
+ return t;
335
+ }
336
+ var pe = class extends m {
337
+ constructor(e, t) {
338
+ super(), this.handle = e, this.context = t, this._isDone = !1, this.owner = t.runtime;
339
+ }
340
+ [Symbol.iterator]() {
341
+ return this;
342
+ }
343
+ next(e) {
344
+ if (!this.alive || this._isDone) return { done: !0, value: void 0 };
345
+ let t = this._next ?? (this._next = this.context.getProp(this.handle, "next"));
346
+ return this.callIteratorMethod(t, e);
347
+ }
348
+ return(e) {
349
+ if (!this.alive) return { done: !0, value: void 0 };
350
+ let t = this.context.getProp(this.handle, "return");
351
+ if (t === this.context.undefined && e === void 0) return this.dispose(), { done: !0, value: void 0 };
352
+ let r = this.callIteratorMethod(t, e);
353
+ return t.dispose(), this.dispose(), r;
354
+ }
355
+ throw(e) {
356
+ if (!this.alive) return { done: !0, value: void 0 };
357
+ let t = e instanceof c ? e : this.context.newError(e), r = this.context.getProp(this.handle, "throw"), i = this.callIteratorMethod(r, e);
358
+ return t.alive && t.dispose(), r.dispose(), this.dispose(), i;
359
+ }
360
+ get alive() {
361
+ return this.handle.alive;
362
+ }
363
+ dispose() {
364
+ var e;
365
+ this._isDone = !0, this.handle.dispose(), (e = this._next) == null || e.dispose();
366
+ }
367
+ callIteratorMethod(e, t) {
368
+ let r = t ? this.context.callFunction(e, this.handle, t) : this.context.callFunction(e, this.handle);
369
+ if (r.error) return this.dispose(), { value: r };
370
+ let i = this.context.getProp(r.value, "done").consume((n) => this.context.dump(n)), s = this.context.getProp(r.value, "value");
371
+ return r.value.dispose(), i && this.dispose(), { value: S.success(s), done: i };
372
+ }
373
+ }, V = -2147483648, F = 2147483647, P = 0;
374
+ function Q(e) {
375
+ return e >> 8;
376
+ }
377
+ var ye = class {
378
+ constructor() {
379
+ this.nextId = V, this.freelist = [], this.groups = /* @__PURE__ */ new Map();
380
+ }
381
+ put(e) {
382
+ let t = this.allocateId(), r = Q(t), i = this.groups.get(r);
383
+ return i || (i = /* @__PURE__ */ new Map(), this.groups.set(r, i)), i.set(t, e), t;
384
+ }
385
+ get(e) {
386
+ if (e === P) throw new p("no host reference id defined");
387
+ let t = Q(e), r = this.groups.get(t);
388
+ if (!r) throw new p(`host reference id ${e} is not defined`);
389
+ let i = r.get(e);
390
+ if (!i) throw new p(`host reference id ${e} is not defined`);
391
+ return i;
392
+ }
393
+ delete(e) {
394
+ if (e === P) throw new p("no host reference id defined");
395
+ let t = Q(e), r = this.groups.get(t);
396
+ if (!r) throw new p(`host reference id ${e} is not defined`);
397
+ r.delete(e), r.size === 0 && this.groups.delete(t), this.freelist.push(e);
398
+ }
399
+ allocateId() {
400
+ if (this.freelist.length > 0) return this.freelist.shift();
401
+ if (this.nextId === P && this.nextId++, this.nextId > F) throw new q(`HostRefMap: too many host refs created without disposing. Max simultaneous host refs: ${F - V}`);
402
+ return this.nextId++;
403
+ }
404
+ }, B = class extends m {
405
+ constructor(e, t, r) {
406
+ if (r === P) throw new p("cannot create HostRef with undefined id");
407
+ super(), this.runtime = e, this.handle = t, this.id = r;
408
+ }
409
+ get alive() {
410
+ return this.handle.alive;
411
+ }
412
+ dispose() {
413
+ this.handle.dispose();
414
+ }
415
+ get value() {
416
+ return this.runtime.hostRefs.get(this.id);
417
+ }
418
+ }, ve = class extends X {
419
+ constructor(e) {
420
+ var t;
421
+ super(e.module), this.scope = new d(), this.copyJSValue = (r) => this.ffi.QTS_DupValuePointer(this.ctx.value, r), this.freeJSValue = (r) => {
422
+ this.ffi.QTS_FreeValuePointer(this.ctx.value, r);
423
+ }, (t = e.ownedLifetimes) == null || t.forEach((r) => this.scope.manage(r)), this.owner = e.owner, this.module = e.module, this.ffi = e.ffi, this.rt = e.rt, this.ctx = this.scope.manage(e.ctx);
424
+ }
425
+ get alive() {
426
+ return this.scope.alive;
427
+ }
428
+ dispose() {
429
+ return this.scope.dispose();
430
+ }
431
+ [Symbol.dispose]() {
432
+ return this.dispose();
433
+ }
434
+ manage(e) {
435
+ return this.scope.manage(e);
436
+ }
437
+ consumeJSCharPointer(e) {
438
+ let t = this.module.UTF8ToString(e);
439
+ return this.ffi.QTS_FreeCString(this.ctx.value, e), t;
440
+ }
441
+ heapValueHandle(e, t) {
442
+ let r = t ? (i) => {
443
+ t(), this.freeJSValue(i);
444
+ } : this.freeJSValue;
445
+ return new c(e, this.copyJSValue, r, this.owner);
446
+ }
447
+ staticHeapValueHandle(e) {
448
+ return this.manage(this.heapValueHandle(e)), new y(e, this.owner);
449
+ }
450
+ }, we = class extends m {
451
+ constructor(e) {
452
+ super(), this._undefined = void 0, this._null = void 0, this._false = void 0, this._true = void 0, this._global = void 0, this._BigInt = void 0, this._Symbol = void 0, this._SymbolIterator = void 0, this._SymbolAsyncIterator = void 0, this.cToHostCallbacks = { callFunction: (t, r, i, s, n) => {
453
+ if (t !== this.ctx.value) throw new Error("QuickJSContext instance received C -> JS call with mismatched ctx");
454
+ let o = this.getFunction(n);
455
+ return d.withScopeMaybeAsync(this, function* (u, a) {
456
+ let l = a.manage(new O(r, this.memory.copyJSValue, this.memory.freeJSValue, this.runtime)), f = new Array(i);
457
+ for (let h = 0; h < i; h++) {
458
+ let g = this.ffi.QTS_ArgvGetJSValueConstPointer(s, h);
459
+ f[h] = a.manage(new O(g, this.memory.copyJSValue, this.memory.freeJSValue, this.runtime));
460
+ }
461
+ try {
462
+ let h = yield* u(o.apply(l, f));
463
+ if (h) {
464
+ if ("error" in h && h.error) throw this.runtime.debugLog("throw error", h.error), h.error;
465
+ let g = a.manage(h instanceof c ? h : h.value);
466
+ return this.ffi.QTS_DupValuePointer(this.ctx.value, g.value);
467
+ }
468
+ return 0;
469
+ } catch (h) {
470
+ return this.errorToHandle(h).consume((g) => this.ffi.QTS_Throw(this.ctx.value, g.value));
471
+ }
472
+ });
473
+ } }, this.runtime = e.runtime, this.module = e.module, this.ffi = e.ffi, this.rt = e.rt, this.ctx = e.ctx, this.memory = new ve({ ...e, owner: this.runtime }), e.callbacks.setContextCallbacks(this.ctx.value, this.cToHostCallbacks), this.dump = this.dump.bind(this), this.getString = this.getString.bind(this), this.getNumber = this.getNumber.bind(this), this.resolvePromise = this.resolvePromise.bind(this), this.uint32Out = this.memory.manage(this.memory.newTypedArray(Uint32Array, 1));
474
+ }
475
+ get alive() {
476
+ return this.memory.alive;
477
+ }
478
+ dispose() {
479
+ this.memory.dispose();
480
+ }
481
+ get undefined() {
482
+ if (this._undefined) return this._undefined;
483
+ let e = this.ffi.QTS_GetUndefined();
484
+ return this._undefined = new y(e);
485
+ }
486
+ get null() {
487
+ if (this._null) return this._null;
488
+ let e = this.ffi.QTS_GetNull();
489
+ return this._null = new y(e);
490
+ }
491
+ get true() {
492
+ if (this._true) return this._true;
493
+ let e = this.ffi.QTS_GetTrue();
494
+ return this._true = new y(e);
495
+ }
496
+ get false() {
497
+ if (this._false) return this._false;
498
+ let e = this.ffi.QTS_GetFalse();
499
+ return this._false = new y(e);
500
+ }
501
+ get global() {
502
+ if (this._global) return this._global;
503
+ let e = this.ffi.QTS_GetGlobalObject(this.ctx.value);
504
+ return this._global = this.memory.staticHeapValueHandle(e), this._global;
505
+ }
506
+ newNumber(e) {
507
+ return this.memory.heapValueHandle(this.ffi.QTS_NewFloat64(this.ctx.value, e));
508
+ }
509
+ newString(e) {
510
+ let t = this.memory.newHeapCharPointer(e).consume((r) => this.ffi.QTS_NewString(this.ctx.value, r.value.ptr));
511
+ return this.memory.heapValueHandle(t);
512
+ }
513
+ newUniqueSymbol(e) {
514
+ let t = (typeof e == "symbol" ? e.description : e) ?? "", r = this.memory.newHeapCharPointer(t).consume((i) => this.ffi.QTS_NewSymbol(this.ctx.value, i.value.ptr, 0));
515
+ return this.memory.heapValueHandle(r);
516
+ }
517
+ newSymbolFor(e) {
518
+ let t = (typeof e == "symbol" ? e.description : e) ?? "", r = this.memory.newHeapCharPointer(t).consume((i) => this.ffi.QTS_NewSymbol(this.ctx.value, i.value.ptr, 1));
519
+ return this.memory.heapValueHandle(r);
520
+ }
521
+ getWellKnownSymbol(e) {
522
+ return this._Symbol ?? (this._Symbol = this.memory.manage(this.getProp(this.global, "Symbol"))), this.getProp(this._Symbol, e);
523
+ }
524
+ newBigInt(e) {
525
+ if (!this._BigInt) {
526
+ let i = this.getProp(this.global, "BigInt");
527
+ this.memory.manage(i), this._BigInt = new y(i.value, this.runtime);
528
+ }
529
+ let t = this._BigInt, r = String(e);
530
+ return this.newString(r).consume((i) => this.unwrapResult(this.callFunction(t, this.undefined, i)));
531
+ }
532
+ newObject(e) {
533
+ e && this.runtime.assertOwned(e);
534
+ let t = e ? this.ffi.QTS_NewObjectProto(this.ctx.value, e.value) : this.ffi.QTS_NewObject(this.ctx.value);
535
+ return this.memory.heapValueHandle(t);
536
+ }
537
+ newArray() {
538
+ let e = this.ffi.QTS_NewArray(this.ctx.value);
539
+ return this.memory.heapValueHandle(e);
540
+ }
541
+ newArrayBuffer(e) {
542
+ let t = new Uint8Array(e), r = this.memory.newHeapBufferPointer(t), i = this.ffi.QTS_NewArrayBuffer(this.ctx.value, r.value.pointer, t.length);
543
+ return this.memory.heapValueHandle(i);
544
+ }
545
+ newPromise(e) {
546
+ let t = d.withScope((r) => {
547
+ let i = r.manage(this.memory.newMutablePointerArray(2)), s = this.ffi.QTS_NewPromiseCapability(this.ctx.value, i.value.ptr), n = this.memory.heapValueHandle(s), [o, u] = Array.from(i.value.typedArray).map((a) => this.memory.heapValueHandle(a));
548
+ return new he({ context: this, promiseHandle: n, resolveHandle: o, rejectHandle: u });
549
+ });
550
+ return e && typeof e == "function" && (e = new Promise(e)), e && Promise.resolve(e).then(t.resolve, (r) => r instanceof c ? t.reject(r) : this.newError(r).consume(t.reject)), t;
551
+ }
552
+ newFunction(e, t) {
553
+ let r = typeof e == "function" ? e : t;
554
+ if (!r) throw new TypeError("Expected a function");
555
+ return this.newFunctionWithOptions({ name: typeof e == "string" ? e : void 0, length: r.length, isConstructor: !1, fn: r });
556
+ }
557
+ newConstructorFunction(e, t) {
558
+ let r = typeof e == "function" ? e : t;
559
+ if (!r) throw new TypeError("Expected a function");
560
+ return this.newFunctionWithOptions({ name: typeof e == "string" ? e : void 0, length: r.length, isConstructor: !0, fn: r });
561
+ }
562
+ newFunctionWithOptions(e) {
563
+ let { name: t, length: r, isConstructor: i, fn: s } = e, n = this.runtime.hostRefs.put(s);
564
+ try {
565
+ return this.memory.heapValueHandle(this.ffi.QTS_NewFunction(this.ctx.value, t ?? "", r, i, n));
566
+ } catch (o) {
567
+ throw this.runtime.hostRefs.delete(n), o;
568
+ }
569
+ }
570
+ newError(e) {
571
+ let t = this.memory.heapValueHandle(this.ffi.QTS_NewError(this.ctx.value));
572
+ return e && typeof e == "object" ? (e.name !== void 0 && this.newString(e.name).consume((r) => this.setProp(t, "name", r)), e.message !== void 0 && this.newString(e.message).consume((r) => this.setProp(t, "message", r))) : typeof e == "string" ? this.newString(e).consume((r) => this.setProp(t, "message", r)) : e !== void 0 && this.newString(String(e)).consume((r) => this.setProp(t, "message", r)), t;
573
+ }
574
+ newHostRef(e) {
575
+ let t = this.runtime.hostRefs.put(e);
576
+ try {
577
+ let r = this.memory.heapValueHandle(this.ffi.QTS_NewHostRef(this.ctx.value, t));
578
+ return new B(this.runtime, r, t);
579
+ } catch (r) {
580
+ throw this.runtime.hostRefs.delete(t), r;
581
+ }
582
+ }
583
+ toHostRef(e) {
584
+ let t = this.ffi.QTS_GetHostRefId(e.value);
585
+ if (t !== 0) return this.runtime.hostRefs.get(t), new B(this.runtime, e.dup(), t);
586
+ }
587
+ unwrapHostRef(e) {
588
+ let t = this.ffi.QTS_GetHostRefId(e.value);
589
+ if (t === 0) throw new p("handle is not a HostRef");
590
+ return this.runtime.hostRefs.get(t);
591
+ }
592
+ typeof(e) {
593
+ return this.runtime.assertOwned(e), this.memory.consumeHeapCharPointer(this.ffi.QTS_Typeof(this.ctx.value, e.value));
594
+ }
595
+ getNumber(e) {
596
+ return this.runtime.assertOwned(e), this.ffi.QTS_GetFloat64(this.ctx.value, e.value);
597
+ }
598
+ getString(e) {
599
+ return this.runtime.assertOwned(e), this.memory.consumeJSCharPointer(this.ffi.QTS_GetString(this.ctx.value, e.value));
600
+ }
601
+ getSymbol(e) {
602
+ this.runtime.assertOwned(e);
603
+ let t = this.memory.consumeJSCharPointer(this.ffi.QTS_GetSymbolDescriptionOrKey(this.ctx.value, e.value));
604
+ return this.ffi.QTS_IsGlobalSymbol(this.ctx.value, e.value) ? Symbol.for(t) : Symbol(t);
605
+ }
606
+ getBigInt(e) {
607
+ this.runtime.assertOwned(e);
608
+ let t = this.getString(e);
609
+ return BigInt(t);
610
+ }
611
+ getArrayBuffer(e) {
612
+ this.runtime.assertOwned(e);
613
+ let t = this.ffi.QTS_GetArrayBufferLength(this.ctx.value, e.value), r = this.ffi.QTS_GetArrayBuffer(this.ctx.value, e.value);
614
+ if (!r) throw new Error("Couldn't allocate memory to get ArrayBuffer");
615
+ return new c(this.module.HEAPU8.subarray(r, r + t), void 0, () => this.module._free(r));
616
+ }
617
+ getPromiseState(e) {
618
+ this.runtime.assertOwned(e);
619
+ let t = this.ffi.QTS_PromiseState(this.ctx.value, e.value);
620
+ if (t < 0) return { type: "fulfilled", value: e, notAPromise: !0 };
621
+ if (t === A.Pending) return { type: "pending", get error() {
622
+ return new z("Cannot unwrap a pending promise");
623
+ } };
624
+ let r = this.ffi.QTS_PromiseResult(this.ctx.value, e.value), i = this.memory.heapValueHandle(r);
625
+ if (t === A.Fulfilled) return { type: "fulfilled", value: i };
626
+ if (t === A.Rejected) return { type: "rejected", error: i };
627
+ throw i.dispose(), new Error(`Unknown JSPromiseStateEnum: ${t}`);
628
+ }
629
+ resolvePromise(e) {
630
+ this.runtime.assertOwned(e);
631
+ let t = d.withScope((r) => {
632
+ let i = r.manage(this.getProp(this.global, "Promise")), s = r.manage(this.getProp(i, "resolve"));
633
+ return this.callFunction(s, i, e);
634
+ });
635
+ return t.error ? Promise.resolve(t) : new Promise((r) => {
636
+ d.withScope((i) => {
637
+ let s = i.manage(this.newFunction("resolve", (a) => {
638
+ r(this.success(a && a.dup()));
639
+ })), n = i.manage(this.newFunction("reject", (a) => {
640
+ r(this.fail(a && a.dup()));
641
+ })), o = i.manage(t.value), u = i.manage(this.getProp(o, "then"));
642
+ this.callFunction(u, o, s, n).unwrap().dispose();
643
+ });
644
+ });
645
+ }
646
+ isEqual(e, t, r = _.IsStrictlyEqual) {
647
+ if (e === t) return !0;
648
+ this.runtime.assertOwned(e), this.runtime.assertOwned(t);
649
+ let i = this.ffi.QTS_IsEqual(this.ctx.value, e.value, t.value, r);
650
+ if (i === -1) throw new j("WASM variant does not expose equality");
651
+ return !!i;
652
+ }
653
+ eq(e, t) {
654
+ return this.isEqual(e, t, _.IsStrictlyEqual);
655
+ }
656
+ sameValue(e, t) {
657
+ return this.isEqual(e, t, _.IsSameValue);
658
+ }
659
+ sameValueZero(e, t) {
660
+ return this.isEqual(e, t, _.IsSameValueZero);
661
+ }
662
+ getProp(e, t) {
663
+ this.runtime.assertOwned(e);
664
+ let r;
665
+ return typeof t == "number" && t >= 0 ? r = this.ffi.QTS_GetPropNumber(this.ctx.value, e.value, t) : r = this.borrowPropertyKey(t).consume((i) => this.ffi.QTS_GetProp(this.ctx.value, e.value, i.value)), this.memory.heapValueHandle(r);
666
+ }
667
+ getLength(e) {
668
+ if (this.runtime.assertOwned(e), !(this.ffi.QTS_GetLength(this.ctx.value, this.uint32Out.value.ptr, e.value) < 0)) return this.uint32Out.value.typedArray[0];
669
+ }
670
+ getOwnPropertyNames(e, t = { strings: !0, numbersAsStrings: !0 }) {
671
+ this.runtime.assertOwned(e), e.value;
672
+ let r = me(t);
673
+ if (r === 0) throw new K("No options set, will return an empty array");
674
+ return d.withScope((i) => {
675
+ let s = i.manage(this.memory.newMutablePointerArray(1)), n = this.ffi.QTS_GetOwnPropertyNames(this.ctx.value, s.value.ptr, this.uint32Out.value.ptr, e.value, r);
676
+ if (n) return this.fail(this.memory.heapValueHandle(n));
677
+ let o = this.uint32Out.value.typedArray[0], u = s.value.typedArray[0], a = new Uint32Array(this.module.HEAP8.buffer, u, o), l = Array.from(a).map((f) => this.memory.heapValueHandle(f));
678
+ return this.ffi.QTS_FreeVoidPointer(this.ctx.value, u), this.success(ue(l));
679
+ });
680
+ }
681
+ getIterator(e) {
682
+ let t = this._SymbolIterator ?? (this._SymbolIterator = this.memory.manage(this.getWellKnownSymbol("iterator")));
683
+ return d.withScope((r) => {
684
+ let i = r.manage(this.getProp(e, t)), s = this.callFunction(i, e);
685
+ return s.error ? s : this.success(new pe(s.value, this));
686
+ });
687
+ }
688
+ setProp(e, t, r) {
689
+ this.runtime.assertOwned(e), this.borrowPropertyKey(t).consume((i) => this.ffi.QTS_SetProp(this.ctx.value, e.value, i.value, r.value));
690
+ }
691
+ defineProp(e, t, r) {
692
+ this.runtime.assertOwned(e), d.withScope((i) => {
693
+ let s = i.manage(this.borrowPropertyKey(t)), n = r.value || this.undefined, o = !!r.configurable, u = !!r.enumerable, a = !!r.value, l = r.get ? i.manage(this.newFunction(r.get.name, r.get)) : this.undefined, f = r.set ? i.manage(this.newFunction(r.set.name, r.set)) : this.undefined;
694
+ this.ffi.QTS_DefineProp(this.ctx.value, e.value, s.value, n.value, l.value, f.value, o, u, a);
695
+ });
696
+ }
697
+ callFunction(e, t, ...r) {
698
+ this.runtime.assertOwned(e);
699
+ let i, s = r[0];
700
+ s === void 0 || Array.isArray(s) ? i = s ?? [] : i = r;
701
+ let n = this.memory.toPointerArray(i).consume((u) => this.ffi.QTS_Call(this.ctx.value, e.value, t.value, i.length, u.value)), o = this.ffi.QTS_ResolveException(this.ctx.value, n);
702
+ return o ? (this.ffi.QTS_FreeValuePointer(this.ctx.value, n), this.fail(this.memory.heapValueHandle(o))) : this.success(this.memory.heapValueHandle(n));
703
+ }
704
+ callMethod(e, t, r = []) {
705
+ return this.getProp(e, t).consume((i) => this.callFunction(i, e, r));
706
+ }
707
+ evalCode(e, t = "eval.js", r) {
708
+ let i = r === void 0 ? 1 : 0, s = de(r), n = this.memory.newHeapCharPointer(e).consume((u) => this.ffi.QTS_Eval(this.ctx.value, u.value.ptr, u.value.strlen, t, i, s)), o = this.ffi.QTS_ResolveException(this.ctx.value, n);
709
+ return o ? (this.ffi.QTS_FreeValuePointer(this.ctx.value, n), this.fail(this.memory.heapValueHandle(o))) : this.success(this.memory.heapValueHandle(n));
710
+ }
711
+ throw(e) {
712
+ return this.errorToHandle(e).consume((t) => this.ffi.QTS_Throw(this.ctx.value, t.value));
713
+ }
714
+ borrowPropertyKey(e) {
715
+ return typeof e == "number" ? this.newNumber(e) : typeof e == "string" ? this.newString(e) : new y(e.value, this.runtime);
716
+ }
717
+ getMemory(e) {
718
+ if (e === this.rt.value) return this.memory;
719
+ throw new Error("Private API. Cannot get memory from a different runtime");
720
+ }
721
+ dump(e) {
722
+ this.runtime.assertOwned(e);
723
+ let t = this.typeof(e);
724
+ if (t === "string") return this.getString(e);
725
+ if (t === "number") return this.getNumber(e);
726
+ if (t === "bigint") return this.getBigInt(e);
727
+ if (t === "undefined") return;
728
+ if (t === "symbol") return this.getSymbol(e);
729
+ let r = this.getPromiseState(e);
730
+ if (r.type === "fulfilled" && !r.notAPromise) return e.dispose(), { type: r.type, value: r.value.consume(this.dump) };
731
+ if (r.type === "pending") return e.dispose(), { type: r.type };
732
+ if (r.type === "rejected") return e.dispose(), { type: r.type, error: r.error.consume(this.dump) };
733
+ let i = this.memory.consumeJSCharPointer(this.ffi.QTS_Dump(this.ctx.value, e.value));
734
+ try {
735
+ return JSON.parse(i);
736
+ } catch {
737
+ return i;
738
+ }
739
+ }
740
+ unwrapResult(e) {
741
+ if (e.error) {
742
+ let t = "context" in e.error ? e.error.context : this, r = e.error.consume((i) => this.dump(i));
743
+ if (r && typeof r == "object" && typeof r.message == "string") {
744
+ let { message: i, name: s, stack: n, ...o } = r, u = new H(r, t);
745
+ typeof s == "string" && (u.name = r.name), u.message = i;
746
+ let a = u.stack;
747
+ throw typeof n == "string" && (u.stack = `${s}: ${i}
748
+ ${r.stack}Host: ${a}`), Object.assign(u, o), u;
749
+ }
750
+ throw new H(r);
751
+ }
752
+ return e.value;
753
+ }
754
+ [Symbol.for("nodejs.util.inspect.custom")]() {
755
+ return this.alive ? `${this.constructor.name} { ctx: ${this.ctx.value} rt: ${this.rt.value} }` : `${this.constructor.name} { disposed }`;
756
+ }
757
+ getFunction(e) {
758
+ let t = this.runtime.hostRefs.get(e);
759
+ if (typeof t != "function") throw new Error(`Host reference ${e} is not a function`);
760
+ return t;
761
+ }
762
+ errorToHandle(e) {
763
+ return e instanceof c ? e : this.newError(e);
764
+ }
765
+ encodeBinaryJSON(e) {
766
+ let t = this.ffi.QTS_bjson_encode(this.ctx.value, e.value);
767
+ return this.memory.heapValueHandle(t);
768
+ }
769
+ decodeBinaryJSON(e) {
770
+ let t = this.ffi.QTS_bjson_decode(this.ctx.value, e.value);
771
+ return this.memory.heapValueHandle(t);
772
+ }
773
+ success(e) {
774
+ return S.success(e);
775
+ }
776
+ fail(e) {
777
+ return S.fail(e, (t) => this.unwrapResult(t));
778
+ }
779
+ }, Se = class extends m {
780
+ constructor(e) {
781
+ var t;
782
+ super(), this.scope = new d(), this.contextMap = /* @__PURE__ */ new Map(), this.hostRefs = new ye(), this._debugMode = !1, this.cToHostCallbacks = { freeHostRef: (r, i) => {
783
+ if (r !== this.rt.value) throw new Error("Runtime pointer mismatch");
784
+ this.hostRefs.delete(i);
785
+ }, shouldInterrupt: (r) => {
786
+ if (r !== this.rt.value) throw new Error("QuickJSContext instance received C -> JS interrupt with mismatched rt");
787
+ let i = this.interruptHandler;
788
+ if (!i) throw new Error("QuickJSContext had no interrupt handler");
789
+ return i(this) ? 1 : 0;
790
+ }, loadModuleSource: L(this, function* (r, i, s, n) {
791
+ let o = this.moduleLoader;
792
+ if (!o) throw new Error("Runtime has no module loader");
793
+ if (i !== this.rt.value) throw new Error("Runtime pointer mismatch");
794
+ let u = this.contextMap.get(s) ?? this.newContext({ contextPointer: s });
795
+ try {
796
+ let a = yield* r(o(n, u));
797
+ if (typeof a == "object" && "error" in a && a.error) throw this.debugLog("cToHostLoadModule: loader returned error", a.error), a.error;
798
+ let l = typeof a == "string" ? a : "value" in a ? a.value : a;
799
+ return this.memory.newHeapCharPointer(l).value.ptr;
800
+ } catch (a) {
801
+ return this.debugLog("cToHostLoadModule: caught error", a), u.throw(a), 0;
802
+ }
803
+ }), normalizeModule: L(this, function* (r, i, s, n, o) {
804
+ let u = this.moduleNormalizer;
805
+ if (!u) throw new Error("Runtime has no module normalizer");
806
+ if (i !== this.rt.value) throw new Error("Runtime pointer mismatch");
807
+ let a = this.contextMap.get(s) ?? this.newContext({ contextPointer: s });
808
+ try {
809
+ let l = yield* r(u(n, o, a));
810
+ if (typeof l == "object" && "error" in l && l.error) throw this.debugLog("cToHostNormalizeModule: normalizer returned error", l.error), l.error;
811
+ let f = typeof l == "string" ? l : l.value;
812
+ return a.getMemory(this.rt.value).newHeapCharPointer(f).value.ptr;
813
+ } catch (l) {
814
+ return this.debugLog("normalizeModule: caught error", l), a.throw(l), 0;
815
+ }
816
+ }) }, (t = e.ownedLifetimes) == null || t.forEach((r) => this.scope.manage(r)), this.module = e.module, this.memory = new X(this.module), this.ffi = e.ffi, this.rt = e.rt, this.callbacks = e.callbacks, this.scope.manage(this.rt), this.callbacks.setRuntimeCallbacks(this.rt.value, this.cToHostCallbacks), this.executePendingJobs = this.executePendingJobs.bind(this);
817
+ }
818
+ get alive() {
819
+ return this.scope.alive;
820
+ }
821
+ dispose() {
822
+ return this.scope.dispose();
823
+ }
824
+ newContext(e = {}) {
825
+ let t = ce(e.intrinsics), r = new c(e.contextPointer || this.ffi.QTS_NewContext(this.rt.value, t), void 0, (s) => {
826
+ this.contextMap.delete(s), this.callbacks.deleteContext(s), this.ffi.QTS_FreeContext(s);
827
+ }), i = new we({ module: this.module, ctx: r, ffi: this.ffi, rt: this.rt, ownedLifetimes: e.ownedLifetimes, runtime: this, callbacks: this.callbacks });
828
+ return this.contextMap.set(r.value, i), i;
829
+ }
830
+ setModuleLoader(e, t) {
831
+ this.moduleLoader = e, this.moduleNormalizer = t, this.ffi.QTS_RuntimeEnableModuleLoader(this.rt.value, this.moduleNormalizer ? 1 : 0);
832
+ }
833
+ removeModuleLoader() {
834
+ this.moduleLoader = void 0, this.ffi.QTS_RuntimeDisableModuleLoader(this.rt.value);
835
+ }
836
+ hasPendingJob() {
837
+ return !!this.ffi.QTS_IsJobPending(this.rt.value);
838
+ }
839
+ setInterruptHandler(e) {
840
+ let t = this.interruptHandler;
841
+ this.interruptHandler = e, t || this.ffi.QTS_RuntimeEnableInterruptHandler(this.rt.value);
842
+ }
843
+ removeInterruptHandler() {
844
+ this.interruptHandler && (this.ffi.QTS_RuntimeDisableInterruptHandler(this.rt.value), this.interruptHandler = void 0);
845
+ }
846
+ executePendingJobs(e = -1) {
847
+ let t = this.memory.newMutablePointerArray(1), r = this.ffi.QTS_ExecutePendingJob(this.rt.value, e ?? -1, t.value.ptr), i = t.value.typedArray[0];
848
+ if (t.dispose(), i === 0) return this.ffi.QTS_FreeValuePointerRuntime(this.rt.value, r), S.success(0);
849
+ let s = this.contextMap.get(i) ?? this.newContext({ contextPointer: i }), n = s.getMemory(this.rt.value).heapValueHandle(r);
850
+ if (s.typeof(n) === "number") {
851
+ let o = s.getNumber(n);
852
+ return n.dispose(), S.success(o);
853
+ } else {
854
+ let o = Object.assign(n, { context: s });
855
+ return S.fail(o, (u) => s.unwrapResult(u));
856
+ }
857
+ }
858
+ setMemoryLimit(e) {
859
+ if (e < 0 && e !== -1) throw new Error("Cannot set memory limit to negative number. To unset, pass -1");
860
+ this.ffi.QTS_RuntimeSetMemoryLimit(this.rt.value, e);
861
+ }
862
+ computeMemoryUsage() {
863
+ let e = this.getSystemContext().getMemory(this.rt.value);
864
+ return e.heapValueHandle(this.ffi.QTS_RuntimeComputeMemoryUsage(this.rt.value, e.ctx.value));
865
+ }
866
+ dumpMemoryUsage() {
867
+ return this.memory.consumeHeapCharPointer(this.ffi.QTS_RuntimeDumpMemoryUsage(this.rt.value));
868
+ }
869
+ setMaxStackSize(e) {
870
+ if (e < 0) throw new Error("Cannot set memory limit to negative number. To unset, pass 0.");
871
+ this.ffi.QTS_RuntimeSetMaxStackSize(this.rt.value, e);
872
+ }
873
+ assertOwned(e) {
874
+ if (e.owner && e.owner.rt !== this.rt) throw new G(`Handle is not owned by this runtime: ${e.owner.rt.value} != ${this.rt.value}`);
875
+ }
876
+ setDebugMode(e) {
877
+ this._debugMode = e, this.ffi.DEBUG && this.rt.alive && this.ffi.QTS_SetDebugLogEnabled(this.rt.value, e ? 1 : 0);
878
+ }
879
+ isDebugMode() {
880
+ return this._debugMode;
881
+ }
882
+ debugLog(...e) {
883
+ this._debugMode && console.log("quickjs-emscripten:", ...e);
884
+ }
885
+ [Symbol.for("nodejs.util.inspect.custom")]() {
886
+ return this.alive ? `${this.constructor.name} { rt: ${this.rt.value} }` : `${this.constructor.name} { disposed }`;
887
+ }
888
+ getSystemContext() {
889
+ return this.context || (this.context = this.scope.manage(this.newContext())), this.context;
890
+ }
891
+ }, ge = class {
892
+ constructor(e) {
893
+ this.freeHostRef = e.freeHostRef, this.callFunction = e.callFunction, this.shouldInterrupt = e.shouldInterrupt, this.loadModuleSource = e.loadModuleSource, this.normalizeModule = e.normalizeModule;
894
+ }
895
+ }, _e = class {
896
+ constructor(e) {
897
+ this.contextCallbacks = /* @__PURE__ */ new Map(), this.runtimeCallbacks = /* @__PURE__ */ new Map(), this.suspendedCount = 0, this.cToHostCallbacks = new ge({ freeHostRef: (t, r, i) => {
898
+ let s = this.runtimeCallbacks.get(r);
899
+ if (!s) throw new Error(`QuickJSRuntime(rt = ${r}) not found when trying to free HostRef(id = ${i})`);
900
+ s.freeHostRef(r, i);
901
+ }, callFunction: (t, r, i, s, n, o) => this.handleAsyncify(t, () => {
902
+ try {
903
+ let u = this.contextCallbacks.get(r);
904
+ if (!u) throw new Error(`QuickJSContext(ctx = ${r}) not found for C function call "${o}"`);
905
+ return u.callFunction(r, i, s, n, o);
906
+ } catch (u) {
907
+ return console.error("[C to host error: returning null]", u), 0;
908
+ }
909
+ }), shouldInterrupt: (t, r) => this.handleAsyncify(t, () => {
910
+ try {
911
+ let i = this.runtimeCallbacks.get(r);
912
+ if (!i) throw new Error(`QuickJSRuntime(rt = ${r}) not found for C interrupt`);
913
+ return i.shouldInterrupt(r);
914
+ } catch (i) {
915
+ return console.error("[C to host interrupt: returning error]", i), 1;
916
+ }
917
+ }), loadModuleSource: (t, r, i, s) => this.handleAsyncify(t, () => {
918
+ try {
919
+ let n = this.runtimeCallbacks.get(r);
920
+ if (!n) throw new Error(`QuickJSRuntime(rt = ${r}) not found for C module loader`);
921
+ let o = n.loadModuleSource;
922
+ if (!o) throw new Error(`QuickJSRuntime(rt = ${r}) does not support module loading`);
923
+ return o(r, i, s);
924
+ } catch (n) {
925
+ return console.error("[C to host module loader error: returning null]", n), 0;
926
+ }
927
+ }), normalizeModule: (t, r, i, s, n) => this.handleAsyncify(t, () => {
928
+ try {
929
+ let o = this.runtimeCallbacks.get(r);
930
+ if (!o) throw new Error(`QuickJSRuntime(rt = ${r}) not found for C module loader`);
931
+ let u = o.normalizeModule;
932
+ if (!u) throw new Error(`QuickJSRuntime(rt = ${r}) does not support module loading`);
933
+ return u(r, i, s, n);
934
+ } catch (o) {
935
+ return console.error("[C to host module loader error: returning null]", o), 0;
936
+ }
937
+ }) }), this.module = e, this.module.callbacks = this.cToHostCallbacks;
938
+ }
939
+ setRuntimeCallbacks(e, t) {
940
+ this.runtimeCallbacks.set(e, t);
941
+ }
942
+ deleteRuntime(e) {
943
+ this.runtimeCallbacks.delete(e);
944
+ }
945
+ setContextCallbacks(e, t) {
946
+ this.contextCallbacks.set(e, t);
947
+ }
948
+ deleteContext(e) {
949
+ this.contextCallbacks.delete(e);
950
+ }
951
+ handleAsyncify(e, t) {
952
+ if (e) return e.handleSleep((i) => {
953
+ try {
954
+ let s = t();
955
+ if (!(s instanceof Promise)) {
956
+ b("asyncify.handleSleep: not suspending:", s), i(s);
957
+ return;
958
+ }
959
+ if (this.suspended) throw new U(`Already suspended at: ${this.suspended.stack}
960
+ Attempted to suspend at:`);
961
+ this.suspended = new D(`(${this.suspendedCount++})`), b("asyncify.handleSleep: suspending:", this.suspended), s.then((n) => {
962
+ this.suspended = void 0, b("asyncify.handleSleep: resolved:", n), i(n);
963
+ }, (n) => {
964
+ b("asyncify.handleSleep: rejected:", n), console.error("QuickJS: cannot handle error in suspended function", n), this.suspended = void 0;
965
+ });
966
+ } catch (s) {
967
+ throw this.suspended = void 0, s;
968
+ }
969
+ });
970
+ let r = t();
971
+ if (r instanceof Promise) throw new Error("Promise return value not supported in non-asyncify context.");
972
+ return r;
973
+ }
974
+ };
975
+ function be(e, t) {
976
+ t.interruptHandler && e.setInterruptHandler(t.interruptHandler), t.maxStackSizeBytes !== void 0 && e.setMaxStackSize(t.maxStackSizeBytes), t.memoryLimitBytes !== void 0 && e.setMemoryLimit(t.memoryLimitBytes);
977
+ }
978
+ function xe(e, t) {
979
+ t.moduleLoader && e.setModuleLoader(t.moduleLoader), t.shouldInterrupt && e.setInterruptHandler(t.shouldInterrupt), t.memoryLimitBytes !== void 0 && e.setMemoryLimit(t.memoryLimitBytes), t.maxStackSizeBytes !== void 0 && e.setMaxStackSize(t.maxStackSizeBytes);
980
+ }
981
+ var Pe = class {
982
+ constructor(e, t) {
983
+ this.module = e, this.ffi = t, this.callbacks = new _e(e);
984
+ }
985
+ newRuntime(e = {}) {
986
+ let t = new c(this.ffi.QTS_NewRuntime(), void 0, (i) => {
987
+ this.ffi.QTS_FreeRuntime(i), this.callbacks.deleteRuntime(i);
988
+ }), r = new Se({ module: this.module, callbacks: this.callbacks, ffi: this.ffi, rt: t });
989
+ return be(r, e), e.moduleLoader && r.setModuleLoader(e.moduleLoader), r;
990
+ }
991
+ newContext(e = {}) {
992
+ let t = this.newRuntime(), r = t.newContext({ ...e, ownedLifetimes: fe(t, e.ownedLifetimes) });
993
+ return t.context = r, r;
994
+ }
995
+ evalCode(e, t = {}) {
996
+ return d.withScope((r) => {
997
+ let i = r.manage(this.newContext());
998
+ xe(i.runtime, t);
999
+ let s = i.evalCode(e, "eval.js");
1000
+ if (t.memoryLimitBytes !== void 0 && i.runtime.setMemoryLimit(-1), s.error) throw i.dump(r.manage(s.error));
1001
+ return i.dump(r.manage(s.value));
1002
+ });
1003
+ }
1004
+ getWasmMemory() {
1005
+ var t, r, i, s;
1006
+ let e = (s = (i = (r = (t = this.module).quickjsEmscriptenInit) == null ? void 0 : r.call(t, () => {
1007
+ })) == null ? void 0 : i.getWasmMemory) == null ? void 0 : s.call(i);
1008
+ if (!e) throw new Error("Variant does not support getting WebAssembly.Memory");
1009
+ return e;
1010
+ }
1011
+ getFFI() {
1012
+ return this.ffi;
1013
+ }
1014
+ };
1015
+ export {
1016
+ _e as QuickJSModuleCallbacks,
1017
+ Pe as QuickJSWASMModule,
1018
+ be as applyBaseRuntimeOptions,
1019
+ xe as applyModuleEvalRuntimeOptions
1020
+ };