(function(){"use strict";var M={exports:{}},ge=M.exports,se;function ye(){return se||(se=1,(function(l,e){(function(t,s){s(e)})(ge,(function(t){class s{kept_object;room_id;id;bc;constructor(i,c){this.kept_object=i,this.room_id=`shared-object-${c}`,this.id="parent",this.bc=new globalThis.BroadcastChannel(this.room_id),this.register()}register(){const i=this.bc;i.onmessage=c=>{const r=c.data;if(r.msg===void 0)throw new Error("Invalid message");this.is_my_msg(r)||r.to===this.id&&(r.msg==="func_call::call"&&this.func_call(r),r.msg==="get::get"&&this.get(r))}}is_my_msg(i){return i.from===this.id}func_call(i){const c=this.bc,{names:r,args:o,id:a}=i;try{if(r.length===1&&r[0]===".self"){const u=this.kept_object(...o);c.postMessage({msg:"func_call::return",ret:u,id:a,from:this.id,to:i.from});return}let f=this.kept_object;for(const u of r)f=f[u];const d=f(...o);c.postMessage({msg:"func_call::return",ret:d,id:a,from:this.id,to:i.from})}catch(f){c.postMessage({msg:"func_call::error",error:f,id:a,from:this.id,to:i.from})}}get(i){const c=this.bc,{names:r,id:o}=i;try{let a=this.kept_object;for(const f of r)a=a[f];c.postMessage({msg:"get::return",ret:a,id:o,from:this.id,to:i.from,can_post:!0})}catch(a){if(a.name==="DataCloneError"){c.postMessage({msg:"get::data_clone_error",error:a,id:o,from:this.id,to:i.from,can_post:!1});return}c.postMessage({msg:"get::error",error:a,id:o,from:this.id,to:i.from,can_post:!1})}}}class n{room_id;id;map=new Map;bc;callbacks=[];constructor(i){this.room_id=`shared-object-${i}`,this.id=this.get_id(),this.bc=new globalThis.BroadcastChannel(this.room_id),this.register()}is_my_msg(i){return i.from===this.id}proxy(){return new Proxy(()=>{},{get:(i,c)=>this.get([c]),apply:(i,c,r)=>this.call([".self"],r)})}addCallback(i){this.callbacks.push(i)}removeCallback(i){const c=this.callbacks.indexOf(i);c!==-1&&this.callbacks.splice(c,1)}register(){const i=this.bc;i.onmessage=c=>{const r=c.data;if(r.msg===void 0)throw new Error("Invalid message");if(!this.is_my_msg(r)&&r.to===this.id){if(r.msg.startsWith("func_call::")){const o=this.map.get(r.id);if(o!==void 0)o(r),this.map.delete(r.id);else throw new Error("what happened? unreachable code")}if(r.msg.startsWith("get::")){const o=this.map.get(r.id);if(o!==void 0)o(r),this.map.delete(r.id);else throw new Error("what happened? unreachable code")}}}}get_id(){return Math.random().toString(36).slice(2)}check_msg_error(i){if(i.msg.endsWith("::error"))throw i.error}get(i){const{promise:c,resolve:r}=Promise.withResolvers(),o=this.get_id();this.map.set(o,r),this.postMessage({msg:"get::get",names:i,id:o,from:this.id,to:"parent"});let a=!1;const d=(async()=>{const h=await c;if(this.check_msg_error(h),h.msg==="get::return"||h.msg==="get::data_clone_error"){a&&console.warn(`Warning! Objects that cannot be transferred are being retrieved.`);const w=h;return w.can_post?w.ret:new Proxy(()=>{},{get:(g,y)=>y==="then"?(a=!0,Promise.resolve(d)):this.get([...i,y]),apply:(g,y,b)=>this.call(i,b)})}throw new Error("what happened? unreachable code")})();return new Proxy(()=>{},{get:(h,w)=>w==="then"?(a=!0,d.then.bind(d)):this.get([...i,w]),apply:(h,w,g)=>this.call(i,g)})}async call(i,c){const{promise:r,resolve:o}=Promise.withResolvers(),a=this.get_id();this.map.set(a,o),this.postMessage({msg:"func_call::call",names:i,args:c,id:a});const f=await r;if(this.check_msg_error(f),f.msg==="func_call::return")return f.ret;throw new Error("what happened? unreachable code")}async call_callback(i){const{name:c,args:r,id:o}=i;try{if(i.msg==="callback::call"){const a=this.callbacks.find(d=>d.name===c);if(a===void 0)throw new Error("function not found");const f=a(...r);if(f instanceof Promise){this.postMessage({msg:"callback::promise",id:o});const d=await f;this.postMessage({msg:"callback::promise_return",ret:d,id:o})}else this.postMessage({msg:"callback::return",ret:f,id:o})}}catch(a){this.postMessage({msg:"callback::error",error:a,id:o})}}postMessage(i){this.bc.postMessage({...i,from:this.id,to:"parent"})}}t.SharedObject=s,t.SharedObjectRef=n,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}))})(M,M.exports)),M.exports}var ne=ye();const G=0,Y=1,S=0,m=8,oe=28,ie=37,be=52,ae=58;class W{static read_bytes(e,t){const s=new W;return s.buf=e.getUint32(t,!0),s.buf_len=e.getUint32(t+4,!0),s}static read_bytes_array(e,t,s){const n=[];for(let _=0;_{}}new xe(!1);class _e extends Error{constructor(e){super("exit with exit code "+e),this.code=e}}class F{share_arrays_memory;constructor(e=new SharedArrayBuffer(10*1024*1024)){this.share_arrays_memory=e;const t=new Int32Array(this.share_arrays_memory);Atomics.store(t,0,0),Atomics.store(t,1,0),Atomics.store(t,2,12)}static init_self(e){return new F(e.share_arrays_memory)}async async_write(e,t,s){const n=new Int32Array(this.share_arrays_memory);for(;;){let _;const{value:i}=Atomics.waitAsync(n,0,1);if(i instanceof Promise?_=await i:_=i,_==="timed-out")throw new Error("timed-out lock");if(Atomics.compareExchange(n,0,0,1)!==0)continue;const r=this.write_inner(e,t,s);return Atomics.store(n,0,0),Atomics.notify(n,0,1),r}}block_write(e,t,s){for(;;){const n=new Int32Array(this.share_arrays_memory);if(Atomics.wait(n,0,1)==="timed-out")throw new Error("timed-out lock");if(Atomics.compareExchange(n,0,0,1)!==0)continue;const c=this.write_inner(e,t,s);return Atomics.store(n,0,0),Atomics.notify(n,0,1),c}}write_inner(e,t,s){const n=new Int32Array(this.share_arrays_memory),_=new Uint8Array(this.share_arrays_memory),i=Atomics.add(n,1,1);let c;i===0?c=Atomics.store(n,2,12):c=Atomics.load(n,2);const r=this.share_arrays_memory.byteLength,o=e.byteLength,a=c+o;if(rthis.share_arrays_memory.byteLength)throw new Error("over memory");Atomics.store(s,2,i);const c=new Int32Array(this.share_arrays_memory,n);c[0]=e.length,c[1]=e.length,c.set(e,2),new Uint32Array(this.share_arrays_memory,n+8+e.length*4).set(t),Atomics.add(s,1,1),this.release_lock()}get_data(e){const t=new Int32Array(this.share_arrays_memory);if(Atomics.load(t,1)===0)return;this.block_lock();const n=Atomics.load(t,1),_=[];let i=12;for(let c=0;c{const e=new Error(l.message);return e.name=l.name,e.stack=l.stack?.replace(/.wasm:/g,`.wasm `),e.cause=l.cause,e};class ${allocator;lock;signature_input;constructor(e,t,s){this.allocator=e,this.lock=t,this.signature_input=s}block_lock_base_func(){const e=new Int32Array(this.lock);for(;;){if(Atomics.wait(e,0,1)==="timed-out")throw new Error("timed-out lock");if(Atomics.compareExchange(e,0,0,1)===0)return}}async async_lock_base_func(){const e=new Int32Array(this.lock);for(;;){const{value:t}=Atomics.waitAsync(e,0,1);if(await t==="timed-out")throw new Error("timed-out lock");if(Atomics.compareExchange(e,0,0,1)===0)return}}call_base_func(){const e=new Int32Array(this.lock);Atomics.store(e,2,1),Atomics.store(e,1,0),Atomics.notify(e,1,1)}block_wait_base_func(){const e=new Int32Array(this.lock);if(Atomics.wait(e,2,1)==="timed-out")throw new Error("timed-out lock")}async async_wait_base_func(){const e=new Int32Array(this.lock),{value:t}=Atomics.waitAsync(e,2,1);if(await t==="timed-out")throw new Error("timed-out lock")}release_base_func(){const e=new Int32Array(this.lock);Atomics.store(e,0,0),Atomics.notify(e,0,1)}new_worker(e,t,s){console.log("new_worker",e,t,s),this.block_lock_base_func();const n=new Int32Array(this.signature_input);Atomics.store(n,0,1);const _=new TextEncoder().encode(e);this.allocator.block_write(_,this.signature_input,1),Atomics.store(n,3,t?.type==="module"?1:0);const i=JSON.stringify(s),c=new TextEncoder().encode(i);this.allocator.block_write(c,this.signature_input,4),this.call_base_func(),this.block_wait_base_func();const r=Atomics.load(n,0);return this.release_base_func(),new Be(r)}async async_start_on_thread(e,t,s){await this.async_lock_base_func();const n=new Int32Array(this.signature_input);Atomics.store(n,0,2);const _=new TextEncoder().encode(e);await this.allocator.async_write(_,this.signature_input,1),Atomics.store(n,3,t?.type==="module"?1:0);const i=JSON.stringify(s),c=new TextEncoder().encode(i);await this.allocator.async_write(c,this.signature_input,4),this.call_base_func(),await this.async_wait_base_func(),this.release_base_func()}block_start_on_thread(e,t,s){this.block_lock_base_func();const n=new Int32Array(this.signature_input);Atomics.store(n,0,2);const _=new TextEncoder().encode(e);this.allocator.block_write(_,this.signature_input,1),Atomics.store(n,3,t?.type==="module"?1:0);const i=JSON.stringify(s),c=new TextEncoder().encode(i);this.allocator.block_write(c,this.signature_input,4),this.call_base_func(),this.block_wait_base_func(),this.release_base_func()}static init_self(e){return new $(F.init_self(e.allocator),e.lock,e.signature_input)}done_notify(e){const t=new Int32Array(this.lock,12);if(Atomics.compareExchange(t,0,0,2)!==0){console.error("what happened?");return}Atomics.store(t,1,e),Atomics.notify(t,0)===0&&Atomics.store(t,0,0)}async async_wait_done_or_error(){const e=new Int32Array(this.lock,12);Atomics.store(e,0,0);let t;const{value:s}=Atomics.waitAsync(e,0,0);if(s instanceof Promise?t=await s:t=s,t==="timed-out")throw new Error("timed-out");const n=Atomics.load(e,0);if(n===3)return Atomics.compareExchange(e,0,3,0)!==3&&console.error("what happened?"),0;if(n===2){const d=Atomics.compareExchange(e,0,2,0),u=Atomics.load(e,1);return d!==2&&console.error("what happened?"),u}if(n!==1)throw new Error("unknown code");const _=Atomics.load(e,1),i=Atomics.load(e,2),c=this.allocator.get_memory(_,i),r=new TextDecoder().decode(c),o=JSON.parse(r),a=de(o);throw Atomics.compareExchange(e,0,1,0)!==1&&console.error("what happened?"),a}block_wait_done_or_error(){const e=new Int32Array(this.lock,12);if(Atomics.store(e,0,0),Atomics.wait(e,0,0)==="timed-out")throw new Error("timed-out");const s=Atomics.load(e,0);if(s===3)return Atomics.compareExchange(e,0,3,0)!==3&&console.error("what happened?"),0;if(s===2){const f=Atomics.compareExchange(e,0,2,0),d=Atomics.load(e,1);return f!==2&&console.error("what happened?"),d}if(s!==1)throw new Error("unknown code");const n=Atomics.load(e,1),_=Atomics.load(e,2),i=this.allocator.get_memory(n,_),c=new TextDecoder().decode(i),r=JSON.parse(c),o=de(r);throw Atomics.compareExchange(e,0,1,0)!==1&&console.error("what happened?"),o}}class Be{id;constructor(e){this.id=e}get_id(){return this.id}}const Se=()=>{const l='let worker_background;class AllocatorUseArrayBuffer{share_arrays_memory;constructor(r=new SharedArrayBuffer(0xa00000)){this.share_arrays_memory=r;let e=new Int32Array(this.share_arrays_memory);Atomics.store(e,0,0),Atomics.store(e,1,0),Atomics.store(e,2,12)}static init_self(r){return new AllocatorUseArrayBuffer(r.share_arrays_memory)}async async_write(r,e,t){let o=new Int32Array(this.share_arrays_memory);for(;;){let{value:s}=Atomics.waitAsync(o,0,1);if("timed-out"===(s instanceof Promise?await s:s))throw Error("timed-out lock");if(0!==Atomics.compareExchange(o,0,0,1))continue;let i=this.write_inner(r,e,t);return Atomics.store(o,0,0),Atomics.notify(o,0,1),i}}block_write(r,e,t){for(;;){let o=new Int32Array(this.share_arrays_memory);if("timed-out"===Atomics.wait(o,0,1))throw Error("timed-out lock");if(0!==Atomics.compareExchange(o,0,0,1))continue;let s=this.write_inner(r,e,t);return Atomics.store(o,0,0),Atomics.notify(o,0,1),s}}write_inner(r,e,t){let o,s,i=new Int32Array(this.share_arrays_memory),a=new Uint8Array(this.share_arrays_memory);o=0===Atomics.add(i,1,1)?Atomics.store(i,2,12):Atomics.load(i,2);let n=this.share_arrays_memory.byteLength,c=r.byteLength,l=o+c;if(n({message:r.message,name:r.name,stack:r.stack,cause:r.cause}),WorkerBackground=class r{override_object;allocator;lock;signature_input;workers=[void 0];start_worker;listen_holder;constructor(r,e,t,o){this.override_object=r,this.lock=e??new SharedArrayBuffer(24),this.allocator=t??new AllocatorUseArrayBuffer(new SharedArrayBuffer(10240)),this.signature_input=o??new SharedArrayBuffer(24),this.listen_holder=this.listen()}static init_self(e,t){return new r(e,t.lock,AllocatorUseArrayBuffer.init_self(t.allocator),t.signature_input)}assign_worker_id(){for(let r=1;r{let r=Atomics.load(e,1),t=Atomics.load(e,2),o=this.allocator.get_memory(r,t);return this.allocator.free(r,t),new Worker(new TextDecoder().decode(o),{type:1===Atomics.load(e,3)?"module":"classic"})},s=()=>{let r=Atomics.load(e,4),t=Atomics.load(e,5),o=this.allocator.get_memory(r,t);return this.allocator.free(r,t),JSON.parse(new TextDecoder().decode(o))};switch(Atomics.load(e,0)){case 1:{let r=o(),t=s(),i=this.assign_worker_id();console.debug("new worker "+i),this.workers[i]=r;let{promise:a,resolve:n}=Promise.withResolvers();r.onmessage=async r=>{let{msg:e}=r.data;if("ready"===e&&n(),"done"===e&&(this.workers[i].terminate(),this.workers[i]=void 0,console.debug(`worker ${i} done so terminate`)),"error"===e||"exit"===e){this.workers[i].terminate(),this.workers[i]=void 0;let t=0;for(let r of this.workers)void 0!==r&&(r.terminate(),"error"===e?console.warn("wasi throw error but child process exists, terminate "+t):console.debug("wasi exit but child process exists, terminate "+t)),t++;void 0!==this.start_worker&&(this.start_worker.terminate(),"error"===e?console.warn("wasi throw error but wasi exists, terminate wasi"):console.debug("wasi exit but wasi exists, terminate wasi")),this.workers=[void 0],this.start_worker=void 0;let o=new Int32Array(this.lock,12);if("error"===e){let e=r.data.error,t=serialize(e),[s,i]=await this.allocator.async_write(new TextEncoder().encode(JSON.stringify(t)),this.lock,4);if(0!==Atomics.compareExchange(o,0,0,1)){console.error("what happened?"),this.allocator.free(s,i);return}1!==Atomics.notify(o,0)&&(console.error(e),this.allocator.free(s,i),Atomics.store(o,0,0))}else{let e=r.data.code;if(0!==Atomics.compareExchange(o,0,0,2))return void console.error("what happened?");Atomics.store(o,1,e),0===Atomics.notify(o,0)&&Atomics.store(o,0,0)}}},r.postMessage({...this.override_object,...t,worker_id:i,worker_background_ref:this.ref()}),await a,Atomics.store(e,0,i);break}case 2:{console.debug("start worker create"),this.start_worker=o();let r=s();this.start_worker.onmessage=async r=>{let{msg:e}=r.data;if("done"===e){let r=0;for(let e of this.workers)void 0!==e&&(e.terminate(),console.warn("wasi done but worker exists, terminate "+r)),r++;this.start_worker.terminate(),this.start_worker=void 0;let e=new Int32Array(this.lock,12);Atomics.store(e,0,3),0===Atomics.notify(e,0)&&Atomics.store(e,0,0),console.debug("start worker done so terminate")}if("error"===e||"exit"===e){let t=0;for(let r of this.workers)void 0!==r&&(r.terminate(),"error"===e?console.warn("wasi throw error but worker exists, terminate "+t):console.debug("wasi exit but worker exists, terminate "+t)),t++;void 0!==this.start_worker&&(this.start_worker.terminate(),"error"===e?console.warn("wasi throw error but wasi exists, terminate start worker"):console.debug("wasi exit but wasi exists, terminate start worker")),this.workers=[void 0],this.start_worker=void 0;let o=new Int32Array(this.lock,12);if("error"===e){let e=r.data.error,t=serialize(e),[s,i]=await this.allocator.async_write(new TextEncoder().encode(JSON.stringify(t)),this.lock,4);if(0!==Atomics.compareExchange(o,0,0,1)){console.error("what happened?"),this.allocator.free(s,i);return}if(0===Atomics.notify(o,0))throw this.allocator.free(s,i),Atomics.store(o,0,0),e}else{let e=r.data.code;if(0!==Atomics.compareExchange(o,0,0,2))return void console.error("what happened?");Atomics.store(o,1,e),0===Atomics.notify(o,0)&&Atomics.store(o,0,0)}}},this.start_worker.postMessage({...this.override_object,...r,worker_background_ref:this.ref()})}}Atomics.store(r,1,1),Atomics.store(r,2,0),0===Atomics.notify(r,2,1)&&console.warn("notify failed, waiter is late")}}};globalThis.onmessage=r=>{let{override_object:e,worker_background_ref_object:t}=r.data;worker_background=WorkerBackground.init_self(e,t),postMessage("ready")};',e=new Blob([l],{type:"application/javascript"});return URL.createObjectURL(e)},je=()=>({allocator:{share_arrays_memory:new SharedArrayBuffer(10*1024)},lock:new SharedArrayBuffer(24),signature_input:new SharedArrayBuffer(24)});class N{share_memory;wasi_farm_refs_object;worker_url;worker_background_ref;worker_background_ref_object;worker_background_worker;worker_background_worker_promise;constructor(e,t,s,n=16777216,_,i,c){if(this.worker_url=e,this.wasi_farm_refs_object=t,s===void 0){const o=n/65536;if(o<16)throw new Error("The stack size must be at least 16 bytes.");const a=1073741824/65536;this.share_memory={memory:new WebAssembly.Memory({initial:o,maximum:a,shared:!0})}}else this.share_memory=s;if(_===void 0){let r;c?r=c:r=Se(),this.worker_background_worker=new Worker(r,{type:"module"}),c||URL.revokeObjectURL(r);const{promise:o,resolve:a}=Promise.withResolvers();this.worker_background_worker_promise=o,this.worker_background_worker.onmessage=()=>{this.worker_background_worker_promise=void 0,a()},this.worker_background_ref_object=je(),this.worker_background_ref=$.init_self(this.worker_background_ref_object),this.worker_background_worker.postMessage({override_object:{sl_object:this.get_object(),thread_spawn_wasm:i},worker_background_ref_object:structuredClone(this.worker_background_ref_object)})}else this.worker_background_ref_object=_,this.worker_background_ref=$.init_self(this.worker_background_ref_object)}async wait_worker_background_worker(){if(this.worker_background_worker_promise){await this.worker_background_worker_promise;return}}check_worker_background_worker(){if(this.worker_background_worker_promise)throw new Error("worker_background_worker is not ready.")}thread_spawn(e,t,s,n){return this.worker_background_ref.new_worker(this.worker_url,{type:"module"},{this_is_thread_spawn:!0,start_arg:e,args:t,env:s,fd_map:n}).get_id()}async async_start_on_thread(e,t,s){if(!self.Worker.toString().includes("[native code]"))if(self.Worker.toString().includes("function"))console.warn("SubWorker(new Worker on Worker) is polyfilled maybe.");else throw new Error("SubWorker(new Worker on Worker) is not supported.");await this.worker_background_ref.async_start_on_thread(this.worker_url,{type:"module"},{this_is_thread_spawn:!0,this_is_start:!0,args:e,env:t,fd_map:s})}block_start_on_thread(e,t,s){if(!self.Worker.toString().includes("[native code]"))if(self.Worker.toString().includes("function"))console.warn("SubWorker(new Worker on Worker) is polyfilled maybe.");else throw new Error("SubWorker(new Worker on Worker) is not supported.");this.worker_background_ref.block_start_on_thread(this.worker_url,{type:"module"},{this_is_thread_spawn:!0,this_is_start:!0,args:e,env:t,fd_map:s})}static init_self(e){return new N(e.worker_url,e.wasi_farm_refs_object,e.share_memory,void 0,e.worker_background_ref_object)}static init_self_with_worker_background_ref(e,t){return new N(e.worker_url,e.wasi_farm_refs_object,e.share_memory,void 0,t)}get_share_memory(){return this.share_memory}get_object(){return{share_memory:this.share_memory,wasi_farm_refs_object:this.wasi_farm_refs_object,worker_url:this.worker_url,worker_background_ref_object:this.worker_background_ref_object}}done_notify(e){this.worker_background_ref.done_notify(e)}async async_wait_done_or_error(){if(this.worker_background_worker===void 0)throw new Error("worker_background_worker is undefined.");return await this.worker_background_ref.async_wait_done_or_error()}block_wait_done_or_error(){if(this.worker_background_worker===void 0)throw new Error("worker_background_worker is undefined.");return this.worker_background_ref.block_wait_done_or_error()}}class Oe{args;env;wasi_farm_refs;id_in_wasi_farm_ref;inst;wasiImport;wasiThreadImport;can_array_buffer;can_thread_spawn;thread_spawner;async wait_worker_background_worker(){await this.thread_spawner?.wait_worker_background_worker()}check_worker_background_worker(){this.thread_spawner?.check_worker_background_worker()}fd_map=[];get_fd_and_wasi_ref(e){const t=this.fd_map[e];if(!t)return[void 0,void 0];const[s,n]=t;return[s,this.wasi_farm_refs[n]]}get_fd_and_wasi_ref_n(e){const t=this.fd_map[e];if(!t)return[void 0,void 0];const[s,n]=t;return[s,n]}start(e){this.inst=e;try{if(e.exports._start(),this.can_thread_spawn){if(!this.thread_spawner)throw new Error("thread_spawner is not defined");this.thread_spawner.done_notify(0)}return 0}catch(t){if(t instanceof _e)return this.can_thread_spawn&&this.thread_spawner.done_notify(t.code),t.code;throw t}}start_only(e){this.inst=e,e.exports._start()}async async_start_on_thread(){if(!this.can_thread_spawn||!this.thread_spawner)throw new Error("thread_spawn is not supported");if(await this.wait_worker_background_worker(),this.inst)throw new Error("what happened?");const e=this.thread_spawner.get_share_memory();for(const n in e)new Uint8Array(e[n].buffer).fill(0);const t=this.thread_spawner.async_wait_done_or_error();return await this.thread_spawner.async_start_on_thread(this.args,this.env,this.fd_map),await t}block_start_on_thread(){if(!this.can_thread_spawn||!this.thread_spawner)throw new Error("thread_spawn is not supported");if(console.log("block_start_on_thread"),this.check_worker_background_worker(),console.log("block_start_on_thread"),this.inst)throw new Error("what happened?");const e=this.thread_spawner.get_share_memory();for(const s in e)new Uint8Array(e[s].buffer).fill(0);console.log("block_start_on_thread: start"),this.thread_spawner.block_start_on_thread(this.args,this.env,this.fd_map),console.log("block_start_on_thread: wait");const t=this.thread_spawner.block_wait_done_or_error();return console.log("block_start_on_thread: done"),t}wasi_thread_start(e,t,s){this.inst=e,e.exports.wasi_thread_start(t,s)}initialize(e){this.inst=e,e.exports._initialize&&e.exports._initialize()}mapping_fds(e,t){this.fd_map=[void 0,void 0,void 0];for(let s=0;s0)for(let t=0;t=U.length){y.set(U,z.buf);break}y.set(U.slice(B,B+z.buf_len),z.buf),B+=z.buf_len}}return v},fd_prestat_get(o,a){r.check_fds();const[f,d]=r.get_fd_and_wasi_ref(o);if(f===void 0||d===void 0)return m;const[u,h]=d.fd_prestat_get(f);if(u){const[w,g]=u,y=new DataView(r.inst.exports.memory.buffer);y.setUint32(a,w,!0),y.setUint32(a+4,g,!0)}return h},fd_prestat_dir_name(o,a,f){r.check_fds();const[d,u]=r.get_fd_and_wasi_ref(o);if(d===void 0||u===void 0)return[void 0,m];const[h,w]=u.fd_prestat_dir_name(d,f);return h&&new Uint8Array(r.inst.exports.memory.buffer).set(h,a),w},fd_pwrite(o,a,f,d,u){r.check_fds();const[h,w]=r.get_fd_and_wasi_ref(o);if(h===void 0||w===void 0)return m;const g=new DataView(r.inst.exports.memory.buffer),y=new Uint8Array(r.inst.exports.memory.buffer),b=L.read_bytes_array(g,a,f),p=new Uint8Array(b.reduce((U,B)=>U+B.buf_len,0));let v=0;for(const U of b)p.set(y.slice(U.buf,U.buf+U.buf_len),v),v+=U.buf_len;const[I,E]=w.fd_pwrite(h,p,d);return I&&g.setUint32(u,I,!0),E},fd_read(o,a,f,d){r.check_fds();const[u,h]=r.get_fd_and_wasi_ref(o);if(u===void 0||h===void 0)return m;const w=new DataView(r.inst.exports.memory.buffer),g=new Uint8Array(r.inst.exports.memory.buffer),y=new Uint32Array(w.buffer,a,f*2),[b,p]=h.fd_read(u,y);if(b){const v=W.read_bytes_array(w,a,f),[I,E]=b;w.setUint32(d,I,!0);let U=0;for(const B of v){if(U+B.buf_len>=E.length){g.set(E,B.buf);break}g.set(E.slice(U,U+B.buf_len),B.buf),U+=B.buf_len}}return p},fd_readdir(o,a,f,d,u){r.check_fds();const[h,w]=r.get_fd_and_wasi_ref(o);if(h===void 0||w===void 0)return[void 0,m];const g=new DataView(r.inst.exports.memory.buffer),y=new Uint8Array(r.inst.exports.memory.buffer),[b,p]=w.fd_readdir(h,f,d);if(b){const[v,I]=b;g.setUint32(u,I,!0),y.set(v,a)}return p},fd_renumber(o,a){r.check_fds();const[f,d]=r.get_fd_and_wasi_ref(a);if(f!==void 0&&d!==void 0){const u=d.fd_close(f);if(r.check_fds(),u!==S)return u}if(r.fd_map[a])throw new Error("fd is already mapped");return r.fd_map[a]=r.fd_map[o],r.fd_map[o]=void 0,S},fd_seek(o,a,f,d){r.check_fds();const[u,h]=r.get_fd_and_wasi_ref(o);if(u===void 0||h===void 0)return[void 0,m];const[w,g]=h.fd_seek(u,a,f);return w&&new DataView(r.inst.exports.memory.buffer).setBigUint64(d,w,!0),g},fd_sync(o){r.check_fds();const[a,f]=r.get_fd_and_wasi_ref(o);return a===void 0||f===void 0?m:f.fd_sync(a)},fd_tell(o,a){r.check_fds();const[f,d]=r.get_fd_and_wasi_ref(o);if(f===void 0||d===void 0)return[void 0,m];const[u,h]=d.fd_tell(f);return u&&new DataView(r.inst.exports.memory.buffer).setBigUint64(a,u,!0),h},fd_write(o,a,f,d){r.check_fds();const[u,h]=r.get_fd_and_wasi_ref(o);if(u===void 0||h===void 0)return m;const w=new DataView(r.inst.exports.memory.buffer),g=new Uint8Array(r.inst.exports.memory.buffer),y=L.read_bytes_array(w,a,f),b=new Uint8Array(y.reduce((E,U)=>E+U.buf_len,0));let p=0;for(const E of y)b.set(g.slice(E.buf,E.buf+E.buf_len),p),p+=E.buf_len;const[v,I]=h.fd_write(u,b);return v&&w.setUint32(d,v,!0),I},path_create_directory(o,a,f){r.check_fds();const[d,u]=r.get_fd_and_wasi_ref(o);if(d===void 0||u===void 0)return m;const w=new Uint8Array(r.inst.exports.memory.buffer).slice(a,a+f);return u.path_create_directory(d,w)},path_filestat_get(o,a,f,d,u){r.check_fds();const[h,w]=r.get_fd_and_wasi_ref(o);if(h===void 0||w===void 0)return[void 0,m];const g=new DataView(r.inst.exports.memory.buffer),b=new Uint8Array(r.inst.exports.memory.buffer).slice(f,f+d),[p,v]=w.path_filestat_get(h,a,b);return p&&p.write_bytes(g,u),v},path_filestat_set_times(o,a,f,d,u,h,w){r.check_fds();const[g,y]=r.get_fd_and_wasi_ref(o);if(g===void 0||y===void 0)return[void 0,m];const p=new Uint8Array(r.inst.exports.memory.buffer).slice(f,f+d);return y.path_filestat_set_times(g,a,p,u,h,w)},path_link(o,a,f,d,u,h,w){r.check_fds();const[g,y]=r.get_fd_and_wasi_ref(o),[b,p]=r.get_fd_and_wasi_ref(u);if(g===void 0||y===void 0||b===void 0||p===void 0||y!==p)return m;const v=new Uint8Array(r.inst.exports.memory.buffer),I=v.slice(f,f+d),E=v.slice(h,h+w);return y.path_link(g,a,I,b,E)},path_open(o,a,f,d,u,h,w,g,y){r.check_fds();const[b,p]=r.get_fd_and_wasi_ref_n(o);if(b===void 0||p===void 0)return m;const v=r.wasi_farm_refs[p],E=new Uint8Array(r.inst.exports.memory.buffer).slice(f,f+d),[U,B]=v.path_open(b,a,E,u,h,w,g);if(U){if(r.fd_map.includes([U,p]))throw new Error("opened_fd already exists");const z=r.map_new_fd_and_notify(U,p);new DataView(r.inst.exports.memory.buffer).setUint32(y,z,!0)}return B},path_readlink(o,a,f,d,u,h){r.check_fds();const[w,g]=r.get_fd_and_wasi_ref(o);if(w===void 0||g===void 0)return[void 0,m];const y=new Uint8Array(r.inst.exports.memory.buffer),b=y.slice(a,a+f),[p,v]=g.path_readlink(w,b,u);return p&&(new DataView(r.inst.exports.memory.buffer).setUint32(h,p.length,!0),y.set(p,d)),v},path_remove_directory(o,a,f){r.check_fds();const[d,u]=r.get_fd_and_wasi_ref(o);if(d===void 0||u===void 0)return m;const w=new Uint8Array(r.inst.exports.memory.buffer).slice(a,a+f);return u.path_remove_directory(d,w)},path_rename(o,a,f,d,u,h){if(o===d)return S;r.check_fds();const[w,g]=r.get_fd_and_wasi_ref(o),[y,b]=r.get_fd_and_wasi_ref(d);if(w===void 0||g===void 0||y===void 0||b===void 0||g!==b)return m;const p=new Uint8Array(r.inst.exports.memory.buffer),v=p.slice(a,a+f),I=p.slice(u,u+h);return g.path_rename(w,v,y,I)},path_symlink(o,a,f,d,u){r.check_fds();const[h,w]=r.get_fd_and_wasi_ref(f);if(h===void 0||w===void 0)return m;const g=new Uint8Array(r.inst.exports.memory.buffer),y=g.slice(o,o+a),b=g.slice(d,d+u);return w.path_symlink(y,h,b)},path_unlink_file(o,a,f){r.check_fds();const[d,u]=r.get_fd_and_wasi_ref(o);if(d===void 0||u===void 0)return m;const w=new Uint8Array(r.inst.exports.memory.buffer).slice(a,a+f);return u.path_unlink_file(d,w)},poll_oneoff(o,a,f,d){if(f===0)return oe;if(f>1)return console.error("poll_oneoff: only a single subscription is supported"),ae;const u=new DataView(r.inst.exports.memory.buffer),h=H.read_bytes(u,o),w=h.eventtype,g=h.clockid,y=h.timeout;if(w!==ke)return console.error("poll_oneoff: only clock subscriptions are supported"),ae;let b;if(g===Y)b=()=>BigInt(Math.round(performance.now()*1e6));else if(g===G)b=()=>BigInt(new Date().getTime())*1000000n;else return oe;const p=((h.flags&ve)!==0?y:b()+y)-h.precision;for(;p>b(););return new Ue(h.userdata,S,w).write_bytes(u,a),u.setUint32(d,1,!0),S},proc_exit(o){throw r.check_fds(),new _e(o)},proc_raise(o){throw r.check_fds(),`raised signal ${o}`},sched_yield(){r.check_fds()},random_get(o,a){r.check_fds();const f=new Uint8Array(r.inst.exports.memory.buffer).subarray(o,o+a);if("crypto"in globalThis&&!(r.inst.exports.memory.buffer instanceof SharedArrayBuffer))for(let d=0;d{if(r.check_fds(),!r.can_thread_spawn||!r.thread_spawner)throw new Error("thread_spawn is not allowed");return r.thread_spawner.thread_spawn(o,r.args,r.env,r.fd_map)}}}}let A;const ue=typeof TextDecoder<"u"?new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}):{decode:()=>{throw Error("TextDecoder not available")}};typeof TextDecoder<"u"&&ue.decode();let C=null;function D(){return(C===null||C.byteLength===0)&&(C=new Uint8Array(A.memory.buffer)),C}function X(l,e){return l=l>>>0,ue.decode(D().subarray(l,l+e))}const O=new Array(128).fill(void 0);O.push(void 0,null,!0,!1);let V=O.length;function le(l){V===O.length&&O.push(O.length+1);const e=V;return V=O[e],O[e]=l,e}function he(l){return O[l]}function Te(l){l<132||(O[l]=V,V=l)}function ee(l){const e=he(l);return Te(l),e}let T=0;function te(l,e){const t=e(l.length*1,1)>>>0;return D().set(l,t/1),T=l.length,t}let P=null;function j(){return(P===null||P.byteLength===0)&&(P=new Int32Array(A.memory.buffer)),P}function Re(l,e){return l=l>>>0,D().subarray(l/1,l/1+e)}const J=typeof TextEncoder<"u"?new TextEncoder("utf-8"):{encode:()=>{throw Error("TextEncoder not available")}},ze=typeof J.encodeInto=="function"?function(l,e){return J.encodeInto(l,e)}:function(l,e){const t=J.encode(l);return e.set(t),{read:l.length,written:t.length}};function De(l,e,t){if(t===void 0){const c=J.encode(l),r=e(c.length,1)>>>0;return D().subarray(r,r+c.length).set(c),T=c.length,r}let s=l.length,n=e(s,1)>>>0;const _=D();let i=0;for(;i127)break;_[n+i]=c}if(i!==s){i!==0&&(l=l.slice(i)),n=t(n,s,s=i+l.length*3,1)>>>0;const c=D().subarray(n+i,n+s),r=ze(l,c);i+=r.written,n=t(n,s,i,1)>>>0}return T=i,n}const re=Object.freeze({ResultSuccess:1,1:"ResultSuccess",NeedsMoreInput:2,2:"NeedsMoreInput",NeedsMoreOutput:3,3:"NeedsMoreOutput"}),Me=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(l=>A.__wbg_brotlidecstream_free(l>>>0));class We{__destroy_into_raw(){const e=this.__wbg_ptr;return this.__wbg_ptr=0,Me.unregister(this),e}free(){const e=this.__destroy_into_raw();A.__wbg_brotlidecstream_free(e)}constructor(){const e=A.brotlidecstream_new();return this.__wbg_ptr=e>>>0,this}dec(e,t){try{const i=A.__wbindgen_add_to_stack_pointer(-16),c=te(e,A.__wbindgen_malloc),r=T;A.brotlidecstream_dec(i,this.__wbg_ptr,c,r,t);var s=j()[i/4+0],n=j()[i/4+1],_=j()[i/4+2];if(_)throw ee(n);return q.__wrap(s)}finally{A.__wbindgen_add_to_stack_pointer(16)}}decompress(e,t){try{const i=A.__wbindgen_add_to_stack_pointer(-16),c=te(e,A.__wbindgen_malloc),r=T;A.brotlidecstream_dec(i,this.__wbg_ptr,c,r,t);var s=j()[i/4+0],n=j()[i/4+1],_=j()[i/4+2];if(_)throw ee(n);return q.__wrap(s)}finally{A.__wbindgen_add_to_stack_pointer(16)}}total_out(){return A.brotlidecstream_total_out(this.__wbg_ptr)>>>0}}const me=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(l=>A.__wbg_brotlistreamresult_free(l>>>0));class q{static __wrap(e){e=e>>>0;const t=Object.create(q.prototype);return t.__wbg_ptr=e,me.register(t,t.__wbg_ptr,t),t}__destroy_into_raw(){const e=this.__wbg_ptr;return this.__wbg_ptr=0,me.unregister(this),e}free(){const e=this.__destroy_into_raw();A.__wbg_brotlistreamresult_free(e)}get code(){return A.__wbg_get_brotlistreamresult_code(this.__wbg_ptr)}set code(e){A.__wbg_set_brotlistreamresult_code(this.__wbg_ptr,e)}get buf(){try{const n=A.__wbindgen_add_to_stack_pointer(-16);A.__wbg_get_brotlistreamresult_buf(n,this.__wbg_ptr);var e=j()[n/4+0],t=j()[n/4+1],s=Re(e,t).slice();return A.__wbindgen_free(e,t*1,1),s}finally{A.__wbindgen_add_to_stack_pointer(16)}}set buf(e){const t=te(e,A.__wbindgen_malloc),s=T;A.__wbg_set_brotlistreamresult_buf(this.__wbg_ptr,t,s)}get input_offset(){return A.__wbg_get_brotlistreamresult_input_offset(this.__wbg_ptr)>>>0}set input_offset(e){A.__wbg_set_brotlistreamresult_input_offset(this.__wbg_ptr,e)}}typeof FinalizationRegistry>"u"||new FinalizationRegistry(l=>A.__wbg_decompressstream_free(l>>>0));async function Le(l,e){if(typeof Response=="function"&&l instanceof Response){if(typeof WebAssembly.instantiateStreaming=="function")try{return await WebAssembly.instantiateStreaming(l,e)}catch(s){if(l.headers.get("Content-Type")!="application/wasm")console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",s);else throw s}const t=await l.arrayBuffer();return await WebAssembly.instantiate(t,e)}else{const t=await WebAssembly.instantiate(l,e);return t instanceof WebAssembly.Instance?{instance:t,module:l}:t}}function Ne(){const l={};return l.wbg={},l.wbg.__wbindgen_error_new=function(e,t){const s=new Error(X(e,t));return le(s)},l.wbg.__wbindgen_object_drop_ref=function(e){ee(e)},l.wbg.__wbg_new_abda76e883ba8a5f=function(){const e=new Error;return le(e)},l.wbg.__wbg_stack_658279fe44541cf6=function(e,t){const s=he(t).stack,n=De(s,A.__wbindgen_malloc,A.__wbindgen_realloc),_=T;j()[e/4+1]=_,j()[e/4+0]=n},l.wbg.__wbg_error_f851667af71bcfc6=function(e,t){let s,n;try{s=e,n=t,console.error(X(e,t))}finally{A.__wbindgen_free(s,n,1)}},l.wbg.__wbindgen_throw=function(e,t){throw new Error(X(e,t))},l}function Ce(l,e){return A=l.exports,we.__wbindgen_wasm_module=e,P=null,C=null,A}async function we(l){if(A!==void 0)return A;typeof l>"u"&&(l=new URL(""+new URL("brotli_dec_wasm_bg-Dn-Szc7i.wasm",self.location.href).href,self.location.href));const e=Ne();(typeof l=="string"||typeof Request=="function"&&l instanceof Request||typeof URL=="function"&&l instanceof URL)&&(l=fetch(l));const{instance:t,module:s}=await Le(await l,e);return Ce(t,s)}var Ve=""+new URL("brotli_dec_wasm_bg-Dn-Szc7i.wasm?wasm",self.location.href).href;const Pe=we(Ve),Fe=1024*1024,$e=async()=>{await Pe;const l=new We;return new TransformStream({transform(t,s){let n,_=0;do{const i=t.slice(_),c=l.decompress(i,Fe);s.enqueue(c.buf),n=c.code,_+=c.input_offset}while(n===re.NeedsMoreOutput);n!==re.NeedsMoreInput&&n!==re.ResultSuccess&&s.error(`Brotli decompression failed with code ${n}`)},flush(t){t.terminate()}})},Je=async l=>{const e=await fetch(l);if(!e.ok)throw new Error("Failed to fetch wasm");if(!e.body)throw new Error("No body in response");return e.body.pipeThrough(await $e())},qe=async l=>{const e=new Response(await Je(l),{headers:{"Content-Type":"application/wasm"}});return await WebAssembly.compileStreaming(e)},Ke=()=>qe("https://oligamiq.github.io/rust_wasm/v0.2.0/llvm_opt.wasm.br");let Ge=class{enable(e){this.log=Ye(e===void 0?!0:e,this.prefix)}get enabled(){return this.isEnabled}constructor(e){this.isEnabled=e,this.prefix="wasi:",this.enable(e)}};function Ye(l,e){return l?console.log.bind(console,"%c%s","color: #265BA0",e):()=>{}}new Ge(!1);function He(l,e){return new Proxy(l,{get(t,s,n){const _=Reflect.get(t,s,n);return e.includes(s)?_:function(...i){console.log(s,"(",...i,")");const c=Reflect.apply(_,n,i);return console.log(" =",c),c}}})}const Ze=[];globalThis.addEventListener("message",async l=>{const{wasi_refs:e,ctx:t}=l.data,s=new ne.SharedObjectRef(t.waiter_id).proxy();for(;!await s.is_rustc_fetch_end();)await new Promise(n=>setTimeout(n,100));console.log("loading llvm"),await Qe(e,t)});let K,R;const Qe=async(l,e)=>{const t=await Ke();console.log("linker_wasm",t),R=new Oe(l,["llvm"],[]),K=await WebAssembly.instantiate(t,{wasi_snapshot_preview1:He(R.wasiImport,[])});const s=K.exports.memory.buffer,n=new Uint8Array(s).slice();Ze.push(new ne.SharedObject((..._)=>{try{_[0]!=="llvm"?R.args=["llvm",..._]:R.args=_,console.log(`wasi.start: ${R.args}`),console.log(R),new Uint8Array(K.exports.memory.buffer).set(n),R.start(K),console.log("wasi.start done")}catch(i){console.error(i)}},e.llvm_id)),console.log("llvm loaded")}})();