@emnapi/runtime 2.0.0-alpha.5 → 2.0.0-alpha.6
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.d.ts +42 -4
- package/dist/emnapi.js +373 -48
- package/dist/emnapi.js.map +1 -1
- package/package.json +1 -1
package/dist/emnapi.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare const enum Version {
|
|
|
33
33
|
NODE_API_SUPPORTED_VERSION_MIN = 1,
|
|
34
34
|
NODE_API_DEFAULT_MODULE_API_VERSION = 8,
|
|
35
35
|
NODE_API_SUPPORTED_VERSION_MAX = 10,
|
|
36
|
-
NODE_MODULE_VERSION =
|
|
36
|
+
NODE_MODULE_VERSION = 137,
|
|
37
37
|
NAPI_VERSION_EXPERIMENTAL = 2147483647 // INT_MAX
|
|
38
38
|
}
|
|
39
39
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -279,6 +279,12 @@ export declare class BaseArrayStore<T> extends Disposable_2 implements ObjectAll
|
|
|
279
279
|
dispose(): void;
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
+
export declare interface CallHandlerConfig {
|
|
283
|
+
callbackWrap: (info: Ptr, callback: Ptr) => Ptr;
|
|
284
|
+
callback: Ptr;
|
|
285
|
+
data: any;
|
|
286
|
+
}
|
|
287
|
+
|
|
282
288
|
export declare type CleanupHookCallbackFunction = number | ((arg: number) => void);
|
|
283
289
|
|
|
284
290
|
export declare class Context {
|
|
@@ -392,6 +398,7 @@ export declare abstract class Env extends Disposable_2 {
|
|
|
392
398
|
filename: string;
|
|
393
399
|
nodeBinding?: any;
|
|
394
400
|
inGcFinalizer: boolean;
|
|
401
|
+
address: number;
|
|
395
402
|
bridge: EnvNativeBridge;
|
|
396
403
|
readonly ctx: Context;
|
|
397
404
|
private store;
|
|
@@ -418,7 +425,7 @@ export declare abstract class Env extends Disposable_2 {
|
|
|
418
425
|
}
|
|
419
426
|
|
|
420
427
|
export declare interface EnvNativeBridge {
|
|
421
|
-
address: number;
|
|
428
|
+
address: number | (() => number);
|
|
422
429
|
deleteEnv: (ptr: number) => void;
|
|
423
430
|
setLastError: (env: napi_env, error_code: napi_status, engine_error_code: uint32_t, engine_reserved: number) => void;
|
|
424
431
|
makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void;
|
|
@@ -474,10 +481,12 @@ export declare class FunctionTemplate extends Template {
|
|
|
474
481
|
data: any;
|
|
475
482
|
className: string | undefined;
|
|
476
483
|
signature: Signature | undefined;
|
|
484
|
+
callHandler: CallHandlerConfig | undefined;
|
|
477
485
|
private _instanceTemplate;
|
|
478
486
|
private _prototypeTemplate;
|
|
479
487
|
private _cached;
|
|
480
488
|
constructor(ctx: Isolate, callback: (info: napi_callback_info, v8FunctionCallback: Ptr) => Ptr, v8FunctionCallback: Ptr, data: any, signature?: Signature);
|
|
489
|
+
setCallHandler(config: CallHandlerConfig): void;
|
|
481
490
|
setClassName(name: string): void;
|
|
482
491
|
instanceTemplate(): ObjectTemplate;
|
|
483
492
|
prototypeTemplate(): ObjectTemplate;
|
|
@@ -556,6 +565,7 @@ export declare class Isolate {
|
|
|
556
565
|
private _scopeStore;
|
|
557
566
|
private _handleStore;
|
|
558
567
|
private _externalMemory;
|
|
568
|
+
private _globalIdentityStore;
|
|
559
569
|
globalHandleStore: PersistentStore;
|
|
560
570
|
readonly features: Features;
|
|
561
571
|
constructor(options?: IsolateOptions);
|
|
@@ -567,6 +577,9 @@ export declare class Isolate {
|
|
|
567
577
|
createReference<T>(...args: [T] | []): Persistent<T>;
|
|
568
578
|
insertRef<T>(persistent: Persistent<T>): void;
|
|
569
579
|
getRef<T = any>(ref: napi_ref): Persistent<T> | undefined;
|
|
580
|
+
acquireGlobalValueIdentity(value: unknown): number;
|
|
581
|
+
retainGlobalValueIdentity(slot: number | bigint): void;
|
|
582
|
+
releaseGlobalValueIdentity(slot: number | bigint): void;
|
|
570
583
|
removeRef(ref: napi_ref, force?: boolean): void;
|
|
571
584
|
getTryCatch(address: number | bigint): TryCatch | undefined;
|
|
572
585
|
pushTryCatch(address: number | bigint): TryCatch;
|
|
@@ -648,11 +661,21 @@ export declare class ObjectTemplate extends Template {
|
|
|
648
661
|
internalFieldCount: number;
|
|
649
662
|
private _accessors;
|
|
650
663
|
private _instances;
|
|
664
|
+
private _namedPropertyHandler;
|
|
665
|
+
private _indexedPropertyHandler;
|
|
666
|
+
private _callAsFunctionHandler;
|
|
651
667
|
constructor(ctx: Isolate, Ctor?: any);
|
|
652
|
-
|
|
668
|
+
setNativeDataProperty(name: string | symbol, getterWrap: (property: Ptr, info: Ptr, getter: Ptr) => Ptr, setterWrap: (property: Ptr, value: Ptr, info: Ptr, setter: Ptr) => Ptr, getter: Ptr, setter: Ptr, data: any, attribute: number, getterSideEffectType: number, setterSideEffectType: number): void;
|
|
669
|
+
setNativeDataPropertyOnInstance(instance: any, name: string | symbol, getterWrap: (property: Ptr, info: Ptr, getter: Ptr) => Ptr, setterWrap: (property: Ptr, value: Ptr, info: Ptr, setter: Ptr) => Ptr, getter: Ptr, setter: Ptr, data: any, attribute: number, getterSideEffectType: number, setterSideEffectType: number): void;
|
|
653
670
|
setInternalFieldCount(value: number): void;
|
|
671
|
+
setNamedPropertyHandler(config: PropertyHandlerConfig): void;
|
|
672
|
+
setIndexedPropertyHandler(config: PropertyHandlerConfig): void;
|
|
673
|
+
setCallAsFunctionHandler(config: CallHandlerConfig): void;
|
|
654
674
|
private _createAccessorWrapper;
|
|
655
|
-
applyToInstance(instance: any):
|
|
675
|
+
applyToInstance(instance: any): any;
|
|
676
|
+
private _createPropertyHandlerProxy;
|
|
677
|
+
private _createAccessorConfig;
|
|
678
|
+
private _defineAccessor;
|
|
656
679
|
newInstance(_context: any): any;
|
|
657
680
|
}
|
|
658
681
|
|
|
@@ -696,6 +719,21 @@ export declare class Private {
|
|
|
696
719
|
static forApi(name: string): Private;
|
|
697
720
|
}
|
|
698
721
|
|
|
722
|
+
export declare interface PropertyHandlerConfig {
|
|
723
|
+
getterWrap: ((property: Ptr, info: Ptr, getter: Ptr) => Ptr) | undefined;
|
|
724
|
+
setterWrap: ((property: Ptr, value: Ptr, info: Ptr, setter: Ptr) => Ptr) | undefined;
|
|
725
|
+
queryWrap: ((property: Ptr, info: Ptr, query: Ptr) => Ptr) | undefined;
|
|
726
|
+
deleterWrap: ((property: Ptr, info: Ptr, deleter: Ptr) => Ptr) | undefined;
|
|
727
|
+
enumeratorWrap: ((info: Ptr, enumerator: Ptr) => Ptr) | undefined;
|
|
728
|
+
getter: Ptr;
|
|
729
|
+
setter: Ptr;
|
|
730
|
+
query: Ptr;
|
|
731
|
+
deleter: Ptr;
|
|
732
|
+
enumerator: Ptr;
|
|
733
|
+
data: any;
|
|
734
|
+
flags: number;
|
|
735
|
+
}
|
|
736
|
+
|
|
699
737
|
export declare class Reference extends RefTracker {
|
|
700
738
|
private static weakCallback;
|
|
701
739
|
id: number;
|
package/dist/emnapi.js
CHANGED
|
@@ -260,12 +260,12 @@ function getDylinkMetadata(binary) {
|
|
|
260
260
|
failIf(offset !== end);
|
|
261
261
|
return customSection;
|
|
262
262
|
}
|
|
263
|
-
const version = "2.0.0-alpha.
|
|
263
|
+
const version = "2.0.0-alpha.6";
|
|
264
264
|
const NODE_API_SUPPORTED_VERSION_MIN = 1;
|
|
265
265
|
const NODE_API_SUPPORTED_VERSION_MAX = 10;
|
|
266
266
|
const NAPI_VERSION_EXPERIMENTAL = 2147483647;
|
|
267
267
|
const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
|
|
268
|
-
const NODE_MODULE_VERSION =
|
|
268
|
+
const NODE_MODULE_VERSION = 137;
|
|
269
269
|
|
|
270
270
|
//#endregion src/util.ts
|
|
271
271
|
|
|
@@ -535,6 +535,66 @@ class PersistentStore extends ArrayStore {
|
|
|
535
535
|
}
|
|
536
536
|
}
|
|
537
537
|
}
|
|
538
|
+
class GlobalIdentityStore {
|
|
539
|
+
constructor(isolate) {
|
|
540
|
+
this._objects = new WeakMap();
|
|
541
|
+
this._primitives = new Map();
|
|
542
|
+
this._bySlot = new Map();
|
|
543
|
+
this._isolate = isolate;
|
|
544
|
+
}
|
|
545
|
+
acquire(value) {
|
|
546
|
+
const objectLike = value !== null &&
|
|
547
|
+
(typeof value === 'object' || typeof value === 'function');
|
|
548
|
+
const table = objectLike ? this._objects : this._primitives;
|
|
549
|
+
let identity = table.get(value);
|
|
550
|
+
if (identity?.retired)
|
|
551
|
+
identity = undefined;
|
|
552
|
+
if (!identity) {
|
|
553
|
+
const reference = new Persistent(this._isolate, value);
|
|
554
|
+
if (objectLike)
|
|
555
|
+
reference.setWeak(undefined, () => { });
|
|
556
|
+
identity = {
|
|
557
|
+
reference,
|
|
558
|
+
count: 0,
|
|
559
|
+
pending: false,
|
|
560
|
+
retired: false,
|
|
561
|
+
objectLike,
|
|
562
|
+
primitiveValue: objectLike ? undefined : value
|
|
563
|
+
};
|
|
564
|
+
table.set(value, identity);
|
|
565
|
+
this._bySlot.set(reference.slot(), identity);
|
|
566
|
+
}
|
|
567
|
+
identity.count++;
|
|
568
|
+
identity.pending = false;
|
|
569
|
+
return identity.reference.slot();
|
|
570
|
+
}
|
|
571
|
+
retain(slot) {
|
|
572
|
+
const identity = this._bySlot.get(Number(slot));
|
|
573
|
+
if (!identity || identity.retired)
|
|
574
|
+
return;
|
|
575
|
+
identity.count++;
|
|
576
|
+
identity.pending = false;
|
|
577
|
+
}
|
|
578
|
+
release(slot) {
|
|
579
|
+
const identity = this._bySlot.get(Number(slot));
|
|
580
|
+
if (!identity || identity.retired || identity.count === 0)
|
|
581
|
+
return;
|
|
582
|
+
if (--identity.count !== 0 || identity.pending)
|
|
583
|
+
return;
|
|
584
|
+
identity.pending = true;
|
|
585
|
+
Promise.resolve().then(() => {
|
|
586
|
+
identity.pending = false;
|
|
587
|
+
if (identity.count !== 0 || identity.retired)
|
|
588
|
+
return;
|
|
589
|
+
identity.retired = true;
|
|
590
|
+
this._bySlot.delete(identity.reference.slot());
|
|
591
|
+
if (!identity.objectLike) {
|
|
592
|
+
this._primitives.delete(identity.primitiveValue);
|
|
593
|
+
}
|
|
594
|
+
identity.reference.dispose();
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
538
598
|
class StrongRef extends Disposable {
|
|
539
599
|
constructor(value) {
|
|
540
600
|
super();
|
|
@@ -717,6 +777,7 @@ class Env extends Disposable {
|
|
|
717
777
|
this.store.insert(this);
|
|
718
778
|
this.lastException = new Persistent(ctx.isolate);
|
|
719
779
|
this.bridge = bridge;
|
|
780
|
+
this.address = typeof bridge.address === 'function' ? bridge.address() : bridge.address;
|
|
720
781
|
}
|
|
721
782
|
canCallIntoJs() {
|
|
722
783
|
return true;
|
|
@@ -734,11 +795,11 @@ class Env extends Disposable {
|
|
|
734
795
|
}
|
|
735
796
|
}
|
|
736
797
|
clearLastError() {
|
|
737
|
-
this.bridge.setLastError(this.
|
|
798
|
+
this.bridge.setLastError(this.address, 0, 0, 0);
|
|
738
799
|
return 0;
|
|
739
800
|
}
|
|
740
801
|
setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
|
|
741
|
-
this.bridge.setLastError(this.
|
|
802
|
+
this.bridge.setLastError(this.address, error_code, engine_error_code, engine_reserved);
|
|
742
803
|
return error_code;
|
|
743
804
|
}
|
|
744
805
|
callIntoModule(fn, handleException = handleThrow) {
|
|
@@ -791,7 +852,7 @@ class Env extends Disposable {
|
|
|
791
852
|
RefTracker.finalizeAll(this.reflist);
|
|
792
853
|
this.lastException.reset();
|
|
793
854
|
this.store.dealloc(this.id);
|
|
794
|
-
this.bridge.deleteEnv(this.
|
|
855
|
+
this.bridge.deleteEnv(this.address);
|
|
795
856
|
}
|
|
796
857
|
dispose() {
|
|
797
858
|
if (this.id === 0)
|
|
@@ -887,7 +948,7 @@ class NodeEnv extends Env {
|
|
|
887
948
|
}
|
|
888
949
|
callFinalizerInternal(forceUncaught, cb, data, hint) {
|
|
889
950
|
const f = this.bridge.makeDynCall_vppp(cb);
|
|
890
|
-
const env = this.
|
|
951
|
+
const env = this.address;
|
|
891
952
|
const scope = this.ctx.openScope(this);
|
|
892
953
|
try {
|
|
893
954
|
this.callbackIntoModule(Boolean(forceUncaught), () => { f(env, data, hint); });
|
|
@@ -1478,7 +1539,19 @@ class Template {
|
|
|
1478
1539
|
//#endregion src/Template.ts
|
|
1479
1540
|
|
|
1480
1541
|
//#region src/ObjectTemplate.ts
|
|
1481
|
-
function findHolder(obj,
|
|
1542
|
+
function findHolder(obj, target) {
|
|
1543
|
+
let value = obj;
|
|
1544
|
+
while (value != null) {
|
|
1545
|
+
const keys = Reflect.ownKeys(value);
|
|
1546
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1547
|
+
const key = keys[i];
|
|
1548
|
+
const desc = Object.getOwnPropertyDescriptor(value, key);
|
|
1549
|
+
if (desc && (desc.value === target || desc.get === target || desc.set === target)) {
|
|
1550
|
+
return value;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
value = Object.getPrototypeOf(value);
|
|
1554
|
+
}
|
|
1482
1555
|
return obj;
|
|
1483
1556
|
}
|
|
1484
1557
|
const internalField = new WeakMap();
|
|
@@ -1507,31 +1580,27 @@ class ObjectTemplate extends Template {
|
|
|
1507
1580
|
this._instances = new WeakSet();
|
|
1508
1581
|
this.Ctor = Ctor ?? Object;
|
|
1509
1582
|
}
|
|
1510
|
-
|
|
1511
|
-
const config =
|
|
1512
|
-
name,
|
|
1513
|
-
getterWrap,
|
|
1514
|
-
setterWrap,
|
|
1515
|
-
getter,
|
|
1516
|
-
setter,
|
|
1517
|
-
data,
|
|
1518
|
-
attribute,
|
|
1519
|
-
getterSideEffectType,
|
|
1520
|
-
setterSideEffectType,
|
|
1521
|
-
getterFunction: undefined,
|
|
1522
|
-
setterFunction: undefined
|
|
1523
|
-
};
|
|
1524
|
-
config.getterFunction = getter
|
|
1525
|
-
? this._createAccessorWrapper('getter', config)
|
|
1526
|
-
: undefined;
|
|
1527
|
-
config.setterFunction = setter
|
|
1528
|
-
? this._createAccessorWrapper('setter', config)
|
|
1529
|
-
: undefined;
|
|
1583
|
+
setNativeDataProperty(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType) {
|
|
1584
|
+
const config = this._createAccessorConfig(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType);
|
|
1530
1585
|
this._accessors.set(name, config);
|
|
1531
1586
|
}
|
|
1587
|
+
setNativeDataPropertyOnInstance(instance, name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType) {
|
|
1588
|
+
const config = this._createAccessorConfig(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType);
|
|
1589
|
+
this._instances.add(instance);
|
|
1590
|
+
this._defineAccessor(instance, config);
|
|
1591
|
+
}
|
|
1532
1592
|
setInternalFieldCount(value) {
|
|
1533
1593
|
this.internalFieldCount = value;
|
|
1534
1594
|
}
|
|
1595
|
+
setNamedPropertyHandler(config) {
|
|
1596
|
+
this._namedPropertyHandler = config;
|
|
1597
|
+
}
|
|
1598
|
+
setIndexedPropertyHandler(config) {
|
|
1599
|
+
this._indexedPropertyHandler = config;
|
|
1600
|
+
}
|
|
1601
|
+
setCallAsFunctionHandler(config) {
|
|
1602
|
+
this._callAsFunctionHandler = config;
|
|
1603
|
+
}
|
|
1535
1604
|
_createAccessorWrapper(type, config) {
|
|
1536
1605
|
const { ctx } = this;
|
|
1537
1606
|
const instances = this._instances;
|
|
@@ -1543,6 +1612,15 @@ class ObjectTemplate extends Template {
|
|
|
1543
1612
|
return holder || receiver;
|
|
1544
1613
|
};
|
|
1545
1614
|
function accessor(value) {
|
|
1615
|
+
if (type === 'setter' && !instances.has(this)) {
|
|
1616
|
+
Object.defineProperty(this, config.name, {
|
|
1617
|
+
value,
|
|
1618
|
+
writable: !(config.attribute & 1),
|
|
1619
|
+
enumerable: !(config.attribute & 2),
|
|
1620
|
+
configurable: !(config.attribute & 4)
|
|
1621
|
+
});
|
|
1622
|
+
return undefined;
|
|
1623
|
+
}
|
|
1546
1624
|
const scope = ctx.openScope();
|
|
1547
1625
|
const callbackInfo = scope.callbackInfo;
|
|
1548
1626
|
let returnValue;
|
|
@@ -1575,25 +1653,237 @@ class ObjectTemplate extends Template {
|
|
|
1575
1653
|
}
|
|
1576
1654
|
applyToInstance(instance) {
|
|
1577
1655
|
this._instances.add(instance);
|
|
1578
|
-
|
|
1656
|
+
const fields = Array(this.internalFieldCount);
|
|
1657
|
+
internalField.set(instance, fields);
|
|
1579
1658
|
this._addPropertiesToInstance(instance);
|
|
1580
|
-
this._accessors.forEach((
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1659
|
+
this._accessors.forEach(config => this._defineAccessor(instance, config));
|
|
1660
|
+
if (this._namedPropertyHandler || this._indexedPropertyHandler) {
|
|
1661
|
+
const proxy = this._createPropertyHandlerProxy(instance);
|
|
1662
|
+
this._instances.add(proxy);
|
|
1663
|
+
internalField.set(proxy, fields);
|
|
1664
|
+
return proxy;
|
|
1665
|
+
}
|
|
1666
|
+
return instance;
|
|
1667
|
+
}
|
|
1668
|
+
_createPropertyHandlerProxy(target) {
|
|
1669
|
+
const { ctx } = this;
|
|
1670
|
+
const configuredHandler = this._namedPropertyHandler || this._indexedPropertyHandler;
|
|
1671
|
+
const resolveHandler = (property) => {
|
|
1672
|
+
const index = typeof property === 'string' && /^(?:0|[1-9]\d*)$/.test(property)
|
|
1673
|
+
? Number(property)
|
|
1674
|
+
: undefined;
|
|
1675
|
+
if (index !== undefined && index <= 0xffffffff - 1 && this._indexedPropertyHandler) {
|
|
1676
|
+
return { config: this._indexedPropertyHandler, index };
|
|
1677
|
+
}
|
|
1678
|
+
if (typeof property === 'string' && this._namedPropertyHandler) {
|
|
1679
|
+
return { config: this._namedPropertyHandler };
|
|
1680
|
+
}
|
|
1681
|
+
return undefined;
|
|
1682
|
+
};
|
|
1683
|
+
const invoke = (config, property, receiver, holder, kind, value) => {
|
|
1684
|
+
const wrap = kind === 'getter'
|
|
1685
|
+
? config.getterWrap
|
|
1686
|
+
: kind === 'setter'
|
|
1687
|
+
? config.setterWrap
|
|
1688
|
+
: kind === 'query'
|
|
1689
|
+
? config.queryWrap
|
|
1690
|
+
: kind === 'deleter'
|
|
1691
|
+
? config.deleterWrap
|
|
1692
|
+
: config.enumeratorWrap;
|
|
1693
|
+
if (!wrap)
|
|
1694
|
+
return { intercepted: false, value: undefined };
|
|
1695
|
+
const scope = ctx.openScope();
|
|
1696
|
+
const callbackInfo = scope.callbackInfo;
|
|
1697
|
+
const trap = function () { };
|
|
1698
|
+
callbackInfo.data = config.data;
|
|
1699
|
+
callbackInfo.args = kind === 'setter' ? [value] : [];
|
|
1700
|
+
callbackInfo.thiz = receiver;
|
|
1701
|
+
callbackInfo.holder = holder;
|
|
1702
|
+
callbackInfo.fn = trap;
|
|
1703
|
+
let result = 0;
|
|
1704
|
+
try {
|
|
1705
|
+
const propertyValue = typeof property === 'number'
|
|
1706
|
+
? property
|
|
1707
|
+
: ctx.napiValueFromJsValue(property);
|
|
1708
|
+
if (kind === 'getter') {
|
|
1709
|
+
result = wrap(propertyValue, scope.id, config.getter);
|
|
1710
|
+
}
|
|
1711
|
+
else if (kind === 'setter') {
|
|
1712
|
+
result = wrap(propertyValue, ctx.napiValueFromJsValue(value), scope.id, config.setter);
|
|
1713
|
+
}
|
|
1714
|
+
else if (kind === 'query') {
|
|
1715
|
+
result = wrap(propertyValue, scope.id, config.query);
|
|
1716
|
+
}
|
|
1717
|
+
else if (kind === 'deleter') {
|
|
1718
|
+
result = wrap(propertyValue, scope.id, config.deleter);
|
|
1719
|
+
}
|
|
1720
|
+
else {
|
|
1721
|
+
result = wrap(scope.id, config.enumerator);
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
catch (err) {
|
|
1725
|
+
ctx.throwException(err);
|
|
1726
|
+
}
|
|
1727
|
+
const returnValue = result ? ctx.jsValueFromNapiValue(result) : undefined;
|
|
1728
|
+
ctx.closeScope(scope);
|
|
1729
|
+
if (ctx.hasPendingException()) {
|
|
1730
|
+
if (TryCatch.top) {
|
|
1731
|
+
TryCatch.top.setError(ctx.getAndClearLastException());
|
|
1732
|
+
}
|
|
1733
|
+
else {
|
|
1734
|
+
throw ctx.getAndClearLastException();
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
return {
|
|
1738
|
+
intercepted: Number(result) !== 0,
|
|
1739
|
+
value: returnValue
|
|
1740
|
+
};
|
|
1741
|
+
};
|
|
1742
|
+
const getOwnPropertyDescriptor = (_target, property) => {
|
|
1743
|
+
const handler = resolveHandler(property);
|
|
1744
|
+
if (!handler || !handler.config.queryWrap)
|
|
1745
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
1746
|
+
const result = invoke(handler.config, handler.index === undefined ? String(property) : handler.index, target, target, 'query');
|
|
1747
|
+
if (!result.intercepted)
|
|
1748
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
1749
|
+
if (result.value === undefined)
|
|
1750
|
+
return undefined;
|
|
1751
|
+
const attr = Number(result.value);
|
|
1752
|
+
return {
|
|
1753
|
+
value: Reflect.get(target, property, target),
|
|
1754
|
+
writable: !(attr & 1),
|
|
1755
|
+
enumerable: !(attr & 2),
|
|
1756
|
+
configurable: !(attr & 4)
|
|
1757
|
+
};
|
|
1758
|
+
};
|
|
1759
|
+
let proxy;
|
|
1760
|
+
const proxyHandler = {
|
|
1761
|
+
get(_target, property, receiver) {
|
|
1762
|
+
const handler = resolveHandler(property);
|
|
1763
|
+
if (!handler || !handler.config.getterWrap)
|
|
1764
|
+
return Reflect.get(target, property, receiver);
|
|
1765
|
+
const result = invoke(handler.config, handler.index === undefined ? String(property) : handler.index, receiver, target, 'getter');
|
|
1766
|
+
return result.intercepted ? result.value : Reflect.get(target, property, receiver);
|
|
1767
|
+
},
|
|
1768
|
+
set(_target, property, value, receiver) {
|
|
1769
|
+
const handler = resolveHandler(property);
|
|
1770
|
+
if (!handler || !handler.config.setterWrap)
|
|
1771
|
+
return Reflect.set(target, property, value, receiver);
|
|
1772
|
+
const result = invoke(handler.config, handler.index === undefined ? String(property) : handler.index, receiver, target, 'setter', value);
|
|
1773
|
+
return result.intercepted ? true : Reflect.set(target, property, value, receiver);
|
|
1774
|
+
},
|
|
1775
|
+
has(_target, property) {
|
|
1776
|
+
const handler = resolveHandler(property);
|
|
1777
|
+
if (!handler || !handler.config.queryWrap)
|
|
1778
|
+
return Reflect.has(target, property);
|
|
1779
|
+
const result = invoke(handler.config, handler.index === undefined ? String(property) : handler.index, proxy, target, 'query');
|
|
1780
|
+
return result.intercepted ? result.value !== undefined : Reflect.has(target, property);
|
|
1781
|
+
},
|
|
1782
|
+
deleteProperty(_target, property) {
|
|
1783
|
+
const handler = resolveHandler(property);
|
|
1784
|
+
if (!handler || !handler.config.deleterWrap)
|
|
1785
|
+
return Reflect.deleteProperty(target, property);
|
|
1786
|
+
const result = invoke(handler.config, handler.index === undefined ? String(property) : handler.index, proxy, target, 'deleter');
|
|
1787
|
+
return result.intercepted ? Boolean(result.value) : Reflect.deleteProperty(target, property);
|
|
1788
|
+
},
|
|
1789
|
+
ownKeys(_target) {
|
|
1790
|
+
const handler = configuredHandler;
|
|
1791
|
+
if (!handler || !handler.enumeratorWrap)
|
|
1792
|
+
return Reflect.ownKeys(target);
|
|
1793
|
+
const result = invoke(handler, '', proxy, target, 'enumerator');
|
|
1794
|
+
if (!Array.isArray(result.value))
|
|
1795
|
+
return Reflect.ownKeys(target);
|
|
1796
|
+
const keys = result.value.map(key => typeof key === 'number' ? String(key) : key);
|
|
1797
|
+
for (const key of Reflect.ownKeys(target)) {
|
|
1798
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
|
|
1799
|
+
if (descriptor && !descriptor.configurable && !keys.includes(key))
|
|
1800
|
+
keys.push(key);
|
|
1801
|
+
}
|
|
1802
|
+
return keys;
|
|
1803
|
+
},
|
|
1804
|
+
getOwnPropertyDescriptor,
|
|
1805
|
+
defineProperty(_target, property, descriptor) {
|
|
1806
|
+
return Reflect.defineProperty(target, property, descriptor);
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
proxy = new Proxy(target, proxyHandler);
|
|
1810
|
+
return proxy;
|
|
1811
|
+
}
|
|
1812
|
+
_createAccessorConfig(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType) {
|
|
1813
|
+
const config = {
|
|
1814
|
+
name,
|
|
1815
|
+
getterWrap,
|
|
1816
|
+
setterWrap,
|
|
1817
|
+
getter,
|
|
1818
|
+
setter,
|
|
1819
|
+
data,
|
|
1820
|
+
attribute,
|
|
1821
|
+
getterSideEffectType,
|
|
1822
|
+
setterSideEffectType,
|
|
1823
|
+
getterFunction: undefined,
|
|
1824
|
+
setterFunction: undefined
|
|
1825
|
+
};
|
|
1826
|
+
config.getterFunction = getter
|
|
1827
|
+
? this._createAccessorWrapper('getter', config)
|
|
1828
|
+
: undefined;
|
|
1829
|
+
config.setterFunction = setter
|
|
1830
|
+
? this._createAccessorWrapper('setter', config)
|
|
1831
|
+
: undefined;
|
|
1832
|
+
return config;
|
|
1833
|
+
}
|
|
1834
|
+
_defineAccessor(instance, config) {
|
|
1835
|
+
Object.defineProperty(instance, config.name, {
|
|
1836
|
+
get: config.getterFunction,
|
|
1837
|
+
set: config.setterFunction,
|
|
1838
|
+
enumerable: !(config.attribute & 2),
|
|
1839
|
+
configurable: !(config.attribute & 4)
|
|
1587
1840
|
});
|
|
1588
1841
|
}
|
|
1589
1842
|
newInstance(_context) {
|
|
1590
1843
|
const { ctx, Ctor } = this;
|
|
1591
1844
|
let instance;
|
|
1592
|
-
|
|
1593
|
-
|
|
1845
|
+
if (this._callAsFunctionHandler) {
|
|
1846
|
+
const template = this;
|
|
1847
|
+
instance = function (...args) {
|
|
1848
|
+
const scope = ctx.openScope();
|
|
1849
|
+
const callbackInfo = scope.callbackInfo;
|
|
1850
|
+
callbackInfo.data = template._callAsFunctionHandler.data;
|
|
1851
|
+
callbackInfo.args = args;
|
|
1852
|
+
callbackInfo.thiz = this;
|
|
1853
|
+
callbackInfo.holder = this;
|
|
1854
|
+
callbackInfo.fn = instance;
|
|
1855
|
+
let ret = 0;
|
|
1856
|
+
try {
|
|
1857
|
+
ret = template._callAsFunctionHandler.callbackWrap(scope.id, template._callAsFunctionHandler.callback);
|
|
1858
|
+
}
|
|
1859
|
+
catch (err) {
|
|
1860
|
+
ctx.throwException(err);
|
|
1861
|
+
}
|
|
1862
|
+
const retNumber = typeof ret === 'bigint' ? Number(ret) : ret;
|
|
1863
|
+
const returnValue = retNumber === 0
|
|
1864
|
+
? 0
|
|
1865
|
+
: (() => {
|
|
1866
|
+
const value = ctx.jsValueFromNapiValue(ret);
|
|
1867
|
+
return value === undefined && retNumber !== 1 && retNumber !== 2
|
|
1868
|
+
? retNumber >= 0x100000000 || typeof ret === 'bigint'
|
|
1869
|
+
? Math.trunc(Number(ret) / 0x100000000)
|
|
1870
|
+
: Number(ret) >> 1
|
|
1871
|
+
: value;
|
|
1872
|
+
})();
|
|
1873
|
+
ctx.closeScope(scope);
|
|
1874
|
+
if (ctx.hasPendingException()) {
|
|
1875
|
+
throw ctx.getAndClearLastException();
|
|
1876
|
+
}
|
|
1877
|
+
return returnValue;
|
|
1878
|
+
};
|
|
1594
1879
|
}
|
|
1595
|
-
|
|
1596
|
-
|
|
1880
|
+
else {
|
|
1881
|
+
try {
|
|
1882
|
+
instance = new Ctor();
|
|
1883
|
+
}
|
|
1884
|
+
catch (err) {
|
|
1885
|
+
ctx.throwException(err);
|
|
1886
|
+
}
|
|
1597
1887
|
}
|
|
1598
1888
|
this.applyToInstance(instance);
|
|
1599
1889
|
return instance;
|
|
@@ -1617,8 +1907,12 @@ class FunctionTemplate extends Template {
|
|
|
1617
1907
|
this.data = data;
|
|
1618
1908
|
this.className = undefined;
|
|
1619
1909
|
this.signature = signature;
|
|
1910
|
+
this.callHandler = undefined;
|
|
1620
1911
|
this._cached = [undefined];
|
|
1621
1912
|
}
|
|
1913
|
+
setCallHandler(config) {
|
|
1914
|
+
this.callHandler = config;
|
|
1915
|
+
}
|
|
1622
1916
|
setClassName(name) {
|
|
1623
1917
|
this.className = name;
|
|
1624
1918
|
}
|
|
@@ -1639,6 +1933,7 @@ class FunctionTemplate extends Template {
|
|
|
1639
1933
|
return this._cached[0];
|
|
1640
1934
|
}
|
|
1641
1935
|
const { ctx, callback, v8FunctionCallback, data, signature, _instanceTemplate } = this;
|
|
1936
|
+
const callHandler = this.callHandler;
|
|
1642
1937
|
function _() {
|
|
1643
1938
|
if (signature && signature.receiver) {
|
|
1644
1939
|
const f = signature.receiver.getFunction();
|
|
@@ -1650,16 +1945,36 @@ class FunctionTemplate extends Template {
|
|
|
1650
1945
|
const callbackInfo = scope.callbackInfo;
|
|
1651
1946
|
let returnValue;
|
|
1652
1947
|
try {
|
|
1653
|
-
|
|
1948
|
+
const callbackThis = _instanceTemplate && this instanceof _
|
|
1949
|
+
? _instanceTemplate.applyToInstance(this)
|
|
1950
|
+
: this;
|
|
1951
|
+
callbackInfo.data = callHandler ? callHandler.data : data;
|
|
1654
1952
|
callbackInfo.args = arguments;
|
|
1655
|
-
callbackInfo.thiz =
|
|
1656
|
-
callbackInfo.holder = findHolder(
|
|
1953
|
+
callbackInfo.thiz = callbackThis;
|
|
1954
|
+
callbackInfo.holder = signature ? callbackThis : (findHolder(callbackThis, _) || callbackThis);
|
|
1657
1955
|
callbackInfo.fn = _;
|
|
1658
|
-
|
|
1659
|
-
|
|
1956
|
+
const ret = callHandler
|
|
1957
|
+
? callHandler.callbackWrap(ctx.getCurrentScope().id, callHandler.callback)
|
|
1958
|
+
: callback
|
|
1959
|
+
? callback(ctx.getCurrentScope().id, v8FunctionCallback)
|
|
1960
|
+
: 0;
|
|
1961
|
+
if (callHandler) {
|
|
1962
|
+
const retNumber = typeof ret === 'bigint' ? Number(ret) : ret;
|
|
1963
|
+
if (retNumber === 0) {
|
|
1964
|
+
returnValue = 0;
|
|
1965
|
+
}
|
|
1966
|
+
else {
|
|
1967
|
+
const value = ctx.jsValueFromNapiValue(ret);
|
|
1968
|
+
returnValue = value === undefined && retNumber !== 1 && retNumber !== 2
|
|
1969
|
+
? retNumber >= 0x100000000 || typeof ret === 'bigint'
|
|
1970
|
+
? Math.trunc(Number(ret) / 0x100000000)
|
|
1971
|
+
: Number(ret) >> 1
|
|
1972
|
+
: value;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
else {
|
|
1976
|
+
returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
|
|
1660
1977
|
}
|
|
1661
|
-
const ret = callback(ctx.getCurrentScope().id, v8FunctionCallback);
|
|
1662
|
-
returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
|
|
1663
1978
|
}
|
|
1664
1979
|
catch (err) {
|
|
1665
1980
|
if (err !== 'unwind') {
|
|
@@ -1790,6 +2105,7 @@ class Isolate {
|
|
|
1790
2105
|
this._scopeStore = new ScopeStore();
|
|
1791
2106
|
this._handleStore = new HandleStore(this.features);
|
|
1792
2107
|
this.globalHandleStore = new PersistentStore();
|
|
2108
|
+
this._globalIdentityStore = new GlobalIdentityStore(this);
|
|
1793
2109
|
this._lastException = new Persistent(this);
|
|
1794
2110
|
this._externalMemory = new ExternalMemory(options?.onExternalMemoryChange);
|
|
1795
2111
|
}
|
|
@@ -1825,6 +2141,15 @@ class Isolate {
|
|
|
1825
2141
|
getRef(ref) {
|
|
1826
2142
|
return this.globalHandleStore.deref(ref);
|
|
1827
2143
|
}
|
|
2144
|
+
acquireGlobalValueIdentity(value) {
|
|
2145
|
+
return this._globalIdentityStore.acquire(value);
|
|
2146
|
+
}
|
|
2147
|
+
retainGlobalValueIdentity(slot) {
|
|
2148
|
+
this._globalIdentityStore.retain(slot);
|
|
2149
|
+
}
|
|
2150
|
+
releaseGlobalValueIdentity(slot) {
|
|
2151
|
+
this._globalIdentityStore.release(slot);
|
|
2152
|
+
}
|
|
1828
2153
|
removeRef(ref, force = false) {
|
|
1829
2154
|
if (force || this._scopeStore.isEmpty()) {
|
|
1830
2155
|
const persistent = this.globalHandleStore.deref(ref);
|
|
@@ -2181,10 +2506,10 @@ class Context {
|
|
|
2181
2506
|
}
|
|
2182
2507
|
createFunction(envObject, napiCallback, data, name, dynamicExecution) {
|
|
2183
2508
|
if (envObject.ctx !== this) {
|
|
2184
|
-
throw new Error(`The napi_env (${envObject.
|
|
2509
|
+
throw new Error(`The napi_env (${envObject.address}) is not created by this context`);
|
|
2185
2510
|
}
|
|
2186
2511
|
const callback = (envObject) => {
|
|
2187
|
-
return napiCallback(envObject.
|
|
2512
|
+
return napiCallback(envObject.address, envObject.ctx.getCurrentScope().id);
|
|
2188
2513
|
};
|
|
2189
2514
|
let _;
|
|
2190
2515
|
const staticFunctionWrapper = (withScope, envObject, data, callbackWrapper, callback) => {
|