"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t=new WeakMap;function e(t,e){return new Proxy(t,{get:(t,s)=>e(t[s])})}class s{constructor(){this.value=void 0,this.exports=null,this.dataAddr=0,this.dataStart=0,this.dataEnd=0}getState(){return this.exports.asyncify_get_state()}assertNoneState(){let t=this.getState();if(0!==t)throw new Error(`Invalid async state ${t}, expected 0.`)}wrapImportFn(t){return(...e)=>{if(null===this.exports)return console.warn("Host functions called during Wasm initialization aren't supported by Asyncify because Wasm exports are not yet available. Function "+t.name+" is ignored."),null;if(2===this.getState())return this.exports.asyncify_stop_rewind(),this.value;this.assertNoneState();let s=t(...e);if(!(a=s)||"object"!=typeof a&&"function"!=typeof a||"function"!=typeof a.then)return s;var a;this.exports.asyncify_start_unwind(this.dataAddr),this.value=s}}wrapModuleImports(t){return e(t,t=>"function"==typeof t?this.wrapImportFn(t):t)}wrapImports(t){if(void 0!==t)return e(t,(t=Object.create(null))=>this.wrapModuleImports(t))}wrapExportFn(e){let s=t.get(e);return void 0!==s||(s=async(...t)=>{this.assertNoneState();let s=e(...t);for(;1===this.getState();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertNoneState(),this.exports.asyncify_start_rewind(this.dataAddr),s=e();return this.assertNoneState(),s},t.set(e,s)),s}wrapExports(e){let s=Object.create(null);for(let t in e){let a=e[t];"function"!=typeof a||t.startsWith("asyncify_")||(a=this.wrapExportFn(a)),Object.defineProperty(s,t,{enumerable:!0,value:a})}return t.set(e,s),s}init(t,e){const{exports:s}=t,r=s.memory||e.env&&e.env.memory;this.dataAddr=s.get_asyncify_stack_space_ptr(),this.dataStart=s.get_asyncify_stack_space_ptr()+8,this.dataEnd=s.get_asyncify_stack_space_ptr()+s.get_asyncify_stack_space_size(),new Int32Array(r.buffer,this.dataAddr).set([this.dataStart,this.dataEnd]),this.exports=this.wrapExports(s),Object.setPrototypeOf(t,a.prototype)}}class a extends WebAssembly.Instance{constructor(t,e){let a=new s;super(t,a.wrapImports(e)),a.init(this,e)}get exports(){return t.get(super.exports)}}Object.defineProperty(a.prototype,"exports",{enumerable:!0}),exports.Instance=a,exports.instantiate=async function(t,e){let a=new s,r=await WebAssembly.instantiate(t,a.wrapImports(e));return a.init(r instanceof WebAssembly.Instance?r:r.instance,e),r},exports.instantiateStreaming=async function(t,e){let a=new s,r=await WebAssembly.instantiateStreaming(t,a.wrapImports(e));return a.init(r.instance,e),r};