@emnapi/runtime 0.31.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.
@@ -0,0 +1,1133 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+
3
+ class CallbackInfo {
4
+ constructor(parent, thiz, data, args, fn) {
5
+ this.parent = parent;
6
+ this.thiz = thiz;
7
+ this.data = data;
8
+ this.args = args;
9
+ this.fn = fn;
10
+ }
11
+ getNewTarget(envObject) {
12
+ const thiz = this.thiz;
13
+ if (thiz == null || thiz.constructor == null)
14
+ return 0;
15
+ return thiz instanceof this.fn ? envObject.ensureHandleId(thiz.constructor) : 0;
16
+ }
17
+ }
18
+ class CallbackInfoStack {
19
+ constructor() {
20
+ this.current = null;
21
+ }
22
+ pop() {
23
+ const current = this.current;
24
+ if (current === null)
25
+ return;
26
+ this.current = current.parent;
27
+ }
28
+ push(thiz, data, args, fn) {
29
+ const info = new CallbackInfo(this.current, thiz, data, args, fn);
30
+ this.current = info;
31
+ return info;
32
+ }
33
+ dispose() {
34
+ this.current = null;
35
+ }
36
+ }
37
+
38
+ class Store {
39
+ constructor(capacity) {
40
+ this._values = [undefined];
41
+ this._values.length = capacity;
42
+ this._size = 1;
43
+ this._freeList = [];
44
+ }
45
+ add(value) {
46
+ let id;
47
+ if (this._freeList.length) {
48
+ id = this._freeList.shift();
49
+ }
50
+ else {
51
+ id = this._size;
52
+ this._size++;
53
+ const capacity = this._values.length;
54
+ if (id >= capacity) {
55
+ this._values.length = Math.ceil(capacity * 1.5);
56
+ }
57
+ }
58
+ value.id = id;
59
+ this._values[id] = value;
60
+ }
61
+ get(id) {
62
+ return this._values[id];
63
+ }
64
+ has(id) {
65
+ return this._values[id] !== undefined;
66
+ }
67
+ remove(id) {
68
+ const value = this._values[id];
69
+ if (value) {
70
+ value.id = 0;
71
+ this._values[id] = undefined;
72
+ this._freeList.push(Number(id));
73
+ }
74
+ }
75
+ dispose() {
76
+ for (let i = 1; i < this._size; ++i) {
77
+ const value = this._values[i];
78
+ value === null || value === void 0 ? void 0 : value.dispose();
79
+ }
80
+ this._values = [undefined];
81
+ this._size = 1;
82
+ this._freeList = [];
83
+ }
84
+ }
85
+
86
+ class EnvStore extends Store {
87
+ constructor() {
88
+ super(2);
89
+ }
90
+ }
91
+
92
+ const supportNewFunction = (function () {
93
+ let f;
94
+ try {
95
+ f = new Function();
96
+ }
97
+ catch (_) {
98
+ return false;
99
+ }
100
+ return typeof f === 'function';
101
+ })();
102
+ const _global = (function () {
103
+ if (typeof globalThis !== 'undefined')
104
+ return globalThis;
105
+ let g = (function () { return this; })();
106
+ if (!g && supportNewFunction) {
107
+ try {
108
+ g = new Function('return this')();
109
+ }
110
+ catch (_) { }
111
+ }
112
+ if (!g) {
113
+ if (typeof __webpack_public_path__ === 'undefined') {
114
+ if (typeof global !== 'undefined')
115
+ return global;
116
+ }
117
+ if (typeof window !== 'undefined')
118
+ return window;
119
+ if (typeof self !== 'undefined')
120
+ return self;
121
+ }
122
+ return g;
123
+ })();
124
+ const emptyException = new Error();
125
+ class TryCatch {
126
+ constructor() {
127
+ this._exception = emptyException;
128
+ }
129
+ hasCaught() {
130
+ return this._exception !== emptyException;
131
+ }
132
+ exception() {
133
+ return this._exception;
134
+ }
135
+ setError(err) {
136
+ this._exception = err;
137
+ }
138
+ reset() {
139
+ this._exception = emptyException;
140
+ }
141
+ extractException() {
142
+ const e = this._exception;
143
+ this.reset();
144
+ return e;
145
+ }
146
+ }
147
+ const canSetFunctionName = (function () {
148
+ var _a;
149
+ try {
150
+ return Boolean((_a = Object.getOwnPropertyDescriptor(Function.prototype, 'name')) === null || _a === void 0 ? void 0 : _a.configurable);
151
+ }
152
+ catch (_) {
153
+ return false;
154
+ }
155
+ })();
156
+ const supportReflect = typeof Reflect === 'object';
157
+ const supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
158
+ const supportBigInt = typeof BigInt !== 'undefined';
159
+ function isReferenceType(v) {
160
+ return (typeof v === 'object' && v !== null) || typeof v === 'function';
161
+ }
162
+ const _setImmediate = typeof setImmediate === 'function'
163
+ ? setImmediate
164
+ : function (f) {
165
+ if (typeof f !== 'function')
166
+ return;
167
+ let channel = new MessageChannel();
168
+ channel.port1.onmessage = function () {
169
+ channel.port1.onmessage = null;
170
+ channel = undefined;
171
+ f();
172
+ };
173
+ channel.port2.postMessage(null);
174
+ };
175
+ const _require = (function () {
176
+ let nativeRequire;
177
+ if (typeof __webpack_public_path__ !== 'undefined') {
178
+ nativeRequire = (function () {
179
+ return typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined;
180
+ })();
181
+ }
182
+ else {
183
+ nativeRequire = (function () {
184
+ return typeof __webpack_public_path__ !== 'undefined' ? (typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined) : (typeof require !== 'undefined' ? require : undefined);
185
+ })();
186
+ }
187
+ return nativeRequire;
188
+ })();
189
+ const Buffer = (function () {
190
+ if (typeof _global.Buffer === 'function')
191
+ return _global.Buffer;
192
+ try {
193
+ return _require('buffer').Buffer;
194
+ }
195
+ catch (_) { }
196
+ return undefined;
197
+ })();
198
+
199
+ class Handle {
200
+ constructor(id, value) {
201
+ this.id = id;
202
+ this.value = value;
203
+ }
204
+ init(id, value) {
205
+ this.id = id;
206
+ this.value = value;
207
+ }
208
+ data(envObject) {
209
+ return envObject.getObjectBinding(this.value).data;
210
+ }
211
+ isEmpty() {
212
+ return this.id === 0;
213
+ }
214
+ isNumber() {
215
+ return !this.isEmpty() && typeof this.value === 'number';
216
+ }
217
+ isBigInt() {
218
+ return !this.isEmpty() && typeof this.value === 'bigint';
219
+ }
220
+ isString() {
221
+ return !this.isEmpty() && typeof this.value === 'string';
222
+ }
223
+ isFunction() {
224
+ return !this.isEmpty() && typeof this.value === 'function';
225
+ }
226
+ isExternal() {
227
+ return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
228
+ }
229
+ isObject() {
230
+ return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
231
+ }
232
+ isArray() {
233
+ return !this.isEmpty() && Array.isArray(this.value);
234
+ }
235
+ isArrayBuffer() {
236
+ return !this.isEmpty() && (this.value instanceof ArrayBuffer);
237
+ }
238
+ isTypedArray() {
239
+ return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
240
+ }
241
+ isBuffer() {
242
+ return !this.isEmpty() && typeof Buffer === 'function' && Buffer.isBuffer(this.value);
243
+ }
244
+ isDataView() {
245
+ return !this.isEmpty() && (this.value instanceof DataView);
246
+ }
247
+ isDate() {
248
+ return !this.isEmpty() && (this.value instanceof Date);
249
+ }
250
+ isPromise() {
251
+ return !this.isEmpty() && (this.value instanceof Promise);
252
+ }
253
+ isBoolean() {
254
+ return !this.isEmpty() && typeof this.value === 'boolean';
255
+ }
256
+ isUndefined() {
257
+ return !this.isEmpty() && this.value === undefined;
258
+ }
259
+ isSymbol() {
260
+ return !this.isEmpty() && typeof this.value === 'symbol';
261
+ }
262
+ isNull() {
263
+ return !this.isEmpty() && this.value === null;
264
+ }
265
+ dispose() {
266
+ if (this.id === 0)
267
+ return;
268
+ this.id = 0;
269
+ this.value = undefined;
270
+ }
271
+ }
272
+ class ConstHandle extends Handle {
273
+ constructor(id, value) {
274
+ super(id, value);
275
+ }
276
+ dispose() { }
277
+ }
278
+ function External() {
279
+ Object.setPrototypeOf(this, null);
280
+ }
281
+ External.prototype = null;
282
+ class HandleStore {
283
+ constructor() {
284
+ this._values = [
285
+ undefined,
286
+ HandleStore.UNDEFINED,
287
+ HandleStore.NULL,
288
+ HandleStore.FALSE,
289
+ HandleStore.TRUE,
290
+ HandleStore.GLOBAL
291
+ ];
292
+ this._next = HandleStore.MIN_ID;
293
+ }
294
+ push(value) {
295
+ let h;
296
+ const next = this._next;
297
+ const values = this._values;
298
+ if (next < values.length) {
299
+ h = values[next];
300
+ h.init(next, value);
301
+ }
302
+ else {
303
+ h = new Handle(next, value);
304
+ values[next] = h;
305
+ }
306
+ this._next++;
307
+ return h;
308
+ }
309
+ erase(start, end) {
310
+ this._next = start;
311
+ const values = this._values;
312
+ for (let i = start; i < end; ++i) {
313
+ values[i].dispose();
314
+ }
315
+ }
316
+ get(id) {
317
+ return this._values[id];
318
+ }
319
+ swap(a, b) {
320
+ const values = this._values;
321
+ const h = values[a];
322
+ values[a] = values[b];
323
+ values[a].id = Number(a);
324
+ values[b] = h;
325
+ h.id = Number(b);
326
+ }
327
+ dispose() {
328
+ this._values.length = HandleStore.MIN_ID;
329
+ this._next = HandleStore.MIN_ID;
330
+ }
331
+ }
332
+ HandleStore.UNDEFINED = new ConstHandle(1, undefined);
333
+ HandleStore.NULL = new ConstHandle(2, null);
334
+ HandleStore.FALSE = new ConstHandle(3, false);
335
+ HandleStore.TRUE = new ConstHandle(4, true);
336
+ HandleStore.GLOBAL = new ConstHandle(5, _global);
337
+ HandleStore.MIN_ID = 6;
338
+
339
+ class HandleScope {
340
+ constructor(handleStore, id, parentScope, start, end = start) {
341
+ this.handleStore = handleStore;
342
+ this.id = id;
343
+ this.parent = parentScope;
344
+ this.child = null;
345
+ if (parentScope !== null)
346
+ parentScope.child = this;
347
+ this.start = start;
348
+ this.end = end;
349
+ this._escapeCalled = false;
350
+ }
351
+ add(value) {
352
+ const h = this.handleStore.push(value);
353
+ this.end++;
354
+ return h;
355
+ }
356
+ addExternal(envObject, data) {
357
+ const value = new External();
358
+ const h = envObject.ctx.handleStore.push(value);
359
+ const binding = envObject.initObjectBinding(value);
360
+ binding.data = data;
361
+ this.end++;
362
+ return h;
363
+ }
364
+ dispose() {
365
+ if (this.start === this.end)
366
+ return;
367
+ this.handleStore.erase(this.start, this.end);
368
+ }
369
+ escape(handle) {
370
+ if (this._escapeCalled)
371
+ return null;
372
+ this._escapeCalled = true;
373
+ if (handle < this.start || handle >= this.end) {
374
+ return null;
375
+ }
376
+ this.handleStore.swap(handle, this.start);
377
+ const h = this.handleStore.get(this.start);
378
+ this.start++;
379
+ this.parent.end++;
380
+ return h;
381
+ }
382
+ escapeCalled() {
383
+ return this._escapeCalled;
384
+ }
385
+ }
386
+
387
+ class ScopeStore {
388
+ constructor() {
389
+ this._rootScope = new HandleScope(null, 0, null, 1, HandleStore.MIN_ID);
390
+ this.currentScope = this._rootScope;
391
+ }
392
+ get(id) {
393
+ id = Number(id);
394
+ let scope = this.currentScope;
395
+ while (scope !== this._rootScope) {
396
+ if (scope.id === id) {
397
+ return scope;
398
+ }
399
+ scope = scope.parent;
400
+ }
401
+ return undefined;
402
+ }
403
+ openScope(envObject) {
404
+ const currentScope = this.currentScope;
405
+ let scope = currentScope.child;
406
+ if (scope !== null) {
407
+ scope.start = scope.end = currentScope.end;
408
+ scope._escapeCalled = false;
409
+ }
410
+ else {
411
+ scope = new HandleScope(envObject.ctx.handleStore, currentScope.id + 1, currentScope, currentScope.end);
412
+ }
413
+ this.currentScope = scope;
414
+ envObject.openHandleScopes++;
415
+ return scope;
416
+ }
417
+ closeScope(envObject) {
418
+ if (envObject.openHandleScopes === 0)
419
+ return;
420
+ const scope = this.currentScope;
421
+ this.currentScope = scope.parent;
422
+ scope.dispose();
423
+ envObject.openHandleScopes--;
424
+ }
425
+ dispose() {
426
+ let scope = this.currentScope;
427
+ while (scope !== null) {
428
+ scope.handleStore = null;
429
+ scope.id = 0;
430
+ scope.parent = null;
431
+ scope.start = HandleStore.MIN_ID;
432
+ scope.end = HandleStore.MIN_ID;
433
+ scope._escapeCalled = false;
434
+ const child = scope.child;
435
+ scope.child = null;
436
+ scope = child;
437
+ }
438
+ this.currentScope = null;
439
+ }
440
+ }
441
+
442
+ class RefStore extends Store {
443
+ constructor() {
444
+ super(8);
445
+ }
446
+ }
447
+
448
+ class DeferredStore extends Store {
449
+ constructor() {
450
+ super(8);
451
+ }
452
+ }
453
+
454
+ class RefTracker {
455
+ constructor() {
456
+ this._next = null;
457
+ this._prev = null;
458
+ }
459
+ finalize() { }
460
+ link(list) {
461
+ this._prev = list;
462
+ this._next = list._next;
463
+ if (this._next !== null) {
464
+ this._next._prev = this;
465
+ }
466
+ list._next = this;
467
+ }
468
+ unlink() {
469
+ if (this._prev !== null) {
470
+ this._prev._next = this._next;
471
+ }
472
+ if (this._next !== null) {
473
+ this._next._prev = this._prev;
474
+ }
475
+ this._prev = null;
476
+ this._next = null;
477
+ }
478
+ static finalizeAll(list) {
479
+ while (list._next !== null) {
480
+ list._next.finalize();
481
+ }
482
+ }
483
+ }
484
+
485
+ class Finalizer {
486
+ constructor(envObject, _finalizeCallback = 0, _finalizeData = 0, _finalizeHint = 0) {
487
+ this.envObject = envObject;
488
+ this._finalizeCallback = _finalizeCallback;
489
+ this._finalizeData = _finalizeData;
490
+ this._finalizeHint = _finalizeHint;
491
+ }
492
+ callback() { return this._finalizeCallback; }
493
+ data() { return this._finalizeData; }
494
+ hint() { return this._finalizeHint; }
495
+ resetFinalizer() {
496
+ this._finalizeCallback = 0;
497
+ this._finalizeData = 0;
498
+ this._finalizeHint = 0;
499
+ }
500
+ dispose() {
501
+ this.envObject = undefined;
502
+ }
503
+ }
504
+
505
+ class RefBase extends Finalizer {
506
+ constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
507
+ super(envObject, finalize_callback, finalize_data, finalize_hint);
508
+ this._next = null;
509
+ this._prev = null;
510
+ this._refcount = initial_refcount;
511
+ this._ownership = ownership;
512
+ this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
513
+ }
514
+ static finalizeAll(list) {
515
+ RefTracker.finalizeAll(list);
516
+ }
517
+ link(list) {
518
+ RefTracker.prototype.link.call(this, list);
519
+ }
520
+ unlink() {
521
+ RefTracker.prototype.unlink.call(this);
522
+ }
523
+ dispose() {
524
+ this.unlink();
525
+ this.envObject.dequeueFinalizer(this);
526
+ super.dispose();
527
+ }
528
+ data() {
529
+ return this._finalizeData;
530
+ }
531
+ ref() {
532
+ return ++this._refcount;
533
+ }
534
+ unref() {
535
+ if (this._refcount === 0) {
536
+ return 0;
537
+ }
538
+ return --this._refcount;
539
+ }
540
+ refCount() {
541
+ return this._refcount;
542
+ }
543
+ ownership() {
544
+ return this._ownership;
545
+ }
546
+ finalize() {
547
+ const ownership = this._ownership;
548
+ const finalize_callback = this._finalizeCallback;
549
+ const finalize_data = this._finalizeData;
550
+ const finalize_hint = this._finalizeHint;
551
+ this.resetFinalizer();
552
+ this.unlink();
553
+ let error;
554
+ let caught = false;
555
+ if (finalize_callback) {
556
+ const fini = Number(finalize_callback);
557
+ try {
558
+ this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
559
+ }
560
+ catch (err) {
561
+ caught = true;
562
+ error = err;
563
+ }
564
+ }
565
+ if (ownership === 0) {
566
+ this.dispose();
567
+ }
568
+ if (caught) {
569
+ throw error;
570
+ }
571
+ }
572
+ }
573
+
574
+ class Env {
575
+ constructor(ctx, makeDynCall_vppp, makeDynCall_vp) {
576
+ this.ctx = ctx;
577
+ this.makeDynCall_vppp = makeDynCall_vppp;
578
+ this.makeDynCall_vp = makeDynCall_vp;
579
+ this.openHandleScopes = 0;
580
+ this.instanceData = null;
581
+ this.tryCatch = new TryCatch();
582
+ this.refs = 1;
583
+ this.reflist = new RefTracker();
584
+ this.finalizing_reflist = new RefTracker();
585
+ this.destructing = false;
586
+ this.finalizationScheduled = false;
587
+ this.pendingFinalizers = [];
588
+ this.lastError = {
589
+ errorCode: 0,
590
+ engineErrorCode: 0,
591
+ engineReserved: 0
592
+ };
593
+ this._bindingMap = new WeakMap();
594
+ this.id = 0;
595
+ }
596
+ static create(ctx, makeDynCall_vppp, makeDynCall_vp) {
597
+ const env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
598
+ ctx.envStore.add(env);
599
+ ctx.addCleanupHook(env, () => { env.unref(); }, 0);
600
+ return env;
601
+ }
602
+ ref() {
603
+ this.refs++;
604
+ }
605
+ unref() {
606
+ this.refs--;
607
+ if (this.refs === 0) {
608
+ this.dispose();
609
+ }
610
+ }
611
+ ensureHandle(value) {
612
+ return this.ctx.ensureHandle(value);
613
+ }
614
+ ensureHandleId(value) {
615
+ return this.ensureHandle(value).id;
616
+ }
617
+ clearLastError() {
618
+ const lastError = this.lastError;
619
+ lastError.errorCode = 0;
620
+ lastError.engineErrorCode = 0;
621
+ lastError.engineReserved = 0;
622
+ return 0;
623
+ }
624
+ setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
625
+ const lastError = this.lastError;
626
+ lastError.errorCode = error_code;
627
+ lastError.engineErrorCode = engine_error_code;
628
+ lastError.engineReserved = engine_reserved;
629
+ return error_code;
630
+ }
631
+ getReturnStatus() {
632
+ return !this.tryCatch.hasCaught() ? 0 : this.setLastError(10);
633
+ }
634
+ callIntoModule(fn) {
635
+ this.clearLastError();
636
+ const r = fn(this);
637
+ if (this.tryCatch.hasCaught()) {
638
+ const err = this.tryCatch.extractException();
639
+ throw err;
640
+ }
641
+ return r;
642
+ }
643
+ callFinalizer(cb, data, hint) {
644
+ const f = this.makeDynCall_vppp(cb);
645
+ const env = this.id;
646
+ const scope = this.ctx.openScope(this);
647
+ try {
648
+ this.callIntoModule(() => { f(env, data, hint); });
649
+ }
650
+ finally {
651
+ this.ctx.closeScope(this, scope);
652
+ }
653
+ }
654
+ enqueueFinalizer(finalizer) {
655
+ if (this.pendingFinalizers.indexOf(finalizer) === -1) {
656
+ this.pendingFinalizers.push(finalizer);
657
+ }
658
+ if (!this.finalizationScheduled && !this.destructing) {
659
+ this.finalizationScheduled = true;
660
+ this.ref();
661
+ _setImmediate(() => {
662
+ this.finalizationScheduled = false;
663
+ this.unref();
664
+ this.drainFinalizerQueue();
665
+ });
666
+ }
667
+ }
668
+ dequeueFinalizer(finalizer) {
669
+ const index = this.pendingFinalizers.indexOf(finalizer);
670
+ if (index !== -1) {
671
+ this.pendingFinalizers.splice(index, 1);
672
+ }
673
+ }
674
+ drainFinalizerQueue() {
675
+ while (this.pendingFinalizers.length > 0) {
676
+ const refTracker = this.pendingFinalizers.shift();
677
+ refTracker.finalize();
678
+ }
679
+ }
680
+ dispose() {
681
+ if (this.id === 0)
682
+ return;
683
+ this.destructing = true;
684
+ this.drainFinalizerQueue();
685
+ RefBase.finalizeAll(this.finalizing_reflist);
686
+ RefBase.finalizeAll(this.reflist);
687
+ this.tryCatch.extractException();
688
+ this.ctx.envStore.remove(this.id);
689
+ this.id = 0;
690
+ }
691
+ initObjectBinding(value) {
692
+ const binding = {
693
+ wrapped: 0,
694
+ tag: null,
695
+ data: 0
696
+ };
697
+ this._bindingMap.set(value, binding);
698
+ return binding;
699
+ }
700
+ getObjectBinding(value) {
701
+ if (this._bindingMap.has(value)) {
702
+ return this._bindingMap.get(value);
703
+ }
704
+ return this.initObjectBinding(value);
705
+ }
706
+ setInstanceData(data, finalize_cb, finalize_hint) {
707
+ if (this.instanceData) {
708
+ this.instanceData.dispose();
709
+ }
710
+ this.instanceData = new RefBase(this, 0, 0, finalize_cb, data, finalize_hint);
711
+ }
712
+ getInstanceData() {
713
+ return this.instanceData ? this.instanceData.data() : 0;
714
+ }
715
+ }
716
+
717
+ class EmnapiError extends Error {
718
+ constructor(message) {
719
+ super(message);
720
+ const ErrorConstructor = new.target;
721
+ const proto = ErrorConstructor.prototype;
722
+ if (!(this instanceof EmnapiError)) {
723
+ const setPrototypeOf = Object.setPrototypeOf;
724
+ if (typeof setPrototypeOf === 'function') {
725
+ setPrototypeOf.call(Object, this, proto);
726
+ }
727
+ else {
728
+ this.__proto__ = proto;
729
+ }
730
+ if (typeof Error.captureStackTrace === 'function') {
731
+ Error.captureStackTrace(this, ErrorConstructor);
732
+ }
733
+ }
734
+ }
735
+ }
736
+ Object.defineProperty(EmnapiError.prototype, 'name', {
737
+ configurable: true,
738
+ writable: true,
739
+ value: 'EmnapiError'
740
+ });
741
+ class NotSupportWeakRefError extends EmnapiError {
742
+ constructor(api, message) {
743
+ super(`${api}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${message ? ` ${message}` : ''}`);
744
+ }
745
+ }
746
+ Object.defineProperty(NotSupportWeakRefError.prototype, 'name', {
747
+ configurable: true,
748
+ writable: true,
749
+ value: 'NotSupportWeakRefError'
750
+ });
751
+ class NotSupportBigIntError extends EmnapiError {
752
+ constructor(api, message) {
753
+ super(`${api}: The current runtime does not support "BigInt".${message ? ` ${message}` : ''}`);
754
+ }
755
+ }
756
+ Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
757
+ configurable: true,
758
+ writable: true,
759
+ value: 'NotSupportBigIntError'
760
+ });
761
+
762
+ class Persistent {
763
+ constructor(value) {
764
+ this._ref = value;
765
+ }
766
+ setWeak(param, callback) {
767
+ if (this._ref === null)
768
+ return;
769
+ if (!supportFinalizer)
770
+ return;
771
+ if (this._ref instanceof WeakRef)
772
+ return;
773
+ this._param = param;
774
+ this._callback = callback;
775
+ Persistent._registry.register(this._ref, this, this);
776
+ this._ref = new WeakRef(this._ref);
777
+ }
778
+ clearWeak() {
779
+ if (this._ref === null)
780
+ return;
781
+ if (!supportFinalizer)
782
+ return;
783
+ if (this._ref instanceof WeakRef) {
784
+ try {
785
+ Persistent._registry.unregister(this);
786
+ }
787
+ catch (_) { }
788
+ this._param = undefined;
789
+ this._callback = undefined;
790
+ this._ref = this._ref.deref();
791
+ }
792
+ }
793
+ reset(other) {
794
+ if (supportFinalizer) {
795
+ try {
796
+ Persistent._registry.unregister(this);
797
+ }
798
+ catch (_) { }
799
+ }
800
+ this._param = undefined;
801
+ this._callback = undefined;
802
+ if (other) {
803
+ this._ref = other;
804
+ }
805
+ else {
806
+ this._ref = null;
807
+ }
808
+ }
809
+ isEmpty() {
810
+ return this._ref === null;
811
+ }
812
+ deref() {
813
+ var _a;
814
+ if (!supportFinalizer) {
815
+ return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
816
+ }
817
+ if (this._ref === null)
818
+ return undefined;
819
+ if (this._ref instanceof WeakRef) {
820
+ return this._ref.deref();
821
+ }
822
+ return this._ref;
823
+ }
824
+ }
825
+ Persistent._registry = supportFinalizer
826
+ ? new FinalizationRegistry((value) => {
827
+ value._ref = null;
828
+ const callback = value._callback;
829
+ const param = value._param;
830
+ value._callback = undefined;
831
+ value._param = undefined;
832
+ if (typeof callback === 'function') {
833
+ callback(param);
834
+ }
835
+ })
836
+ : undefined;
837
+
838
+ function weakCallback(ref) {
839
+ ref.persistent.reset();
840
+ ref.envObject.enqueueFinalizer(ref);
841
+ }
842
+ class Reference extends RefBase {
843
+ constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
844
+ super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
845
+ this.id = 0;
846
+ }
847
+ static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
848
+ const handle = envObject.ctx.handleStore.get(handle_id);
849
+ if (!isReferenceType(handle.value)) {
850
+ throw new TypeError('Invalid reference value');
851
+ }
852
+ const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
853
+ envObject.ctx.refStore.add(ref);
854
+ ref.persistent = new Persistent(handle.value);
855
+ if (initialRefcount === 0) {
856
+ ref._setWeak();
857
+ }
858
+ return ref;
859
+ }
860
+ ref() {
861
+ if (this.persistent.isEmpty()) {
862
+ return 0;
863
+ }
864
+ const count = super.ref();
865
+ if (count === 1) {
866
+ const obj = this.persistent.deref();
867
+ if (obj) {
868
+ this.persistent.clearWeak();
869
+ }
870
+ }
871
+ return count;
872
+ }
873
+ unref() {
874
+ if (this.persistent.isEmpty()) {
875
+ return 0;
876
+ }
877
+ const oldRefcount = this.refCount();
878
+ const refcount = super.unref();
879
+ if (oldRefcount === 1 && refcount === 0) {
880
+ const obj = this.persistent.deref();
881
+ if (obj) {
882
+ this._setWeak();
883
+ }
884
+ }
885
+ return refcount;
886
+ }
887
+ get() {
888
+ const obj = this.persistent.deref();
889
+ if (obj) {
890
+ const handle = this.envObject.ensureHandle(obj);
891
+ return handle.id;
892
+ }
893
+ return 0;
894
+ }
895
+ _setWeak() {
896
+ this.persistent.setWeak(this, weakCallback);
897
+ }
898
+ finalize() {
899
+ this.persistent.reset();
900
+ super.finalize();
901
+ }
902
+ dispose() {
903
+ if (this.id === 0)
904
+ return;
905
+ this.persistent.reset();
906
+ this.envObject.ctx.refStore.remove(this.id);
907
+ super.dispose();
908
+ this.id = 0;
909
+ }
910
+ }
911
+
912
+ class Deferred {
913
+ constructor(ctx, value) {
914
+ this.id = 0;
915
+ this.ctx = ctx;
916
+ this.value = value;
917
+ }
918
+ static create(ctx, value) {
919
+ const deferred = new Deferred(ctx, value);
920
+ ctx.deferredStore.add(deferred);
921
+ return deferred;
922
+ }
923
+ resolve(value) {
924
+ this.value.resolve(value);
925
+ this.dispose();
926
+ }
927
+ reject(reason) {
928
+ this.value.reject(reason);
929
+ this.dispose();
930
+ }
931
+ dispose() {
932
+ this.ctx.deferredStore.remove(this.id);
933
+ this.id = 0;
934
+ this.value = null;
935
+ this.ctx = null;
936
+ }
937
+ }
938
+
939
+ class CleanupHookCallback {
940
+ constructor(envObject, fn, arg, order) {
941
+ this.envObject = envObject;
942
+ this.fn = fn;
943
+ this.arg = arg;
944
+ this.order = order;
945
+ }
946
+ }
947
+ class CleanupQueue {
948
+ constructor() {
949
+ this._cleanupHooks = [];
950
+ this._cleanupHookCounter = 0;
951
+ }
952
+ empty() {
953
+ return this._cleanupHooks.length === 0;
954
+ }
955
+ add(envObject, fn, arg) {
956
+ if (this._cleanupHooks.filter((hook) => (hook.envObject === envObject && hook.fn === fn && hook.arg === arg)).length > 0) {
957
+ throw new Error('Can not add same fn and arg twice');
958
+ }
959
+ this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg, this._cleanupHookCounter++));
960
+ }
961
+ remove(envObject, fn, arg) {
962
+ for (let i = 0; i < this._cleanupHooks.length; ++i) {
963
+ const hook = this._cleanupHooks[i];
964
+ if (hook.envObject === envObject && hook.fn === fn && hook.arg === arg) {
965
+ this._cleanupHooks.splice(i, 1);
966
+ return;
967
+ }
968
+ }
969
+ }
970
+ drain() {
971
+ const hooks = this._cleanupHooks.slice();
972
+ hooks.sort((a, b) => (b.order - a.order));
973
+ for (let i = 0; i < hooks.length; ++i) {
974
+ const cb = hooks[i];
975
+ if (typeof cb.fn === 'number') {
976
+ cb.envObject.makeDynCall_vp(cb.fn)(cb.arg);
977
+ }
978
+ else {
979
+ cb.fn(cb.arg);
980
+ }
981
+ this._cleanupHooks.splice(this._cleanupHooks.indexOf(cb), 1);
982
+ }
983
+ }
984
+ dispose() {
985
+ this._cleanupHooks.length = 0;
986
+ this._cleanupHookCounter = 0;
987
+ }
988
+ }
989
+ class NodejsWaitingRequestCounter {
990
+ constructor() {
991
+ this.refHandle = new MessageChannel().port1;
992
+ this.count = 0;
993
+ }
994
+ increase() {
995
+ if (this.count === 0) {
996
+ this.refHandle.ref();
997
+ }
998
+ this.count++;
999
+ }
1000
+ decrease() {
1001
+ if (this.count === 0)
1002
+ return;
1003
+ if (this.count === 1) {
1004
+ this.refHandle.unref();
1005
+ }
1006
+ this.count--;
1007
+ }
1008
+ }
1009
+ class Context {
1010
+ constructor() {
1011
+ this.envStore = new EnvStore();
1012
+ this.scopeStore = new ScopeStore();
1013
+ this.refStore = new RefStore();
1014
+ this.deferredStore = new DeferredStore();
1015
+ this.handleStore = new HandleStore();
1016
+ this.cbinfoStack = new CallbackInfoStack();
1017
+ this.feature = {
1018
+ supportReflect,
1019
+ supportFinalizer,
1020
+ supportBigInt,
1021
+ supportNewFunction,
1022
+ canSetFunctionName,
1023
+ setImmediate: _setImmediate,
1024
+ Buffer
1025
+ };
1026
+ this.cleanupQueue = new CleanupQueue();
1027
+ if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1028
+ this.refCounter = new NodejsWaitingRequestCounter();
1029
+ process.once('beforeExit', () => {
1030
+ this.runCleanup();
1031
+ });
1032
+ }
1033
+ }
1034
+ createNotSupportWeakRefError(api, message) {
1035
+ return new NotSupportWeakRefError(api, message);
1036
+ }
1037
+ createNotSupportBigIntError(api, message) {
1038
+ return new NotSupportBigIntError(api, message);
1039
+ }
1040
+ createReference(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
1041
+ return Reference.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1042
+ }
1043
+ createDeferred(value) {
1044
+ return Deferred.create(this, value);
1045
+ }
1046
+ createEnv(makeDynCall_vppp, makeDynCall_vp) {
1047
+ return Env.create(this, makeDynCall_vppp, makeDynCall_vp);
1048
+ }
1049
+ getCurrentScope() {
1050
+ return this.scopeStore.currentScope;
1051
+ }
1052
+ addToCurrentScope(value) {
1053
+ return this.scopeStore.currentScope.add(value);
1054
+ }
1055
+ openScope(envObject) {
1056
+ return this.scopeStore.openScope(envObject);
1057
+ }
1058
+ closeScope(envObject, _scope) {
1059
+ return this.scopeStore.closeScope(envObject);
1060
+ }
1061
+ ensureHandle(value) {
1062
+ switch (value) {
1063
+ case undefined: return HandleStore.UNDEFINED;
1064
+ case null: return HandleStore.NULL;
1065
+ case true: return HandleStore.TRUE;
1066
+ case false: return HandleStore.FALSE;
1067
+ case _global: return HandleStore.GLOBAL;
1068
+ }
1069
+ return this.addToCurrentScope(value);
1070
+ }
1071
+ addCleanupHook(envObject, fn, arg) {
1072
+ this.cleanupQueue.add(envObject, fn, arg);
1073
+ }
1074
+ removeCleanupHook(envObject, fn, arg) {
1075
+ this.cleanupQueue.remove(envObject, fn, arg);
1076
+ }
1077
+ runCleanup() {
1078
+ while (!this.cleanupQueue.empty()) {
1079
+ this.cleanupQueue.drain();
1080
+ }
1081
+ }
1082
+ increaseWaitingRequestCounter() {
1083
+ var _a;
1084
+ (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.increase();
1085
+ }
1086
+ decreaseWaitingRequestCounter() {
1087
+ var _a;
1088
+ (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1089
+ }
1090
+ dispose() {
1091
+ this.runCleanup();
1092
+ }
1093
+ }
1094
+ let defaultContext;
1095
+ function createContext() {
1096
+ return new Context();
1097
+ }
1098
+ function getDefaultContext() {
1099
+ if (!defaultContext) {
1100
+ defaultContext = createContext();
1101
+ }
1102
+ return defaultContext;
1103
+ }
1104
+
1105
+ const version = "0.31.0";
1106
+
1107
+ exports.CallbackInfo = CallbackInfo;
1108
+ exports.CallbackInfoStack = CallbackInfoStack;
1109
+ exports.ConstHandle = ConstHandle;
1110
+ exports.Context = Context;
1111
+ exports.Deferred = Deferred;
1112
+ exports.DeferredStore = DeferredStore;
1113
+ exports.EmnapiError = EmnapiError;
1114
+ exports.Env = Env;
1115
+ exports.EnvStore = EnvStore;
1116
+ exports.Finalizer = Finalizer;
1117
+ exports.Handle = Handle;
1118
+ exports.HandleScope = HandleScope;
1119
+ exports.HandleStore = HandleStore;
1120
+ exports.NotSupportBigIntError = NotSupportBigIntError;
1121
+ exports.NotSupportWeakRefError = NotSupportWeakRefError;
1122
+ exports.Persistent = Persistent;
1123
+ exports.RefBase = RefBase;
1124
+ exports.RefStore = RefStore;
1125
+ exports.RefTracker = RefTracker;
1126
+ exports.Reference = Reference;
1127
+ exports.ScopeStore = ScopeStore;
1128
+ exports.Store = Store;
1129
+ exports.TryCatch = TryCatch;
1130
+ exports.createContext = createContext;
1131
+ exports.getDefaultContext = getDefaultContext;
1132
+ exports.isReferenceType = isReferenceType;
1133
+ exports.version = version;