@emnapi/runtime 0.35.0 → 0.36.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 +52 -35
- package/dist/emnapi.cjs.min.js +1 -1
- package/dist/emnapi.d.ts +3 -2
- package/dist/emnapi.esm-bundler.js +60 -35
- package/dist/emnapi.iife.d.ts +3 -2
- package/dist/emnapi.iife.js +60 -35
- package/dist/emnapi.js +60 -35
- package/dist/emnapi.min.js +1 -1
- package/dist/emnapi.min.mjs +1 -1
- package/dist/emnapi.mjs +52 -35
- package/package.json +1 -1
package/dist/emnapi.cjs.js
CHANGED
|
@@ -155,6 +155,17 @@ const canSetFunctionName = (function () {
|
|
|
155
155
|
})();
|
|
156
156
|
const supportReflect = typeof Reflect === 'object';
|
|
157
157
|
const supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
158
|
+
const supportWeakSymbol = (function () {
|
|
159
|
+
try {
|
|
160
|
+
const sym = Symbol();
|
|
161
|
+
new WeakRef(sym);
|
|
162
|
+
new WeakMap().set(sym, undefined);
|
|
163
|
+
}
|
|
164
|
+
catch (_) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
})();
|
|
158
169
|
const supportBigInt = typeof BigInt !== 'undefined';
|
|
159
170
|
function isReferenceType(v) {
|
|
160
171
|
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
@@ -759,26 +770,37 @@ Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
|
|
|
759
770
|
value: 'NotSupportBigIntError'
|
|
760
771
|
});
|
|
761
772
|
|
|
773
|
+
class StrongRef {
|
|
774
|
+
constructor(value) {
|
|
775
|
+
this._value = value;
|
|
776
|
+
}
|
|
777
|
+
deref() {
|
|
778
|
+
return this._value;
|
|
779
|
+
}
|
|
780
|
+
dispose() {
|
|
781
|
+
this._value = undefined;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
762
784
|
class Persistent {
|
|
763
785
|
constructor(value) {
|
|
764
|
-
this._ref = value;
|
|
786
|
+
this._ref = new StrongRef(value);
|
|
765
787
|
}
|
|
766
788
|
setWeak(param, callback) {
|
|
767
|
-
if (this._ref ===
|
|
768
|
-
return;
|
|
769
|
-
if (!supportFinalizer)
|
|
770
|
-
return;
|
|
771
|
-
if (this._ref instanceof WeakRef)
|
|
789
|
+
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef)
|
|
772
790
|
return;
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
791
|
+
const value = this._ref.deref();
|
|
792
|
+
try {
|
|
793
|
+
Persistent._registry.register(value, this, this);
|
|
794
|
+
const weakRef = new WeakRef(value);
|
|
795
|
+
this._ref.dispose();
|
|
796
|
+
this._ref = weakRef;
|
|
797
|
+
this._param = param;
|
|
798
|
+
this._callback = callback;
|
|
799
|
+
}
|
|
800
|
+
catch (_) { }
|
|
777
801
|
}
|
|
778
802
|
clearWeak() {
|
|
779
|
-
if (this._ref ===
|
|
780
|
-
return;
|
|
781
|
-
if (!supportFinalizer)
|
|
803
|
+
if (!supportFinalizer || this._ref === undefined)
|
|
782
804
|
return;
|
|
783
805
|
if (this._ref instanceof WeakRef) {
|
|
784
806
|
try {
|
|
@@ -787,10 +809,16 @@ class Persistent {
|
|
|
787
809
|
catch (_) { }
|
|
788
810
|
this._param = undefined;
|
|
789
811
|
this._callback = undefined;
|
|
790
|
-
|
|
812
|
+
const value = this._ref.deref();
|
|
813
|
+
if (value === undefined) {
|
|
814
|
+
this._ref = value;
|
|
815
|
+
}
|
|
816
|
+
else {
|
|
817
|
+
this._ref = new StrongRef(value);
|
|
818
|
+
}
|
|
791
819
|
}
|
|
792
820
|
}
|
|
793
|
-
reset(
|
|
821
|
+
reset() {
|
|
794
822
|
if (supportFinalizer) {
|
|
795
823
|
try {
|
|
796
824
|
Persistent._registry.unregister(this);
|
|
@@ -799,32 +827,23 @@ class Persistent {
|
|
|
799
827
|
}
|
|
800
828
|
this._param = undefined;
|
|
801
829
|
this._callback = undefined;
|
|
802
|
-
if (
|
|
803
|
-
this._ref
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
this._ref = null;
|
|
830
|
+
if (this._ref instanceof StrongRef) {
|
|
831
|
+
this._ref.dispose();
|
|
807
832
|
}
|
|
833
|
+
this._ref = undefined;
|
|
808
834
|
}
|
|
809
835
|
isEmpty() {
|
|
810
|
-
return this._ref ===
|
|
836
|
+
return this._ref === undefined;
|
|
811
837
|
}
|
|
812
838
|
deref() {
|
|
813
|
-
|
|
814
|
-
if (!supportFinalizer) {
|
|
815
|
-
return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
|
|
816
|
-
}
|
|
817
|
-
if (this._ref === null)
|
|
839
|
+
if (this._ref === undefined)
|
|
818
840
|
return undefined;
|
|
819
|
-
|
|
820
|
-
return this._ref.deref();
|
|
821
|
-
}
|
|
822
|
-
return this._ref;
|
|
841
|
+
return this._ref.deref();
|
|
823
842
|
}
|
|
824
843
|
}
|
|
825
844
|
Persistent._registry = supportFinalizer
|
|
826
845
|
? new FinalizationRegistry((value) => {
|
|
827
|
-
value._ref =
|
|
846
|
+
value._ref = undefined;
|
|
828
847
|
const callback = value._callback;
|
|
829
848
|
const param = value._param;
|
|
830
849
|
value._callback = undefined;
|
|
@@ -846,9 +865,6 @@ class Reference extends RefBase {
|
|
|
846
865
|
}
|
|
847
866
|
static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
848
867
|
const handle = envObject.ctx.handleStore.get(handle_id);
|
|
849
|
-
if (!isReferenceType(handle.value)) {
|
|
850
|
-
throw new TypeError('Invalid reference value');
|
|
851
|
-
}
|
|
852
868
|
const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
853
869
|
envObject.ctx.refStore.add(ref);
|
|
854
870
|
ref.persistent = new Persistent(handle.value);
|
|
@@ -1017,6 +1033,7 @@ class Context {
|
|
|
1017
1033
|
this.feature = {
|
|
1018
1034
|
supportReflect,
|
|
1019
1035
|
supportFinalizer,
|
|
1036
|
+
supportWeakSymbol,
|
|
1020
1037
|
supportBigInt,
|
|
1021
1038
|
supportNewFunction,
|
|
1022
1039
|
canSetFunctionName,
|
|
@@ -1102,7 +1119,7 @@ function getDefaultContext() {
|
|
|
1102
1119
|
return defaultContext;
|
|
1103
1120
|
}
|
|
1104
1121
|
|
|
1105
|
-
const version = "0.
|
|
1122
|
+
const version = "0.36.0";
|
|
1106
1123
|
|
|
1107
1124
|
exports.CallbackInfo = CallbackInfo;
|
|
1108
1125
|
exports.CallbackInfoStack = CallbackInfoStack;
|
package/dist/emnapi.cjs.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});class e{constructor(e,t,s,i,r){this.parent=e,this.thiz=t,this.data=s,this.args=i,this.fn=r}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}}class t{constructor(){this.current=null}pop(){const e=this.current;null!==e&&(this.current=e.parent)}push(t,s,i,r){const n=new e(this.current,t,s,i,r);return this.current=n,n}dispose(){this.current=null}}class s{constructor(e){this._values=[void 0],this._values.length=e,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=Math.ceil(1.5*e))}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class i extends s{constructor(){super(2)}}const r=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),n=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&r)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}(),o=new Error;class a{constructor(){this._exception=o}hasCaught(){return this._exception!==o}exception(){return this._exception}setError(e){this._exception=e}reset(){this._exception=o}extractException(){const e=this._exception;return this.reset(),e}}const u=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),l="object"==typeof Reflect,c="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,h="undefined"!=typeof BigInt;function p(e){return"object"==typeof e&&null!==e||"function"==typeof e}const f="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)return;let t=new MessageChannel;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)},d=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),_=function(){if("function"==typeof n.Buffer)return n.Buffer;try{return d("buffer").Buffer}catch(e){}}();class v{constructor(e,t){this.id=e,this.value=t}init(e,t){this.id=e,this.value=t}data(e){return e.getObjectBinding(this.value).data}isEmpty(){return 0===this.id}isNumber(){return!this.isEmpty()&&"number"==typeof this.value}isBigInt(){return!this.isEmpty()&&"bigint"==typeof this.value}isString(){return!this.isEmpty()&&"string"==typeof this.value}isFunction(){return!this.isEmpty()&&"function"==typeof this.value}isExternal(){return!this.isEmpty()&&p(this.value)&&null===Object.getPrototypeOf(this.value)}isObject(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value}isArray(){return!this.isEmpty()&&Array.isArray(this.value)}isArrayBuffer(){return!this.isEmpty()&&this.value instanceof ArrayBuffer}isTypedArray(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(){return!this.isEmpty()&&"function"==typeof _&&_.isBuffer(this.value)}isDataView(){return!this.isEmpty()&&this.value instanceof DataView}isDate(){return!this.isEmpty()&&this.value instanceof Date}isPromise(){return!this.isEmpty()&&this.value instanceof Promise}isBoolean(){return!this.isEmpty()&&"boolean"==typeof this.value}isUndefined(){return!this.isEmpty()&&void 0===this.value}isSymbol(){return!this.isEmpty()&&"symbol"==typeof this.value}isNull(){return!this.isEmpty()&&null===this.value}dispose(){0!==this.id&&(this.id=0,this.value=void 0)}}class y extends v{constructor(e,t){super(e,t)}dispose(){}}function g(){Object.setPrototypeOf(this,null)}g.prototype=null;class x{constructor(){this._values=[void 0,x.UNDEFINED,x.NULL,x.FALSE,x.TRUE,x.GLOBAL],this._next=x.MIN_ID}push(e){let t;const s=this._next,i=this._values;return s<i.length?(t=i[s],t.init(s,e)):(t=new v(s,e),i[s]=t),this._next++,t}erase(e,t){this._next=e;const s=this._values;for(let i=e;i<t;++i)s[i].dispose()}get(e){return this._values[e]}swap(e,t){const s=this._values,i=s[e];s[e]=s[t],s[e].id=Number(e),s[t]=i,i.id=Number(t)}dispose(){this._values.length=x.MIN_ID,this._next=x.MIN_ID}}x.UNDEFINED=new y(1,void 0),x.NULL=new y(2,null),x.FALSE=new y(3,!1),x.TRUE=new y(4,!0),x.GLOBAL=new y(5,n),x.MIN_ID=6;class S{constructor(e,t,s,i,r=i){this.handleStore=e,this.id=t,this.parent=s,this.child=null,null!==s&&(s.child=this),this.start=i,this.end=r,this._escapeCalled=!1}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e,t){const s=new g,i=e.ctx.handleStore.push(s);return e.initObjectBinding(s).data=t,this.end++,i}dispose(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class b{constructor(){this._rootScope=new S(null,0,null,1,x.MIN_ID),this.currentScope=this._rootScope}get(e){e=Number(e);let t=this.currentScope;for(;t!==this._rootScope;){if(t.id===e)return t;t=t.parent}}openScope(e){const t=this.currentScope;let s=t.child;return null!==s?(s.start=s.end=t.end,s._escapeCalled=!1):s=new S(e.ctx.handleStore,t.id+1,t,t.end),this.currentScope=s,e.openHandleScopes++,s}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){let e=this.currentScope;for(;null!==e;){e.handleStore=null,e.id=0,e.parent=null,e.start=x.MIN_ID,e.end=x.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class w extends s{constructor(){super(8)}}class k extends s{constructor(){super(8)}}class m{constructor(){this._next=null,this._prev=null}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class E{constructor(e,t=0,s=0,i=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=s,this._finalizeHint=i}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}dispose(){this.envObject=void 0}}class z extends E{constructor(e,t,s,i,r,n){super(e,i,r,n),this._next=null,this._prev=null,this._refcount=t,this._ownership=s,this.link(i?e.finalizing_reflist:e.reflist)}static finalizeAll(e){m.finalizeAll(e)}link(e){m.prototype.link.call(this,e)}unlink(){m.prototype.unlink.call(this)}dispose(){this.unlink(),this.envObject.dequeueFinalizer(this),super.dispose()}data(){return this._finalizeData}ref(){return++this._refcount}unref(){return 0===this._refcount?0:--this._refcount}refCount(){return this._refcount}ownership(){return this._ownership}finalize(){const e=this._ownership,t=this._finalizeCallback,s=this._finalizeData,i=this._finalizeHint;let r;this.resetFinalizer(),this.unlink();let n=!1;if(t){const e=Number(t);try{this.envObject.callFinalizer(e,s,i)}catch(e){n=!0,r=e}}if(0===e&&this.dispose(),n)throw r}}class C{constructor(e,t,s){this.ctx=e,this.makeDynCall_vppp=t,this.makeDynCall_vp=s,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new a,this.refs=1,this.reflist=new m,this.finalizing_reflist=new m,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}static create(e,t,s){const i=new C(e,t,s);return e.envStore.add(i),e.addCleanupHook(i,(()=>{i.unref()}),0),i}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return e.errorCode=0,e.engineErrorCode=0,e.engineReserved=0,0}setLastError(e,t=0,s=0){const i=this.lastError;return i.errorCode=e,i.engineErrorCode=t,i.engineReserved=s,e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e){this.clearLastError();const t=e(this);if(this.tryCatch.hasCaught()){throw this.tryCatch.extractException()}return t}callFinalizer(e,t,s){const i=this.makeDynCall_vppp(e),r=this.id,n=this.ctx.openScope(this);try{this.callIntoModule((()=>{i(r,t,s)}))}finally{this.ctx.closeScope(this,n)}}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),f((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}dispose(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),z.finalizeAll(this.finalizing_reflist),z.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null,data:0};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,s){this.instanceData&&this.instanceData.dispose(),this.instanceData=new z(this,0,0,t,e,s)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class D extends Error{constructor(e){super(e);const t=new.target,s=t.prototype;if(!(this instanceof D)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,s):this.__proto__=s,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(D.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class H extends D{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(H.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class I extends D{constructor(e,t){super(`${e}: The current runtime does not support "BigInt".${t?` ${t}`:""}`)}}Object.defineProperty(I.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});class O{constructor(e){this._ref=e}setWeak(e,t){null!==this._ref&&c&&(this._ref instanceof WeakRef||(this._param=e,this._callback=t,O._registry.register(this._ref,this,this),this._ref=new WeakRef(this._ref)))}clearWeak(){if(null!==this._ref&&c&&this._ref instanceof WeakRef){try{O._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref=this._ref.deref()}}reset(e){if(c)try{O._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref=e||null}isEmpty(){return null===this._ref}deref(){var e;return c?null!==this._ref?this._ref instanceof WeakRef?this._ref.deref():this._ref:void 0:null!==(e=this._ref)&&void 0!==e?e:void 0}}function F(e){e.persistent.reset(),e.envObject.enqueueFinalizer(e)}O._registry=c?new FinalizationRegistry((e=>{e._ref=null;const t=e._callback,s=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(s)})):void 0;class j extends z{constructor(e,t,s,i=0,r=0,n=0){super(e,t>>>0,s,i,r,n),this.id=0}static create(e,t,s,i,r=0,n=0,o=0){const a=e.ctx.handleStore.get(t);if(!p(a.value))throw new TypeError("Invalid reference value");const u=new j(e,s,i,r,n,o);return e.ctx.refStore.add(u),u.persistent=new O(a.value),0===s&&u._setWeak(),u}ref(){if(this.persistent.isEmpty())return 0;const e=super.ref();if(1===e){this.persistent.deref()&&this.persistent.clearWeak()}return e}unref(){if(this.persistent.isEmpty())return 0;const e=this.refCount(),t=super.unref();if(1===e&&0===t){this.persistent.deref()&&this._setWeak()}return t}get(){const e=this.persistent.deref();if(e){return this.envObject.ensureHandle(e).id}return 0}_setWeak(){this.persistent.setWeak(this,F)}finalize(){this.persistent.reset(),super.finalize()}dispose(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.id=0)}}class N{constructor(e,t){this.id=0,this.ctx=e,this.value=t}static create(e,t){const s=new N(e,t);return e.deferredStore.add(s),s}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class R{constructor(e,t,s,i){this.envObject=e,this.fn=t,this.arg=s,this.order=i}}class B{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,s){if(this._cleanupHooks.filter((i=>i.envObject===e&&i.fn===t&&i.arg===s)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new R(e,t,s,this._cleanupHookCounter++))}remove(e,t,s){for(let i=0;i<this._cleanupHooks.length;++i){const r=this._cleanupHooks[i];if(r.envObject===e&&r.fn===t&&r.arg===s)return void this._cleanupHooks.splice(i,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const s=e[t];"number"==typeof s.fn?s.envObject.makeDynCall_vp(s.fn)(s.arg):s.fn(s.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(s),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class L{constructor(){this.refHandle=(new MessageChannel).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class M{constructor(){this.envStore=new i,this.scopeStore=new b,this.refStore=new w,this.deferredStore=new k,this.handleStore=new x,this.cbinfoStack=new t,this.feature={supportReflect:l,supportFinalizer:c,supportBigInt:h,supportNewFunction:r,canSetFunctionName:u,setImmediate:f,Buffer:_},this.cleanupQueue=new B,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new L,process.once("beforeExit",(()=>{this.runCleanup()})))}createNotSupportWeakRefError(e,t){return new H(e,t)}createNotSupportBigIntError(e,t){return new I(e,t)}createReference(e,t,s,i,r=0,n=0,o=0){return j.create(e,t,s,i,r,n,o)}createDeferred(e){return N.create(this,e)}createEnv(e,t){return C.create(this,e,t)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return x.UNDEFINED;case null:return x.NULL;case!0:return x.TRUE;case!1:return x.FALSE;case n:return x.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,s){this.cleanupQueue.add(e,t,s)}removeCleanupHook(e,t,s){this.cleanupQueue.remove(e,t,s)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}dispose(){this.runCleanup()}}let W;function A(){return new M}exports.CallbackInfo=e,exports.CallbackInfoStack=t,exports.ConstHandle=y,exports.Context=M,exports.Deferred=N,exports.DeferredStore=k,exports.EmnapiError=D,exports.Env=C,exports.EnvStore=i,exports.Finalizer=E,exports.Handle=v,exports.HandleScope=S,exports.HandleStore=x,exports.NotSupportBigIntError=I,exports.NotSupportWeakRefError=H,exports.Persistent=O,exports.RefBase=z,exports.RefStore=w,exports.RefTracker=m,exports.Reference=j,exports.ScopeStore=b,exports.Store=s,exports.TryCatch=a,exports.createContext=A,exports.getDefaultContext=function(){return W||(W=A()),W},exports.isReferenceType=p,exports.version="0.35.0";
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});class e{constructor(e,t,s,i,r){this.parent=e,this.thiz=t,this.data=s,this.args=i,this.fn=r}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}}class t{constructor(){this.current=null}pop(){const e=this.current;null!==e&&(this.current=e.parent)}push(t,s,i,r){const n=new e(this.current,t,s,i,r);return this.current=n,n}dispose(){this.current=null}}class s{constructor(e){this._values=[void 0],this._values.length=e,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=Math.ceil(1.5*e))}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class i extends s{constructor(){super(2)}}const r=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),n=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&r)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}(),o=new Error;class a{constructor(){this._exception=o}hasCaught(){return this._exception!==o}exception(){return this._exception}setError(e){this._exception=e}reset(){this._exception=o}extractException(){const e=this._exception;return this.reset(),e}}const u=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),c="object"==typeof Reflect,l="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,h=function(){try{const e=Symbol();new WeakRef(e),(new WeakMap).set(e,void 0)}catch(e){return!1}return!0}(),p="undefined"!=typeof BigInt;function d(e){return"object"==typeof e&&null!==e||"function"==typeof e}const f="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)return;let t=new MessageChannel;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)},_=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),v=function(){if("function"==typeof n.Buffer)return n.Buffer;try{return _("buffer").Buffer}catch(e){}}();class y{constructor(e,t){this.id=e,this.value=t}init(e,t){this.id=e,this.value=t}data(e){return e.getObjectBinding(this.value).data}isEmpty(){return 0===this.id}isNumber(){return!this.isEmpty()&&"number"==typeof this.value}isBigInt(){return!this.isEmpty()&&"bigint"==typeof this.value}isString(){return!this.isEmpty()&&"string"==typeof this.value}isFunction(){return!this.isEmpty()&&"function"==typeof this.value}isExternal(){return!this.isEmpty()&&d(this.value)&&null===Object.getPrototypeOf(this.value)}isObject(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value}isArray(){return!this.isEmpty()&&Array.isArray(this.value)}isArrayBuffer(){return!this.isEmpty()&&this.value instanceof ArrayBuffer}isTypedArray(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(){return!this.isEmpty()&&"function"==typeof v&&v.isBuffer(this.value)}isDataView(){return!this.isEmpty()&&this.value instanceof DataView}isDate(){return!this.isEmpty()&&this.value instanceof Date}isPromise(){return!this.isEmpty()&&this.value instanceof Promise}isBoolean(){return!this.isEmpty()&&"boolean"==typeof this.value}isUndefined(){return!this.isEmpty()&&void 0===this.value}isSymbol(){return!this.isEmpty()&&"symbol"==typeof this.value}isNull(){return!this.isEmpty()&&null===this.value}dispose(){0!==this.id&&(this.id=0,this.value=void 0)}}class g extends y{constructor(e,t){super(e,t)}dispose(){}}function x(){Object.setPrototypeOf(this,null)}x.prototype=null;class S{constructor(){this._values=[void 0,S.UNDEFINED,S.NULL,S.FALSE,S.TRUE,S.GLOBAL],this._next=S.MIN_ID}push(e){let t;const s=this._next,i=this._values;return s<i.length?(t=i[s],t.init(s,e)):(t=new y(s,e),i[s]=t),this._next++,t}erase(e,t){this._next=e;const s=this._values;for(let i=e;i<t;++i)s[i].dispose()}get(e){return this._values[e]}swap(e,t){const s=this._values,i=s[e];s[e]=s[t],s[e].id=Number(e),s[t]=i,i.id=Number(t)}dispose(){this._values.length=S.MIN_ID,this._next=S.MIN_ID}}S.UNDEFINED=new g(1,void 0),S.NULL=new g(2,null),S.FALSE=new g(3,!1),S.TRUE=new g(4,!0),S.GLOBAL=new g(5,n),S.MIN_ID=6;class b{constructor(e,t,s,i,r=i){this.handleStore=e,this.id=t,this.parent=s,this.child=null,null!==s&&(s.child=this),this.start=i,this.end=r,this._escapeCalled=!1}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e,t){const s=new x,i=e.ctx.handleStore.push(s);return e.initObjectBinding(s).data=t,this.end++,i}dispose(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class w{constructor(){this._rootScope=new b(null,0,null,1,S.MIN_ID),this.currentScope=this._rootScope}get(e){e=Number(e);let t=this.currentScope;for(;t!==this._rootScope;){if(t.id===e)return t;t=t.parent}}openScope(e){const t=this.currentScope;let s=t.child;return null!==s?(s.start=s.end=t.end,s._escapeCalled=!1):s=new b(e.ctx.handleStore,t.id+1,t,t.end),this.currentScope=s,e.openHandleScopes++,s}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){let e=this.currentScope;for(;null!==e;){e.handleStore=null,e.id=0,e.parent=null,e.start=S.MIN_ID,e.end=S.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class k extends s{constructor(){super(8)}}class m extends s{constructor(){super(8)}}class E{constructor(){this._next=null,this._prev=null}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class z{constructor(e,t=0,s=0,i=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=s,this._finalizeHint=i}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}dispose(){this.envObject=void 0}}class C extends z{constructor(e,t,s,i,r,n){super(e,i,r,n),this._next=null,this._prev=null,this._refcount=t,this._ownership=s,this.link(i?e.finalizing_reflist:e.reflist)}static finalizeAll(e){E.finalizeAll(e)}link(e){E.prototype.link.call(this,e)}unlink(){E.prototype.unlink.call(this)}dispose(){this.unlink(),this.envObject.dequeueFinalizer(this),super.dispose()}data(){return this._finalizeData}ref(){return++this._refcount}unref(){return 0===this._refcount?0:--this._refcount}refCount(){return this._refcount}ownership(){return this._ownership}finalize(){const e=this._ownership,t=this._finalizeCallback,s=this._finalizeData,i=this._finalizeHint;let r;this.resetFinalizer(),this.unlink();let n=!1;if(t){const e=Number(t);try{this.envObject.callFinalizer(e,s,i)}catch(e){n=!0,r=e}}if(0===e&&this.dispose(),n)throw r}}class D{constructor(e,t,s){this.ctx=e,this.makeDynCall_vppp=t,this.makeDynCall_vp=s,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new a,this.refs=1,this.reflist=new E,this.finalizing_reflist=new E,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}static create(e,t,s){const i=new D(e,t,s);return e.envStore.add(i),e.addCleanupHook(i,(()=>{i.unref()}),0),i}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return e.errorCode=0,e.engineErrorCode=0,e.engineReserved=0,0}setLastError(e,t=0,s=0){const i=this.lastError;return i.errorCode=e,i.engineErrorCode=t,i.engineReserved=s,e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e){this.clearLastError();const t=e(this);if(this.tryCatch.hasCaught()){throw this.tryCatch.extractException()}return t}callFinalizer(e,t,s){const i=this.makeDynCall_vppp(e),r=this.id,n=this.ctx.openScope(this);try{this.callIntoModule((()=>{i(r,t,s)}))}finally{this.ctx.closeScope(this,n)}}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),f((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}dispose(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),C.finalizeAll(this.finalizing_reflist),C.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null,data:0};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,s){this.instanceData&&this.instanceData.dispose(),this.instanceData=new C(this,0,0,t,e,s)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class H extends Error{constructor(e){super(e);const t=new.target,s=t.prototype;if(!(this instanceof H)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,s):this.__proto__=s,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(H.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class O extends H{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(O.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class F extends H{constructor(e,t){super(`${e}: The current runtime does not support "BigInt".${t?` ${t}`:""}`)}}Object.defineProperty(F.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});class I{constructor(e){this._value=e}deref(){return this._value}dispose(){this._value=void 0}}class j{constructor(e){this._ref=new I(e)}setWeak(e,t){if(!l||void 0===this._ref||this._ref instanceof WeakRef)return;const s=this._ref.deref();try{j._registry.register(s,this,this);const i=new WeakRef(s);this._ref.dispose(),this._ref=i,this._param=e,this._callback=t}catch(e){}}clearWeak(){if(l&&void 0!==this._ref&&this._ref instanceof WeakRef){try{j._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0;const e=this._ref.deref();this._ref=void 0===e?e:new I(e)}}reset(){if(l)try{j._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref instanceof I&&this._ref.dispose(),this._ref=void 0}isEmpty(){return void 0===this._ref}deref(){if(void 0!==this._ref)return this._ref.deref()}}function N(e){e.persistent.reset(),e.envObject.enqueueFinalizer(e)}j._registry=l?new FinalizationRegistry((e=>{e._ref=void 0;const t=e._callback,s=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(s)})):void 0;class R extends C{constructor(e,t,s,i=0,r=0,n=0){super(e,t>>>0,s,i,r,n),this.id=0}static create(e,t,s,i,r=0,n=0,o=0){const a=e.ctx.handleStore.get(t),u=new R(e,s,i,r,n,o);return e.ctx.refStore.add(u),u.persistent=new j(a.value),0===s&&u._setWeak(),u}ref(){if(this.persistent.isEmpty())return 0;const e=super.ref();if(1===e){this.persistent.deref()&&this.persistent.clearWeak()}return e}unref(){if(this.persistent.isEmpty())return 0;const e=this.refCount(),t=super.unref();if(1===e&&0===t){this.persistent.deref()&&this._setWeak()}return t}get(){const e=this.persistent.deref();if(e){return this.envObject.ensureHandle(e).id}return 0}_setWeak(){this.persistent.setWeak(this,N)}finalize(){this.persistent.reset(),super.finalize()}dispose(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.id=0)}}class B{constructor(e,t){this.id=0,this.ctx=e,this.value=t}static create(e,t){const s=new B(e,t);return e.deferredStore.add(s),s}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class L{constructor(e,t,s,i){this.envObject=e,this.fn=t,this.arg=s,this.order=i}}class W{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,s){if(this._cleanupHooks.filter((i=>i.envObject===e&&i.fn===t&&i.arg===s)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new L(e,t,s,this._cleanupHookCounter++))}remove(e,t,s){for(let i=0;i<this._cleanupHooks.length;++i){const r=this._cleanupHooks[i];if(r.envObject===e&&r.fn===t&&r.arg===s)return void this._cleanupHooks.splice(i,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const s=e[t];"number"==typeof s.fn?s.envObject.makeDynCall_vp(s.fn)(s.arg):s.fn(s.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(s),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class M{constructor(){this.refHandle=(new MessageChannel).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class A{constructor(){this.envStore=new i,this.scopeStore=new w,this.refStore=new k,this.deferredStore=new m,this.handleStore=new S,this.cbinfoStack=new t,this.feature={supportReflect:c,supportFinalizer:l,supportWeakSymbol:h,supportBigInt:p,supportNewFunction:r,canSetFunctionName:u,setImmediate:f,Buffer:v},this.cleanupQueue=new W,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new M,process.once("beforeExit",(()=>{this.runCleanup()})))}createNotSupportWeakRefError(e,t){return new O(e,t)}createNotSupportBigIntError(e,t){return new F(e,t)}createReference(e,t,s,i,r=0,n=0,o=0){return R.create(e,t,s,i,r,n,o)}createDeferred(e){return B.create(this,e)}createEnv(e,t){return D.create(this,e,t)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return S.UNDEFINED;case null:return S.NULL;case!0:return S.TRUE;case!1:return S.FALSE;case n:return S.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,s){this.cleanupQueue.add(e,t,s)}removeCleanupHook(e,t,s){this.cleanupQueue.remove(e,t,s)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}dispose(){this.runCleanup()}}let T;function P(){return new A}exports.CallbackInfo=e,exports.CallbackInfoStack=t,exports.ConstHandle=g,exports.Context=A,exports.Deferred=B,exports.DeferredStore=m,exports.EmnapiError=H,exports.Env=D,exports.EnvStore=i,exports.Finalizer=z,exports.Handle=y,exports.HandleScope=b,exports.HandleStore=S,exports.NotSupportBigIntError=F,exports.NotSupportWeakRefError=O,exports.Persistent=j,exports.RefBase=C,exports.RefStore=k,exports.RefTracker=E,exports.Reference=R,exports.ScopeStore=w,exports.Store=s,exports.TryCatch=a,exports.createContext=P,exports.getDefaultContext=function(){return T||(T=P()),T},exports.isReferenceType=d,exports.version="0.36.0";
|
package/dist/emnapi.d.ts
CHANGED
|
@@ -279,6 +279,7 @@ export declare class Context {
|
|
|
279
279
|
feature: {
|
|
280
280
|
supportReflect: boolean;
|
|
281
281
|
supportFinalizer: boolean;
|
|
282
|
+
supportWeakSymbol: boolean;
|
|
282
283
|
supportBigInt: boolean;
|
|
283
284
|
supportNewFunction: boolean;
|
|
284
285
|
canSetFunctionName: boolean;
|
|
@@ -471,7 +472,7 @@ export declare class NotSupportWeakRefError extends EmnapiError {
|
|
|
471
472
|
constructor(api: string, message: string);
|
|
472
473
|
}
|
|
473
474
|
|
|
474
|
-
export declare class Persistent<T
|
|
475
|
+
export declare class Persistent<T> {
|
|
475
476
|
private _ref;
|
|
476
477
|
private _param;
|
|
477
478
|
private _callback;
|
|
@@ -479,7 +480,7 @@ export declare class Persistent<T extends object> {
|
|
|
479
480
|
constructor(value: T);
|
|
480
481
|
setWeak<P>(param: P, callback: (param: P) => void): void;
|
|
481
482
|
clearWeak(): void;
|
|
482
|
-
reset(
|
|
483
|
+
reset(): void;
|
|
483
484
|
isEmpty(): boolean;
|
|
484
485
|
deref(): T | undefined;
|
|
485
486
|
}
|
|
@@ -161,6 +161,19 @@ var canSetFunctionName = /*#__PURE__*/ (function () {
|
|
|
161
161
|
})();
|
|
162
162
|
var supportReflect = typeof Reflect === 'object';
|
|
163
163
|
var supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
164
|
+
var supportWeakSymbol = /*#__PURE__*/ (function () {
|
|
165
|
+
try {
|
|
166
|
+
// eslint-disable-next-line symbol-description
|
|
167
|
+
var sym = Symbol();
|
|
168
|
+
// eslint-disable-next-line no-new
|
|
169
|
+
new WeakRef(sym);
|
|
170
|
+
new WeakMap().set(sym, undefined);
|
|
171
|
+
}
|
|
172
|
+
catch (_) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
})();
|
|
164
177
|
var supportBigInt = typeof BigInt !== 'undefined';
|
|
165
178
|
function isReferenceType(v) {
|
|
166
179
|
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
@@ -800,26 +813,43 @@ Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
|
|
|
800
813
|
value: 'NotSupportBigIntError'
|
|
801
814
|
});
|
|
802
815
|
|
|
816
|
+
var StrongRef = /*#__PURE__*/ (function () {
|
|
817
|
+
function StrongRef(value) {
|
|
818
|
+
this._value = value;
|
|
819
|
+
}
|
|
820
|
+
StrongRef.prototype.deref = function () {
|
|
821
|
+
return this._value;
|
|
822
|
+
};
|
|
823
|
+
StrongRef.prototype.dispose = function () {
|
|
824
|
+
this._value = undefined;
|
|
825
|
+
};
|
|
826
|
+
return StrongRef;
|
|
827
|
+
}());
|
|
803
828
|
var Persistent = /*#__PURE__*/ (function () {
|
|
804
829
|
function Persistent(value) {
|
|
805
|
-
this._ref = value;
|
|
830
|
+
this._ref = new StrongRef(value);
|
|
806
831
|
}
|
|
807
832
|
Persistent.prototype.setWeak = function (param, callback) {
|
|
808
|
-
if (this._ref ===
|
|
833
|
+
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef)
|
|
809
834
|
return;
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
835
|
+
var value = this._ref.deref();
|
|
836
|
+
try {
|
|
837
|
+
// try {
|
|
838
|
+
Persistent._registry.register(value, this, this);
|
|
839
|
+
var weakRef = new WeakRef(value);
|
|
840
|
+
this._ref.dispose();
|
|
841
|
+
this._ref = weakRef;
|
|
842
|
+
// } catch (_) {
|
|
843
|
+
// Persistent._registry.register(this._ref, this, this)
|
|
844
|
+
// this._ref = new WeakRef<any>(this._ref)
|
|
845
|
+
// }
|
|
846
|
+
this._param = param;
|
|
847
|
+
this._callback = callback;
|
|
848
|
+
}
|
|
849
|
+
catch (_) { }
|
|
818
850
|
};
|
|
819
851
|
Persistent.prototype.clearWeak = function () {
|
|
820
|
-
if (this._ref ===
|
|
821
|
-
return;
|
|
822
|
-
if (!supportFinalizer)
|
|
852
|
+
if (!supportFinalizer || this._ref === undefined)
|
|
823
853
|
return;
|
|
824
854
|
if (this._ref instanceof WeakRef) {
|
|
825
855
|
try {
|
|
@@ -828,10 +858,16 @@ var Persistent = /*#__PURE__*/ (function () {
|
|
|
828
858
|
catch (_) { }
|
|
829
859
|
this._param = undefined;
|
|
830
860
|
this._callback = undefined;
|
|
831
|
-
|
|
861
|
+
var value = this._ref.deref();
|
|
862
|
+
if (value === undefined) {
|
|
863
|
+
this._ref = value;
|
|
864
|
+
}
|
|
865
|
+
else {
|
|
866
|
+
this._ref = new StrongRef(value);
|
|
867
|
+
}
|
|
832
868
|
}
|
|
833
869
|
};
|
|
834
|
-
Persistent.prototype.reset = function (
|
|
870
|
+
Persistent.prototype.reset = function () {
|
|
835
871
|
if (supportFinalizer) {
|
|
836
872
|
try {
|
|
837
873
|
Persistent._registry.unregister(this);
|
|
@@ -840,31 +876,22 @@ var Persistent = /*#__PURE__*/ (function () {
|
|
|
840
876
|
}
|
|
841
877
|
this._param = undefined;
|
|
842
878
|
this._callback = undefined;
|
|
843
|
-
if (
|
|
844
|
-
this._ref
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
this._ref = null;
|
|
879
|
+
if (this._ref instanceof StrongRef) {
|
|
880
|
+
this._ref.dispose();
|
|
848
881
|
}
|
|
882
|
+
this._ref = undefined;
|
|
849
883
|
};
|
|
850
884
|
Persistent.prototype.isEmpty = function () {
|
|
851
|
-
return this._ref ===
|
|
885
|
+
return this._ref === undefined;
|
|
852
886
|
};
|
|
853
887
|
Persistent.prototype.deref = function () {
|
|
854
|
-
|
|
855
|
-
if (!supportFinalizer) {
|
|
856
|
-
return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
|
|
857
|
-
}
|
|
858
|
-
if (this._ref === null)
|
|
888
|
+
if (this._ref === undefined)
|
|
859
889
|
return undefined;
|
|
860
|
-
|
|
861
|
-
return this._ref.deref();
|
|
862
|
-
}
|
|
863
|
-
return this._ref;
|
|
890
|
+
return this._ref.deref();
|
|
864
891
|
};
|
|
865
892
|
Persistent._registry = supportFinalizer
|
|
866
893
|
? new FinalizationRegistry(function (value) {
|
|
867
|
-
value._ref =
|
|
894
|
+
value._ref = undefined;
|
|
868
895
|
var callback = value._callback;
|
|
869
896
|
var param = value._param;
|
|
870
897
|
value._callback = undefined;
|
|
@@ -896,9 +923,6 @@ var Reference = /*#__PURE__*/ (function (_super) {
|
|
|
896
923
|
if (finalize_data === void 0) { finalize_data = 0; }
|
|
897
924
|
if (finalize_hint === void 0) { finalize_hint = 0; }
|
|
898
925
|
var handle = envObject.ctx.handleStore.get(handle_id);
|
|
899
|
-
if (!isReferenceType(handle.value)) {
|
|
900
|
-
throw new TypeError('Invalid reference value');
|
|
901
|
-
}
|
|
902
926
|
var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
903
927
|
envObject.ctx.refStore.add(ref);
|
|
904
928
|
ref.persistent = new Persistent(handle.value);
|
|
@@ -1073,6 +1097,7 @@ var Context = /*#__PURE__*/ (function () {
|
|
|
1073
1097
|
this.feature = {
|
|
1074
1098
|
supportReflect: supportReflect,
|
|
1075
1099
|
supportFinalizer: supportFinalizer,
|
|
1100
|
+
supportWeakSymbol: supportWeakSymbol,
|
|
1076
1101
|
supportBigInt: supportBigInt,
|
|
1077
1102
|
supportNewFunction: supportNewFunction,
|
|
1078
1103
|
canSetFunctionName: canSetFunctionName,
|
|
@@ -1162,6 +1187,6 @@ function getDefaultContext() {
|
|
|
1162
1187
|
return defaultContext;
|
|
1163
1188
|
}
|
|
1164
1189
|
|
|
1165
|
-
var version = "0.
|
|
1190
|
+
var version = "0.36.0";
|
|
1166
1191
|
|
|
1167
1192
|
export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, DeferredStore, EmnapiError, Env, EnvStore, Finalizer, Handle, HandleScope, HandleStore, NotSupportBigIntError, NotSupportWeakRefError, Persistent, RefBase, RefStore, RefTracker, Reference, ScopeStore, Store, TryCatch, createContext, getDefaultContext, isReferenceType, version };
|
package/dist/emnapi.iife.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export class Context {
|
|
|
36
36
|
feature: {
|
|
37
37
|
supportReflect: boolean;
|
|
38
38
|
supportFinalizer: boolean;
|
|
39
|
+
supportWeakSymbol: boolean;
|
|
39
40
|
supportBigInt: boolean;
|
|
40
41
|
supportNewFunction: boolean;
|
|
41
42
|
canSetFunctionName: boolean;
|
|
@@ -228,7 +229,7 @@ export class NotSupportWeakRefError extends EmnapiError {
|
|
|
228
229
|
constructor(api: string, message: string);
|
|
229
230
|
}
|
|
230
231
|
|
|
231
|
-
export class Persistent<T
|
|
232
|
+
export class Persistent<T> {
|
|
232
233
|
private _ref;
|
|
233
234
|
private _param;
|
|
234
235
|
private _callback;
|
|
@@ -236,7 +237,7 @@ export class Persistent<T extends object> {
|
|
|
236
237
|
constructor(value: T);
|
|
237
238
|
setWeak<P>(param: P, callback: (param: P) => void): void;
|
|
238
239
|
clearWeak(): void;
|
|
239
|
-
reset(
|
|
240
|
+
reset(): void;
|
|
240
241
|
isEmpty(): boolean;
|
|
241
242
|
deref(): T | undefined;
|
|
242
243
|
}
|
package/dist/emnapi.iife.js
CHANGED
|
@@ -192,6 +192,19 @@ var emnapi = (function (exports) {
|
|
|
192
192
|
})();
|
|
193
193
|
var supportReflect = typeof Reflect === 'object';
|
|
194
194
|
var supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
195
|
+
var supportWeakSymbol = /*#__PURE__*/ (function () {
|
|
196
|
+
try {
|
|
197
|
+
// eslint-disable-next-line symbol-description
|
|
198
|
+
var sym = Symbol();
|
|
199
|
+
// eslint-disable-next-line no-new
|
|
200
|
+
new WeakRef(sym);
|
|
201
|
+
new WeakMap().set(sym, undefined);
|
|
202
|
+
}
|
|
203
|
+
catch (_) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
})();
|
|
195
208
|
var supportBigInt = typeof BigInt !== 'undefined';
|
|
196
209
|
function isReferenceType(v) {
|
|
197
210
|
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
@@ -831,26 +844,43 @@ var emnapi = (function (exports) {
|
|
|
831
844
|
value: 'NotSupportBigIntError'
|
|
832
845
|
});
|
|
833
846
|
|
|
847
|
+
var StrongRef = /*#__PURE__*/ (function () {
|
|
848
|
+
function StrongRef(value) {
|
|
849
|
+
this._value = value;
|
|
850
|
+
}
|
|
851
|
+
StrongRef.prototype.deref = function () {
|
|
852
|
+
return this._value;
|
|
853
|
+
};
|
|
854
|
+
StrongRef.prototype.dispose = function () {
|
|
855
|
+
this._value = undefined;
|
|
856
|
+
};
|
|
857
|
+
return StrongRef;
|
|
858
|
+
}());
|
|
834
859
|
var Persistent = /*#__PURE__*/ (function () {
|
|
835
860
|
function Persistent(value) {
|
|
836
|
-
this._ref = value;
|
|
861
|
+
this._ref = new StrongRef(value);
|
|
837
862
|
}
|
|
838
863
|
Persistent.prototype.setWeak = function (param, callback) {
|
|
839
|
-
if (this._ref ===
|
|
864
|
+
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef)
|
|
840
865
|
return;
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
866
|
+
var value = this._ref.deref();
|
|
867
|
+
try {
|
|
868
|
+
// try {
|
|
869
|
+
Persistent._registry.register(value, this, this);
|
|
870
|
+
var weakRef = new WeakRef(value);
|
|
871
|
+
this._ref.dispose();
|
|
872
|
+
this._ref = weakRef;
|
|
873
|
+
// } catch (_) {
|
|
874
|
+
// Persistent._registry.register(this._ref, this, this)
|
|
875
|
+
// this._ref = new WeakRef<any>(this._ref)
|
|
876
|
+
// }
|
|
877
|
+
this._param = param;
|
|
878
|
+
this._callback = callback;
|
|
879
|
+
}
|
|
880
|
+
catch (_) { }
|
|
849
881
|
};
|
|
850
882
|
Persistent.prototype.clearWeak = function () {
|
|
851
|
-
if (this._ref ===
|
|
852
|
-
return;
|
|
853
|
-
if (!supportFinalizer)
|
|
883
|
+
if (!supportFinalizer || this._ref === undefined)
|
|
854
884
|
return;
|
|
855
885
|
if (this._ref instanceof WeakRef) {
|
|
856
886
|
try {
|
|
@@ -859,10 +889,16 @@ var emnapi = (function (exports) {
|
|
|
859
889
|
catch (_) { }
|
|
860
890
|
this._param = undefined;
|
|
861
891
|
this._callback = undefined;
|
|
862
|
-
|
|
892
|
+
var value = this._ref.deref();
|
|
893
|
+
if (value === undefined) {
|
|
894
|
+
this._ref = value;
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
this._ref = new StrongRef(value);
|
|
898
|
+
}
|
|
863
899
|
}
|
|
864
900
|
};
|
|
865
|
-
Persistent.prototype.reset = function (
|
|
901
|
+
Persistent.prototype.reset = function () {
|
|
866
902
|
if (supportFinalizer) {
|
|
867
903
|
try {
|
|
868
904
|
Persistent._registry.unregister(this);
|
|
@@ -871,31 +907,22 @@ var emnapi = (function (exports) {
|
|
|
871
907
|
}
|
|
872
908
|
this._param = undefined;
|
|
873
909
|
this._callback = undefined;
|
|
874
|
-
if (
|
|
875
|
-
this._ref
|
|
876
|
-
}
|
|
877
|
-
else {
|
|
878
|
-
this._ref = null;
|
|
910
|
+
if (this._ref instanceof StrongRef) {
|
|
911
|
+
this._ref.dispose();
|
|
879
912
|
}
|
|
913
|
+
this._ref = undefined;
|
|
880
914
|
};
|
|
881
915
|
Persistent.prototype.isEmpty = function () {
|
|
882
|
-
return this._ref ===
|
|
916
|
+
return this._ref === undefined;
|
|
883
917
|
};
|
|
884
918
|
Persistent.prototype.deref = function () {
|
|
885
|
-
|
|
886
|
-
if (!supportFinalizer) {
|
|
887
|
-
return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
|
|
888
|
-
}
|
|
889
|
-
if (this._ref === null)
|
|
919
|
+
if (this._ref === undefined)
|
|
890
920
|
return undefined;
|
|
891
|
-
|
|
892
|
-
return this._ref.deref();
|
|
893
|
-
}
|
|
894
|
-
return this._ref;
|
|
921
|
+
return this._ref.deref();
|
|
895
922
|
};
|
|
896
923
|
Persistent._registry = supportFinalizer
|
|
897
924
|
? new FinalizationRegistry(function (value) {
|
|
898
|
-
value._ref =
|
|
925
|
+
value._ref = undefined;
|
|
899
926
|
var callback = value._callback;
|
|
900
927
|
var param = value._param;
|
|
901
928
|
value._callback = undefined;
|
|
@@ -927,9 +954,6 @@ var emnapi = (function (exports) {
|
|
|
927
954
|
if (finalize_data === void 0) { finalize_data = 0; }
|
|
928
955
|
if (finalize_hint === void 0) { finalize_hint = 0; }
|
|
929
956
|
var handle = envObject.ctx.handleStore.get(handle_id);
|
|
930
|
-
if (!isReferenceType(handle.value)) {
|
|
931
|
-
throw new TypeError('Invalid reference value');
|
|
932
|
-
}
|
|
933
957
|
var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
934
958
|
envObject.ctx.refStore.add(ref);
|
|
935
959
|
ref.persistent = new Persistent(handle.value);
|
|
@@ -1104,6 +1128,7 @@ var emnapi = (function (exports) {
|
|
|
1104
1128
|
this.feature = {
|
|
1105
1129
|
supportReflect: supportReflect,
|
|
1106
1130
|
supportFinalizer: supportFinalizer,
|
|
1131
|
+
supportWeakSymbol: supportWeakSymbol,
|
|
1107
1132
|
supportBigInt: supportBigInt,
|
|
1108
1133
|
supportNewFunction: supportNewFunction,
|
|
1109
1134
|
canSetFunctionName: canSetFunctionName,
|
|
@@ -1193,7 +1218,7 @@ var emnapi = (function (exports) {
|
|
|
1193
1218
|
return defaultContext;
|
|
1194
1219
|
}
|
|
1195
1220
|
|
|
1196
|
-
var version = "0.
|
|
1221
|
+
var version = "0.36.0";
|
|
1197
1222
|
|
|
1198
1223
|
exports.CallbackInfo = CallbackInfo;
|
|
1199
1224
|
exports.CallbackInfoStack = CallbackInfoStack;
|
package/dist/emnapi.js
CHANGED
|
@@ -195,6 +195,19 @@
|
|
|
195
195
|
})();
|
|
196
196
|
var supportReflect = typeof Reflect === 'object';
|
|
197
197
|
var supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
198
|
+
var supportWeakSymbol = /*#__PURE__*/ (function () {
|
|
199
|
+
try {
|
|
200
|
+
// eslint-disable-next-line symbol-description
|
|
201
|
+
var sym = Symbol();
|
|
202
|
+
// eslint-disable-next-line no-new
|
|
203
|
+
new WeakRef(sym);
|
|
204
|
+
new WeakMap().set(sym, undefined);
|
|
205
|
+
}
|
|
206
|
+
catch (_) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
})();
|
|
198
211
|
var supportBigInt = typeof BigInt !== 'undefined';
|
|
199
212
|
function isReferenceType(v) {
|
|
200
213
|
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
@@ -834,26 +847,43 @@
|
|
|
834
847
|
value: 'NotSupportBigIntError'
|
|
835
848
|
});
|
|
836
849
|
|
|
850
|
+
var StrongRef = /*#__PURE__*/ (function () {
|
|
851
|
+
function StrongRef(value) {
|
|
852
|
+
this._value = value;
|
|
853
|
+
}
|
|
854
|
+
StrongRef.prototype.deref = function () {
|
|
855
|
+
return this._value;
|
|
856
|
+
};
|
|
857
|
+
StrongRef.prototype.dispose = function () {
|
|
858
|
+
this._value = undefined;
|
|
859
|
+
};
|
|
860
|
+
return StrongRef;
|
|
861
|
+
}());
|
|
837
862
|
var Persistent = /*#__PURE__*/ (function () {
|
|
838
863
|
function Persistent(value) {
|
|
839
|
-
this._ref = value;
|
|
864
|
+
this._ref = new StrongRef(value);
|
|
840
865
|
}
|
|
841
866
|
Persistent.prototype.setWeak = function (param, callback) {
|
|
842
|
-
if (this._ref ===
|
|
867
|
+
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef)
|
|
843
868
|
return;
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
869
|
+
var value = this._ref.deref();
|
|
870
|
+
try {
|
|
871
|
+
// try {
|
|
872
|
+
Persistent._registry.register(value, this, this);
|
|
873
|
+
var weakRef = new WeakRef(value);
|
|
874
|
+
this._ref.dispose();
|
|
875
|
+
this._ref = weakRef;
|
|
876
|
+
// } catch (_) {
|
|
877
|
+
// Persistent._registry.register(this._ref, this, this)
|
|
878
|
+
// this._ref = new WeakRef<any>(this._ref)
|
|
879
|
+
// }
|
|
880
|
+
this._param = param;
|
|
881
|
+
this._callback = callback;
|
|
882
|
+
}
|
|
883
|
+
catch (_) { }
|
|
852
884
|
};
|
|
853
885
|
Persistent.prototype.clearWeak = function () {
|
|
854
|
-
if (this._ref ===
|
|
855
|
-
return;
|
|
856
|
-
if (!supportFinalizer)
|
|
886
|
+
if (!supportFinalizer || this._ref === undefined)
|
|
857
887
|
return;
|
|
858
888
|
if (this._ref instanceof WeakRef) {
|
|
859
889
|
try {
|
|
@@ -862,10 +892,16 @@
|
|
|
862
892
|
catch (_) { }
|
|
863
893
|
this._param = undefined;
|
|
864
894
|
this._callback = undefined;
|
|
865
|
-
|
|
895
|
+
var value = this._ref.deref();
|
|
896
|
+
if (value === undefined) {
|
|
897
|
+
this._ref = value;
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
this._ref = new StrongRef(value);
|
|
901
|
+
}
|
|
866
902
|
}
|
|
867
903
|
};
|
|
868
|
-
Persistent.prototype.reset = function (
|
|
904
|
+
Persistent.prototype.reset = function () {
|
|
869
905
|
if (supportFinalizer) {
|
|
870
906
|
try {
|
|
871
907
|
Persistent._registry.unregister(this);
|
|
@@ -874,31 +910,22 @@
|
|
|
874
910
|
}
|
|
875
911
|
this._param = undefined;
|
|
876
912
|
this._callback = undefined;
|
|
877
|
-
if (
|
|
878
|
-
this._ref
|
|
879
|
-
}
|
|
880
|
-
else {
|
|
881
|
-
this._ref = null;
|
|
913
|
+
if (this._ref instanceof StrongRef) {
|
|
914
|
+
this._ref.dispose();
|
|
882
915
|
}
|
|
916
|
+
this._ref = undefined;
|
|
883
917
|
};
|
|
884
918
|
Persistent.prototype.isEmpty = function () {
|
|
885
|
-
return this._ref ===
|
|
919
|
+
return this._ref === undefined;
|
|
886
920
|
};
|
|
887
921
|
Persistent.prototype.deref = function () {
|
|
888
|
-
|
|
889
|
-
if (!supportFinalizer) {
|
|
890
|
-
return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
|
|
891
|
-
}
|
|
892
|
-
if (this._ref === null)
|
|
922
|
+
if (this._ref === undefined)
|
|
893
923
|
return undefined;
|
|
894
|
-
|
|
895
|
-
return this._ref.deref();
|
|
896
|
-
}
|
|
897
|
-
return this._ref;
|
|
924
|
+
return this._ref.deref();
|
|
898
925
|
};
|
|
899
926
|
Persistent._registry = supportFinalizer
|
|
900
927
|
? new FinalizationRegistry(function (value) {
|
|
901
|
-
value._ref =
|
|
928
|
+
value._ref = undefined;
|
|
902
929
|
var callback = value._callback;
|
|
903
930
|
var param = value._param;
|
|
904
931
|
value._callback = undefined;
|
|
@@ -930,9 +957,6 @@
|
|
|
930
957
|
if (finalize_data === void 0) { finalize_data = 0; }
|
|
931
958
|
if (finalize_hint === void 0) { finalize_hint = 0; }
|
|
932
959
|
var handle = envObject.ctx.handleStore.get(handle_id);
|
|
933
|
-
if (!isReferenceType(handle.value)) {
|
|
934
|
-
throw new TypeError('Invalid reference value');
|
|
935
|
-
}
|
|
936
960
|
var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
937
961
|
envObject.ctx.refStore.add(ref);
|
|
938
962
|
ref.persistent = new Persistent(handle.value);
|
|
@@ -1107,6 +1131,7 @@
|
|
|
1107
1131
|
this.feature = {
|
|
1108
1132
|
supportReflect: supportReflect,
|
|
1109
1133
|
supportFinalizer: supportFinalizer,
|
|
1134
|
+
supportWeakSymbol: supportWeakSymbol,
|
|
1110
1135
|
supportBigInt: supportBigInt,
|
|
1111
1136
|
supportNewFunction: supportNewFunction,
|
|
1112
1137
|
canSetFunctionName: canSetFunctionName,
|
|
@@ -1196,7 +1221,7 @@
|
|
|
1196
1221
|
return defaultContext;
|
|
1197
1222
|
}
|
|
1198
1223
|
|
|
1199
|
-
var version = "0.
|
|
1224
|
+
var version = "0.36.0";
|
|
1200
1225
|
|
|
1201
1226
|
exports.CallbackInfo = CallbackInfo;
|
|
1202
1227
|
exports.CallbackInfoStack = CallbackInfoStack;
|
package/dist/emnapi.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).emnapi={})}(this,(function(t){var e=function(){function t(t,e,n,i,r){this.parent=t,this.thiz=e,this.data=n,this.args=i,this.fn=r}return t.prototype.getNewTarget=function(t){var e=this.thiz;return null==e||null==e.constructor?0:e instanceof this.fn?t.ensureHandleId(e.constructor):0},t}(),n=function(){function t(){this.current=null}return t.prototype.pop=function(){var t=this.current;null!==t&&(this.current=t.parent)},t.prototype.push=function(t,n,i,r){var o=new e(this.current,t,n,i,r);return this.current=o,o},t.prototype.dispose=function(){this.current=null},t}(),i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},i(t,e)};function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var o=function(){function t(t){this._values=[void 0],this._values.length=t,this._size=1,this._freeList=[]}return t.prototype.add=function(t){var e;if(this._freeList.length)e=this._freeList.shift();else{e=this._size,this._size++;var n=this._values.length;e>=n&&(this._values.length=Math.ceil(1.5*n))}t.id=e,this._values[e]=t},t.prototype.get=function(t){return this._values[t]},t.prototype.has=function(t){return void 0!==this._values[t]},t.prototype.remove=function(t){var e=this._values[t];e&&(e.id=0,this._values[t]=void 0,this._freeList.push(Number(t)))},t.prototype.dispose=function(){for(var t=1;t<this._size;++t){var e=this._values[t];null==e||e.dispose()}this._values=[void 0],this._size=1,this._freeList=[]},t}(),s=function(t){function e(){return t.call(this,2)||this}return r(e,t),e}(o),u=function(){var t;try{t=new Function}catch(t){return!1}return"function"==typeof t}(),a=function(){if("undefined"!=typeof globalThis)return globalThis;var t=function(){return this}();if(!t&&u)try{t=new Function("return this")()}catch(t){}if(!t){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return t}(),p=new Error,c=function(){function t(){this._exception=p}return t.prototype.hasCaught=function(){return this._exception!==p},t.prototype.exception=function(){return this._exception},t.prototype.setError=function(t){this._exception=t},t.prototype.reset=function(){this._exception=p},t.prototype.extractException=function(){var t=this._exception;return this.reset(),t},t}(),f=function(){var t;try{return Boolean(null===(t=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===t?void 0:t.configurable)}catch(t){return!1}}(),l="object"==typeof Reflect,h="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,d="undefined"!=typeof BigInt;function y(t){return"object"==typeof t&&null!==t||"function"==typeof t}var v="function"==typeof setImmediate?setImmediate:function(t){if("function"==typeof t){var e=new MessageChannel;e.port1.onmessage=function(){e.port1.onmessage=null,e=void 0,t()},e.port2.postMessage(null)}},_=function(){return"undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0}(),g=function(){if("function"==typeof a.Buffer)return a.Buffer;try{return _("buffer").Buffer}catch(t){}}(),b=function(){function t(t,e){this.id=t,this.value=e}return t.prototype.init=function(t,e){this.id=t,this.value=e},t.prototype.data=function(t){return t.getObjectBinding(this.value).data},t.prototype.isEmpty=function(){return 0===this.id},t.prototype.isNumber=function(){return!this.isEmpty()&&"number"==typeof this.value},t.prototype.isBigInt=function(){return!this.isEmpty()&&"bigint"==typeof this.value},t.prototype.isString=function(){return!this.isEmpty()&&"string"==typeof this.value},t.prototype.isFunction=function(){return!this.isEmpty()&&"function"==typeof this.value},t.prototype.isExternal=function(){return!this.isEmpty()&&y(this.value)&&null===Object.getPrototypeOf(this.value)},t.prototype.isObject=function(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value},t.prototype.isArray=function(){return!this.isEmpty()&&Array.isArray(this.value)},t.prototype.isArrayBuffer=function(){return!this.isEmpty()&&this.value instanceof ArrayBuffer},t.prototype.isTypedArray=function(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)},t.prototype.isBuffer=function(){return!this.isEmpty()&&"function"==typeof g&&g.isBuffer(this.value)},t.prototype.isDataView=function(){return!this.isEmpty()&&this.value instanceof DataView},t.prototype.isDate=function(){return!this.isEmpty()&&this.value instanceof Date},t.prototype.isPromise=function(){return!this.isEmpty()&&this.value instanceof Promise},t.prototype.isBoolean=function(){return!this.isEmpty()&&"boolean"==typeof this.value},t.prototype.isUndefined=function(){return!this.isEmpty()&&void 0===this.value},t.prototype.isSymbol=function(){return!this.isEmpty()&&"symbol"==typeof this.value},t.prototype.isNull=function(){return!this.isEmpty()&&null===this.value},t.prototype.dispose=function(){0!==this.id&&(this.id=0,this.value=void 0)},t}(),S=function(t){function e(e,n){return t.call(this,e,n)||this}return r(e,t),e.prototype.dispose=function(){},e}(b);function w(){Object.setPrototypeOf(this,null)}w.prototype=null;var m=function(){function t(){this._values=[void 0,t.UNDEFINED,t.NULL,t.FALSE,t.TRUE,t.GLOBAL],this._next=t.MIN_ID}return t.prototype.push=function(t){var e,n=this._next,i=this._values;return n<i.length?(e=i[n]).init(n,t):(e=new b(n,t),i[n]=e),this._next++,e},t.prototype.erase=function(t,e){this._next=t;for(var n=this._values,i=t;i<e;++i)n[i].dispose()},t.prototype.get=function(t){return this._values[t]},t.prototype.swap=function(t,e){var n=this._values,i=n[t];n[t]=n[e],n[t].id=Number(t),n[e]=i,i.id=Number(e)},t.prototype.dispose=function(){this._values.length=t.MIN_ID,this._next=t.MIN_ID},t.UNDEFINED=new S(1,void 0),t.NULL=new S(2,null),t.FALSE=new S(3,!1),t.TRUE=new S(4,!0),t.GLOBAL=new S(5,a),t.MIN_ID=6,t}(),E=function(){function t(t,e,n,i,r){void 0===r&&(r=i),this.handleStore=t,this.id=e,this.parent=n,this.child=null,null!==n&&(n.child=this),this.start=i,this.end=r,this._escapeCalled=!1}return t.prototype.add=function(t){var e=this.handleStore.push(t);return this.end++,e},t.prototype.addExternal=function(t,e){var n=new w,i=t.ctx.handleStore.push(n);return t.initObjectBinding(n).data=e,this.end++,i},t.prototype.dispose=function(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)},t.prototype.escape=function(t){if(this._escapeCalled)return null;if(this._escapeCalled=!0,t<this.start||t>=this.end)return null;this.handleStore.swap(t,this.start);var e=this.handleStore.get(this.start);return this.start++,this.parent.end++,e},t.prototype.escapeCalled=function(){return this._escapeCalled},t}(),k=function(){function t(){this._rootScope=new E(null,0,null,1,m.MIN_ID),this.currentScope=this._rootScope}return t.prototype.get=function(t){t=Number(t);for(var e=this.currentScope;e!==this._rootScope;){if(e.id===t)return e;e=e.parent}},t.prototype.openScope=function(t){var e=this.currentScope,n=e.child;return null!==n?(n.start=n.end=e.end,n._escapeCalled=!1):n=new E(t.ctx.handleStore,e.id+1,e,e.end),this.currentScope=n,t.openHandleScopes++,n},t.prototype.closeScope=function(t){if(0!==t.openHandleScopes){var e=this.currentScope;this.currentScope=e.parent,e.dispose(),t.openHandleScopes--}},t.prototype.dispose=function(){for(var t=this.currentScope;null!==t;){t.handleStore=null,t.id=0,t.parent=null,t.start=m.MIN_ID,t.end=m.MIN_ID,t._escapeCalled=!1;var e=t.child;t.child=null,t=e}this.currentScope=null},t}(),z=function(t){function e(){return t.call(this,8)||this}return r(e,t),e}(o),C=function(t){function e(){return t.call(this,8)||this}return r(e,t),e}(o),x=function(){function t(){this._next=null,this._prev=null}return t.prototype.finalize=function(){},t.prototype.link=function(t){this._prev=t,this._next=t._next,null!==this._next&&(this._next._prev=this),t._next=this},t.prototype.unlink=function(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null},t.finalizeAll=function(t){for(;null!==t._next;)t._next.finalize()},t}(),O=function(){function t(t,e,n,i){void 0===e&&(e=0),void 0===n&&(n=0),void 0===i&&(i=0),this.envObject=t,this._finalizeCallback=e,this._finalizeData=n,this._finalizeHint=i}return t.prototype.callback=function(){return this._finalizeCallback},t.prototype.data=function(){return this._finalizeData},t.prototype.hint=function(){return this._finalizeHint},t.prototype.resetFinalizer=function(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0},t.prototype.dispose=function(){this.envObject=void 0},t}(),D=function(t){function e(e,n,i,r,o,s){var u=t.call(this,e,r,o,s)||this;return u._next=null,u._prev=null,u._refcount=n,u._ownership=i,u.link(r?e.finalizing_reflist:e.reflist),u}return r(e,t),e.finalizeAll=function(t){x.finalizeAll(t)},e.prototype.link=function(t){x.prototype.link.call(this,t)},e.prototype.unlink=function(){x.prototype.unlink.call(this)},e.prototype.dispose=function(){this.unlink(),this.envObject.dequeueFinalizer(this),t.prototype.dispose.call(this)},e.prototype.data=function(){return this._finalizeData},e.prototype.ref=function(){return++this._refcount},e.prototype.unref=function(){return 0===this._refcount?0:--this._refcount},e.prototype.refCount=function(){return this._refcount},e.prototype.ownership=function(){return this._ownership},e.prototype.finalize=function(){var t,e=this._ownership,n=this._finalizeCallback,i=this._finalizeData,r=this._finalizeHint;this.resetFinalizer(),this.unlink();var o=!1;if(n){var s=Number(n);try{this.envObject.callFinalizer(s,i,r)}catch(e){o=!0,t=e}}if(0===e&&this.dispose(),o)throw t},e}(O),H=function(){function t(t,e,n){this.ctx=t,this.makeDynCall_vppp=e,this.makeDynCall_vp=n,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new c,this.refs=1,this.reflist=new x,this.finalizing_reflist=new x,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}return t.create=function(e,n,i){var r=new t(e,n,i);return e.envStore.add(r),e.addCleanupHook(r,(function(){r.unref()}),0),r},t.prototype.ref=function(){this.refs++},t.prototype.unref=function(){this.refs--,0===this.refs&&this.dispose()},t.prototype.ensureHandle=function(t){return this.ctx.ensureHandle(t)},t.prototype.ensureHandleId=function(t){return this.ensureHandle(t).id},t.prototype.clearLastError=function(){var t=this.lastError;return t.errorCode=0,t.engineErrorCode=0,t.engineReserved=0,0},t.prototype.setLastError=function(t,e,n){void 0===e&&(e=0),void 0===n&&(n=0);var i=this.lastError;return i.errorCode=t,i.engineErrorCode=e,i.engineReserved=n,t},t.prototype.getReturnStatus=function(){return this.tryCatch.hasCaught()?this.setLastError(10):0},t.prototype.callIntoModule=function(t){this.clearLastError();var e=t(this);if(this.tryCatch.hasCaught())throw this.tryCatch.extractException();return e},t.prototype.callFinalizer=function(t,e,n){var i=this.makeDynCall_vppp(t),r=this.id,o=this.ctx.openScope(this);try{this.callIntoModule((function(){i(r,e,n)}))}finally{this.ctx.closeScope(this,o)}},t.prototype.enqueueFinalizer=function(t){var e=this;-1===this.pendingFinalizers.indexOf(t)&&this.pendingFinalizers.push(t),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),v((function(){e.finalizationScheduled=!1,e.unref(),e.drainFinalizerQueue()})))},t.prototype.dequeueFinalizer=function(t){var e=this.pendingFinalizers.indexOf(t);-1!==e&&this.pendingFinalizers.splice(e,1)},t.prototype.drainFinalizerQueue=function(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}},t.prototype.dispose=function(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),D.finalizeAll(this.finalizing_reflist),D.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)},t.prototype.initObjectBinding=function(t){var e={wrapped:0,tag:null,data:0};return this._bindingMap.set(t,e),e},t.prototype.getObjectBinding=function(t){return this._bindingMap.has(t)?this._bindingMap.get(t):this.initObjectBinding(t)},t.prototype.setInstanceData=function(t,e,n){this.instanceData&&this.instanceData.dispose(),this.instanceData=new D(this,0,0,e,t,n)},t.prototype.getInstanceData=function(){return this.instanceData?this.instanceData.data():0},t}(),j=function(t){function e(n){var i=this.constructor,r=t.call(this,n)||this,o=i,s=o.prototype;if(!(r instanceof e)){var u=Object.setPrototypeOf;"function"==typeof u?u.call(Object,r,s):r.__proto__=s,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(r,o)}return r}return r(e,t),e}(Error);Object.defineProperty(j.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});var I=function(t){function e(e,n){return t.call(this,"".concat(e,': The current runtime does not support "FinalizationRegistry" and "WeakRef".').concat(n?" ".concat(n):""))||this}return r(e,t),e}(j);Object.defineProperty(I.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});var F=function(t){function e(e,n){return t.call(this,"".concat(e,': The current runtime does not support "BigInt".').concat(n?" ".concat(n):""))||this}return r(e,t),e}(j);Object.defineProperty(F.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});var N=function(){function t(t){this._ref=t}return t.prototype.setWeak=function(e,n){null!==this._ref&&h&&(this._ref instanceof WeakRef||(this._param=e,this._callback=n,t._registry.register(this._ref,this,this),this._ref=new WeakRef(this._ref)))},t.prototype.clearWeak=function(){if(null!==this._ref&&h&&this._ref instanceof WeakRef){try{t._registry.unregister(this)}catch(t){}this._param=void 0,this._callback=void 0,this._ref=this._ref.deref()}},t.prototype.reset=function(e){if(h)try{t._registry.unregister(this)}catch(t){}this._param=void 0,this._callback=void 0,this._ref=e||null},t.prototype.isEmpty=function(){return null===this._ref},t.prototype.deref=function(){var t;return h?null!==this._ref?this._ref instanceof WeakRef?this._ref.deref():this._ref:void 0:null!==(t=this._ref)&&void 0!==t?t:void 0},t._registry=h?new FinalizationRegistry((function(t){t._ref=null;var e=t._callback,n=t._param;t._callback=void 0,t._param=void 0,"function"==typeof e&&e(n)})):void 0,t}();function R(t){t.persistent.reset(),t.envObject.enqueueFinalizer(t)}var B,L=function(t){function e(e,n,i,r,o,s){void 0===r&&(r=0),void 0===o&&(o=0),void 0===s&&(s=0);var u=t.call(this,e,n>>>0,i,r,o,s)||this;return u.id=0,u}return r(e,t),e.create=function(t,n,i,r,o,s,u){void 0===o&&(o=0),void 0===s&&(s=0),void 0===u&&(u=0);var a=t.ctx.handleStore.get(n);if(!y(a.value))throw new TypeError("Invalid reference value");var p=new e(t,i,r,o,s,u);return t.ctx.refStore.add(p),p.persistent=new N(a.value),0===i&&p._setWeak(),p},e.prototype.ref=function(){if(this.persistent.isEmpty())return 0;var e=t.prototype.ref.call(this);1===e&&(this.persistent.deref()&&this.persistent.clearWeak());return e},e.prototype.unref=function(){if(this.persistent.isEmpty())return 0;var e=this.refCount(),n=t.prototype.unref.call(this);1===e&&0===n&&(this.persistent.deref()&&this._setWeak());return n},e.prototype.get=function(){var t=this.persistent.deref();return t?this.envObject.ensureHandle(t).id:0},e.prototype._setWeak=function(){this.persistent.setWeak(this,R)},e.prototype.finalize=function(){this.persistent.reset(),t.prototype.finalize.call(this)},e.prototype.dispose=function(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),t.prototype.dispose.call(this),this.id=0)},e}(D),T=function(){function t(t,e){this.id=0,this.ctx=t,this.value=e}return t.create=function(e,n){var i=new t(e,n);return e.deferredStore.add(i),i},t.prototype.resolve=function(t){this.value.resolve(t),this.dispose()},t.prototype.reject=function(t){this.value.reject(t),this.dispose()},t.prototype.dispose=function(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null},t}(),A=function(){return function(t,e,n,i){this.envObject=t,this.fn=e,this.arg=n,this.order=i}}(),M=function(){function t(){this._cleanupHooks=[],this._cleanupHookCounter=0}return t.prototype.empty=function(){return 0===this._cleanupHooks.length},t.prototype.add=function(t,e,n){if(this._cleanupHooks.filter((function(i){return i.envObject===t&&i.fn===e&&i.arg===n})).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new A(t,e,n,this._cleanupHookCounter++))},t.prototype.remove=function(t,e,n){for(var i=0;i<this._cleanupHooks.length;++i){var r=this._cleanupHooks[i];if(r.envObject===t&&r.fn===e&&r.arg===n)return void this._cleanupHooks.splice(i,1)}},t.prototype.drain=function(){var t=this._cleanupHooks.slice();t.sort((function(t,e){return e.order-t.order}));for(var e=0;e<t.length;++e){var n=t[e];"number"==typeof n.fn?n.envObject.makeDynCall_vp(n.fn)(n.arg):n.fn(n.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(n),1)}},t.prototype.dispose=function(){this._cleanupHooks.length=0,this._cleanupHookCounter=0},t}(),W=function(){function t(){this.refHandle=(new MessageChannel).port1,this.count=0}return t.prototype.increase=function(){0===this.count&&this.refHandle.ref(),this.count++},t.prototype.decrease=function(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)},t}(),P=function(){function t(){var t=this;this.envStore=new s,this.scopeStore=new k,this.refStore=new z,this.deferredStore=new C,this.handleStore=new m,this.cbinfoStack=new n,this.feature={supportReflect:l,supportFinalizer:h,supportBigInt:d,supportNewFunction:u,canSetFunctionName:f,setImmediate:v,Buffer:g},this.cleanupQueue=new M,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new W,process.once("beforeExit",(function(){t.runCleanup()})))}return t.prototype.createNotSupportWeakRefError=function(t,e){return new I(t,e)},t.prototype.createNotSupportBigIntError=function(t,e){return new F(t,e)},t.prototype.createReference=function(t,e,n,i,r,o,s){return void 0===r&&(r=0),void 0===o&&(o=0),void 0===s&&(s=0),L.create(t,e,n,i,r,o,s)},t.prototype.createDeferred=function(t){return T.create(this,t)},t.prototype.createEnv=function(t,e){return H.create(this,t,e)},t.prototype.getCurrentScope=function(){return this.scopeStore.currentScope},t.prototype.addToCurrentScope=function(t){return this.scopeStore.currentScope.add(t)},t.prototype.openScope=function(t){return this.scopeStore.openScope(t)},t.prototype.closeScope=function(t,e){this.scopeStore.closeScope(t)},t.prototype.ensureHandle=function(t){switch(t){case void 0:return m.UNDEFINED;case null:return m.NULL;case!0:return m.TRUE;case!1:return m.FALSE;case a:return m.GLOBAL}return this.addToCurrentScope(t)},t.prototype.addCleanupHook=function(t,e,n){this.cleanupQueue.add(t,e,n)},t.prototype.removeCleanupHook=function(t,e,n){this.cleanupQueue.remove(t,e,n)},t.prototype.runCleanup=function(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()},t.prototype.increaseWaitingRequestCounter=function(){var t;null===(t=this.refCounter)||void 0===t||t.increase()},t.prototype.decreaseWaitingRequestCounter=function(){var t;null===(t=this.refCounter)||void 0===t||t.decrease()},t.prototype.dispose=function(){this.runCleanup()},t}();function q(){return new P}t.CallbackInfo=e,t.CallbackInfoStack=n,t.ConstHandle=S,t.Context=P,t.Deferred=T,t.DeferredStore=C,t.EmnapiError=j,t.Env=H,t.EnvStore=s,t.Finalizer=O,t.Handle=b,t.HandleScope=E,t.HandleStore=m,t.NotSupportBigIntError=F,t.NotSupportWeakRefError=I,t.Persistent=N,t.RefBase=D,t.RefStore=z,t.RefTracker=x,t.Reference=L,t.ScopeStore=k,t.Store=o,t.TryCatch=c,t.createContext=q,t.getDefaultContext=function(){return B||(B=q()),B},t.isReferenceType=y,t.version="0.35.0",Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).emnapi={})}(this,(function(t){var e=function(){function t(t,e,n,i,r){this.parent=t,this.thiz=e,this.data=n,this.args=i,this.fn=r}return t.prototype.getNewTarget=function(t){var e=this.thiz;return null==e||null==e.constructor?0:e instanceof this.fn?t.ensureHandleId(e.constructor):0},t}(),n=function(){function t(){this.current=null}return t.prototype.pop=function(){var t=this.current;null!==t&&(this.current=t.parent)},t.prototype.push=function(t,n,i,r){var o=new e(this.current,t,n,i,r);return this.current=o,o},t.prototype.dispose=function(){this.current=null},t}(),i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},i(t,e)};function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var o=function(){function t(t){this._values=[void 0],this._values.length=t,this._size=1,this._freeList=[]}return t.prototype.add=function(t){var e;if(this._freeList.length)e=this._freeList.shift();else{e=this._size,this._size++;var n=this._values.length;e>=n&&(this._values.length=Math.ceil(1.5*n))}t.id=e,this._values[e]=t},t.prototype.get=function(t){return this._values[t]},t.prototype.has=function(t){return void 0!==this._values[t]},t.prototype.remove=function(t){var e=this._values[t];e&&(e.id=0,this._values[t]=void 0,this._freeList.push(Number(t)))},t.prototype.dispose=function(){for(var t=1;t<this._size;++t){var e=this._values[t];null==e||e.dispose()}this._values=[void 0],this._size=1,this._freeList=[]},t}(),s=function(t){function e(){return t.call(this,2)||this}return r(e,t),e}(o),u=function(){var t;try{t=new Function}catch(t){return!1}return"function"==typeof t}(),a=function(){if("undefined"!=typeof globalThis)return globalThis;var t=function(){return this}();if(!t&&u)try{t=new Function("return this")()}catch(t){}if(!t){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return t}(),p=new Error,c=function(){function t(){this._exception=p}return t.prototype.hasCaught=function(){return this._exception!==p},t.prototype.exception=function(){return this._exception},t.prototype.setError=function(t){this._exception=t},t.prototype.reset=function(){this._exception=p},t.prototype.extractException=function(){var t=this._exception;return this.reset(),t},t}(),f=function(){var t;try{return Boolean(null===(t=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===t?void 0:t.configurable)}catch(t){return!1}}(),h="object"==typeof Reflect,l="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,d=function(){try{var t=Symbol();new WeakRef(t),(new WeakMap).set(t,void 0)}catch(t){return!1}return!0}(),y="undefined"!=typeof BigInt;function v(t){return"object"==typeof t&&null!==t||"function"==typeof t}var _="function"==typeof setImmediate?setImmediate:function(t){if("function"==typeof t){var e=new MessageChannel;e.port1.onmessage=function(){e.port1.onmessage=null,e=void 0,t()},e.port2.postMessage(null)}},g=function(){return"undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0}(),b=function(){if("function"==typeof a.Buffer)return a.Buffer;try{return g("buffer").Buffer}catch(t){}}(),S=function(){function t(t,e){this.id=t,this.value=e}return t.prototype.init=function(t,e){this.id=t,this.value=e},t.prototype.data=function(t){return t.getObjectBinding(this.value).data},t.prototype.isEmpty=function(){return 0===this.id},t.prototype.isNumber=function(){return!this.isEmpty()&&"number"==typeof this.value},t.prototype.isBigInt=function(){return!this.isEmpty()&&"bigint"==typeof this.value},t.prototype.isString=function(){return!this.isEmpty()&&"string"==typeof this.value},t.prototype.isFunction=function(){return!this.isEmpty()&&"function"==typeof this.value},t.prototype.isExternal=function(){return!this.isEmpty()&&v(this.value)&&null===Object.getPrototypeOf(this.value)},t.prototype.isObject=function(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value},t.prototype.isArray=function(){return!this.isEmpty()&&Array.isArray(this.value)},t.prototype.isArrayBuffer=function(){return!this.isEmpty()&&this.value instanceof ArrayBuffer},t.prototype.isTypedArray=function(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)},t.prototype.isBuffer=function(){return!this.isEmpty()&&"function"==typeof b&&b.isBuffer(this.value)},t.prototype.isDataView=function(){return!this.isEmpty()&&this.value instanceof DataView},t.prototype.isDate=function(){return!this.isEmpty()&&this.value instanceof Date},t.prototype.isPromise=function(){return!this.isEmpty()&&this.value instanceof Promise},t.prototype.isBoolean=function(){return!this.isEmpty()&&"boolean"==typeof this.value},t.prototype.isUndefined=function(){return!this.isEmpty()&&void 0===this.value},t.prototype.isSymbol=function(){return!this.isEmpty()&&"symbol"==typeof this.value},t.prototype.isNull=function(){return!this.isEmpty()&&null===this.value},t.prototype.dispose=function(){0!==this.id&&(this.id=0,this.value=void 0)},t}(),w=function(t){function e(e,n){return t.call(this,e,n)||this}return r(e,t),e.prototype.dispose=function(){},e}(S);function m(){Object.setPrototypeOf(this,null)}m.prototype=null;var k=function(){function t(){this._values=[void 0,t.UNDEFINED,t.NULL,t.FALSE,t.TRUE,t.GLOBAL],this._next=t.MIN_ID}return t.prototype.push=function(t){var e,n=this._next,i=this._values;return n<i.length?(e=i[n]).init(n,t):(e=new S(n,t),i[n]=e),this._next++,e},t.prototype.erase=function(t,e){this._next=t;for(var n=this._values,i=t;i<e;++i)n[i].dispose()},t.prototype.get=function(t){return this._values[t]},t.prototype.swap=function(t,e){var n=this._values,i=n[t];n[t]=n[e],n[t].id=Number(t),n[e]=i,i.id=Number(e)},t.prototype.dispose=function(){this._values.length=t.MIN_ID,this._next=t.MIN_ID},t.UNDEFINED=new w(1,void 0),t.NULL=new w(2,null),t.FALSE=new w(3,!1),t.TRUE=new w(4,!0),t.GLOBAL=new w(5,a),t.MIN_ID=6,t}(),E=function(){function t(t,e,n,i,r){void 0===r&&(r=i),this.handleStore=t,this.id=e,this.parent=n,this.child=null,null!==n&&(n.child=this),this.start=i,this.end=r,this._escapeCalled=!1}return t.prototype.add=function(t){var e=this.handleStore.push(t);return this.end++,e},t.prototype.addExternal=function(t,e){var n=new m,i=t.ctx.handleStore.push(n);return t.initObjectBinding(n).data=e,this.end++,i},t.prototype.dispose=function(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)},t.prototype.escape=function(t){if(this._escapeCalled)return null;if(this._escapeCalled=!0,t<this.start||t>=this.end)return null;this.handleStore.swap(t,this.start);var e=this.handleStore.get(this.start);return this.start++,this.parent.end++,e},t.prototype.escapeCalled=function(){return this._escapeCalled},t}(),z=function(){function t(){this._rootScope=new E(null,0,null,1,k.MIN_ID),this.currentScope=this._rootScope}return t.prototype.get=function(t){t=Number(t);for(var e=this.currentScope;e!==this._rootScope;){if(e.id===t)return e;e=e.parent}},t.prototype.openScope=function(t){var e=this.currentScope,n=e.child;return null!==n?(n.start=n.end=e.end,n._escapeCalled=!1):n=new E(t.ctx.handleStore,e.id+1,e,e.end),this.currentScope=n,t.openHandleScopes++,n},t.prototype.closeScope=function(t){if(0!==t.openHandleScopes){var e=this.currentScope;this.currentScope=e.parent,e.dispose(),t.openHandleScopes--}},t.prototype.dispose=function(){for(var t=this.currentScope;null!==t;){t.handleStore=null,t.id=0,t.parent=null,t.start=k.MIN_ID,t.end=k.MIN_ID,t._escapeCalled=!1;var e=t.child;t.child=null,t=e}this.currentScope=null},t}(),C=function(t){function e(){return t.call(this,8)||this}return r(e,t),e}(o),x=function(t){function e(){return t.call(this,8)||this}return r(e,t),e}(o),O=function(){function t(){this._next=null,this._prev=null}return t.prototype.finalize=function(){},t.prototype.link=function(t){this._prev=t,this._next=t._next,null!==this._next&&(this._next._prev=this),t._next=this},t.prototype.unlink=function(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null},t.finalizeAll=function(t){for(;null!==t._next;)t._next.finalize()},t}(),D=function(){function t(t,e,n,i){void 0===e&&(e=0),void 0===n&&(n=0),void 0===i&&(i=0),this.envObject=t,this._finalizeCallback=e,this._finalizeData=n,this._finalizeHint=i}return t.prototype.callback=function(){return this._finalizeCallback},t.prototype.data=function(){return this._finalizeData},t.prototype.hint=function(){return this._finalizeHint},t.prototype.resetFinalizer=function(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0},t.prototype.dispose=function(){this.envObject=void 0},t}(),H=function(t){function e(e,n,i,r,o,s){var u=t.call(this,e,r,o,s)||this;return u._next=null,u._prev=null,u._refcount=n,u._ownership=i,u.link(r?e.finalizing_reflist:e.reflist),u}return r(e,t),e.finalizeAll=function(t){O.finalizeAll(t)},e.prototype.link=function(t){O.prototype.link.call(this,t)},e.prototype.unlink=function(){O.prototype.unlink.call(this)},e.prototype.dispose=function(){this.unlink(),this.envObject.dequeueFinalizer(this),t.prototype.dispose.call(this)},e.prototype.data=function(){return this._finalizeData},e.prototype.ref=function(){return++this._refcount},e.prototype.unref=function(){return 0===this._refcount?0:--this._refcount},e.prototype.refCount=function(){return this._refcount},e.prototype.ownership=function(){return this._ownership},e.prototype.finalize=function(){var t,e=this._ownership,n=this._finalizeCallback,i=this._finalizeData,r=this._finalizeHint;this.resetFinalizer(),this.unlink();var o=!1;if(n){var s=Number(n);try{this.envObject.callFinalizer(s,i,r)}catch(e){o=!0,t=e}}if(0===e&&this.dispose(),o)throw t},e}(D),j=function(){function t(t,e,n){this.ctx=t,this.makeDynCall_vppp=e,this.makeDynCall_vp=n,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new c,this.refs=1,this.reflist=new O,this.finalizing_reflist=new O,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}return t.create=function(e,n,i){var r=new t(e,n,i);return e.envStore.add(r),e.addCleanupHook(r,(function(){r.unref()}),0),r},t.prototype.ref=function(){this.refs++},t.prototype.unref=function(){this.refs--,0===this.refs&&this.dispose()},t.prototype.ensureHandle=function(t){return this.ctx.ensureHandle(t)},t.prototype.ensureHandleId=function(t){return this.ensureHandle(t).id},t.prototype.clearLastError=function(){var t=this.lastError;return t.errorCode=0,t.engineErrorCode=0,t.engineReserved=0,0},t.prototype.setLastError=function(t,e,n){void 0===e&&(e=0),void 0===n&&(n=0);var i=this.lastError;return i.errorCode=t,i.engineErrorCode=e,i.engineReserved=n,t},t.prototype.getReturnStatus=function(){return this.tryCatch.hasCaught()?this.setLastError(10):0},t.prototype.callIntoModule=function(t){this.clearLastError();var e=t(this);if(this.tryCatch.hasCaught())throw this.tryCatch.extractException();return e},t.prototype.callFinalizer=function(t,e,n){var i=this.makeDynCall_vppp(t),r=this.id,o=this.ctx.openScope(this);try{this.callIntoModule((function(){i(r,e,n)}))}finally{this.ctx.closeScope(this,o)}},t.prototype.enqueueFinalizer=function(t){var e=this;-1===this.pendingFinalizers.indexOf(t)&&this.pendingFinalizers.push(t),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),_((function(){e.finalizationScheduled=!1,e.unref(),e.drainFinalizerQueue()})))},t.prototype.dequeueFinalizer=function(t){var e=this.pendingFinalizers.indexOf(t);-1!==e&&this.pendingFinalizers.splice(e,1)},t.prototype.drainFinalizerQueue=function(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}},t.prototype.dispose=function(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),H.finalizeAll(this.finalizing_reflist),H.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)},t.prototype.initObjectBinding=function(t){var e={wrapped:0,tag:null,data:0};return this._bindingMap.set(t,e),e},t.prototype.getObjectBinding=function(t){return this._bindingMap.has(t)?this._bindingMap.get(t):this.initObjectBinding(t)},t.prototype.setInstanceData=function(t,e,n){this.instanceData&&this.instanceData.dispose(),this.instanceData=new H(this,0,0,e,t,n)},t.prototype.getInstanceData=function(){return this.instanceData?this.instanceData.data():0},t}(),F=function(t){function e(n){var i=this.constructor,r=t.call(this,n)||this,o=i,s=o.prototype;if(!(r instanceof e)){var u=Object.setPrototypeOf;"function"==typeof u?u.call(Object,r,s):r.__proto__=s,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(r,o)}return r}return r(e,t),e}(Error);Object.defineProperty(F.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});var I=function(t){function e(e,n){return t.call(this,"".concat(e,': The current runtime does not support "FinalizationRegistry" and "WeakRef".').concat(n?" ".concat(n):""))||this}return r(e,t),e}(F);Object.defineProperty(I.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});var N=function(t){function e(e,n){return t.call(this,"".concat(e,': The current runtime does not support "BigInt".').concat(n?" ".concat(n):""))||this}return r(e,t),e}(F);Object.defineProperty(N.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});var R=function(){function t(t){this._value=t}return t.prototype.deref=function(){return this._value},t.prototype.dispose=function(){this._value=void 0},t}(),B=function(){function t(t){this._ref=new R(t)}return t.prototype.setWeak=function(e,n){if(l&&void 0!==this._ref&&!(this._ref instanceof WeakRef)){var i=this._ref.deref();try{t._registry.register(i,this,this);var r=new WeakRef(i);this._ref.dispose(),this._ref=r,this._param=e,this._callback=n}catch(t){}}},t.prototype.clearWeak=function(){if(l&&void 0!==this._ref&&this._ref instanceof WeakRef){try{t._registry.unregister(this)}catch(t){}this._param=void 0,this._callback=void 0;var e=this._ref.deref();this._ref=void 0===e?e:new R(e)}},t.prototype.reset=function(){if(l)try{t._registry.unregister(this)}catch(t){}this._param=void 0,this._callback=void 0,this._ref instanceof R&&this._ref.dispose(),this._ref=void 0},t.prototype.isEmpty=function(){return void 0===this._ref},t.prototype.deref=function(){if(void 0!==this._ref)return this._ref.deref()},t._registry=l?new FinalizationRegistry((function(t){t._ref=void 0;var e=t._callback,n=t._param;t._callback=void 0,t._param=void 0,"function"==typeof e&&e(n)})):void 0,t}();function L(t){t.persistent.reset(),t.envObject.enqueueFinalizer(t)}var W,M=function(t){function e(e,n,i,r,o,s){void 0===r&&(r=0),void 0===o&&(o=0),void 0===s&&(s=0);var u=t.call(this,e,n>>>0,i,r,o,s)||this;return u.id=0,u}return r(e,t),e.create=function(t,n,i,r,o,s,u){void 0===o&&(o=0),void 0===s&&(s=0),void 0===u&&(u=0);var a=t.ctx.handleStore.get(n),p=new e(t,i,r,o,s,u);return t.ctx.refStore.add(p),p.persistent=new B(a.value),0===i&&p._setWeak(),p},e.prototype.ref=function(){if(this.persistent.isEmpty())return 0;var e=t.prototype.ref.call(this);1===e&&(this.persistent.deref()&&this.persistent.clearWeak());return e},e.prototype.unref=function(){if(this.persistent.isEmpty())return 0;var e=this.refCount(),n=t.prototype.unref.call(this);1===e&&0===n&&(this.persistent.deref()&&this._setWeak());return n},e.prototype.get=function(){var t=this.persistent.deref();return t?this.envObject.ensureHandle(t).id:0},e.prototype._setWeak=function(){this.persistent.setWeak(this,L)},e.prototype.finalize=function(){this.persistent.reset(),t.prototype.finalize.call(this)},e.prototype.dispose=function(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),t.prototype.dispose.call(this),this.id=0)},e}(H),A=function(){function t(t,e){this.id=0,this.ctx=t,this.value=e}return t.create=function(e,n){var i=new t(e,n);return e.deferredStore.add(i),i},t.prototype.resolve=function(t){this.value.resolve(t),this.dispose()},t.prototype.reject=function(t){this.value.reject(t),this.dispose()},t.prototype.dispose=function(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null},t}(),T=function(){return function(t,e,n,i){this.envObject=t,this.fn=e,this.arg=n,this.order=i}}(),P=function(){function t(){this._cleanupHooks=[],this._cleanupHookCounter=0}return t.prototype.empty=function(){return 0===this._cleanupHooks.length},t.prototype.add=function(t,e,n){if(this._cleanupHooks.filter((function(i){return i.envObject===t&&i.fn===e&&i.arg===n})).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new T(t,e,n,this._cleanupHookCounter++))},t.prototype.remove=function(t,e,n){for(var i=0;i<this._cleanupHooks.length;++i){var r=this._cleanupHooks[i];if(r.envObject===t&&r.fn===e&&r.arg===n)return void this._cleanupHooks.splice(i,1)}},t.prototype.drain=function(){var t=this._cleanupHooks.slice();t.sort((function(t,e){return e.order-t.order}));for(var e=0;e<t.length;++e){var n=t[e];"number"==typeof n.fn?n.envObject.makeDynCall_vp(n.fn)(n.arg):n.fn(n.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(n),1)}},t.prototype.dispose=function(){this._cleanupHooks.length=0,this._cleanupHookCounter=0},t}(),q=function(){function t(){this.refHandle=(new MessageChannel).port1,this.count=0}return t.prototype.increase=function(){0===this.count&&this.refHandle.ref(),this.count++},t.prototype.decrease=function(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)},t}(),U=function(){function t(){var t=this;this.envStore=new s,this.scopeStore=new z,this.refStore=new C,this.deferredStore=new x,this.handleStore=new k,this.cbinfoStack=new n,this.feature={supportReflect:h,supportFinalizer:l,supportWeakSymbol:d,supportBigInt:y,supportNewFunction:u,canSetFunctionName:f,setImmediate:_,Buffer:b},this.cleanupQueue=new P,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new q,process.once("beforeExit",(function(){t.runCleanup()})))}return t.prototype.createNotSupportWeakRefError=function(t,e){return new I(t,e)},t.prototype.createNotSupportBigIntError=function(t,e){return new N(t,e)},t.prototype.createReference=function(t,e,n,i,r,o,s){return void 0===r&&(r=0),void 0===o&&(o=0),void 0===s&&(s=0),M.create(t,e,n,i,r,o,s)},t.prototype.createDeferred=function(t){return A.create(this,t)},t.prototype.createEnv=function(t,e){return j.create(this,t,e)},t.prototype.getCurrentScope=function(){return this.scopeStore.currentScope},t.prototype.addToCurrentScope=function(t){return this.scopeStore.currentScope.add(t)},t.prototype.openScope=function(t){return this.scopeStore.openScope(t)},t.prototype.closeScope=function(t,e){this.scopeStore.closeScope(t)},t.prototype.ensureHandle=function(t){switch(t){case void 0:return k.UNDEFINED;case null:return k.NULL;case!0:return k.TRUE;case!1:return k.FALSE;case a:return k.GLOBAL}return this.addToCurrentScope(t)},t.prototype.addCleanupHook=function(t,e,n){this.cleanupQueue.add(t,e,n)},t.prototype.removeCleanupHook=function(t,e,n){this.cleanupQueue.remove(t,e,n)},t.prototype.runCleanup=function(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()},t.prototype.increaseWaitingRequestCounter=function(){var t;null===(t=this.refCounter)||void 0===t||t.increase()},t.prototype.decreaseWaitingRequestCounter=function(){var t;null===(t=this.refCounter)||void 0===t||t.decrease()},t.prototype.dispose=function(){this.runCleanup()},t}();function Q(){return new U}t.CallbackInfo=e,t.CallbackInfoStack=n,t.ConstHandle=w,t.Context=U,t.Deferred=A,t.DeferredStore=x,t.EmnapiError=F,t.Env=j,t.EnvStore=s,t.Finalizer=D,t.Handle=S,t.HandleScope=E,t.HandleStore=k,t.NotSupportBigIntError=N,t.NotSupportWeakRefError=I,t.Persistent=B,t.RefBase=H,t.RefStore=C,t.RefTracker=O,t.Reference=M,t.ScopeStore=z,t.Store=o,t.TryCatch=c,t.createContext=Q,t.getDefaultContext=function(){return W||(W=Q()),W},t.isReferenceType=v,t.version="0.36.0",Object.defineProperty(t,"__esModule",{value:!0})}));
|
package/dist/emnapi.min.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e{constructor(e,t,i,s,n){this.parent=e,this.thiz=t,this.data=i,this.args=s,this.fn=n}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}}class t{constructor(){this.current=null}pop(){const e=this.current;null!==e&&(this.current=e.parent)}push(t,i,s,n){const r=new e(this.current,t,i,s,n);return this.current=r,r}dispose(){this.current=null}}class i{constructor(e){this._values=[void 0],this._values.length=e,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=Math.ceil(1.5*e))}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class s extends i{constructor(){super(2)}}const n=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),r=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&n)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}(),o=new Error;class a{constructor(){this._exception=o}hasCaught(){return this._exception!==o}exception(){return this._exception}setError(e){this._exception=e}reset(){this._exception=o}extractException(){const e=this._exception;return this.reset(),e}}const u=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),l="object"==typeof Reflect,h="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,c="undefined"!=typeof BigInt;function p(e){return"object"==typeof e&&null!==e||"function"==typeof e}const f="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)return;let t=new MessageChannel;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)},d=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),_=function(){if("function"==typeof r.Buffer)return r.Buffer;try{return d("buffer").Buffer}catch(e){}}();class v{constructor(e,t){this.id=e,this.value=t}init(e,t){this.id=e,this.value=t}data(e){return e.getObjectBinding(this.value).data}isEmpty(){return 0===this.id}isNumber(){return!this.isEmpty()&&"number"==typeof this.value}isBigInt(){return!this.isEmpty()&&"bigint"==typeof this.value}isString(){return!this.isEmpty()&&"string"==typeof this.value}isFunction(){return!this.isEmpty()&&"function"==typeof this.value}isExternal(){return!this.isEmpty()&&p(this.value)&&null===Object.getPrototypeOf(this.value)}isObject(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value}isArray(){return!this.isEmpty()&&Array.isArray(this.value)}isArrayBuffer(){return!this.isEmpty()&&this.value instanceof ArrayBuffer}isTypedArray(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(){return!this.isEmpty()&&"function"==typeof _&&_.isBuffer(this.value)}isDataView(){return!this.isEmpty()&&this.value instanceof DataView}isDate(){return!this.isEmpty()&&this.value instanceof Date}isPromise(){return!this.isEmpty()&&this.value instanceof Promise}isBoolean(){return!this.isEmpty()&&"boolean"==typeof this.value}isUndefined(){return!this.isEmpty()&&void 0===this.value}isSymbol(){return!this.isEmpty()&&"symbol"==typeof this.value}isNull(){return!this.isEmpty()&&null===this.value}dispose(){0!==this.id&&(this.id=0,this.value=void 0)}}class y extends v{constructor(e,t){super(e,t)}dispose(){}}function g(){Object.setPrototypeOf(this,null)}g.prototype=null;class b{constructor(){this._values=[void 0,b.UNDEFINED,b.NULL,b.FALSE,b.TRUE,b.GLOBAL],this._next=b.MIN_ID}push(e){let t;const i=this._next,s=this._values;return i<s.length?(t=s[i],t.init(i,e)):(t=new v(i,e),s[i]=t),this._next++,t}erase(e,t){this._next=e;const i=this._values;for(let s=e;s<t;++s)i[s].dispose()}get(e){return this._values[e]}swap(e,t){const i=this._values,s=i[e];i[e]=i[t],i[e].id=Number(e),i[t]=s,s.id=Number(t)}dispose(){this._values.length=b.MIN_ID,this._next=b.MIN_ID}}b.UNDEFINED=new y(1,void 0),b.NULL=new y(2,null),b.FALSE=new y(3,!1),b.TRUE=new y(4,!0),b.GLOBAL=new y(5,r),b.MIN_ID=6;class w{constructor(e,t,i,s,n=s){this.handleStore=e,this.id=t,this.parent=i,this.child=null,null!==i&&(i.child=this),this.start=s,this.end=n,this._escapeCalled=!1}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e,t){const i=new g,s=e.ctx.handleStore.push(i);return e.initObjectBinding(i).data=t,this.end++,s}dispose(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class S{constructor(){this._rootScope=new w(null,0,null,1,b.MIN_ID),this.currentScope=this._rootScope}get(e){e=Number(e);let t=this.currentScope;for(;t!==this._rootScope;){if(t.id===e)return t;t=t.parent}}openScope(e){const t=this.currentScope;let i=t.child;return null!==i?(i.start=i.end=t.end,i._escapeCalled=!1):i=new w(e.ctx.handleStore,t.id+1,t,t.end),this.currentScope=i,e.openHandleScopes++,i}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){let e=this.currentScope;for(;null!==e;){e.handleStore=null,e.id=0,e.parent=null,e.start=b.MIN_ID,e.end=b.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class m extends i{constructor(){super(8)}}class k extends i{constructor(){super(8)}}class E{constructor(){this._next=null,this._prev=null}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class x{constructor(e,t=0,i=0,s=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=i,this._finalizeHint=s}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}dispose(){this.envObject=void 0}}class z extends x{constructor(e,t,i,s,n,r){super(e,s,n,r),this._next=null,this._prev=null,this._refcount=t,this._ownership=i,this.link(s?e.finalizing_reflist:e.reflist)}static finalizeAll(e){E.finalizeAll(e)}link(e){E.prototype.link.call(this,e)}unlink(){E.prototype.unlink.call(this)}dispose(){this.unlink(),this.envObject.dequeueFinalizer(this),super.dispose()}data(){return this._finalizeData}ref(){return++this._refcount}unref(){return 0===this._refcount?0:--this._refcount}refCount(){return this._refcount}ownership(){return this._ownership}finalize(){const e=this._ownership,t=this._finalizeCallback,i=this._finalizeData,s=this._finalizeHint;let n;this.resetFinalizer(),this.unlink();let r=!1;if(t){const e=Number(t);try{this.envObject.callFinalizer(e,i,s)}catch(e){r=!0,n=e}}if(0===e&&this.dispose(),r)throw n}}class C{constructor(e,t,i){this.ctx=e,this.makeDynCall_vppp=t,this.makeDynCall_vp=i,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new a,this.refs=1,this.reflist=new E,this.finalizing_reflist=new E,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}static create(e,t,i){const s=new C(e,t,i);return e.envStore.add(s),e.addCleanupHook(s,(()=>{s.unref()}),0),s}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return e.errorCode=0,e.engineErrorCode=0,e.engineReserved=0,0}setLastError(e,t=0,i=0){const s=this.lastError;return s.errorCode=e,s.engineErrorCode=t,s.engineReserved=i,e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e){this.clearLastError();const t=e(this);if(this.tryCatch.hasCaught()){throw this.tryCatch.extractException()}return t}callFinalizer(e,t,i){const s=this.makeDynCall_vppp(e),n=this.id,r=this.ctx.openScope(this);try{this.callIntoModule((()=>{s(n,t,i)}))}finally{this.ctx.closeScope(this,r)}}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),f((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}dispose(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),z.finalizeAll(this.finalizing_reflist),z.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null,data:0};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,i){this.instanceData&&this.instanceData.dispose(),this.instanceData=new z(this,0,0,t,e,i)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class D extends Error{constructor(e){super(e);const t=new.target,i=t.prototype;if(!(this instanceof D)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,i):this.__proto__=i,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(D.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class H extends D{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(H.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class O extends D{constructor(e,t){super(`${e}: The current runtime does not support "BigInt".${t?` ${t}`:""}`)}}Object.defineProperty(O.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});class F{constructor(e){this._ref=e}setWeak(e,t){null!==this._ref&&h&&(this._ref instanceof WeakRef||(this._param=e,this._callback=t,F._registry.register(this._ref,this,this),this._ref=new WeakRef(this._ref)))}clearWeak(){if(null!==this._ref&&h&&this._ref instanceof WeakRef){try{F._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref=this._ref.deref()}}reset(e){if(h)try{F._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref=e||null}isEmpty(){return null===this._ref}deref(){var e;return h?null!==this._ref?this._ref instanceof WeakRef?this._ref.deref():this._ref:void 0:null!==(e=this._ref)&&void 0!==e?e:void 0}}function I(e){e.persistent.reset(),e.envObject.enqueueFinalizer(e)}F._registry=h?new FinalizationRegistry((e=>{e._ref=null;const t=e._callback,i=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(i)})):void 0;class j extends z{constructor(e,t,i,s=0,n=0,r=0){super(e,t>>>0,i,s,n,r),this.id=0}static create(e,t,i,s,n=0,r=0,o=0){const a=e.ctx.handleStore.get(t);if(!p(a.value))throw new TypeError("Invalid reference value");const u=new j(e,i,s,n,r,o);return e.ctx.refStore.add(u),u.persistent=new F(a.value),0===i&&u._setWeak(),u}ref(){if(this.persistent.isEmpty())return 0;const e=super.ref();if(1===e){this.persistent.deref()&&this.persistent.clearWeak()}return e}unref(){if(this.persistent.isEmpty())return 0;const e=this.refCount(),t=super.unref();if(1===e&&0===t){this.persistent.deref()&&this._setWeak()}return t}get(){const e=this.persistent.deref();if(e){return this.envObject.ensureHandle(e).id}return 0}_setWeak(){this.persistent.setWeak(this,I)}finalize(){this.persistent.reset(),super.finalize()}dispose(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.id=0)}}class N{constructor(e,t){this.id=0,this.ctx=e,this.value=t}static create(e,t){const i=new N(e,t);return e.deferredStore.add(i),i}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class B{constructor(e,t,i,s){this.envObject=e,this.fn=t,this.arg=i,this.order=s}}class L{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,i){if(this._cleanupHooks.filter((s=>s.envObject===e&&s.fn===t&&s.arg===i)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new B(e,t,i,this._cleanupHookCounter++))}remove(e,t,i){for(let s=0;s<this._cleanupHooks.length;++s){const n=this._cleanupHooks[s];if(n.envObject===e&&n.fn===t&&n.arg===i)return void this._cleanupHooks.splice(s,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const i=e[t];"number"==typeof i.fn?i.envObject.makeDynCall_vp(i.fn)(i.arg):i.fn(i.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(i),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class R{constructor(){this.refHandle=(new MessageChannel).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class A{constructor(){this.envStore=new s,this.scopeStore=new S,this.refStore=new m,this.deferredStore=new k,this.handleStore=new b,this.cbinfoStack=new t,this.feature={supportReflect:l,supportFinalizer:h,supportBigInt:c,supportNewFunction:n,canSetFunctionName:u,setImmediate:f,Buffer:_},this.cleanupQueue=new L,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new R,process.once("beforeExit",(()=>{this.runCleanup()})))}createNotSupportWeakRefError(e,t){return new H(e,t)}createNotSupportBigIntError(e,t){return new O(e,t)}createReference(e,t,i,s,n=0,r=0,o=0){return j.create(e,t,i,s,n,r,o)}createDeferred(e){return N.create(this,e)}createEnv(e,t){return C.create(this,e,t)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return b.UNDEFINED;case null:return b.NULL;case!0:return b.TRUE;case!1:return b.FALSE;case r:return b.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,i){this.cleanupQueue.add(e,t,i)}removeCleanupHook(e,t,i){this.cleanupQueue.remove(e,t,i)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}dispose(){this.runCleanup()}}let M;function W(){return new A}function T(){return M||(M=W()),M}const q="0.35.0";export{e as CallbackInfo,t as CallbackInfoStack,y as ConstHandle,A as Context,N as Deferred,k as DeferredStore,D as EmnapiError,C as Env,s as EnvStore,x as Finalizer,v as Handle,w as HandleScope,b as HandleStore,O as NotSupportBigIntError,H as NotSupportWeakRefError,F as Persistent,z as RefBase,m as RefStore,E as RefTracker,j as Reference,S as ScopeStore,i as Store,a as TryCatch,W as createContext,T as getDefaultContext,p as isReferenceType,q as version};
|
|
1
|
+
class e{constructor(e,t,i,s,n){this.parent=e,this.thiz=t,this.data=i,this.args=s,this.fn=n}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}}class t{constructor(){this.current=null}pop(){const e=this.current;null!==e&&(this.current=e.parent)}push(t,i,s,n){const r=new e(this.current,t,i,s,n);return this.current=r,r}dispose(){this.current=null}}class i{constructor(e){this._values=[void 0],this._values.length=e,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=Math.ceil(1.5*e))}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class s extends i{constructor(){super(2)}}const n=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),r=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&n)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}(),o=new Error;class a{constructor(){this._exception=o}hasCaught(){return this._exception!==o}exception(){return this._exception}setError(e){this._exception=e}reset(){this._exception=o}extractException(){const e=this._exception;return this.reset(),e}}const u=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),c="object"==typeof Reflect,h="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,l=function(){try{const e=Symbol();new WeakRef(e),(new WeakMap).set(e,void 0)}catch(e){return!1}return!0}(),p="undefined"!=typeof BigInt;function d(e){return"object"==typeof e&&null!==e||"function"==typeof e}const f="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)return;let t=new MessageChannel;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)},_=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),v=function(){if("function"==typeof r.Buffer)return r.Buffer;try{return _("buffer").Buffer}catch(e){}}();class y{constructor(e,t){this.id=e,this.value=t}init(e,t){this.id=e,this.value=t}data(e){return e.getObjectBinding(this.value).data}isEmpty(){return 0===this.id}isNumber(){return!this.isEmpty()&&"number"==typeof this.value}isBigInt(){return!this.isEmpty()&&"bigint"==typeof this.value}isString(){return!this.isEmpty()&&"string"==typeof this.value}isFunction(){return!this.isEmpty()&&"function"==typeof this.value}isExternal(){return!this.isEmpty()&&d(this.value)&&null===Object.getPrototypeOf(this.value)}isObject(){return!this.isEmpty()&&"object"==typeof this.value&&null!==this.value}isArray(){return!this.isEmpty()&&Array.isArray(this.value)}isArrayBuffer(){return!this.isEmpty()&&this.value instanceof ArrayBuffer}isTypedArray(){return!this.isEmpty()&&ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(){return!this.isEmpty()&&"function"==typeof v&&v.isBuffer(this.value)}isDataView(){return!this.isEmpty()&&this.value instanceof DataView}isDate(){return!this.isEmpty()&&this.value instanceof Date}isPromise(){return!this.isEmpty()&&this.value instanceof Promise}isBoolean(){return!this.isEmpty()&&"boolean"==typeof this.value}isUndefined(){return!this.isEmpty()&&void 0===this.value}isSymbol(){return!this.isEmpty()&&"symbol"==typeof this.value}isNull(){return!this.isEmpty()&&null===this.value}dispose(){0!==this.id&&(this.id=0,this.value=void 0)}}class g extends y{constructor(e,t){super(e,t)}dispose(){}}function b(){Object.setPrototypeOf(this,null)}b.prototype=null;class w{constructor(){this._values=[void 0,w.UNDEFINED,w.NULL,w.FALSE,w.TRUE,w.GLOBAL],this._next=w.MIN_ID}push(e){let t;const i=this._next,s=this._values;return i<s.length?(t=s[i],t.init(i,e)):(t=new y(i,e),s[i]=t),this._next++,t}erase(e,t){this._next=e;const i=this._values;for(let s=e;s<t;++s)i[s].dispose()}get(e){return this._values[e]}swap(e,t){const i=this._values,s=i[e];i[e]=i[t],i[e].id=Number(e),i[t]=s,s.id=Number(t)}dispose(){this._values.length=w.MIN_ID,this._next=w.MIN_ID}}w.UNDEFINED=new g(1,void 0),w.NULL=new g(2,null),w.FALSE=new g(3,!1),w.TRUE=new g(4,!0),w.GLOBAL=new g(5,r),w.MIN_ID=6;class S{constructor(e,t,i,s,n=s){this.handleStore=e,this.id=t,this.parent=i,this.child=null,null!==i&&(i.child=this),this.start=s,this.end=n,this._escapeCalled=!1}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e,t){const i=new b,s=e.ctx.handleStore.push(i);return e.initObjectBinding(i).data=t,this.end++,s}dispose(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class m{constructor(){this._rootScope=new S(null,0,null,1,w.MIN_ID),this.currentScope=this._rootScope}get(e){e=Number(e);let t=this.currentScope;for(;t!==this._rootScope;){if(t.id===e)return t;t=t.parent}}openScope(e){const t=this.currentScope;let i=t.child;return null!==i?(i.start=i.end=t.end,i._escapeCalled=!1):i=new S(e.ctx.handleStore,t.id+1,t,t.end),this.currentScope=i,e.openHandleScopes++,i}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){let e=this.currentScope;for(;null!==e;){e.handleStore=null,e.id=0,e.parent=null,e.start=w.MIN_ID,e.end=w.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class k extends i{constructor(){super(8)}}class E extends i{constructor(){super(8)}}class x{constructor(){this._next=null,this._prev=null}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class z{constructor(e,t=0,i=0,s=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=i,this._finalizeHint=s}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}dispose(){this.envObject=void 0}}class C extends z{constructor(e,t,i,s,n,r){super(e,s,n,r),this._next=null,this._prev=null,this._refcount=t,this._ownership=i,this.link(s?e.finalizing_reflist:e.reflist)}static finalizeAll(e){x.finalizeAll(e)}link(e){x.prototype.link.call(this,e)}unlink(){x.prototype.unlink.call(this)}dispose(){this.unlink(),this.envObject.dequeueFinalizer(this),super.dispose()}data(){return this._finalizeData}ref(){return++this._refcount}unref(){return 0===this._refcount?0:--this._refcount}refCount(){return this._refcount}ownership(){return this._ownership}finalize(){const e=this._ownership,t=this._finalizeCallback,i=this._finalizeData,s=this._finalizeHint;let n;this.resetFinalizer(),this.unlink();let r=!1;if(t){const e=Number(t);try{this.envObject.callFinalizer(e,i,s)}catch(e){r=!0,n=e}}if(0===e&&this.dispose(),r)throw n}}class D{constructor(e,t,i){this.ctx=e,this.makeDynCall_vppp=t,this.makeDynCall_vp=i,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new a,this.refs=1,this.reflist=new x,this.finalizing_reflist=new x,this.destructing=!1,this.finalizationScheduled=!1,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this._bindingMap=new WeakMap,this.id=0}static create(e,t,i){const s=new D(e,t,i);return e.envStore.add(s),e.addCleanupHook(s,(()=>{s.unref()}),0),s}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return e.errorCode=0,e.engineErrorCode=0,e.engineReserved=0,0}setLastError(e,t=0,i=0){const s=this.lastError;return s.errorCode=e,s.engineErrorCode=t,s.engineReserved=i,e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e){this.clearLastError();const t=e(this);if(this.tryCatch.hasCaught()){throw this.tryCatch.extractException()}return t}callFinalizer(e,t,i){const s=this.makeDynCall_vppp(e),n=this.id,r=this.ctx.openScope(this);try{this.callIntoModule((()=>{s(n,t,i)}))}finally{this.ctx.closeScope(this,r)}}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),f((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}dispose(){0!==this.id&&(this.destructing=!0,this.drainFinalizerQueue(),C.finalizeAll(this.finalizing_reflist),C.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null,data:0};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,i){this.instanceData&&this.instanceData.dispose(),this.instanceData=new C(this,0,0,t,e,i)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class H extends Error{constructor(e){super(e);const t=new.target,i=t.prototype;if(!(this instanceof H)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,i):this.__proto__=i,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(H.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class O extends H{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(O.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class F extends H{constructor(e,t){super(`${e}: The current runtime does not support "BigInt".${t?` ${t}`:""}`)}}Object.defineProperty(F.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBigIntError"});class I{constructor(e){this._value=e}deref(){return this._value}dispose(){this._value=void 0}}class j{constructor(e){this._ref=new I(e)}setWeak(e,t){if(!h||void 0===this._ref||this._ref instanceof WeakRef)return;const i=this._ref.deref();try{j._registry.register(i,this,this);const s=new WeakRef(i);this._ref.dispose(),this._ref=s,this._param=e,this._callback=t}catch(e){}}clearWeak(){if(h&&void 0!==this._ref&&this._ref instanceof WeakRef){try{j._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0;const e=this._ref.deref();this._ref=void 0===e?e:new I(e)}}reset(){if(h)try{j._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref instanceof I&&this._ref.dispose(),this._ref=void 0}isEmpty(){return void 0===this._ref}deref(){if(void 0!==this._ref)return this._ref.deref()}}function N(e){e.persistent.reset(),e.envObject.enqueueFinalizer(e)}j._registry=h?new FinalizationRegistry((e=>{e._ref=void 0;const t=e._callback,i=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(i)})):void 0;class B extends C{constructor(e,t,i,s=0,n=0,r=0){super(e,t>>>0,i,s,n,r),this.id=0}static create(e,t,i,s,n=0,r=0,o=0){const a=e.ctx.handleStore.get(t),u=new B(e,i,s,n,r,o);return e.ctx.refStore.add(u),u.persistent=new j(a.value),0===i&&u._setWeak(),u}ref(){if(this.persistent.isEmpty())return 0;const e=super.ref();if(1===e){this.persistent.deref()&&this.persistent.clearWeak()}return e}unref(){if(this.persistent.isEmpty())return 0;const e=this.refCount(),t=super.unref();if(1===e&&0===t){this.persistent.deref()&&this._setWeak()}return t}get(){const e=this.persistent.deref();if(e){return this.envObject.ensureHandle(e).id}return 0}_setWeak(){this.persistent.setWeak(this,N)}finalize(){this.persistent.reset(),super.finalize()}dispose(){0!==this.id&&(this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.id=0)}}class L{constructor(e,t){this.id=0,this.ctx=e,this.value=t}static create(e,t){const i=new L(e,t);return e.deferredStore.add(i),i}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class R{constructor(e,t,i,s){this.envObject=e,this.fn=t,this.arg=i,this.order=s}}class W{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,i){if(this._cleanupHooks.filter((s=>s.envObject===e&&s.fn===t&&s.arg===i)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new R(e,t,i,this._cleanupHookCounter++))}remove(e,t,i){for(let s=0;s<this._cleanupHooks.length;++s){const n=this._cleanupHooks[s];if(n.envObject===e&&n.fn===t&&n.arg===i)return void this._cleanupHooks.splice(s,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const i=e[t];"number"==typeof i.fn?i.envObject.makeDynCall_vp(i.fn)(i.arg):i.fn(i.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(i),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class M{constructor(){this.refHandle=(new MessageChannel).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class A{constructor(){this.envStore=new s,this.scopeStore=new m,this.refStore=new k,this.deferredStore=new E,this.handleStore=new w,this.cbinfoStack=new t,this.feature={supportReflect:c,supportFinalizer:h,supportWeakSymbol:l,supportBigInt:p,supportNewFunction:n,canSetFunctionName:u,setImmediate:f,Buffer:v},this.cleanupQueue=new W,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new M,process.once("beforeExit",(()=>{this.runCleanup()})))}createNotSupportWeakRefError(e,t){return new O(e,t)}createNotSupportBigIntError(e,t){return new F(e,t)}createReference(e,t,i,s,n=0,r=0,o=0){return B.create(e,t,i,s,n,r,o)}createDeferred(e){return L.create(this,e)}createEnv(e,t){return D.create(this,e,t)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return w.UNDEFINED;case null:return w.NULL;case!0:return w.TRUE;case!1:return w.FALSE;case r:return w.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,i){this.cleanupQueue.add(e,t,i)}removeCleanupHook(e,t,i){this.cleanupQueue.remove(e,t,i)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}dispose(){this.runCleanup()}}let T;function q(){return new A}function U(){return T||(T=q()),T}const P="0.36.0";export{e as CallbackInfo,t as CallbackInfoStack,g as ConstHandle,A as Context,L as Deferred,E as DeferredStore,H as EmnapiError,D as Env,s as EnvStore,z as Finalizer,y as Handle,S as HandleScope,w as HandleStore,F as NotSupportBigIntError,O as NotSupportWeakRefError,j as Persistent,C as RefBase,k as RefStore,x as RefTracker,B as Reference,m as ScopeStore,i as Store,a as TryCatch,q as createContext,U as getDefaultContext,d as isReferenceType,P as version};
|
package/dist/emnapi.mjs
CHANGED
|
@@ -153,6 +153,17 @@ const canSetFunctionName = (function () {
|
|
|
153
153
|
})();
|
|
154
154
|
const supportReflect = typeof Reflect === 'object';
|
|
155
155
|
const supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
156
|
+
const supportWeakSymbol = (function () {
|
|
157
|
+
try {
|
|
158
|
+
const sym = Symbol();
|
|
159
|
+
new WeakRef(sym);
|
|
160
|
+
new WeakMap().set(sym, undefined);
|
|
161
|
+
}
|
|
162
|
+
catch (_) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
})();
|
|
156
167
|
const supportBigInt = typeof BigInt !== 'undefined';
|
|
157
168
|
function isReferenceType(v) {
|
|
158
169
|
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
@@ -757,26 +768,37 @@ Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
|
|
|
757
768
|
value: 'NotSupportBigIntError'
|
|
758
769
|
});
|
|
759
770
|
|
|
771
|
+
class StrongRef {
|
|
772
|
+
constructor(value) {
|
|
773
|
+
this._value = value;
|
|
774
|
+
}
|
|
775
|
+
deref() {
|
|
776
|
+
return this._value;
|
|
777
|
+
}
|
|
778
|
+
dispose() {
|
|
779
|
+
this._value = undefined;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
760
782
|
class Persistent {
|
|
761
783
|
constructor(value) {
|
|
762
|
-
this._ref = value;
|
|
784
|
+
this._ref = new StrongRef(value);
|
|
763
785
|
}
|
|
764
786
|
setWeak(param, callback) {
|
|
765
|
-
if (this._ref ===
|
|
766
|
-
return;
|
|
767
|
-
if (!supportFinalizer)
|
|
768
|
-
return;
|
|
769
|
-
if (this._ref instanceof WeakRef)
|
|
787
|
+
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef)
|
|
770
788
|
return;
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
789
|
+
const value = this._ref.deref();
|
|
790
|
+
try {
|
|
791
|
+
Persistent._registry.register(value, this, this);
|
|
792
|
+
const weakRef = new WeakRef(value);
|
|
793
|
+
this._ref.dispose();
|
|
794
|
+
this._ref = weakRef;
|
|
795
|
+
this._param = param;
|
|
796
|
+
this._callback = callback;
|
|
797
|
+
}
|
|
798
|
+
catch (_) { }
|
|
775
799
|
}
|
|
776
800
|
clearWeak() {
|
|
777
|
-
if (this._ref ===
|
|
778
|
-
return;
|
|
779
|
-
if (!supportFinalizer)
|
|
801
|
+
if (!supportFinalizer || this._ref === undefined)
|
|
780
802
|
return;
|
|
781
803
|
if (this._ref instanceof WeakRef) {
|
|
782
804
|
try {
|
|
@@ -785,10 +807,16 @@ class Persistent {
|
|
|
785
807
|
catch (_) { }
|
|
786
808
|
this._param = undefined;
|
|
787
809
|
this._callback = undefined;
|
|
788
|
-
|
|
810
|
+
const value = this._ref.deref();
|
|
811
|
+
if (value === undefined) {
|
|
812
|
+
this._ref = value;
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
this._ref = new StrongRef(value);
|
|
816
|
+
}
|
|
789
817
|
}
|
|
790
818
|
}
|
|
791
|
-
reset(
|
|
819
|
+
reset() {
|
|
792
820
|
if (supportFinalizer) {
|
|
793
821
|
try {
|
|
794
822
|
Persistent._registry.unregister(this);
|
|
@@ -797,32 +825,23 @@ class Persistent {
|
|
|
797
825
|
}
|
|
798
826
|
this._param = undefined;
|
|
799
827
|
this._callback = undefined;
|
|
800
|
-
if (
|
|
801
|
-
this._ref
|
|
802
|
-
}
|
|
803
|
-
else {
|
|
804
|
-
this._ref = null;
|
|
828
|
+
if (this._ref instanceof StrongRef) {
|
|
829
|
+
this._ref.dispose();
|
|
805
830
|
}
|
|
831
|
+
this._ref = undefined;
|
|
806
832
|
}
|
|
807
833
|
isEmpty() {
|
|
808
|
-
return this._ref ===
|
|
834
|
+
return this._ref === undefined;
|
|
809
835
|
}
|
|
810
836
|
deref() {
|
|
811
|
-
|
|
812
|
-
if (!supportFinalizer) {
|
|
813
|
-
return (_a = this._ref) !== null && _a !== void 0 ? _a : undefined;
|
|
814
|
-
}
|
|
815
|
-
if (this._ref === null)
|
|
837
|
+
if (this._ref === undefined)
|
|
816
838
|
return undefined;
|
|
817
|
-
|
|
818
|
-
return this._ref.deref();
|
|
819
|
-
}
|
|
820
|
-
return this._ref;
|
|
839
|
+
return this._ref.deref();
|
|
821
840
|
}
|
|
822
841
|
}
|
|
823
842
|
Persistent._registry = supportFinalizer
|
|
824
843
|
? new FinalizationRegistry((value) => {
|
|
825
|
-
value._ref =
|
|
844
|
+
value._ref = undefined;
|
|
826
845
|
const callback = value._callback;
|
|
827
846
|
const param = value._param;
|
|
828
847
|
value._callback = undefined;
|
|
@@ -844,9 +863,6 @@ class Reference extends RefBase {
|
|
|
844
863
|
}
|
|
845
864
|
static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
846
865
|
const handle = envObject.ctx.handleStore.get(handle_id);
|
|
847
|
-
if (!isReferenceType(handle.value)) {
|
|
848
|
-
throw new TypeError('Invalid reference value');
|
|
849
|
-
}
|
|
850
866
|
const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
851
867
|
envObject.ctx.refStore.add(ref);
|
|
852
868
|
ref.persistent = new Persistent(handle.value);
|
|
@@ -1015,6 +1031,7 @@ class Context {
|
|
|
1015
1031
|
this.feature = {
|
|
1016
1032
|
supportReflect,
|
|
1017
1033
|
supportFinalizer,
|
|
1034
|
+
supportWeakSymbol,
|
|
1018
1035
|
supportBigInt,
|
|
1019
1036
|
supportNewFunction,
|
|
1020
1037
|
canSetFunctionName,
|
|
@@ -1100,6 +1117,6 @@ function getDefaultContext() {
|
|
|
1100
1117
|
return defaultContext;
|
|
1101
1118
|
}
|
|
1102
1119
|
|
|
1103
|
-
const version = "0.
|
|
1120
|
+
const version = "0.36.0";
|
|
1104
1121
|
|
|
1105
1122
|
export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, DeferredStore, EmnapiError, Env, EnvStore, Finalizer, Handle, HandleScope, HandleStore, NotSupportBigIntError, NotSupportWeakRefError, Persistent, RefBase, RefStore, RefTracker, Reference, ScopeStore, Store, TryCatch, createContext, getDefaultContext, isReferenceType, version };
|