const e=[];class t{constructor(e,t,i,n,s,r,o){this.id=e,this.parent=t,this.child=i,this.thiz=n,this.data=s,this.args=r,this.fn=o}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}dispose(){void 0!==this.thiz&&(this.thiz=void 0),this.args=e,this.fn=null}}const i=new t(0,null,null,null,0,null,null);class n{constructor(){this.current=i}get(e){if(1===e)return i.child;let t=i;for(let i=0;i=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 E{constructor(){this._rootScope=new x(null,0,null,1,C.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 x(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=C.MIN_ID,e.end=C.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class I{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 F{constructor(e,t=0,i=0,n=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=i,this._finalizeHint=n}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 O extends F{static finalizeAll(e){I.finalizeAll(e)}link(e){I.prototype.link.call(this,e)}unlink(){I.prototype.unlink.call(this)}static create(e,t,i,n){return new O(e,t,i,n)}constructor(e,t,i,n){super(e,t,i,n),this._next=null,this._prev=null,this.link(t?e.finalizing_reflist:e.reflist)}dispose(){this.unlink(),this.envObject.dequeueFinalizer(this),super.dispose()}finalize(){this.finalizeCore(!0)}finalizeCore(e){const t=this._finalizeCallback,i=this._finalizeData,n=this._finalizeHint;let s;this.resetFinalizer(),this.unlink();let r=!1;if(t){const e=Number(t);try{this.envObject.callFinalizer(e,i,n)}catch(e){r=!0,s=e}}if(e&&this.dispose(),r)throw s}}class D extends O{constructor(e,t,i,n,s,r){super(e,n,s,r),this._refcount=t,this._ownership=i}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(){this.finalizeCore(0===this._ownership)}}function N(e,t){if(!e.terminatedOrTerminating())throw t}class H{constructor(e,t,i,n,s){this.ctx=e,this.moduleApiVersion=t,this.makeDynCall_vppp=i,this.makeDynCall_vp=n,this.abort=s,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new o,this.refs=1,this.reflist=new I,this.finalizing_reflist=new I,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this.inGcFinalizer=!1,this._bindingMap=new WeakMap,this.id=0}canCallIntoJs(){return!0}terminatedOrTerminating(){return!this.canCallIntoJs()}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 0!==e.errorCode&&(e.errorCode=0),0!==e.engineErrorCode&&(e.engineErrorCode=0),0!==e.engineReserved&&(e.engineReserved=0),0}setLastError(e,t=0,i=0){const n=this.lastError;return n.errorCode!==e&&(n.errorCode=e),n.engineErrorCode!==t&&(n.engineErrorCode=t),n.engineReserved!==i&&(n.engineReserved=i),e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e,t=N){const i=this.openHandleScopes;this.clearLastError();const n=e(this);if(i!==this.openHandleScopes&&this.abort("open_handle_scopes != open_handle_scopes_before"),this.tryCatch.hasCaught()){t(this,this.tryCatch.extractException())}return n}callFinalizer(e,t,i){const n=this.makeDynCall_vppp(e),s=this.id,r=this.ctx.openScope(this);try{this.callIntoModule((()=>{n(s,t,i)}))}finally{this.ctx.closeScope(this,r)}}invokeFinalizerFromGC(e){if(this.moduleApiVersion!==w)this.enqueueFinalizer(e);else{const t=this.inGcFinalizer;this.inGcFinalizer=!0;try{e.finalize()}finally{this.inGcFinalizer=t}}}checkGCAccess(){this.moduleApiVersion===w&&this.inGcFinalizer&&this.abort("Finalizer is calling a function that may affect GC state.\nThe finalizers are run directly from GC and must not affect GC state.\nUse `node_api_post_finalizer` from inside of the finalizer to work around this issue.\nIt schedules the call as a new task in the event loop.")}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e)}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}deleteMe(){D.finalizeAll(this.finalizing_reflist),D.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id)}dispose(){0!==this.id&&(this.deleteMe(),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 D(this,0,0,t,e,i)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class j extends H{constructor(e,t,i,n,s,r,o){super(e,i,n,s,r),this.filename=t,this.nodeBinding=o,this.destructing=!1,this.finalizationScheduled=!1}deleteMe(){this.destructing=!0,this.drainFinalizerQueue(),super.deleteMe()}canCallIntoJs(){return super.canCallIntoJs()&&this.ctx.canCallIntoJs()}triggerFatalException(e){if(this.nodeBinding)this.nodeBinding.napi.fatalException(e);else{if("object"!=typeof process||null===process||"function"!=typeof process._fatalException)throw e;process._fatalException(e)||(console.error(e),process.exit(1))}}callbackIntoModule(e,t){return this.callIntoModule(t,((t,i)=>{if(t.terminatedOrTerminating())return;const n="object"==typeof process&&null!==process,s=!!n&&Boolean(process.execArgv&&-1!==process.execArgv.indexOf("--force-node-api-uncaught-exceptions-policy"));if(t.moduleApiVersion{s(r,i,n)}))}finally{this.ctx.closeScope(this,o)}}enqueueFinalizer(e){super.enqueueFinalizer(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),_((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}}function A(e,t,i,n,s,r,o){(i="number"!=typeof i?8:i)<8?i=8:i>b&&i!==w&&function(e,t){throw new Error(`${e} requires Node-API version ${t}, but this version of Node.js only supports version ${b} add-ons.`)}(t,i);const a=new j(e,t,i,n,s,r,o);return e.envStore.add(a),e.addCleanupHook(a,(()=>{a.unref()}),0),a}class B extends Error{constructor(e){super(e);const t=new.target,i=t.prototype;if(!(this instanceof B)){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(B.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class R extends B{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(R.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class M extends B{constructor(e,t){super(`${e}: The current runtime does not support "Buffer". Consider using buffer polyfill to make sure \`globalThis.Buffer\` is defined.${t?` ${t}`:""}`)}}Object.defineProperty(M.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBufferError"});class L{constructor(e){this._value=e}deref(){return this._value}dispose(){this._value=void 0}}class W{constructor(e){this._ref=new L(e)}setWeak(e,t){if(!c||void 0===this._ref||this._ref instanceof WeakRef)return;const i=this._ref.deref();try{W._registry.register(i,this,this);const n=new WeakRef(i);this._ref.dispose(),this._ref=n,this._param=e,this._callback=t}catch(e){if("symbol"!=typeof i)throw e}}clearWeak(){if(c&&void 0!==this._ref&&this._ref instanceof WeakRef){try{W._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 L(e)}}reset(){if(c)try{W._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref instanceof L&&this._ref.dispose(),this._ref=void 0}isEmpty(){return void 0===this._ref}deref(){if(void 0!==this._ref)return this._ref.deref()}}function T(e){e.persistent.reset(),e.envObject.invokeFinalizerFromGC(e)}W._registry=c?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 P extends D{static create(e,t,i,n,s=0,r=0,o=0){const a=e.ctx.handleStore.get(t),l=new P(e,i,n,s,r,o);var c;return e.ctx.refStore.add(l),l.canBeWeak=(c=a).isObject()||c.isFunction()||c.isSymbol(),l.persistent=new W(a.value),0===i&&l._setWeak(),l}constructor(e,t,i,n=0,s=0,r=0){super(e,t>>>0,i,n,s,r),this.id=0}ref(){if(this.persistent.isEmpty())return 0;const e=super.ref();return 1===e&&this.canBeWeak&&this.persistent.clearWeak(),e}unref(){if(this.persistent.isEmpty())return 0;const e=this.refCount(),t=super.unref();return 1===e&&0===t&&this._setWeak(),t}get(){if(this.persistent.isEmpty())return 0;const e=this.persistent.deref();return this.envObject.ensureHandle(e).id}_setWeak(){this.canBeWeak?this.persistent.setWeak(this,T):this.persistent.reset()}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 U{static create(e,t){const i=new U(e,t);return e.deferredStore.add(i),i}constructor(e,t){this.id=0,this.ctx=e,this.value=t}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 G{constructor(){this._values=[void 0],this._values.length=4,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=e+(e>>1)+16)}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;en.envObject===e&&n.fn===t&&n.arg===i)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new q(e,t,i,this._cleanupHookCounter++))}remove(e,t,i){for(let n=0;nt.order-e.order));for(let t=0;t{this.destroy()})))}getRuntimeVersions(){return{version:g,NODE_API_SUPPORTED_VERSION_MAX:b,NAPI_VERSION_EXPERIMENTAL:w,NODE_API_DEFAULT_MODULE_API_VERSION:8}}createNotSupportWeakRefError(e,t){return new R(e,t)}createNotSupportBufferError(e,t){return new M(e,t)}createReference(e,t,i,n,s=0,r=0,o=0){return P.create(e,t,i,n,s,r,o)}createDeferred(e){return U.create(this,e)}createEnv(e,t,i,n,s,r){return A(this,e,t,i,n,s,r)}createTrackedFinalizer(e,t,i,n){return O.create(e,t,i,n)}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 C.UNDEFINED;case null:return C.NULL;case!0:return C.TRUE;case!1:return C.FALSE;case r:return C.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()}setCanCallIntoJs(e){this._canCallIntoJs=e}setStopping(e){this._isStopping=e}canCallIntoJs(){return this._canCallIntoJs&&!this._isStopping}destroy(){this.setStopping(!0),this.setCanCallIntoJs(!1),this.runCleanup()}}let Q;function X(){return new J}function K(){return Q||(Q=X()),Q}export{t as CallbackInfo,n as CallbackInfoStack,k as ConstHandle,J as Context,U as Deferred,B as EmnapiError,H as Env,F as Finalizer,z as Handle,x as HandleScope,C as HandleStore,w as NAPI_VERSION_EXPERIMENTAL,S as NODE_API_DEFAULT_MODULE_API_VERSION,b as NODE_API_SUPPORTED_VERSION_MAX,y as NODE_API_SUPPORTED_VERSION_MIN,j as NodeEnv,M as NotSupportBufferError,R as NotSupportWeakRefError,W as Persistent,D as RefBase,I as RefTracker,P as Reference,E as ScopeStore,G as Store,O as TrackedFinalizer,o as TryCatch,X as createContext,K as getDefaultContext,p as isReferenceType,g as version};