@emnapi/runtime 0.38.3 → 0.40.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/dist/emnapi.cjs.js +288 -181
- package/dist/emnapi.cjs.min.js +1 -1
- package/dist/emnapi.d.ts +56 -27
- package/dist/emnapi.esm-bundler.js +281 -173
- package/dist/emnapi.iife.d.ts +48 -26
- package/dist/emnapi.iife.js +312 -205
- package/dist/emnapi.js +312 -205
- package/dist/emnapi.min.js +1 -1
- package/dist/emnapi.min.mjs +1 -1
- package/dist/emnapi.mjs +285 -177
- package/package.json +5 -5
package/dist/emnapi.cjs.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
-
|
|
3
1
|
class CallbackInfo {
|
|
4
2
|
constructor(parent, thiz, data, args, fn) {
|
|
5
3
|
this.parent = parent;
|
|
@@ -35,60 +33,6 @@ class CallbackInfoStack {
|
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
35
|
|
|
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
36
|
const supportNewFunction = (function () {
|
|
93
37
|
let f;
|
|
94
38
|
try {
|
|
@@ -121,22 +65,24 @@ const _global = (function () {
|
|
|
121
65
|
}
|
|
122
66
|
return g;
|
|
123
67
|
})();
|
|
124
|
-
const emptyException = new Error();
|
|
125
68
|
class TryCatch {
|
|
126
69
|
constructor() {
|
|
127
|
-
this._exception =
|
|
70
|
+
this._exception = undefined;
|
|
71
|
+
this._caught = false;
|
|
128
72
|
}
|
|
129
73
|
hasCaught() {
|
|
130
|
-
return this.
|
|
74
|
+
return this._caught;
|
|
131
75
|
}
|
|
132
76
|
exception() {
|
|
133
77
|
return this._exception;
|
|
134
78
|
}
|
|
135
79
|
setError(err) {
|
|
80
|
+
this._caught = true;
|
|
136
81
|
this._exception = err;
|
|
137
82
|
}
|
|
138
83
|
reset() {
|
|
139
|
-
this.
|
|
84
|
+
this._caught = false;
|
|
85
|
+
this._exception = undefined;
|
|
140
86
|
}
|
|
141
87
|
extractException() {
|
|
142
88
|
const e = this._exception;
|
|
@@ -221,77 +167,71 @@ const _Buffer = typeof Buffer === 'function'
|
|
|
221
167
|
catch (_) { }
|
|
222
168
|
return undefined;
|
|
223
169
|
})();
|
|
170
|
+
const version = "0.40.0";
|
|
171
|
+
const NAPI_VERSION = 9;
|
|
172
|
+
const NAPI_VERSION_EXPERIMENTAL = 2147483647;
|
|
173
|
+
const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
|
|
224
174
|
|
|
225
175
|
class Handle {
|
|
226
176
|
constructor(id, value) {
|
|
227
177
|
this.id = id;
|
|
228
178
|
this.value = value;
|
|
229
179
|
}
|
|
230
|
-
init(id, value) {
|
|
231
|
-
this.id = id;
|
|
232
|
-
this.value = value;
|
|
233
|
-
}
|
|
234
180
|
data(envObject) {
|
|
235
181
|
return envObject.getObjectBinding(this.value).data;
|
|
236
182
|
}
|
|
237
|
-
isEmpty() {
|
|
238
|
-
return this.id === 0;
|
|
239
|
-
}
|
|
240
183
|
isNumber() {
|
|
241
|
-
return
|
|
184
|
+
return typeof this.value === 'number';
|
|
242
185
|
}
|
|
243
186
|
isBigInt() {
|
|
244
|
-
return
|
|
187
|
+
return typeof this.value === 'bigint';
|
|
245
188
|
}
|
|
246
189
|
isString() {
|
|
247
|
-
return
|
|
190
|
+
return typeof this.value === 'string';
|
|
248
191
|
}
|
|
249
192
|
isFunction() {
|
|
250
|
-
return
|
|
193
|
+
return typeof this.value === 'function';
|
|
251
194
|
}
|
|
252
195
|
isExternal() {
|
|
253
|
-
return
|
|
196
|
+
return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
|
|
254
197
|
}
|
|
255
198
|
isObject() {
|
|
256
|
-
return
|
|
199
|
+
return typeof this.value === 'object' && this.value !== null;
|
|
257
200
|
}
|
|
258
201
|
isArray() {
|
|
259
|
-
return
|
|
202
|
+
return Array.isArray(this.value);
|
|
260
203
|
}
|
|
261
204
|
isArrayBuffer() {
|
|
262
|
-
return
|
|
205
|
+
return (this.value instanceof ArrayBuffer);
|
|
263
206
|
}
|
|
264
207
|
isTypedArray() {
|
|
265
|
-
return
|
|
208
|
+
return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
|
|
266
209
|
}
|
|
267
210
|
isBuffer() {
|
|
268
|
-
return
|
|
211
|
+
return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
|
|
269
212
|
}
|
|
270
213
|
isDataView() {
|
|
271
|
-
return
|
|
214
|
+
return (this.value instanceof DataView);
|
|
272
215
|
}
|
|
273
216
|
isDate() {
|
|
274
|
-
return
|
|
217
|
+
return (this.value instanceof Date);
|
|
275
218
|
}
|
|
276
219
|
isPromise() {
|
|
277
|
-
return
|
|
220
|
+
return (this.value instanceof Promise);
|
|
278
221
|
}
|
|
279
222
|
isBoolean() {
|
|
280
|
-
return
|
|
223
|
+
return typeof this.value === 'boolean';
|
|
281
224
|
}
|
|
282
225
|
isUndefined() {
|
|
283
|
-
return
|
|
226
|
+
return this.value === undefined;
|
|
284
227
|
}
|
|
285
228
|
isSymbol() {
|
|
286
|
-
return
|
|
229
|
+
return typeof this.value === 'symbol';
|
|
287
230
|
}
|
|
288
231
|
isNull() {
|
|
289
|
-
return
|
|
232
|
+
return this.value === null;
|
|
290
233
|
}
|
|
291
234
|
dispose() {
|
|
292
|
-
if (this.id === 0)
|
|
293
|
-
return;
|
|
294
|
-
this.id = 0;
|
|
295
235
|
this.value = undefined;
|
|
296
236
|
}
|
|
297
237
|
}
|
|
@@ -323,7 +263,7 @@ class HandleStore {
|
|
|
323
263
|
const values = this._values;
|
|
324
264
|
if (next < values.length) {
|
|
325
265
|
h = values[next];
|
|
326
|
-
h.
|
|
266
|
+
h.value = value;
|
|
327
267
|
}
|
|
328
268
|
else {
|
|
329
269
|
h = new Handle(next, value);
|
|
@@ -465,18 +405,6 @@ class ScopeStore {
|
|
|
465
405
|
}
|
|
466
406
|
}
|
|
467
407
|
|
|
468
|
-
class RefStore extends Store {
|
|
469
|
-
constructor() {
|
|
470
|
-
super(8);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
class DeferredStore extends Store {
|
|
475
|
-
constructor() {
|
|
476
|
-
super(8);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
408
|
class RefTracker {
|
|
481
409
|
constructor() {
|
|
482
410
|
this._next = null;
|
|
@@ -529,14 +457,6 @@ class Finalizer {
|
|
|
529
457
|
}
|
|
530
458
|
|
|
531
459
|
class RefBase extends Finalizer {
|
|
532
|
-
constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
|
|
533
|
-
super(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
534
|
-
this._next = null;
|
|
535
|
-
this._prev = null;
|
|
536
|
-
this._refcount = initial_refcount;
|
|
537
|
-
this._ownership = ownership;
|
|
538
|
-
this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
|
|
539
|
-
}
|
|
540
460
|
static finalizeAll(list) {
|
|
541
461
|
RefTracker.finalizeAll(list);
|
|
542
462
|
}
|
|
@@ -546,6 +466,14 @@ class RefBase extends Finalizer {
|
|
|
546
466
|
unlink() {
|
|
547
467
|
RefTracker.prototype.unlink.call(this);
|
|
548
468
|
}
|
|
469
|
+
constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
|
|
470
|
+
super(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
471
|
+
this._next = null;
|
|
472
|
+
this._prev = null;
|
|
473
|
+
this._refcount = initial_refcount;
|
|
474
|
+
this._ownership = ownership;
|
|
475
|
+
this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
|
|
476
|
+
}
|
|
549
477
|
dispose() {
|
|
550
478
|
this.unlink();
|
|
551
479
|
this.envObject.dequeueFinalizer(this);
|
|
@@ -597,19 +525,29 @@ class RefBase extends Finalizer {
|
|
|
597
525
|
}
|
|
598
526
|
}
|
|
599
527
|
|
|
528
|
+
function throwNodeApiVersionError(moduleName, moduleApiVersion) {
|
|
529
|
+
const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NAPI_VERSION} add-ons.`;
|
|
530
|
+
throw new Error(errorMessage);
|
|
531
|
+
}
|
|
532
|
+
function handleThrow(envObject, value) {
|
|
533
|
+
if (envObject.terminatedOrTerminating()) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
throw value;
|
|
537
|
+
}
|
|
600
538
|
class Env {
|
|
601
|
-
constructor(ctx, makeDynCall_vppp, makeDynCall_vp) {
|
|
539
|
+
constructor(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
|
|
602
540
|
this.ctx = ctx;
|
|
541
|
+
this.moduleApiVersion = moduleApiVersion;
|
|
603
542
|
this.makeDynCall_vppp = makeDynCall_vppp;
|
|
604
543
|
this.makeDynCall_vp = makeDynCall_vp;
|
|
544
|
+
this.abort = abort;
|
|
605
545
|
this.openHandleScopes = 0;
|
|
606
546
|
this.instanceData = null;
|
|
607
547
|
this.tryCatch = new TryCatch();
|
|
608
548
|
this.refs = 1;
|
|
609
549
|
this.reflist = new RefTracker();
|
|
610
550
|
this.finalizing_reflist = new RefTracker();
|
|
611
|
-
this.destructing = false;
|
|
612
|
-
this.finalizationScheduled = false;
|
|
613
551
|
this.pendingFinalizers = [];
|
|
614
552
|
this.lastError = {
|
|
615
553
|
errorCode: 0,
|
|
@@ -619,11 +557,11 @@ class Env {
|
|
|
619
557
|
this._bindingMap = new WeakMap();
|
|
620
558
|
this.id = 0;
|
|
621
559
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return
|
|
560
|
+
canCallIntoJs() {
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
terminatedOrTerminating() {
|
|
564
|
+
return !this.canCallIntoJs();
|
|
627
565
|
}
|
|
628
566
|
ref() {
|
|
629
567
|
this.refs++;
|
|
@@ -642,27 +580,37 @@ class Env {
|
|
|
642
580
|
}
|
|
643
581
|
clearLastError() {
|
|
644
582
|
const lastError = this.lastError;
|
|
645
|
-
lastError.errorCode
|
|
646
|
-
|
|
647
|
-
lastError.
|
|
583
|
+
if (lastError.errorCode !== 0)
|
|
584
|
+
lastError.errorCode = 0;
|
|
585
|
+
if (lastError.engineErrorCode !== 0)
|
|
586
|
+
lastError.engineErrorCode = 0;
|
|
587
|
+
if (lastError.engineReserved !== 0)
|
|
588
|
+
lastError.engineReserved = 0;
|
|
648
589
|
return 0;
|
|
649
590
|
}
|
|
650
591
|
setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
|
|
651
592
|
const lastError = this.lastError;
|
|
652
|
-
lastError.errorCode
|
|
653
|
-
|
|
654
|
-
lastError.
|
|
593
|
+
if (lastError.errorCode !== error_code)
|
|
594
|
+
lastError.errorCode = error_code;
|
|
595
|
+
if (lastError.engineErrorCode !== engine_error_code)
|
|
596
|
+
lastError.engineErrorCode = engine_error_code;
|
|
597
|
+
if (lastError.engineReserved !== engine_reserved)
|
|
598
|
+
lastError.engineReserved = engine_reserved;
|
|
655
599
|
return error_code;
|
|
656
600
|
}
|
|
657
601
|
getReturnStatus() {
|
|
658
602
|
return !this.tryCatch.hasCaught() ? 0 : this.setLastError(10);
|
|
659
603
|
}
|
|
660
|
-
callIntoModule(fn) {
|
|
604
|
+
callIntoModule(fn, handleException = handleThrow) {
|
|
605
|
+
const openHandleScopesBefore = this.openHandleScopes;
|
|
661
606
|
this.clearLastError();
|
|
662
607
|
const r = fn(this);
|
|
608
|
+
if (openHandleScopesBefore !== this.openHandleScopes) {
|
|
609
|
+
this.abort('open_handle_scopes != open_handle_scopes_before');
|
|
610
|
+
}
|
|
663
611
|
if (this.tryCatch.hasCaught()) {
|
|
664
612
|
const err = this.tryCatch.extractException();
|
|
665
|
-
|
|
613
|
+
handleException(this, err);
|
|
666
614
|
}
|
|
667
615
|
return r;
|
|
668
616
|
}
|
|
@@ -681,15 +629,6 @@ class Env {
|
|
|
681
629
|
if (this.pendingFinalizers.indexOf(finalizer) === -1) {
|
|
682
630
|
this.pendingFinalizers.push(finalizer);
|
|
683
631
|
}
|
|
684
|
-
if (!this.finalizationScheduled && !this.destructing) {
|
|
685
|
-
this.finalizationScheduled = true;
|
|
686
|
-
this.ref();
|
|
687
|
-
_setImmediate(() => {
|
|
688
|
-
this.finalizationScheduled = false;
|
|
689
|
-
this.unref();
|
|
690
|
-
this.drainFinalizerQueue();
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
632
|
}
|
|
694
633
|
dequeueFinalizer(finalizer) {
|
|
695
634
|
const index = this.pendingFinalizers.indexOf(finalizer);
|
|
@@ -697,21 +636,16 @@ class Env {
|
|
|
697
636
|
this.pendingFinalizers.splice(index, 1);
|
|
698
637
|
}
|
|
699
638
|
}
|
|
700
|
-
|
|
701
|
-
while (this.pendingFinalizers.length > 0) {
|
|
702
|
-
const refTracker = this.pendingFinalizers.shift();
|
|
703
|
-
refTracker.finalize();
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
dispose() {
|
|
707
|
-
if (this.id === 0)
|
|
708
|
-
return;
|
|
709
|
-
this.destructing = true;
|
|
710
|
-
this.drainFinalizerQueue();
|
|
639
|
+
deleteMe() {
|
|
711
640
|
RefBase.finalizeAll(this.finalizing_reflist);
|
|
712
641
|
RefBase.finalizeAll(this.reflist);
|
|
713
642
|
this.tryCatch.extractException();
|
|
714
643
|
this.ctx.envStore.remove(this.id);
|
|
644
|
+
}
|
|
645
|
+
dispose() {
|
|
646
|
+
if (this.id === 0)
|
|
647
|
+
return;
|
|
648
|
+
this.deleteMe();
|
|
715
649
|
this.id = 0;
|
|
716
650
|
}
|
|
717
651
|
initObjectBinding(value) {
|
|
@@ -739,6 +673,103 @@ class Env {
|
|
|
739
673
|
return this.instanceData ? this.instanceData.data() : 0;
|
|
740
674
|
}
|
|
741
675
|
}
|
|
676
|
+
class NodeEnv extends Env {
|
|
677
|
+
constructor(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
|
|
678
|
+
super(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort);
|
|
679
|
+
this.filename = filename;
|
|
680
|
+
this.nodeBinding = nodeBinding;
|
|
681
|
+
this.destructing = false;
|
|
682
|
+
this.finalizationScheduled = false;
|
|
683
|
+
}
|
|
684
|
+
deleteMe() {
|
|
685
|
+
this.destructing = true;
|
|
686
|
+
this.drainFinalizerQueue();
|
|
687
|
+
super.deleteMe();
|
|
688
|
+
}
|
|
689
|
+
canCallIntoJs() {
|
|
690
|
+
return this.ctx.canCallIntoJs();
|
|
691
|
+
}
|
|
692
|
+
triggerFatalException(err) {
|
|
693
|
+
if (this.nodeBinding) {
|
|
694
|
+
this.nodeBinding.napi.fatalException(err);
|
|
695
|
+
}
|
|
696
|
+
else {
|
|
697
|
+
if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
|
|
698
|
+
const handled = process._fatalException(err);
|
|
699
|
+
if (!handled) {
|
|
700
|
+
console.error(err);
|
|
701
|
+
process.exit(1);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
throw err;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
callbackIntoModule(enforceUncaughtExceptionPolicy, fn) {
|
|
710
|
+
return this.callIntoModule(fn, (envObject, err) => {
|
|
711
|
+
if (envObject.terminatedOrTerminating()) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const hasProcess = typeof process === 'object' && process !== null;
|
|
715
|
+
const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
|
|
716
|
+
if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
|
|
717
|
+
if (hasProcess && typeof process.emitWarning === 'function') {
|
|
718
|
+
process.emitWarning('Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.', 'DeprecationWarning', 'DEP0168');
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
throw err;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
envObject.triggerFatalException(err);
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
callFinalizer(cb, data, hint) {
|
|
728
|
+
this.callFinalizerInternal(1, cb, data, hint);
|
|
729
|
+
}
|
|
730
|
+
callFinalizerInternal(forceUncaught, cb, data, hint) {
|
|
731
|
+
const f = this.makeDynCall_vppp(cb);
|
|
732
|
+
const env = this.id;
|
|
733
|
+
const scope = this.ctx.openScope(this);
|
|
734
|
+
try {
|
|
735
|
+
this.callbackIntoModule(Boolean(forceUncaught), () => { f(env, data, hint); });
|
|
736
|
+
}
|
|
737
|
+
finally {
|
|
738
|
+
this.ctx.closeScope(this, scope);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
enqueueFinalizer(finalizer) {
|
|
742
|
+
super.enqueueFinalizer(finalizer);
|
|
743
|
+
if (!this.finalizationScheduled && !this.destructing) {
|
|
744
|
+
this.finalizationScheduled = true;
|
|
745
|
+
this.ref();
|
|
746
|
+
_setImmediate(() => {
|
|
747
|
+
this.finalizationScheduled = false;
|
|
748
|
+
this.unref();
|
|
749
|
+
this.drainFinalizerQueue();
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
drainFinalizerQueue() {
|
|
754
|
+
while (this.pendingFinalizers.length > 0) {
|
|
755
|
+
const refTracker = this.pendingFinalizers.shift();
|
|
756
|
+
refTracker.finalize();
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
|
|
761
|
+
moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
|
|
762
|
+
if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
|
|
763
|
+
moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
|
|
764
|
+
}
|
|
765
|
+
else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
|
|
766
|
+
throwNodeApiVersionError(filename, moduleApiVersion);
|
|
767
|
+
}
|
|
768
|
+
const env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
|
|
769
|
+
ctx.envStore.add(env);
|
|
770
|
+
ctx.addCleanupHook(env, () => { env.unref(); }, 0);
|
|
771
|
+
return env;
|
|
772
|
+
}
|
|
742
773
|
|
|
743
774
|
class EmnapiError extends Error {
|
|
744
775
|
constructor(message) {
|
|
@@ -822,7 +853,12 @@ class Persistent {
|
|
|
822
853
|
this._param = param;
|
|
823
854
|
this._callback = callback;
|
|
824
855
|
}
|
|
825
|
-
catch (
|
|
856
|
+
catch (err) {
|
|
857
|
+
if (typeof value === 'symbol') ;
|
|
858
|
+
else {
|
|
859
|
+
throw err;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
826
862
|
}
|
|
827
863
|
clearWeak() {
|
|
828
864
|
if (!supportFinalizer || this._ref === undefined)
|
|
@@ -883,31 +919,32 @@ function weakCallback(ref) {
|
|
|
883
919
|
ref.persistent.reset();
|
|
884
920
|
ref.envObject.enqueueFinalizer(ref);
|
|
885
921
|
}
|
|
922
|
+
function canBeHeldWeakly(value) {
|
|
923
|
+
return value.isObject() || value.isFunction() || value.isSymbol();
|
|
924
|
+
}
|
|
886
925
|
class Reference extends RefBase {
|
|
887
|
-
constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
888
|
-
super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
889
|
-
this.id = 0;
|
|
890
|
-
}
|
|
891
926
|
static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
892
927
|
const handle = envObject.ctx.handleStore.get(handle_id);
|
|
893
928
|
const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
894
929
|
envObject.ctx.refStore.add(ref);
|
|
930
|
+
ref.canBeWeak = canBeHeldWeakly(handle);
|
|
895
931
|
ref.persistent = new Persistent(handle.value);
|
|
896
932
|
if (initialRefcount === 0) {
|
|
897
933
|
ref._setWeak();
|
|
898
934
|
}
|
|
899
935
|
return ref;
|
|
900
936
|
}
|
|
937
|
+
constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
938
|
+
super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
939
|
+
this.id = 0;
|
|
940
|
+
}
|
|
901
941
|
ref() {
|
|
902
942
|
if (this.persistent.isEmpty()) {
|
|
903
943
|
return 0;
|
|
904
944
|
}
|
|
905
945
|
const count = super.ref();
|
|
906
|
-
if (count === 1) {
|
|
907
|
-
|
|
908
|
-
if (obj) {
|
|
909
|
-
this.persistent.clearWeak();
|
|
910
|
-
}
|
|
946
|
+
if (count === 1 && this.canBeWeak) {
|
|
947
|
+
this.persistent.clearWeak();
|
|
911
948
|
}
|
|
912
949
|
return count;
|
|
913
950
|
}
|
|
@@ -918,23 +955,25 @@ class Reference extends RefBase {
|
|
|
918
955
|
const oldRefcount = this.refCount();
|
|
919
956
|
const refcount = super.unref();
|
|
920
957
|
if (oldRefcount === 1 && refcount === 0) {
|
|
921
|
-
|
|
922
|
-
if (obj) {
|
|
923
|
-
this._setWeak();
|
|
924
|
-
}
|
|
958
|
+
this._setWeak();
|
|
925
959
|
}
|
|
926
960
|
return refcount;
|
|
927
961
|
}
|
|
928
962
|
get() {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
const handle = this.envObject.ensureHandle(obj);
|
|
932
|
-
return handle.id;
|
|
963
|
+
if (this.persistent.isEmpty()) {
|
|
964
|
+
return 0;
|
|
933
965
|
}
|
|
934
|
-
|
|
966
|
+
const obj = this.persistent.deref();
|
|
967
|
+
const handle = this.envObject.ensureHandle(obj);
|
|
968
|
+
return handle.id;
|
|
935
969
|
}
|
|
936
970
|
_setWeak() {
|
|
937
|
-
this.
|
|
971
|
+
if (this.canBeWeak) {
|
|
972
|
+
this.persistent.setWeak(this, weakCallback);
|
|
973
|
+
}
|
|
974
|
+
else {
|
|
975
|
+
this.persistent.reset();
|
|
976
|
+
}
|
|
938
977
|
}
|
|
939
978
|
finalize() {
|
|
940
979
|
this.persistent.reset();
|
|
@@ -951,16 +990,16 @@ class Reference extends RefBase {
|
|
|
951
990
|
}
|
|
952
991
|
|
|
953
992
|
class Deferred {
|
|
954
|
-
constructor(ctx, value) {
|
|
955
|
-
this.id = 0;
|
|
956
|
-
this.ctx = ctx;
|
|
957
|
-
this.value = value;
|
|
958
|
-
}
|
|
959
993
|
static create(ctx, value) {
|
|
960
994
|
const deferred = new Deferred(ctx, value);
|
|
961
995
|
ctx.deferredStore.add(deferred);
|
|
962
996
|
return deferred;
|
|
963
997
|
}
|
|
998
|
+
constructor(ctx, value) {
|
|
999
|
+
this.id = 0;
|
|
1000
|
+
this.ctx = ctx;
|
|
1001
|
+
this.value = value;
|
|
1002
|
+
}
|
|
964
1003
|
resolve(value) {
|
|
965
1004
|
this.value.resolve(value);
|
|
966
1005
|
this.dispose();
|
|
@@ -977,6 +1016,54 @@ class Deferred {
|
|
|
977
1016
|
}
|
|
978
1017
|
}
|
|
979
1018
|
|
|
1019
|
+
class Store {
|
|
1020
|
+
constructor() {
|
|
1021
|
+
this._values = [undefined];
|
|
1022
|
+
this._values.length = 4;
|
|
1023
|
+
this._size = 1;
|
|
1024
|
+
this._freeList = [];
|
|
1025
|
+
}
|
|
1026
|
+
add(value) {
|
|
1027
|
+
let id;
|
|
1028
|
+
if (this._freeList.length) {
|
|
1029
|
+
id = this._freeList.shift();
|
|
1030
|
+
}
|
|
1031
|
+
else {
|
|
1032
|
+
id = this._size;
|
|
1033
|
+
this._size++;
|
|
1034
|
+
const capacity = this._values.length;
|
|
1035
|
+
if (id >= capacity) {
|
|
1036
|
+
this._values.length = capacity + (capacity >> 1) + 16;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
value.id = id;
|
|
1040
|
+
this._values[id] = value;
|
|
1041
|
+
}
|
|
1042
|
+
get(id) {
|
|
1043
|
+
return this._values[id];
|
|
1044
|
+
}
|
|
1045
|
+
has(id) {
|
|
1046
|
+
return this._values[id] !== undefined;
|
|
1047
|
+
}
|
|
1048
|
+
remove(id) {
|
|
1049
|
+
const value = this._values[id];
|
|
1050
|
+
if (value) {
|
|
1051
|
+
value.id = 0;
|
|
1052
|
+
this._values[id] = undefined;
|
|
1053
|
+
this._freeList.push(Number(id));
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
dispose() {
|
|
1057
|
+
for (let i = 1; i < this._size; ++i) {
|
|
1058
|
+
const value = this._values[i];
|
|
1059
|
+
value === null || value === void 0 ? void 0 : value.dispose();
|
|
1060
|
+
}
|
|
1061
|
+
this._values = [undefined];
|
|
1062
|
+
this._size = 1;
|
|
1063
|
+
this._freeList = [];
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
|
|
980
1067
|
class CleanupHookCallback {
|
|
981
1068
|
constructor(envObject, fn, arg, order) {
|
|
982
1069
|
this.envObject = envObject;
|
|
@@ -1049,10 +1136,12 @@ class NodejsWaitingRequestCounter {
|
|
|
1049
1136
|
}
|
|
1050
1137
|
class Context {
|
|
1051
1138
|
constructor() {
|
|
1052
|
-
this.
|
|
1139
|
+
this._isStopping = false;
|
|
1140
|
+
this._canCallIntoJs = true;
|
|
1141
|
+
this.envStore = new Store();
|
|
1053
1142
|
this.scopeStore = new ScopeStore();
|
|
1054
|
-
this.refStore = new
|
|
1055
|
-
this.deferredStore = new
|
|
1143
|
+
this.refStore = new Store();
|
|
1144
|
+
this.deferredStore = new Store();
|
|
1056
1145
|
this.handleStore = new HandleStore();
|
|
1057
1146
|
this.cbinfoStack = new CallbackInfoStack();
|
|
1058
1147
|
this.feature = {
|
|
@@ -1070,10 +1159,18 @@ class Context {
|
|
|
1070
1159
|
if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
|
|
1071
1160
|
this.refCounter = new NodejsWaitingRequestCounter();
|
|
1072
1161
|
process.once('beforeExit', () => {
|
|
1073
|
-
this.
|
|
1162
|
+
this.destroy();
|
|
1074
1163
|
});
|
|
1075
1164
|
}
|
|
1076
1165
|
}
|
|
1166
|
+
getRuntimeVersions() {
|
|
1167
|
+
return {
|
|
1168
|
+
version,
|
|
1169
|
+
NAPI_VERSION,
|
|
1170
|
+
NAPI_VERSION_EXPERIMENTAL,
|
|
1171
|
+
NODE_API_DEFAULT_MODULE_API_VERSION
|
|
1172
|
+
};
|
|
1173
|
+
}
|
|
1077
1174
|
createNotSupportWeakRefError(api, message) {
|
|
1078
1175
|
return new NotSupportWeakRefError(api, message);
|
|
1079
1176
|
}
|
|
@@ -1089,8 +1186,8 @@ class Context {
|
|
|
1089
1186
|
createDeferred(value) {
|
|
1090
1187
|
return Deferred.create(this, value);
|
|
1091
1188
|
}
|
|
1092
|
-
createEnv(makeDynCall_vppp, makeDynCall_vp) {
|
|
1093
|
-
return
|
|
1189
|
+
createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
|
|
1190
|
+
return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
|
|
1094
1191
|
}
|
|
1095
1192
|
getCurrentScope() {
|
|
1096
1193
|
return this.scopeStore.currentScope;
|
|
@@ -1133,7 +1230,18 @@ class Context {
|
|
|
1133
1230
|
var _a;
|
|
1134
1231
|
(_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
|
|
1135
1232
|
}
|
|
1136
|
-
|
|
1233
|
+
setCanCallIntoJs(value) {
|
|
1234
|
+
this._canCallIntoJs = value;
|
|
1235
|
+
}
|
|
1236
|
+
setStopping(value) {
|
|
1237
|
+
this._isStopping = value;
|
|
1238
|
+
}
|
|
1239
|
+
canCallIntoJs() {
|
|
1240
|
+
return this._canCallIntoJs && !this._isStopping;
|
|
1241
|
+
}
|
|
1242
|
+
destroy() {
|
|
1243
|
+
this.setStopping(true);
|
|
1244
|
+
this.setCanCallIntoJs(false);
|
|
1137
1245
|
this.runCleanup();
|
|
1138
1246
|
}
|
|
1139
1247
|
}
|
|
@@ -1148,27 +1256,26 @@ function getDefaultContext() {
|
|
|
1148
1256
|
return defaultContext;
|
|
1149
1257
|
}
|
|
1150
1258
|
|
|
1151
|
-
const version = "0.38.3";
|
|
1152
|
-
|
|
1153
1259
|
exports.CallbackInfo = CallbackInfo;
|
|
1154
1260
|
exports.CallbackInfoStack = CallbackInfoStack;
|
|
1155
1261
|
exports.ConstHandle = ConstHandle;
|
|
1156
1262
|
exports.Context = Context;
|
|
1157
1263
|
exports.Deferred = Deferred;
|
|
1158
|
-
exports.DeferredStore = DeferredStore;
|
|
1159
1264
|
exports.EmnapiError = EmnapiError;
|
|
1160
1265
|
exports.Env = Env;
|
|
1161
|
-
exports.EnvStore = EnvStore;
|
|
1162
1266
|
exports.Finalizer = Finalizer;
|
|
1163
1267
|
exports.Handle = Handle;
|
|
1164
1268
|
exports.HandleScope = HandleScope;
|
|
1165
1269
|
exports.HandleStore = HandleStore;
|
|
1270
|
+
exports.NAPI_VERSION = NAPI_VERSION;
|
|
1271
|
+
exports.NAPI_VERSION_EXPERIMENTAL = NAPI_VERSION_EXPERIMENTAL;
|
|
1272
|
+
exports.NODE_API_DEFAULT_MODULE_API_VERSION = NODE_API_DEFAULT_MODULE_API_VERSION;
|
|
1273
|
+
exports.NodeEnv = NodeEnv;
|
|
1166
1274
|
exports.NotSupportBigIntError = NotSupportBigIntError;
|
|
1167
1275
|
exports.NotSupportBufferError = NotSupportBufferError;
|
|
1168
1276
|
exports.NotSupportWeakRefError = NotSupportWeakRefError;
|
|
1169
1277
|
exports.Persistent = Persistent;
|
|
1170
1278
|
exports.RefBase = RefBase;
|
|
1171
|
-
exports.RefStore = RefStore;
|
|
1172
1279
|
exports.RefTracker = RefTracker;
|
|
1173
1280
|
exports.Reference = Reference;
|
|
1174
1281
|
exports.ScopeStore = ScopeStore;
|