img2num 0.0.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ryan Millard
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,14 @@
1
+ var createImg2NumModule = (() => {
2
+
3
+ return (
4
+ async function(moduleArg = {}) {
5
+ var moduleRtn;
6
+
7
+ var Module=moduleArg;var ENVIRONMENT_IS_WEB=typeof window=="object";var ENVIRONMENT_IS_WORKER=typeof WorkerGlobalScope!="undefined";var ENVIRONMENT_IS_NODE=typeof process=="object"&&process.versions?.node&&process.type!="renderer";var arguments_=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var _scriptName=import.meta.url;var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var readAsync,readBinary;if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){try{scriptDirectory=new URL(".",_scriptName).href}catch{}{if(ENVIRONMENT_IS_WORKER){readBinary=url=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}}readAsync=async url=>{var response=await fetch(url,{credentials:"same-origin"});if(response.ok){return response.arrayBuffer()}throw new Error(response.status+" : "+response.url)}}}else{}var out=console.log.bind(console);var err=console.error.bind(console);var wasmBinary;var ABORT=false;var EXITSTATUS;function assert(condition,text){if(!condition){abort(text)}}function writeStackCookie(){var max=_emscripten_stack_get_end();if(max==0){max+=4}HEAPU32[max>>>2>>>0]=34821223;HEAPU32[max+4>>>2>>>0]=2310721022;HEAPU32[0>>>2>>>0]=1668509029}function checkStackCookie(){if(ABORT)return;var max=_emscripten_stack_get_end();if(max==0){max+=4}var cookie1=HEAPU32[max>>>2>>>0];var cookie2=HEAPU32[max+4>>>2>>>0];if(cookie1!=34821223||cookie2!=2310721022){abort(`Stack overflow! Stack cookie has been overwritten at ${ptrToString(max)}, expected hex dwords 0x89BACDFE and 0x2135467, but received ${ptrToString(cookie2)} ${ptrToString(cookie1)}`)}if(HEAPU32[0>>>2>>>0]!=1668509029){abort("Runtime error: The application has corrupted its heap memory area (address zero)!")}}var readyPromiseResolve,readyPromiseReject;var wasmMemory;var HEAP8,HEAPU8,HEAP16,HEAPU16,HEAP32,HEAPU32,HEAPF32,HEAPF64;var HEAP64,HEAPU64;var runtimeInitialized=false;function updateMemoryViews(){var b=wasmMemory.buffer;HEAP8=new Int8Array(b);HEAP16=new Int16Array(b);Module["HEAPU8"]=HEAPU8=new Uint8Array(b);HEAPU16=new Uint16Array(b);Module["HEAP32"]=HEAP32=new Int32Array(b);HEAPU32=new Uint32Array(b);HEAPF32=new Float32Array(b);HEAPF64=new Float64Array(b);HEAP64=new BigInt64Array(b);HEAPU64=new BigUint64Array(b)}function preRun(){if(Module["preRun"]){if(typeof Module["preRun"]=="function")Module["preRun"]=[Module["preRun"]];while(Module["preRun"].length){addOnPreRun(Module["preRun"].shift())}}callRuntimeCallbacks(onPreRuns)}function initRuntime(){runtimeInitialized=true;checkStackCookie();if(!Module["noFSInit"]&&!FS.initialized)FS.init();TTY.init();wasmExports["sa"]();FS.ignorePermissions=false}function postRun(){checkStackCookie();if(Module["postRun"]){if(typeof Module["postRun"]=="function")Module["postRun"]=[Module["postRun"]];while(Module["postRun"].length){addOnPostRun(Module["postRun"].shift())}}callRuntimeCallbacks(onPostRuns)}var runDependencies=0;var dependenciesFulfilled=null;function addRunDependency(id){runDependencies++;Module["monitorRunDependencies"]?.(runDependencies)}function removeRunDependency(id){runDependencies--;Module["monitorRunDependencies"]?.(runDependencies);if(runDependencies==0){if(dependenciesFulfilled){var callback=dependenciesFulfilled;dependenciesFulfilled=null;callback()}}}function abort(what){Module["onAbort"]?.(what);what="Aborted("+what+")";err(what);ABORT=true;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);readyPromiseReject?.(e);throw e}var wasmBinaryFile;function findWasmBinary(){if(Module["locateFile"]){return locateFile("index.wasm")}return new URL("index.wasm",import.meta.url).href}function getBinarySync(file){if(file==wasmBinaryFile&&wasmBinary){return new Uint8Array(wasmBinary)}if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}async function getWasmBinary(binaryFile){if(!wasmBinary){try{var response=await readAsync(binaryFile);return new Uint8Array(response)}catch{}}return getBinarySync(binaryFile)}async function instantiateArrayBuffer(binaryFile,imports){try{var binary=await getWasmBinary(binaryFile);var instance=await WebAssembly.instantiate(binary,imports);return instance}catch(reason){err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)}}async function instantiateAsync(binary,binaryFile,imports){if(!binary&&typeof WebAssembly.instantiateStreaming=="function"){try{var response=fetch(binaryFile,{credentials:"same-origin"});var instantiationResult=await WebAssembly.instantiateStreaming(response,imports);return instantiationResult}catch(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation")}}return instantiateArrayBuffer(binaryFile,imports)}function getWasmImports(){return{a:wasmImports}}async function createWasm(){function receiveInstance(instance,module){wasmExports=instance.exports;wasmExports=Asyncify.instrumentWasmExports(wasmExports);wasmExports=applySignatureConversions(wasmExports);wasmMemory=wasmExports["ra"];updateMemoryViews();wasmTable=wasmExports["Ba"];assignWasmExports(wasmExports);removeRunDependency("wasm-instantiate");return wasmExports}addRunDependency("wasm-instantiate");function receiveInstantiationResult(result){return receiveInstance(result["instance"])}var info=getWasmImports();if(Module["instantiateWasm"]){return new Promise((resolve,reject)=>{Module["instantiateWasm"](info,(mod,inst)=>{resolve(receiveInstance(mod,inst))})})}wasmBinaryFile??=findWasmBinary();var result=await instantiateAsync(wasmBinary,wasmBinaryFile,info);var exports=receiveInstantiationResult(result);return exports}class ExitStatus{name="ExitStatus";constructor(status){this.message=`Program terminated with exit(${status})`;this.status=status}}var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var onPostRuns=[];var addOnPostRun=cb=>onPostRuns.push(cb);var onPreRuns=[];var addOnPreRun=cb=>onPreRuns.push(cb);var dynCalls={};function getValue(ptr,type="i8"){if(type.endsWith("*"))type="*";switch(type){case"i1":return HEAP8[ptr>>>0];case"i8":return HEAP8[ptr>>>0];case"i16":return HEAP16[ptr>>>1>>>0];case"i32":return HEAP32[ptr>>>2>>>0];case"i64":return HEAP64[ptr>>>3>>>0];case"float":return HEAPF32[ptr>>>2>>>0];case"double":return HEAPF64[ptr>>>3>>>0];case"*":return HEAPU32[ptr>>>2>>>0];default:abort(`invalid type for getValue: ${type}`)}}var noExitRuntime=true;var ptrToString=ptr=>"0x"+ptr.toString(16).padStart(8,"0");function setValue(ptr,value,type="i8"){if(type.endsWith("*"))type="*";switch(type){case"i1":HEAP8[ptr>>>0]=value;break;case"i8":HEAP8[ptr>>>0]=value;break;case"i16":HEAP16[ptr>>>1>>>0]=value;break;case"i32":HEAP32[ptr>>>2>>>0]=value;break;case"i64":HEAP64[ptr>>>3>>>0]=BigInt(value);break;case"float":HEAPF32[ptr>>>2>>>0]=value;break;case"double":HEAPF64[ptr>>>3>>>0]=value;break;case"*":HEAPU32[ptr>>>2>>>0]=value;break;default:abort(`invalid type for setValue: ${type}`)}}var stackRestore=val=>__emscripten_stack_restore(val);var stackSave=()=>_emscripten_stack_get_current();var exceptionCaught=[];var uncaughtExceptionCount=0;var INT53_MAX=9007199254740992;var INT53_MIN=-9007199254740992;var bigintToI53Checked=num=>num<INT53_MIN||num>INT53_MAX?NaN:Number(num);function ___cxa_begin_catch(ptr){ptr>>>=0;var info=new ExceptionInfo(ptr);if(!info.get_caught()){info.set_caught(true);uncaughtExceptionCount--}info.set_rethrown(false);exceptionCaught.push(info);___cxa_increment_exception_refcount(ptr);return ___cxa_get_exception_ptr(ptr)}var exceptionLast=0;var ___cxa_end_catch=()=>{_setThrew(0,0);var info=exceptionCaught.pop();___cxa_decrement_exception_refcount(info.excPtr);exceptionLast=0};class ExceptionInfo{constructor(excPtr){this.excPtr=excPtr;this.ptr=excPtr-24}set_type(type){HEAPU32[this.ptr+4>>>2>>>0]=type}get_type(){return HEAPU32[this.ptr+4>>>2>>>0]}set_destructor(destructor){HEAPU32[this.ptr+8>>>2>>>0]=destructor}get_destructor(){return HEAPU32[this.ptr+8>>>2>>>0]}set_caught(caught){caught=caught?1:0;HEAP8[this.ptr+12>>>0]=caught}get_caught(){return HEAP8[this.ptr+12>>>0]!=0}set_rethrown(rethrown){rethrown=rethrown?1:0;HEAP8[this.ptr+13>>>0]=rethrown}get_rethrown(){return HEAP8[this.ptr+13>>>0]!=0}init(type,destructor){this.set_adjusted_ptr(0);this.set_type(type);this.set_destructor(destructor)}set_adjusted_ptr(adjustedPtr){HEAPU32[this.ptr+16>>>2>>>0]=adjustedPtr}get_adjusted_ptr(){return HEAPU32[this.ptr+16>>>2>>>0]}}var setTempRet0=val=>__emscripten_tempret_set(val);var findMatchingCatch=args=>{var thrown=exceptionLast;if(!thrown){setTempRet0(0);return 0}var info=new ExceptionInfo(thrown);info.set_adjusted_ptr(thrown);var thrownType=info.get_type();if(!thrownType){setTempRet0(0);return thrown}for(var caughtType of args){if(caughtType===0||caughtType===thrownType){break}var adjusted_ptr_addr=info.ptr+16;if(___cxa_can_catch(caughtType,thrownType,adjusted_ptr_addr)){setTempRet0(caughtType);return thrown}}setTempRet0(thrownType);return thrown};function ___cxa_find_matching_catch_2(){return findMatchingCatch([])}function ___cxa_find_matching_catch_3(arg0){arg0>>>=0;return findMatchingCatch([arg0])}var ___cxa_rethrow=()=>{var info=exceptionCaught.pop();if(!info){abort("no exception to throw")}var ptr=info.excPtr;if(!info.get_rethrown()){exceptionCaught.push(info);info.set_rethrown(true);info.set_caught(false);uncaughtExceptionCount++}exceptionLast=ptr;throw exceptionLast};function ___cxa_throw(ptr,type,destructor){ptr>>>=0;type>>>=0;destructor>>>=0;var info=new ExceptionInfo(ptr);info.init(type,destructor);exceptionLast=ptr;uncaughtExceptionCount++;throw exceptionLast}var ___cxa_uncaught_exceptions=()=>uncaughtExceptionCount;function ___resumeException(ptr){ptr>>>=0;if(!exceptionLast){exceptionLast=ptr}throw exceptionLast}var __abort_js=()=>abort("");var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{outIdx>>>=0;if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i<str.length;++i){var u=str.codePointAt(i);if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++>>>0]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++>>>0]=192|u>>6;heap[outIdx++>>>0]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++>>>0]=224|u>>12;heap[outIdx++>>>0]=128|u>>6&63;heap[outIdx++>>>0]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++>>>0]=240|u>>18;heap[outIdx++>>>0]=128|u>>12&63;heap[outIdx++>>>0]=128|u>>6&63;heap[outIdx++>>>0]=128|u&63;i++}}heap[outIdx>>>0]=0;return outIdx-startIdx};var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);var __tzset_js=function(timezone,daylight,std_name,dst_name){timezone>>>=0;daylight>>>=0;std_name>>>=0;dst_name>>>=0;var currentYear=(new Date).getFullYear();var winter=new Date(currentYear,0,1);var summer=new Date(currentYear,6,1);var winterOffset=winter.getTimezoneOffset();var summerOffset=summer.getTimezoneOffset();var stdTimezoneOffset=Math.max(winterOffset,summerOffset);HEAPU32[timezone>>>2>>>0]=stdTimezoneOffset*60;HEAP32[daylight>>>2>>>0]=Number(winterOffset!=summerOffset);var extractZone=timezoneOffset=>{var sign=timezoneOffset>=0?"-":"+";var absOffset=Math.abs(timezoneOffset);var hours=String(Math.floor(absOffset/60)).padStart(2,"0");var minutes=String(absOffset%60).padStart(2,"0");return`UTC${sign}${hours}${minutes}`};var winterName=extractZone(winterOffset);var summerName=extractZone(summerOffset);if(summerOffset<winterOffset){stringToUTF8(winterName,std_name,17);stringToUTF8(summerName,dst_name,17)}else{stringToUTF8(winterName,dst_name,17);stringToUTF8(summerName,std_name,17)}};var _emscripten_has_asyncify=()=>1;var getHeapMax=()=>4294901760;var alignMemory=(size,alignment)=>Math.ceil(size/alignment)*alignment;var growMemory=size=>{var b=wasmMemory.buffer;var pages=(size-b.byteLength+65535)/65536|0;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};function _emscripten_resize_heap(requestedSize){requestedSize>>>=0;var oldSize=HEAPU8.length;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignMemory(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}return false}var handleException=e=>{if(e instanceof ExitStatus||e=="unwind"){return EXITSTATUS}checkStackCookie();if(e instanceof WebAssembly.RuntimeError){if(_emscripten_stack_get_current()<=0){err("Stack overflow detected. You can try increasing -sSTACK_SIZE (currently set to 65536)")}}quit_(1,e)};var runtimeKeepaliveCounter=0;var keepRuntimeAlive=()=>noExitRuntime||runtimeKeepaliveCounter>0;var _proc_exit=code=>{EXITSTATUS=code;if(!keepRuntimeAlive()){Module["onExit"]?.(code);ABORT=true}quit_(code,new ExitStatus(code))};var exitJS=(status,implicit)=>{EXITSTATUS=status;_proc_exit(status)};var _exit=exitJS;var maybeExit=()=>{if(!keepRuntimeAlive()){try{_exit(EXITSTATUS)}catch(e){handleException(e)}}};var callUserCallback=func=>{if(ABORT){return}try{func();maybeExit()}catch(e){handleException(e)}};var safeSetTimeout=(func,timeout)=>setTimeout(()=>{callUserCallback(func)},timeout);var _emscripten_sleep=ms=>Asyncify.handleSleep(wakeUp=>safeSetTimeout(wakeUp,ms));_emscripten_sleep.isAsync=true;var lengthBytesUTF8=str=>{var len=0;for(var i=0;i<str.length;++i){var c=str.charCodeAt(i);if(c<=127){len++}else if(c<=2047){len+=2}else if(c>=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var stackAlloc=sz=>__emscripten_stack_alloc(sz);var stringToUTF8OnStack=str=>{var size=lengthBytesUTF8(str)+1;var ret=stackAlloc(size);stringToUTF8(str,ret,size);return ret};var UTF8Decoder=typeof TextDecoder!="undefined"?new TextDecoder:undefined;var UTF8ArrayToString=(heapOrArray,idx=0,maxBytesToRead=NaN)=>{idx>>>=0;var endIdx=idx+maxBytesToRead;var endPtr=idx;while(heapOrArray[endPtr]&&!(endPtr>=endIdx))++endPtr;if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx<endPtr){var u0=heapOrArray[idx++];if(!(u0&128)){str+=String.fromCharCode(u0);continue}var u1=heapOrArray[idx++]&63;if((u0&224)==192){str+=String.fromCharCode((u0&31)<<6|u1);continue}var u2=heapOrArray[idx++]&63;if((u0&240)==224){u0=(u0&15)<<12|u1<<6|u2}else{u0=(u0&7)<<18|u1<<12|u2<<6|heapOrArray[idx++]&63}if(u0<65536){str+=String.fromCharCode(u0)}else{var ch=u0-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}}return str};var UTF8ToString=(ptr,maxBytesToRead)=>{ptr>>>=0;return ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead):""};var stringToNewUTF8=str=>{var size=lengthBytesUTF8(str)+1;var ret=_malloc(size);if(ret)stringToUTF8(str,ret,size);return ret};var WebGPU={Internals:{jsObjects:[],jsObjectInsert:(ptr,jsObject)=>{WebGPU.Internals.jsObjects[ptr]=jsObject},bufferOnUnmaps:[],futures:[],futureInsert:(futureId,promise)=>{WebGPU.Internals.futures[futureId]=new Promise(resolve=>promise.finally(()=>resolve(futureId)))}},getJsObject:ptr=>{if(!ptr)return undefined;return WebGPU.Internals.jsObjects[ptr]},importJsAdapter:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateAdapter(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsBindGroup:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateBindGroup(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsBindGroupLayout:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateBindGroupLayout(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsBuffer:(buffer,parentPtr=0)=>{assert(buffer.mapState!="pending");var mapState=buffer.mapState=="mapped"?3:1;var bufferPtr=_emwgpuCreateBuffer(parentPtr,mapState);WebGPU.Internals.jsObjectInsert(bufferPtr,buffer);if(buffer.mapState=="mapped"){WebGPU.Internals.bufferOnUnmaps[bufferPtr]=[]}return bufferPtr},importJsCommandBuffer:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateCommandBuffer(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsCommandEncoder:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateCommandEncoder(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsComputePassEncoder:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateComputePassEncoder(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsComputePipeline:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateComputePipeline(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsDevice:(device,parentPtr=0)=>{var queuePtr=_emwgpuCreateQueue(parentPtr);var devicePtr=_emwgpuCreateDevice(parentPtr,queuePtr);WebGPU.Internals.jsObjectInsert(queuePtr,device.queue);WebGPU.Internals.jsObjectInsert(devicePtr,device);return devicePtr},importJsPipelineLayout:(obj,parentPtr=0)=>{var ptr=_emwgpuCreatePipelineLayout(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsQuerySet:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateQuerySet(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsQueue:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateQueue(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsRenderBundle:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateRenderBundle(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsRenderBundleEncoder:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateRenderBundleEncoder(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsRenderPassEncoder:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateRenderPassEncoder(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsRenderPipeline:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateRenderPipeline(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsSampler:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateSampler(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsShaderModule:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateShaderModule(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsSurface:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateSurface(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsTexture:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateTexture(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},importJsTextureView:(obj,parentPtr=0)=>{var ptr=_emwgpuCreateTextureView(parentPtr);WebGPU.Internals.jsObjects[ptr]=obj;return ptr},errorCallback:(callback,type,message,userdata)=>{var sp=stackSave();var messagePtr=stringToUTF8OnStack(message);((a1,a2,a3)=>dynCall_viii(callback,a1,a2,a3))(type,messagePtr,userdata);stackRestore(sp)},setStringView:(ptr,data,length)=>{HEAPU32[ptr>>>2>>>0]=data;HEAPU32[ptr+4>>>2>>>0]=length},makeStringFromStringView:stringViewPtr=>{var ptr=HEAPU32[stringViewPtr>>>2>>>0];var length=HEAPU32[stringViewPtr+4>>>2>>>0];return UTF8ToString(ptr,length)},makeStringFromOptionalStringView:stringViewPtr=>{var ptr=HEAPU32[stringViewPtr>>>2>>>0];var length=HEAPU32[stringViewPtr+4>>>2>>>0];if(!ptr){if(length===0){return""}return undefined}return UTF8ToString(ptr,length)},makeColor:ptr=>({r:HEAPF64[ptr>>>3>>>0],g:HEAPF64[ptr+8>>>3>>>0],b:HEAPF64[ptr+16>>>3>>>0],a:HEAPF64[ptr+24>>>3>>>0]}),makeExtent3D:ptr=>({width:HEAPU32[ptr>>>2>>>0],height:HEAPU32[ptr+4>>>2>>>0],depthOrArrayLayers:HEAPU32[ptr+8>>>2>>>0]}),makeOrigin3D:ptr=>({x:HEAPU32[ptr>>>2>>>0],y:HEAPU32[ptr+4>>>2>>>0],z:HEAPU32[ptr+8>>>2>>>0]}),makeTexelCopyTextureInfo:ptr=>({texture:WebGPU.getJsObject(HEAPU32[ptr>>>2>>>0]),mipLevel:HEAPU32[ptr+4>>>2>>>0],origin:WebGPU.makeOrigin3D(ptr+8),aspect:WebGPU.TextureAspect[HEAPU32[ptr+20>>>2>>>0]]}),makeTexelCopyBufferLayout:ptr=>{var bytesPerRow=HEAPU32[ptr+8>>>2>>>0];var rowsPerImage=HEAPU32[ptr+12>>>2>>>0];return{offset:HEAPU32[ptr+4>>>2>>>0]*4294967296+HEAPU32[ptr>>>2>>>0],bytesPerRow:bytesPerRow===4294967295?undefined:bytesPerRow,rowsPerImage:rowsPerImage===4294967295?undefined:rowsPerImage}},makeTexelCopyBufferInfo:ptr=>{var layoutPtr=ptr+0;var bufferCopyView=WebGPU.makeTexelCopyBufferLayout(layoutPtr);bufferCopyView["buffer"]=WebGPU.getJsObject(HEAPU32[ptr+16>>>2>>>0]);return bufferCopyView},makePassTimestampWrites:ptr=>{if(ptr===0)return undefined;return{querySet:WebGPU.getJsObject(HEAPU32[ptr+4>>>2>>>0]),beginningOfPassWriteIndex:HEAPU32[ptr+8>>>2>>>0],endOfPassWriteIndex:HEAPU32[ptr+12>>>2>>>0]}},makePipelineConstants:(constantCount,constantsPtr)=>{if(!constantCount)return;var constants={};for(var i=0;i<constantCount;++i){var entryPtr=constantsPtr+24*i;var key=WebGPU.makeStringFromStringView(entryPtr+4);constants[key]=HEAPF64[entryPtr+16>>>3>>>0]}return constants},makePipelineLayout:layoutPtr=>{if(!layoutPtr)return"auto";return WebGPU.getJsObject(layoutPtr)},makeComputeState:ptr=>{if(!ptr)return undefined;var desc={module:WebGPU.getJsObject(HEAPU32[ptr+4>>>2>>>0]),constants:WebGPU.makePipelineConstants(HEAPU32[ptr+16>>>2>>>0],HEAPU32[ptr+20>>>2>>>0]),entryPoint:WebGPU.makeStringFromOptionalStringView(ptr+8)};return desc},makeComputePipelineDesc:descriptor=>{var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),layout:WebGPU.makePipelineLayout(HEAPU32[descriptor+12>>>2>>>0]),compute:WebGPU.makeComputeState(descriptor+16)};return desc},makeRenderPipelineDesc:descriptor=>{function makePrimitiveState(psPtr){if(!psPtr)return undefined;return{topology:WebGPU.PrimitiveTopology[HEAPU32[psPtr+4>>>2>>>0]],stripIndexFormat:WebGPU.IndexFormat[HEAPU32[psPtr+8>>>2>>>0]],frontFace:WebGPU.FrontFace[HEAPU32[psPtr+12>>>2>>>0]],cullMode:WebGPU.CullMode[HEAPU32[psPtr+16>>>2>>>0]],unclippedDepth:!!HEAPU32[psPtr+20>>>2>>>0]}}function makeBlendComponent(bdPtr){if(!bdPtr)return undefined;return{operation:WebGPU.BlendOperation[HEAPU32[bdPtr>>>2>>>0]],srcFactor:WebGPU.BlendFactor[HEAPU32[bdPtr+4>>>2>>>0]],dstFactor:WebGPU.BlendFactor[HEAPU32[bdPtr+8>>>2>>>0]]}}function makeBlendState(bsPtr){if(!bsPtr)return undefined;return{alpha:makeBlendComponent(bsPtr+12),color:makeBlendComponent(bsPtr+0)}}function makeColorState(csPtr){var formatInt=HEAPU32[csPtr+4>>>2>>>0];return formatInt===0?undefined:{format:WebGPU.TextureFormat[formatInt],blend:makeBlendState(HEAPU32[csPtr+8>>>2>>>0]),writeMask:HEAPU32[csPtr+16>>>2>>>0]}}function makeColorStates(count,csArrayPtr){var states=[];for(var i=0;i<count;++i){states.push(makeColorState(csArrayPtr+24*i))}return states}function makeStencilStateFace(ssfPtr){return{compare:WebGPU.CompareFunction[HEAPU32[ssfPtr>>>2>>>0]],failOp:WebGPU.StencilOperation[HEAPU32[ssfPtr+4>>>2>>>0]],depthFailOp:WebGPU.StencilOperation[HEAPU32[ssfPtr+8>>>2>>>0]],passOp:WebGPU.StencilOperation[HEAPU32[ssfPtr+12>>>2>>>0]]}}function makeDepthStencilState(dssPtr){if(!dssPtr)return undefined;return{format:WebGPU.TextureFormat[HEAPU32[dssPtr+4>>>2>>>0]],depthWriteEnabled:!!HEAPU32[dssPtr+8>>>2>>>0],depthCompare:WebGPU.CompareFunction[HEAPU32[dssPtr+12>>>2>>>0]],stencilFront:makeStencilStateFace(dssPtr+16),stencilBack:makeStencilStateFace(dssPtr+32),stencilReadMask:HEAPU32[dssPtr+48>>>2>>>0],stencilWriteMask:HEAPU32[dssPtr+52>>>2>>>0],depthBias:HEAP32[dssPtr+56>>>2>>>0],depthBiasSlopeScale:HEAPF32[dssPtr+60>>>2>>>0],depthBiasClamp:HEAPF32[dssPtr+64>>>2>>>0]}}function makeVertexAttribute(vaPtr){return{format:WebGPU.VertexFormat[HEAPU32[vaPtr+4>>>2>>>0]],offset:HEAPU32[vaPtr+4+8>>>2>>>0]*4294967296+HEAPU32[vaPtr+8>>>2>>>0],shaderLocation:HEAPU32[vaPtr+16>>>2>>>0]}}function makeVertexAttributes(count,vaArrayPtr){var vas=[];for(var i=0;i<count;++i){vas.push(makeVertexAttribute(vaArrayPtr+i*24))}return vas}function makeVertexBuffer(vbPtr){if(!vbPtr)return undefined;var stepModeInt=HEAPU32[vbPtr+4>>>2>>>0];var attributeCountInt=HEAPU32[vbPtr+16>>>2>>>0];if(stepModeInt===0&&attributeCountInt===0){return null}return{arrayStride:HEAPU32[vbPtr+4+8>>>2>>>0]*4294967296+HEAPU32[vbPtr+8>>>2>>>0],stepMode:WebGPU.VertexStepMode[stepModeInt],attributes:makeVertexAttributes(attributeCountInt,HEAPU32[vbPtr+20>>>2>>>0])}}function makeVertexBuffers(count,vbArrayPtr){if(!count)return undefined;var vbs=[];for(var i=0;i<count;++i){vbs.push(makeVertexBuffer(vbArrayPtr+i*24))}return vbs}function makeVertexState(viPtr){if(!viPtr)return undefined;var desc={module:WebGPU.getJsObject(HEAPU32[viPtr+4>>>2>>>0]),constants:WebGPU.makePipelineConstants(HEAPU32[viPtr+16>>>2>>>0],HEAPU32[viPtr+20>>>2>>>0]),buffers:makeVertexBuffers(HEAPU32[viPtr+24>>>2>>>0],HEAPU32[viPtr+28>>>2>>>0]),entryPoint:WebGPU.makeStringFromOptionalStringView(viPtr+8)};return desc}function makeMultisampleState(msPtr){if(!msPtr)return undefined;return{count:HEAPU32[msPtr+4>>>2>>>0],mask:HEAPU32[msPtr+8>>>2>>>0],alphaToCoverageEnabled:!!HEAPU32[msPtr+12>>>2>>>0]}}function makeFragmentState(fsPtr){if(!fsPtr)return undefined;var desc={module:WebGPU.getJsObject(HEAPU32[fsPtr+4>>>2>>>0]),constants:WebGPU.makePipelineConstants(HEAPU32[fsPtr+16>>>2>>>0],HEAPU32[fsPtr+20>>>2>>>0]),targets:makeColorStates(HEAPU32[fsPtr+24>>>2>>>0],HEAPU32[fsPtr+28>>>2>>>0]),entryPoint:WebGPU.makeStringFromOptionalStringView(fsPtr+8)};return desc}var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),layout:WebGPU.makePipelineLayout(HEAPU32[descriptor+12>>>2>>>0]),vertex:makeVertexState(descriptor+16),primitive:makePrimitiveState(descriptor+48),depthStencil:makeDepthStencilState(HEAPU32[descriptor+72>>>2>>>0]),multisample:makeMultisampleState(descriptor+76),fragment:makeFragmentState(HEAPU32[descriptor+92>>>2>>>0])};return desc},fillLimitStruct:(limits,limitsOutPtr)=>{function setLimitValueU32(name,limitOffset){var limitValue=limits[name];HEAP32[limitsOutPtr+limitOffset>>>2>>>0]=limitValue}function setLimitValueU64(name,limitOffset){var limitValue=limits[name];HEAP64[limitsOutPtr+limitOffset>>>3>>>0]=BigInt(limitValue)}setLimitValueU32("maxTextureDimension1D",4);setLimitValueU32("maxTextureDimension2D",8);setLimitValueU32("maxTextureDimension3D",12);setLimitValueU32("maxTextureArrayLayers",16);setLimitValueU32("maxBindGroups",20);setLimitValueU32("maxBindGroupsPlusVertexBuffers",24);setLimitValueU32("maxBindingsPerBindGroup",28);setLimitValueU32("maxDynamicUniformBuffersPerPipelineLayout",32);setLimitValueU32("maxDynamicStorageBuffersPerPipelineLayout",36);setLimitValueU32("maxSampledTexturesPerShaderStage",40);setLimitValueU32("maxSamplersPerShaderStage",44);setLimitValueU32("maxStorageBuffersPerShaderStage",48);setLimitValueU32("maxStorageTexturesPerShaderStage",52);setLimitValueU32("maxUniformBuffersPerShaderStage",56);setLimitValueU32("minUniformBufferOffsetAlignment",80);setLimitValueU32("minStorageBufferOffsetAlignment",84);setLimitValueU64("maxUniformBufferBindingSize",64);setLimitValueU64("maxStorageBufferBindingSize",72);setLimitValueU32("maxVertexBuffers",88);setLimitValueU64("maxBufferSize",96);setLimitValueU32("maxVertexAttributes",104);setLimitValueU32("maxVertexBufferArrayStride",108);setLimitValueU32("maxInterStageShaderVariables",112);setLimitValueU32("maxColorAttachments",116);setLimitValueU32("maxColorAttachmentBytesPerSample",120);setLimitValueU32("maxComputeWorkgroupStorageSize",124);setLimitValueU32("maxComputeInvocationsPerWorkgroup",128);setLimitValueU32("maxComputeWorkgroupSizeX",132);setLimitValueU32("maxComputeWorkgroupSizeY",136);setLimitValueU32("maxComputeWorkgroupSizeZ",140);setLimitValueU32("maxComputeWorkgroupsPerDimension",144);if(limits.maxImmediateSize!==undefined){setLimitValueU32("maxImmediateSize",148)}},fillAdapterInfoStruct:(info,infoStruct)=>{HEAP32[infoStruct+52>>>2>>>0]=info.subgroupMinSize;HEAP32[infoStruct+56>>>2>>>0]=info.subgroupMaxSize;var strs=info.vendor+info.architecture+info.device+info.description;var strPtr=stringToNewUTF8(strs);var vendorLen=lengthBytesUTF8(info.vendor);WebGPU.setStringView(infoStruct+4,strPtr,vendorLen);strPtr+=vendorLen;var architectureLen=lengthBytesUTF8(info.architecture);WebGPU.setStringView(infoStruct+12,strPtr,architectureLen);strPtr+=architectureLen;var deviceLen=lengthBytesUTF8(info.device);WebGPU.setStringView(infoStruct+20,strPtr,deviceLen);strPtr+=deviceLen;var descriptionLen=lengthBytesUTF8(info.description);WebGPU.setStringView(infoStruct+28,strPtr,descriptionLen);strPtr+=descriptionLen;HEAP32[infoStruct+36>>>2>>>0]=2;var adapterType=info.isFallbackAdapter?3:4;HEAP32[infoStruct+40>>>2>>>0]=adapterType;HEAP32[infoStruct+44>>>2>>>0]=0;HEAP32[infoStruct+48>>>2>>>0]=0},Int_BufferMapState:{unmapped:1,pending:2,mapped:3},Int_CompilationMessageType:{error:1,warning:2,info:3},Int_DeviceLostReason:{undefined:1,unknown:1,destroyed:2},Int_PreferredFormat:{rgba8unorm:18,bgra8unorm:23},AddressMode:[,"clamp-to-edge","repeat","mirror-repeat"],BlendFactor:[,"zero","one","src","one-minus-src","src-alpha","one-minus-src-alpha","dst","one-minus-dst","dst-alpha","one-minus-dst-alpha","src-alpha-saturated","constant","one-minus-constant","src1","one-minus-src1","src1alpha","one-minus-src1alpha"],BlendOperation:[,"add","subtract","reverse-subtract","min","max"],BufferBindingType:["binding-not-used",,"uniform","storage","read-only-storage"],BufferMapState:{1:"unmapped",2:"pending",3:"mapped"},CompareFunction:[,"never","less","equal","less-equal","greater","not-equal","greater-equal","always"],CompilationInfoRequestStatus:{1:"success",2:"callback-cancelled"},CompositeAlphaMode:[,"opaque","premultiplied","unpremultiplied","inherit"],CullMode:[,"none","front","back"],ErrorFilter:{1:"validation",2:"out-of-memory",3:"internal"},FeatureLevel:[,"compatibility","core"],FeatureName:{1:"depth-clip-control",2:"depth32float-stencil8",3:"timestamp-query",4:"texture-compression-bc",5:"texture-compression-bc-sliced-3d",6:"texture-compression-etc2",7:"texture-compression-astc",8:"texture-compression-astc-sliced-3d",9:"indirect-first-instance",10:"shader-f16",11:"rg11b10ufloat-renderable",12:"bgra8unorm-storage",13:"float32-filterable",14:"float32-blendable",15:"clip-distances",16:"dual-source-blending",17:"subgroups",18:"core-features-and-limits",327692:"chromium-experimental-unorm16-texture-formats",327693:"chromium-experimental-snorm16-texture-formats",327732:"chromium-experimental-multi-draw-indirect"},FilterMode:[,"nearest","linear"],FrontFace:[,"ccw","cw"],IndexFormat:[,"uint16","uint32"],LoadOp:[,"load","clear"],MipmapFilterMode:[,"nearest","linear"],OptionalBool:["false","true"],PowerPreference:[,"low-power","high-performance"],PredefinedColorSpace:{1:"srgb",2:"display-p3"},PrimitiveTopology:[,"point-list","line-list","line-strip","triangle-list","triangle-strip"],QueryType:{1:"occlusion",2:"timestamp"},SamplerBindingType:["binding-not-used",,"filtering","non-filtering","comparison"],Status:{1:"success",2:"error"},StencilOperation:[,"keep","zero","replace","invert","increment-clamp","decrement-clamp","increment-wrap","decrement-wrap"],StorageTextureAccess:["binding-not-used",,"write-only","read-only","read-write"],StoreOp:[,"store","discard"],SurfaceGetCurrentTextureStatus:{1:"success-optimal",2:"success-suboptimal",3:"timeout",4:"outdated",5:"lost",6:"error"},TextureAspect:[,"all","stencil-only","depth-only"],TextureDimension:[,"1d","2d","3d"],TextureFormat:[,"r8unorm","r8snorm","r8uint","r8sint","r16uint","r16sint","r16float","rg8unorm","rg8snorm","rg8uint","rg8sint","r32float","r32uint","r32sint","rg16uint","rg16sint","rg16float","rgba8unorm","rgba8unorm-srgb","rgba8snorm","rgba8uint","rgba8sint","bgra8unorm","bgra8unorm-srgb","rgb10a2uint","rgb10a2unorm","rg11b10ufloat","rgb9e5ufloat","rg32float","rg32uint","rg32sint","rgba16uint","rgba16sint","rgba16float","rgba32float","rgba32uint","rgba32sint","stencil8","depth16unorm","depth24plus","depth24plus-stencil8","depth32float","depth32float-stencil8","bc1-rgba-unorm","bc1-rgba-unorm-srgb","bc2-rgba-unorm","bc2-rgba-unorm-srgb","bc3-rgba-unorm","bc3-rgba-unorm-srgb","bc4-r-unorm","bc4-r-snorm","bc5-rg-unorm","bc5-rg-snorm","bc6h-rgb-ufloat","bc6h-rgb-float","bc7-rgba-unorm","bc7-rgba-unorm-srgb","etc2-rgb8unorm","etc2-rgb8unorm-srgb","etc2-rgb8a1unorm","etc2-rgb8a1unorm-srgb","etc2-rgba8unorm","etc2-rgba8unorm-srgb","eac-r11unorm","eac-r11snorm","eac-rg11unorm","eac-rg11snorm","astc-4x4-unorm","astc-4x4-unorm-srgb","astc-5x4-unorm","astc-5x4-unorm-srgb","astc-5x5-unorm","astc-5x5-unorm-srgb","astc-6x5-unorm","astc-6x5-unorm-srgb","astc-6x6-unorm","astc-6x6-unorm-srgb","astc-8x5-unorm","astc-8x5-unorm-srgb","astc-8x6-unorm","astc-8x6-unorm-srgb","astc-8x8-unorm","astc-8x8-unorm-srgb","astc-10x5-unorm","astc-10x5-unorm-srgb","astc-10x6-unorm","astc-10x6-unorm-srgb","astc-10x8-unorm","astc-10x8-unorm-srgb","astc-10x10-unorm","astc-10x10-unorm-srgb","astc-12x10-unorm","astc-12x10-unorm-srgb","astc-12x12-unorm","astc-12x12-unorm-srgb"],TextureSampleType:["binding-not-used",,"float","unfilterable-float","depth","sint","uint"],TextureViewDimension:[,"1d","2d","2d-array","cube","cube-array","3d"],ToneMappingMode:{1:"standard",2:"extended"},VertexFormat:{1:"uint8",2:"uint8x2",3:"uint8x4",4:"sint8",5:"sint8x2",6:"sint8x4",7:"unorm8",8:"unorm8x2",9:"unorm8x4",10:"snorm8",11:"snorm8x2",12:"snorm8x4",13:"uint16",14:"uint16x2",15:"uint16x4",16:"sint16",17:"sint16x2",18:"sint16x4",19:"unorm16",20:"unorm16x2",21:"unorm16x4",22:"snorm16",23:"snorm16x2",24:"snorm16x4",25:"float16",26:"float16x2",27:"float16x4",28:"float32",29:"float32x2",30:"float32x3",31:"float32x4",32:"uint32",33:"uint32x2",34:"uint32x3",35:"uint32x4",36:"sint32",37:"sint32x2",38:"sint32x3",39:"sint32x4",40:"unorm10-10-10-2",41:"unorm8x4-bgra"},VertexStepMode:[,"vertex","instance"],WGSLLanguageFeatureName:{1:"readonly_and_readwrite_storage_textures",2:"packed_4x8_integer_dot_product",3:"unrestricted_pointer_parameters",4:"pointer_composite_access",5:"sized_binding_array"},FeatureNameString2Enum:{"depth-clip-control":"1","depth32float-stencil8":"2","timestamp-query":"3","texture-compression-bc":"4","texture-compression-bc-sliced-3d":"5","texture-compression-etc2":"6","texture-compression-astc":"7","texture-compression-astc-sliced-3d":"8","indirect-first-instance":"9","shader-f16":"10","rg11b10ufloat-renderable":"11","bgra8unorm-storage":"12","float32-filterable":"13","float32-blendable":"14","clip-distances":"15","dual-source-blending":"16",subgroups:"17","core-features-and-limits":"18","chromium-experimental-unorm16-texture-formats":"327692","chromium-experimental-snorm16-texture-formats":"327693","chromium-experimental-multi-draw-indirect":"327732"},WGSLLanguageFeatureNameString2Enum:{readonly_and_readwrite_storage_textures:"1",packed_4x8_integer_dot_product:"2",unrestricted_pointer_parameters:"3",pointer_composite_access:"4",sized_binding_array:"5"}};function _emwgpuAdapterRequestDevice(adapterPtr,futureId,deviceLostFutureId,devicePtr,queuePtr,descriptor){adapterPtr>>>=0;futureId=bigintToI53Checked(futureId);deviceLostFutureId=bigintToI53Checked(deviceLostFutureId);devicePtr>>>=0;queuePtr>>>=0;descriptor>>>=0;var adapter=WebGPU.getJsObject(adapterPtr);var desc={};if(descriptor){var requiredFeatureCount=HEAPU32[descriptor+12>>>2>>>0];if(requiredFeatureCount){var requiredFeaturesPtr=HEAPU32[descriptor+16>>>2>>>0];desc["requiredFeatures"]=Array.from(HEAPU32.subarray(requiredFeaturesPtr>>>2>>>0,requiredFeaturesPtr+requiredFeatureCount*4>>>2>>>0),feature=>WebGPU.FeatureName[feature])}var limitsPtr=HEAPU32[descriptor+20>>>2>>>0];if(limitsPtr){var requiredLimits={};function setLimitU32IfDefined(name,limitOffset,ignoreIfZero=false){var ptr=limitsPtr+limitOffset;var value=HEAPU32[ptr>>>2>>>0];if(value!=4294967295&&(!ignoreIfZero||value!=0)){requiredLimits[name]=value}}function setLimitU64IfDefined(name,limitOffset){var ptr=limitsPtr+limitOffset;var limitPart1=HEAPU32[ptr>>>2>>>0];var limitPart2=HEAPU32[ptr+4>>>2>>>0];if(limitPart1!=4294967295||limitPart2!=4294967295){requiredLimits[name]=HEAPU32[ptr+4>>>2>>>0]*4294967296+HEAPU32[ptr>>>2>>>0]}}setLimitU32IfDefined("maxTextureDimension1D",4);setLimitU32IfDefined("maxTextureDimension2D",8);setLimitU32IfDefined("maxTextureDimension3D",12);setLimitU32IfDefined("maxTextureArrayLayers",16);setLimitU32IfDefined("maxBindGroups",20);setLimitU32IfDefined("maxBindGroupsPlusVertexBuffers",24);setLimitU32IfDefined("maxDynamicUniformBuffersPerPipelineLayout",32);setLimitU32IfDefined("maxDynamicStorageBuffersPerPipelineLayout",36);setLimitU32IfDefined("maxSampledTexturesPerShaderStage",40);setLimitU32IfDefined("maxSamplersPerShaderStage",44);setLimitU32IfDefined("maxStorageBuffersPerShaderStage",48);setLimitU32IfDefined("maxStorageTexturesPerShaderStage",52);setLimitU32IfDefined("maxUniformBuffersPerShaderStage",56);setLimitU32IfDefined("minUniformBufferOffsetAlignment",80);setLimitU32IfDefined("minStorageBufferOffsetAlignment",84);setLimitU64IfDefined("maxUniformBufferBindingSize",64);setLimitU64IfDefined("maxStorageBufferBindingSize",72);setLimitU32IfDefined("maxVertexBuffers",88);setLimitU64IfDefined("maxBufferSize",96);setLimitU32IfDefined("maxVertexAttributes",104);setLimitU32IfDefined("maxVertexBufferArrayStride",108);setLimitU32IfDefined("maxInterStageShaderVariables",112);setLimitU32IfDefined("maxColorAttachments",116);setLimitU32IfDefined("maxColorAttachmentBytesPerSample",120);setLimitU32IfDefined("maxComputeWorkgroupStorageSize",124);setLimitU32IfDefined("maxComputeInvocationsPerWorkgroup",128);setLimitU32IfDefined("maxComputeWorkgroupSizeX",132);setLimitU32IfDefined("maxComputeWorkgroupSizeY",136);setLimitU32IfDefined("maxComputeWorkgroupSizeZ",140);setLimitU32IfDefined("maxComputeWorkgroupsPerDimension",144);setLimitU32IfDefined("maxImmediateSize",148,true);desc["requiredLimits"]=requiredLimits}var defaultQueuePtr=HEAPU32[descriptor+24>>>2>>>0];if(defaultQueuePtr){var defaultQueueDesc={label:WebGPU.makeStringFromOptionalStringView(defaultQueuePtr+4)};desc["defaultQueue"]=defaultQueueDesc}desc["label"]=WebGPU.makeStringFromOptionalStringView(descriptor+4)}WebGPU.Internals.futureInsert(futureId,adapter.requestDevice(desc).then(device=>{WebGPU.Internals.jsObjectInsert(queuePtr,device.queue);WebGPU.Internals.jsObjectInsert(devicePtr,device);if(deviceLostFutureId){WebGPU.Internals.futureInsert(deviceLostFutureId,device.lost.then(info=>{device.onuncapturederror=ev=>{};var sp=stackSave();var messagePtr=stringToUTF8OnStack(info.message);_emwgpuOnDeviceLostCompleted(deviceLostFutureId,WebGPU.Int_DeviceLostReason[info.reason],messagePtr);stackRestore(sp)}))}device.onuncapturederror=ev=>{var type=5;if(ev.error instanceof GPUValidationError)type=2;else if(ev.error instanceof GPUOutOfMemoryError)type=3;else if(ev.error instanceof GPUInternalError)type=4;var sp=stackSave();var messagePtr=stringToUTF8OnStack(ev.error.message);_emwgpuOnUncapturedError(devicePtr,type,messagePtr);stackRestore(sp)};_emwgpuOnRequestDeviceCompleted(futureId,1,devicePtr,0)},ex=>{var sp=stackSave();var messagePtr=stringToUTF8OnStack(ex.message);_emwgpuOnRequestDeviceCompleted(futureId,3,devicePtr,messagePtr);if(deviceLostFutureId){_emwgpuOnDeviceLostCompleted(deviceLostFutureId,4,messagePtr)}stackRestore(sp)}))}function _emwgpuBufferDestroy(bufferPtr){bufferPtr>>>=0;var buffer=WebGPU.getJsObject(bufferPtr);var onUnmap=WebGPU.Internals.bufferOnUnmaps[bufferPtr];if(onUnmap){for(var i=0;i<onUnmap.length;++i){onUnmap[i]()}delete WebGPU.Internals.bufferOnUnmaps[bufferPtr]}buffer.destroy()}var warnOnce=text=>{warnOnce.shown||={};if(!warnOnce.shown[text]){warnOnce.shown[text]=1;err(text)}};function _emwgpuBufferGetConstMappedRange(bufferPtr,offset,size){bufferPtr>>>=0;offset>>>=0;size>>>=0;var buffer=WebGPU.getJsObject(bufferPtr);if(size===0)warnOnce("getMappedRange size=0 no longer means WGPU_WHOLE_MAP_SIZE");if(size==4294967295)size=undefined;var mapped;try{mapped=buffer.getMappedRange(offset,size)}catch(ex){return 0}var data=_memalign(16,mapped.byteLength);HEAPU8.set(new Uint8Array(mapped),data>>>0);WebGPU.Internals.bufferOnUnmaps[bufferPtr].push(()=>_free(data));return data}var _emwgpuBufferMapAsync=function(bufferPtr,futureId,mode,offset,size){bufferPtr>>>=0;futureId=bigintToI53Checked(futureId);mode=bigintToI53Checked(mode);offset>>>=0;size>>>=0;var buffer=WebGPU.getJsObject(bufferPtr);WebGPU.Internals.bufferOnUnmaps[bufferPtr]=[];if(size==4294967295)size=undefined;WebGPU.Internals.futureInsert(futureId,buffer.mapAsync(mode,offset,size).then(()=>{_emwgpuOnMapAsyncCompleted(futureId,1,0)},ex=>{var sp=stackSave();var messagePtr=stringToUTF8OnStack(ex.message);var status=ex.name==="AbortError"?4:ex.name==="OperationError"?3:0;_emwgpuOnMapAsyncCompleted(futureId,status,messagePtr);delete WebGPU.Internals.bufferOnUnmaps[bufferPtr]}))};function _emwgpuBufferUnmap(bufferPtr){bufferPtr>>>=0;var buffer=WebGPU.getJsObject(bufferPtr);var onUnmap=WebGPU.Internals.bufferOnUnmaps[bufferPtr];if(!onUnmap){return}for(var i=0;i<onUnmap.length;++i){onUnmap[i]()}delete WebGPU.Internals.bufferOnUnmaps[bufferPtr];buffer.unmap()}function _emwgpuDelete(ptr){ptr>>>=0;delete WebGPU.Internals.jsObjects[ptr]}function _emwgpuDeviceCreateBuffer(devicePtr,descriptor,bufferPtr){devicePtr>>>=0;descriptor>>>=0;bufferPtr>>>=0;var mappedAtCreation=!!HEAPU32[descriptor+32>>>2>>>0];var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),usage:HEAPU32[descriptor+16>>>2>>>0],size:HEAPU32[descriptor+4+24>>>2>>>0]*4294967296+HEAPU32[descriptor+24>>>2>>>0],mappedAtCreation};var device=WebGPU.getJsObject(devicePtr);var buffer;try{buffer=device.createBuffer(desc)}catch(ex){return false}WebGPU.Internals.jsObjectInsert(bufferPtr,buffer);if(mappedAtCreation){WebGPU.Internals.bufferOnUnmaps[bufferPtr]=[]}return true}function _emwgpuDeviceCreateShaderModule(devicePtr,descriptor,shaderModulePtr){devicePtr>>>=0;descriptor>>>=0;shaderModulePtr>>>=0;var nextInChainPtr=HEAPU32[descriptor>>>2>>>0];var sType=HEAPU32[nextInChainPtr+4>>>2>>>0];var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),code:""};switch(sType){case 2:{desc["code"]=WebGPU.makeStringFromStringView(nextInChainPtr+8);break}}var device=WebGPU.getJsObject(devicePtr);WebGPU.Internals.jsObjectInsert(shaderModulePtr,device.createShaderModule(desc))}var _emwgpuDeviceDestroy=devicePtr=>{WebGPU.getJsObject(devicePtr).destroy()};function _emwgpuInstanceRequestAdapter(instancePtr,futureId,options,adapterPtr){instancePtr>>>=0;futureId=bigintToI53Checked(futureId);options>>>=0;adapterPtr>>>=0;var opts;if(options){var featureLevel=HEAPU32[options+4>>>2>>>0];opts={featureLevel:WebGPU.FeatureLevel[featureLevel],powerPreference:WebGPU.PowerPreference[HEAPU32[options+8>>>2>>>0]],forceFallbackAdapter:!!HEAPU32[options+12>>>2>>>0]};var nextInChainPtr=HEAPU32[options>>>2>>>0];if(nextInChainPtr!==0){var sType=HEAPU32[nextInChainPtr+4>>>2>>>0];var webxrOptions=nextInChainPtr;opts.xrCompatible=!!HEAPU32[webxrOptions+8>>>2>>>0]}}if(!("gpu"in navigator)){var sp=stackSave();var messagePtr=stringToUTF8OnStack("WebGPU not available on this browser (navigator.gpu is not available)");_emwgpuOnRequestAdapterCompleted(futureId,3,adapterPtr,messagePtr);stackRestore(sp);return}WebGPU.Internals.futureInsert(futureId,navigator["gpu"]["requestAdapter"](opts).then(adapter=>{if(adapter){WebGPU.Internals.jsObjectInsert(adapterPtr,adapter);_emwgpuOnRequestAdapterCompleted(futureId,1,adapterPtr,0)}else{var sp=stackSave();var messagePtr=stringToUTF8OnStack("WebGPU not available on this browser (requestAdapter returned null)");_emwgpuOnRequestAdapterCompleted(futureId,3,adapterPtr,messagePtr);stackRestore(sp)}},ex=>{var sp=stackSave();var messagePtr=stringToUTF8OnStack(ex.message);_emwgpuOnRequestAdapterCompleted(futureId,4,adapterPtr,messagePtr);stackRestore(sp)}))}var ENV={};var getExecutableName=()=>thisProgram||"./this.program";var getEnvStrings=()=>{if(!getEnvStrings.strings){var lang=(typeof navigator=="object"&&navigator.language||"C").replace("-","_")+".UTF-8";var env={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:lang,_:getExecutableName()};for(var x in ENV){if(ENV[x]===undefined)delete env[x];else env[x]=ENV[x]}var strings=[];for(var x in env){strings.push(`${x}=${env[x]}`)}getEnvStrings.strings=strings}return getEnvStrings.strings};function _environ_get(__environ,environ_buf){__environ>>>=0;environ_buf>>>=0;var bufSize=0;var envp=0;for(var string of getEnvStrings()){var ptr=environ_buf+bufSize;HEAPU32[__environ+envp>>>2>>>0]=ptr;bufSize+=stringToUTF8(string,ptr,Infinity)+1;envp+=4}return 0}function _environ_sizes_get(penviron_count,penviron_buf_size){penviron_count>>>=0;penviron_buf_size>>>=0;var strings=getEnvStrings();HEAPU32[penviron_count>>>2>>>0]=strings.length;var bufSize=0;for(var string of strings){bufSize+=lengthBytesUTF8(string)+1}HEAPU32[penviron_buf_size>>>2>>>0]=bufSize;return 0}var PATH={isAbs:path=>path.charAt(0)==="/",splitPath:filename=>{var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;return splitPathRe.exec(filename).slice(1)},normalizeArray:(parts,allowAboveRoot)=>{var up=0;for(var i=parts.length-1;i>=0;i--){var last=parts[i];if(last==="."){parts.splice(i,1)}else if(last===".."){parts.splice(i,1);up++}else if(up){parts.splice(i,1);up--}}if(allowAboveRoot){for(;up;up--){parts.unshift("..")}}return parts},normalize:path=>{var isAbsolute=PATH.isAbs(path),trailingSlash=path.slice(-1)==="/";path=PATH.normalizeArray(path.split("/").filter(p=>!!p),!isAbsolute).join("/");if(!path&&!isAbsolute){path="."}if(path&&trailingSlash){path+="/"}return(isAbsolute?"/":"")+path},dirname:path=>{var result=PATH.splitPath(path),root=result[0],dir=result[1];if(!root&&!dir){return"."}if(dir){dir=dir.slice(0,-1)}return root+dir},basename:path=>path&&path.match(/([^\/]+|\/)\/*$/)[1],join:(...paths)=>PATH.normalize(paths.join("/")),join2:(l,r)=>PATH.normalize(l+"/"+r)};var initRandomFill=()=>view=>crypto.getRandomValues(view);var randomFill=view=>{(randomFill=initRandomFill())(view)};var PATH_FS={resolve:(...args)=>{var resolvedPath="",resolvedAbsolute=false;for(var i=args.length-1;i>=-1&&!resolvedAbsolute;i--){var path=i>=0?args[i]:FS.cwd();if(typeof path!="string"){throw new TypeError("Arguments to path.resolve must be strings")}else if(!path){return""}resolvedPath=path+"/"+resolvedPath;resolvedAbsolute=PATH.isAbs(path)}resolvedPath=PATH.normalizeArray(resolvedPath.split("/").filter(p=>!!p),!resolvedAbsolute).join("/");return(resolvedAbsolute?"/":"")+resolvedPath||"."},relative:(from,to)=>{from=PATH_FS.resolve(from).slice(1);to=PATH_FS.resolve(to).slice(1);function trim(arr){var start=0;for(;start<arr.length;start++){if(arr[start]!=="")break}var end=arr.length-1;for(;end>=0;end--){if(arr[end]!=="")break}if(start>end)return[];return arr.slice(start,end-start+1)}var fromParts=trim(from.split("/"));var toParts=trim(to.split("/"));var length=Math.min(fromParts.length,toParts.length);var samePartsLength=length;for(var i=0;i<length;i++){if(fromParts[i]!==toParts[i]){samePartsLength=i;break}}var outputParts=[];for(var i=samePartsLength;i<fromParts.length;i++){outputParts.push("..")}outputParts=outputParts.concat(toParts.slice(samePartsLength));return outputParts.join("/")}};var FS_stdin_getChar_buffer=[];var intArrayFromString=(stringy,dontAddNull,length)=>{var len=length>0?length:lengthBytesUTF8(stringy)+1;var u8array=new Array(len);var numBytesWritten=stringToUTF8Array(stringy,u8array,0,u8array.length);if(dontAddNull)u8array.length=numBytesWritten;return u8array};var FS_stdin_getChar=()=>{if(!FS_stdin_getChar_buffer.length){var result=null;if(typeof window!="undefined"&&typeof window.prompt=="function"){result=window.prompt("Input: ");if(result!==null){result+="\n"}}else{}if(!result){return null}FS_stdin_getChar_buffer=intArrayFromString(result,true)}return FS_stdin_getChar_buffer.shift()};var TTY={ttys:[],init(){},shutdown(){},register(dev,ops){TTY.ttys[dev]={input:[],output:[],ops};FS.registerDevice(dev,TTY.stream_ops)},stream_ops:{open(stream){var tty=TTY.ttys[stream.node.rdev];if(!tty){throw new FS.ErrnoError(43)}stream.tty=tty;stream.seekable=false},close(stream){stream.tty.ops.fsync(stream.tty)},fsync(stream){stream.tty.ops.fsync(stream.tty)},read(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.get_char){throw new FS.ErrnoError(60)}var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=stream.tty.ops.get_char(stream.tty)}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.put_char){throw new FS.ErrnoError(60)}try{for(var i=0;i<length;i++){stream.tty.ops.put_char(stream.tty,buffer[offset+i])}}catch(e){throw new FS.ErrnoError(29)}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}},default_tty_ops:{get_char(tty){return FS_stdin_getChar()},put_char(tty,val){if(val===null||val===10){out(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output?.length>0){out(UTF8ArrayToString(tty.output));tty.output=[]}},ioctl_tcgets(tty){return{c_iflag:25856,c_oflag:5,c_cflag:191,c_lflag:35387,c_cc:[3,28,127,21,4,0,1,0,17,19,26,0,18,15,23,22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},ioctl_tcsets(tty,optional_actions,data){return 0},ioctl_tiocgwinsz(tty){return[24,80]}},default_tty1_ops:{put_char(tty,val){if(val===null||val===10){err(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output?.length>0){err(UTF8ArrayToString(tty.output));tty.output=[]}}}};var mmapAlloc=size=>{abort()};var MEMFS={ops_table:null,mount(mount){return MEMFS.createNode(null,"/",16895,0)},createNode(parent,name,mode,dev){if(FS.isBlkdev(mode)||FS.isFIFO(mode)){throw new FS.ErrnoError(63)}MEMFS.ops_table||={dir:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,lookup:MEMFS.node_ops.lookup,mknod:MEMFS.node_ops.mknod,rename:MEMFS.node_ops.rename,unlink:MEMFS.node_ops.unlink,rmdir:MEMFS.node_ops.rmdir,readdir:MEMFS.node_ops.readdir,symlink:MEMFS.node_ops.symlink},stream:{llseek:MEMFS.stream_ops.llseek}},file:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:{llseek:MEMFS.stream_ops.llseek,read:MEMFS.stream_ops.read,write:MEMFS.stream_ops.write,mmap:MEMFS.stream_ops.mmap,msync:MEMFS.stream_ops.msync}},link:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,readlink:MEMFS.node_ops.readlink},stream:{}},chrdev:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:FS.chrdev_stream_ops}};var node=FS.createNode(parent,name,mode,dev);if(FS.isDir(node.mode)){node.node_ops=MEMFS.ops_table.dir.node;node.stream_ops=MEMFS.ops_table.dir.stream;node.contents={}}else if(FS.isFile(node.mode)){node.node_ops=MEMFS.ops_table.file.node;node.stream_ops=MEMFS.ops_table.file.stream;node.usedBytes=0;node.contents=null}else if(FS.isLink(node.mode)){node.node_ops=MEMFS.ops_table.link.node;node.stream_ops=MEMFS.ops_table.link.stream}else if(FS.isChrdev(node.mode)){node.node_ops=MEMFS.ops_table.chrdev.node;node.stream_ops=MEMFS.ops_table.chrdev.stream}node.atime=node.mtime=node.ctime=Date.now();if(parent){parent.contents[name]=node;parent.atime=parent.mtime=parent.ctime=node.atime}return node},getFileDataAsTypedArray(node){if(!node.contents)return new Uint8Array(0);if(node.contents.subarray)return node.contents.subarray(0,node.usedBytes);return new Uint8Array(node.contents)},expandFileStorage(node,newCapacity){var prevCapacity=node.contents?node.contents.length:0;if(prevCapacity>=newCapacity)return;var CAPACITY_DOUBLING_MAX=1024*1024;newCapacity=Math.max(newCapacity,prevCapacity*(prevCapacity<CAPACITY_DOUBLING_MAX?2:1.125)>>>0);if(prevCapacity!=0)newCapacity=Math.max(newCapacity,256);var oldContents=node.contents;node.contents=new Uint8Array(newCapacity);if(node.usedBytes>0)node.contents.set(oldContents.subarray(0,node.usedBytes),0)},resizeFileStorage(node,newSize){if(node.usedBytes==newSize)return;if(newSize==0){node.contents=null;node.usedBytes=0}else{var oldContents=node.contents;node.contents=new Uint8Array(newSize);if(oldContents){node.contents.set(oldContents.subarray(0,Math.min(newSize,node.usedBytes)))}node.usedBytes=newSize}},node_ops:{getattr(node){var attr={};attr.dev=FS.isChrdev(node.mode)?node.id:1;attr.ino=node.id;attr.mode=node.mode;attr.nlink=1;attr.uid=0;attr.gid=0;attr.rdev=node.rdev;if(FS.isDir(node.mode)){attr.size=4096}else if(FS.isFile(node.mode)){attr.size=node.usedBytes}else if(FS.isLink(node.mode)){attr.size=node.link.length}else{attr.size=0}attr.atime=new Date(node.atime);attr.mtime=new Date(node.mtime);attr.ctime=new Date(node.ctime);attr.blksize=4096;attr.blocks=Math.ceil(attr.size/attr.blksize);return attr},setattr(node,attr){for(const key of["mode","atime","mtime","ctime"]){if(attr[key]!=null){node[key]=attr[key]}}if(attr.size!==undefined){MEMFS.resizeFileStorage(node,attr.size)}},lookup(parent,name){throw MEMFS.doesNotExistError},mknod(parent,name,mode,dev){return MEMFS.createNode(parent,name,mode,dev)},rename(old_node,new_dir,new_name){var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(new_node){if(FS.isDir(old_node.mode)){for(var i in new_node.contents){throw new FS.ErrnoError(55)}}FS.hashRemoveNode(new_node)}delete old_node.parent.contents[old_node.name];new_dir.contents[new_name]=old_node;old_node.name=new_name;new_dir.ctime=new_dir.mtime=old_node.parent.ctime=old_node.parent.mtime=Date.now()},unlink(parent,name){delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},rmdir(parent,name){var node=FS.lookupNode(parent,name);for(var i in node.contents){throw new FS.ErrnoError(55)}delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},readdir(node){return[".","..",...Object.keys(node.contents)]},symlink(parent,newname,oldpath){var node=MEMFS.createNode(parent,newname,511|40960,0);node.link=oldpath;return node},readlink(node){if(!FS.isLink(node.mode)){throw new FS.ErrnoError(28)}return node.link}},stream_ops:{read(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=stream.node.usedBytes)return 0;var size=Math.min(stream.node.usedBytes-position,length);if(size>8&&contents.subarray){buffer.set(contents.subarray(position,position+size),offset)}else{for(var i=0;i<size;i++)buffer[offset+i]=contents[position+i]}return size},write(stream,buffer,offset,length,position,canOwn){if(buffer.buffer===HEAP8.buffer){canOwn=false}if(!length)return 0;var node=stream.node;node.mtime=node.ctime=Date.now();if(buffer.subarray&&(!node.contents||node.contents.subarray)){if(canOwn){node.contents=buffer.subarray(offset,offset+length);node.usedBytes=length;return length}else if(node.usedBytes===0&&position===0){node.contents=buffer.slice(offset,offset+length);node.usedBytes=length;return length}else if(position+length<=node.usedBytes){node.contents.set(buffer.subarray(offset,offset+length),position);return length}}MEMFS.expandFileStorage(node,position+length);if(node.contents.subarray&&buffer.subarray){node.contents.set(buffer.subarray(offset,offset+length),position)}else{for(var i=0;i<length;i++){node.contents[position+i]=buffer[offset+i]}}node.usedBytes=Math.max(node.usedBytes,position+length);return length},llseek(stream,offset,whence){var position=offset;if(whence===1){position+=stream.position}else if(whence===2){if(FS.isFile(stream.node.mode)){position+=stream.node.usedBytes}}if(position<0){throw new FS.ErrnoError(28)}return position},mmap(stream,length,position,prot,flags){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}var ptr;var allocated;var contents=stream.node.contents;if(!(flags&2)&&contents&&contents.buffer===HEAP8.buffer){allocated=false;ptr=contents.byteOffset}else{allocated=true;ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}if(contents){if(position>0||position+length<contents.length){if(contents.subarray){contents=contents.subarray(position,position+length)}else{contents=Array.prototype.slice.call(contents,position,position+length)}}HEAP8.set(contents,ptr>>>0)}}return{ptr,allocated}},msync(stream,buffer,offset,length,mmapFlags){MEMFS.stream_ops.write(stream,buffer,0,length,offset,false);return 0}}};var asyncLoad=async url=>{var arrayBuffer=await readAsync(url);return new Uint8Array(arrayBuffer)};asyncLoad.isAsync=true;var FS_createDataFile=(...args)=>FS.createDataFile(...args);var getUniqueRunDependency=id=>id;var preloadPlugins=[];var FS_handledByPreloadPlugin=(byteArray,fullname,finish,onerror)=>{if(typeof Browser!="undefined")Browser.init();var handled=false;preloadPlugins.forEach(plugin=>{if(handled)return;if(plugin["canHandle"](fullname)){plugin["handle"](byteArray,fullname,finish,onerror);handled=true}});return handled};var FS_createPreloadedFile=(parent,name,url,canRead,canWrite,onload,onerror,dontCreateFile,canOwn,preFinish)=>{var fullname=name?PATH_FS.resolve(PATH.join2(parent,name)):parent;var dep=getUniqueRunDependency(`cp ${fullname}`);function processData(byteArray){function finish(byteArray){preFinish?.();if(!dontCreateFile){FS_createDataFile(parent,name,byteArray,canRead,canWrite,canOwn)}onload?.();removeRunDependency(dep)}if(FS_handledByPreloadPlugin(byteArray,fullname,finish,()=>{onerror?.();removeRunDependency(dep)})){return}finish(byteArray)}addRunDependency(dep);if(typeof url=="string"){asyncLoad(url).then(processData,onerror)}else{processData(url)}};var FS_modeStringToFlags=str=>{var flagModes={r:0,"r+":2,w:512|64|1,"w+":512|64|2,a:1024|64|1,"a+":1024|64|2};var flags=flagModes[str];if(typeof flags=="undefined"){throw new Error(`Unknown file open mode: ${str}`)}return flags};var FS_getMode=(canRead,canWrite)=>{var mode=0;if(canRead)mode|=292|73;if(canWrite)mode|=146;return mode};var FS={root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,filesystems:null,syncFSRequests:0,readFiles:{},ErrnoError:class{name="ErrnoError";constructor(errno){this.errno=errno}},FSStream:class{shared={};get object(){return this.node}set object(val){this.node=val}get isRead(){return(this.flags&2097155)!==1}get isWrite(){return(this.flags&2097155)!==0}get isAppend(){return this.flags&1024}get flags(){return this.shared.flags}set flags(val){this.shared.flags=val}get position(){return this.shared.position}set position(val){this.shared.position=val}},FSNode:class{node_ops={};stream_ops={};readMode=292|73;writeMode=146;mounted=null;constructor(parent,name,mode,rdev){if(!parent){parent=this}this.parent=parent;this.mount=parent.mount;this.id=FS.nextInode++;this.name=name;this.mode=mode;this.rdev=rdev;this.atime=this.mtime=this.ctime=Date.now()}get read(){return(this.mode&this.readMode)===this.readMode}set read(val){val?this.mode|=this.readMode:this.mode&=~this.readMode}get write(){return(this.mode&this.writeMode)===this.writeMode}set write(val){val?this.mode|=this.writeMode:this.mode&=~this.writeMode}get isFolder(){return FS.isDir(this.mode)}get isDevice(){return FS.isChrdev(this.mode)}},lookupPath(path,opts={}){if(!path){throw new FS.ErrnoError(44)}opts.follow_mount??=true;if(!PATH.isAbs(path)){path=FS.cwd()+"/"+path}linkloop:for(var nlinks=0;nlinks<40;nlinks++){var parts=path.split("/").filter(p=>!!p);var current=FS.root;var current_path="/";for(var i=0;i<parts.length;i++){var islast=i===parts.length-1;if(islast&&opts.parent){break}if(parts[i]==="."){continue}if(parts[i]===".."){current_path=PATH.dirname(current_path);if(FS.isRoot(current)){path=current_path+"/"+parts.slice(i+1).join("/");continue linkloop}else{current=current.parent}continue}current_path=PATH.join2(current_path,parts[i]);try{current=FS.lookupNode(current,parts[i])}catch(e){if(e?.errno===44&&islast&&opts.noent_okay){return{path:current_path}}throw e}if(FS.isMountpoint(current)&&(!islast||opts.follow_mount)){current=current.mounted.root}if(FS.isLink(current.mode)&&(!islast||opts.follow)){if(!current.node_ops.readlink){throw new FS.ErrnoError(52)}var link=current.node_ops.readlink(current);if(!PATH.isAbs(link)){link=PATH.dirname(current_path)+"/"+link}path=link+"/"+parts.slice(i+1).join("/");continue linkloop}}return{path:current_path,node:current}}throw new FS.ErrnoError(32)},getPath(node){var path;while(true){if(FS.isRoot(node)){var mount=node.mount.mountpoint;if(!path)return mount;return mount[mount.length-1]!=="/"?`${mount}/${path}`:mount+path}path=path?`${node.name}/${path}`:node.name;node=node.parent}},hashName(parentid,name){var hash=0;for(var i=0;i<name.length;i++){hash=(hash<<5)-hash+name.charCodeAt(i)|0}return(parentid+hash>>>0)%FS.nameTable.length},hashAddNode(node){var hash=FS.hashName(node.parent.id,node.name);node.name_next=FS.nameTable[hash];FS.nameTable[hash]=node},hashRemoveNode(node){var hash=FS.hashName(node.parent.id,node.name);if(FS.nameTable[hash]===node){FS.nameTable[hash]=node.name_next}else{var current=FS.nameTable[hash];while(current){if(current.name_next===node){current.name_next=node.name_next;break}current=current.name_next}}},lookupNode(parent,name){var errCode=FS.mayLookup(parent);if(errCode){throw new FS.ErrnoError(errCode)}var hash=FS.hashName(parent.id,name);for(var node=FS.nameTable[hash];node;node=node.name_next){var nodeName=node.name;if(node.parent.id===parent.id&&nodeName===name){return node}}return FS.lookup(parent,name)},createNode(parent,name,mode,rdev){var node=new FS.FSNode(parent,name,mode,rdev);FS.hashAddNode(node);return node},destroyNode(node){FS.hashRemoveNode(node)},isRoot(node){return node===node.parent},isMountpoint(node){return!!node.mounted},isFile(mode){return(mode&61440)===32768},isDir(mode){return(mode&61440)===16384},isLink(mode){return(mode&61440)===40960},isChrdev(mode){return(mode&61440)===8192},isBlkdev(mode){return(mode&61440)===24576},isFIFO(mode){return(mode&61440)===4096},isSocket(mode){return(mode&49152)===49152},flagsToPermissionString(flag){var perms=["r","w","rw"][flag&3];if(flag&512){perms+="w"}return perms},nodePermissions(node,perms){if(FS.ignorePermissions){return 0}if(perms.includes("r")&&!(node.mode&292)){return 2}else if(perms.includes("w")&&!(node.mode&146)){return 2}else if(perms.includes("x")&&!(node.mode&73)){return 2}return 0},mayLookup(dir){if(!FS.isDir(dir.mode))return 54;var errCode=FS.nodePermissions(dir,"x");if(errCode)return errCode;if(!dir.node_ops.lookup)return 2;return 0},mayCreate(dir,name){if(!FS.isDir(dir.mode)){return 54}try{var node=FS.lookupNode(dir,name);return 20}catch(e){}return FS.nodePermissions(dir,"wx")},mayDelete(dir,name,isdir){var node;try{node=FS.lookupNode(dir,name)}catch(e){return e.errno}var errCode=FS.nodePermissions(dir,"wx");if(errCode){return errCode}if(isdir){if(!FS.isDir(node.mode)){return 54}if(FS.isRoot(node)||FS.getPath(node)===FS.cwd()){return 10}}else{if(FS.isDir(node.mode)){return 31}}return 0},mayOpen(node,flags){if(!node){return 44}if(FS.isLink(node.mode)){return 32}else if(FS.isDir(node.mode)){if(FS.flagsToPermissionString(flags)!=="r"||flags&(512|64)){return 31}}return FS.nodePermissions(node,FS.flagsToPermissionString(flags))},checkOpExists(op,err){if(!op){throw new FS.ErrnoError(err)}return op},MAX_OPEN_FDS:4096,nextfd(){for(var fd=0;fd<=FS.MAX_OPEN_FDS;fd++){if(!FS.streams[fd]){return fd}}throw new FS.ErrnoError(33)},getStreamChecked(fd){var stream=FS.getStream(fd);if(!stream){throw new FS.ErrnoError(8)}return stream},getStream:fd=>FS.streams[fd],createStream(stream,fd=-1){stream=Object.assign(new FS.FSStream,stream);if(fd==-1){fd=FS.nextfd()}stream.fd=fd;FS.streams[fd]=stream;return stream},closeStream(fd){FS.streams[fd]=null},dupStream(origStream,fd=-1){var stream=FS.createStream(origStream,fd);stream.stream_ops?.dup?.(stream);return stream},doSetAttr(stream,node,attr){var setattr=stream?.stream_ops.setattr;var arg=setattr?stream:node;setattr??=node.node_ops.setattr;FS.checkOpExists(setattr,63);setattr(arg,attr)},chrdev_stream_ops:{open(stream){var device=FS.getDevice(stream.node.rdev);stream.stream_ops=device.stream_ops;stream.stream_ops.open?.(stream)},llseek(){throw new FS.ErrnoError(70)}},major:dev=>dev>>8,minor:dev=>dev&255,makedev:(ma,mi)=>ma<<8|mi,registerDevice(dev,ops){FS.devices[dev]={stream_ops:ops}},getDevice:dev=>FS.devices[dev],getMounts(mount){var mounts=[];var check=[mount];while(check.length){var m=check.pop();mounts.push(m);check.push(...m.mounts)}return mounts},syncfs(populate,callback){if(typeof populate=="function"){callback=populate;populate=false}FS.syncFSRequests++;if(FS.syncFSRequests>1){err(`warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work`)}var mounts=FS.getMounts(FS.root.mount);var completed=0;function doCallback(errCode){FS.syncFSRequests--;return callback(errCode)}function done(errCode){if(errCode){if(!done.errored){done.errored=true;return doCallback(errCode)}return}if(++completed>=mounts.length){doCallback(null)}}mounts.forEach(mount=>{if(!mount.type.syncfs){return done(null)}mount.type.syncfs(mount,populate,done)})},mount(type,opts,mountpoint){var root=mountpoint==="/";var pseudo=!mountpoint;var node;if(root&&FS.root){throw new FS.ErrnoError(10)}else if(!root&&!pseudo){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});mountpoint=lookup.path;node=lookup.node;if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}if(!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}}var mount={type,opts,mountpoint,mounts:[]};var mountRoot=type.mount(mount);mountRoot.mount=mount;mount.root=mountRoot;if(root){FS.root=mountRoot}else if(node){node.mounted=mount;if(node.mount){node.mount.mounts.push(mount)}}return mountRoot},unmount(mountpoint){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});if(!FS.isMountpoint(lookup.node)){throw new FS.ErrnoError(28)}var node=lookup.node;var mount=node.mounted;var mounts=FS.getMounts(mount);Object.keys(FS.nameTable).forEach(hash=>{var current=FS.nameTable[hash];while(current){var next=current.name_next;if(mounts.includes(current.mount)){FS.destroyNode(current)}current=next}});node.mounted=null;var idx=node.mount.mounts.indexOf(mount);node.mount.mounts.splice(idx,1)},lookup(parent,name){return parent.node_ops.lookup(parent,name)},mknod(path,mode,dev){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);if(!name){throw new FS.ErrnoError(28)}if(name==="."||name===".."){throw new FS.ErrnoError(20)}var errCode=FS.mayCreate(parent,name);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.mknod){throw new FS.ErrnoError(63)}return parent.node_ops.mknod(parent,name,mode,dev)},statfs(path){return FS.statfsNode(FS.lookupPath(path,{follow:true}).node)},statfsStream(stream){return FS.statfsNode(stream.node)},statfsNode(node){var rtn={bsize:4096,frsize:4096,blocks:1e6,bfree:5e5,bavail:5e5,files:FS.nextInode,ffree:FS.nextInode-1,fsid:42,flags:2,namelen:255};if(node.node_ops.statfs){Object.assign(rtn,node.node_ops.statfs(node.mount.opts.root))}return rtn},create(path,mode=438){mode&=4095;mode|=32768;return FS.mknod(path,mode,0)},mkdir(path,mode=511){mode&=511|512;mode|=16384;return FS.mknod(path,mode,0)},mkdirTree(path,mode){var dirs=path.split("/");var d="";for(var dir of dirs){if(!dir)continue;if(d||PATH.isAbs(path))d+="/";d+=dir;try{FS.mkdir(d,mode)}catch(e){if(e.errno!=20)throw e}}},mkdev(path,mode,dev){if(typeof dev=="undefined"){dev=mode;mode=438}mode|=8192;return FS.mknod(path,mode,dev)},symlink(oldpath,newpath){if(!PATH_FS.resolve(oldpath)){throw new FS.ErrnoError(44)}var lookup=FS.lookupPath(newpath,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var newname=PATH.basename(newpath);var errCode=FS.mayCreate(parent,newname);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.symlink){throw new FS.ErrnoError(63)}return parent.node_ops.symlink(parent,newname,oldpath)},rename(old_path,new_path){var old_dirname=PATH.dirname(old_path);var new_dirname=PATH.dirname(new_path);var old_name=PATH.basename(old_path);var new_name=PATH.basename(new_path);var lookup,old_dir,new_dir;lookup=FS.lookupPath(old_path,{parent:true});old_dir=lookup.node;lookup=FS.lookupPath(new_path,{parent:true});new_dir=lookup.node;if(!old_dir||!new_dir)throw new FS.ErrnoError(44);if(old_dir.mount!==new_dir.mount){throw new FS.ErrnoError(75)}var old_node=FS.lookupNode(old_dir,old_name);var relative=PATH_FS.relative(old_path,new_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(28)}relative=PATH_FS.relative(new_path,old_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(55)}var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(old_node===new_node){return}var isdir=FS.isDir(old_node.mode);var errCode=FS.mayDelete(old_dir,old_name,isdir);if(errCode){throw new FS.ErrnoError(errCode)}errCode=new_node?FS.mayDelete(new_dir,new_name,isdir):FS.mayCreate(new_dir,new_name);if(errCode){throw new FS.ErrnoError(errCode)}if(!old_dir.node_ops.rename){throw new FS.ErrnoError(63)}if(FS.isMountpoint(old_node)||new_node&&FS.isMountpoint(new_node)){throw new FS.ErrnoError(10)}if(new_dir!==old_dir){errCode=FS.nodePermissions(old_dir,"w");if(errCode){throw new FS.ErrnoError(errCode)}}FS.hashRemoveNode(old_node);try{old_dir.node_ops.rename(old_node,new_dir,new_name);old_node.parent=new_dir}catch(e){throw e}finally{FS.hashAddNode(old_node)}},rmdir(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,true);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.rmdir){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.rmdir(parent,name);FS.destroyNode(node)},readdir(path){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;var readdir=FS.checkOpExists(node.node_ops.readdir,54);return readdir(node)},unlink(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,false);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.unlink){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.unlink(parent,name);FS.destroyNode(node)},readlink(path){var lookup=FS.lookupPath(path);var link=lookup.node;if(!link){throw new FS.ErrnoError(44)}if(!link.node_ops.readlink){throw new FS.ErrnoError(28)}return link.node_ops.readlink(link)},stat(path,dontFollow){var lookup=FS.lookupPath(path,{follow:!dontFollow});var node=lookup.node;var getattr=FS.checkOpExists(node.node_ops.getattr,63);return getattr(node)},fstat(fd){var stream=FS.getStreamChecked(fd);var node=stream.node;var getattr=stream.stream_ops.getattr;var arg=getattr?stream:node;getattr??=node.node_ops.getattr;FS.checkOpExists(getattr,63);return getattr(arg)},lstat(path){return FS.stat(path,true)},doChmod(stream,node,mode,dontFollow){FS.doSetAttr(stream,node,{mode:mode&4095|node.mode&~4095,ctime:Date.now(),dontFollow})},chmod(path,mode,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}FS.doChmod(null,node,mode,dontFollow)},lchmod(path,mode){FS.chmod(path,mode,true)},fchmod(fd,mode){var stream=FS.getStreamChecked(fd);FS.doChmod(stream,stream.node,mode,false)},doChown(stream,node,dontFollow){FS.doSetAttr(stream,node,{timestamp:Date.now(),dontFollow})},chown(path,uid,gid,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}FS.doChown(null,node,dontFollow)},lchown(path,uid,gid){FS.chown(path,uid,gid,true)},fchown(fd,uid,gid){var stream=FS.getStreamChecked(fd);FS.doChown(stream,stream.node,false)},doTruncate(stream,node,len){if(FS.isDir(node.mode)){throw new FS.ErrnoError(31)}if(!FS.isFile(node.mode)){throw new FS.ErrnoError(28)}var errCode=FS.nodePermissions(node,"w");if(errCode){throw new FS.ErrnoError(errCode)}FS.doSetAttr(stream,node,{size:len,timestamp:Date.now()})},truncate(path,len){if(len<0){throw new FS.ErrnoError(28)}var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:true});node=lookup.node}else{node=path}FS.doTruncate(null,node,len)},ftruncate(fd,len){var stream=FS.getStreamChecked(fd);if(len<0||(stream.flags&2097155)===0){throw new FS.ErrnoError(28)}FS.doTruncate(stream,stream.node,len)},utime(path,atime,mtime){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;var setattr=FS.checkOpExists(node.node_ops.setattr,63);setattr(node,{atime,mtime})},open(path,flags,mode=438){if(path===""){throw new FS.ErrnoError(44)}flags=typeof flags=="string"?FS_modeStringToFlags(flags):flags;if(flags&64){mode=mode&4095|32768}else{mode=0}var node;var isDirPath;if(typeof path=="object"){node=path}else{isDirPath=path.endsWith("/");var lookup=FS.lookupPath(path,{follow:!(flags&131072),noent_okay:true});node=lookup.node;path=lookup.path}var created=false;if(flags&64){if(node){if(flags&128){throw new FS.ErrnoError(20)}}else if(isDirPath){throw new FS.ErrnoError(31)}else{node=FS.mknod(path,mode|511,0);created=true}}if(!node){throw new FS.ErrnoError(44)}if(FS.isChrdev(node.mode)){flags&=~512}if(flags&65536&&!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}if(!created){var errCode=FS.mayOpen(node,flags);if(errCode){throw new FS.ErrnoError(errCode)}}if(flags&512&&!created){FS.truncate(node,0)}flags&=~(128|512|131072);var stream=FS.createStream({node,path:FS.getPath(node),flags,seekable:true,position:0,stream_ops:node.stream_ops,ungotten:[],error:false});if(stream.stream_ops.open){stream.stream_ops.open(stream)}if(created){FS.chmod(node,mode&511)}if(Module["logReadFiles"]&&!(flags&1)){if(!(path in FS.readFiles)){FS.readFiles[path]=1}}return stream},close(stream){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(stream.getdents)stream.getdents=null;try{if(stream.stream_ops.close){stream.stream_ops.close(stream)}}catch(e){throw e}finally{FS.closeStream(stream.fd)}stream.fd=null},isClosed(stream){return stream.fd===null},llseek(stream,offset,whence){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(!stream.seekable||!stream.stream_ops.llseek){throw new FS.ErrnoError(70)}if(whence!=0&&whence!=1&&whence!=2){throw new FS.ErrnoError(28)}stream.position=stream.stream_ops.llseek(stream,offset,whence);stream.ungotten=[];return stream.position},read(stream,buffer,offset,length,position){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.read){throw new FS.ErrnoError(28)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesRead=stream.stream_ops.read(stream,buffer,offset,length,position);if(!seeking)stream.position+=bytesRead;return bytesRead},write(stream,buffer,offset,length,position,canOwn){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===0){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.write){throw new FS.ErrnoError(28)}if(stream.seekable&&stream.flags&1024){FS.llseek(stream,0,2)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesWritten=stream.stream_ops.write(stream,buffer,offset,length,position,canOwn);if(!seeking)stream.position+=bytesWritten;return bytesWritten},mmap(stream,length,position,prot,flags){if((prot&2)!==0&&(flags&2)===0&&(stream.flags&2097155)!==2){throw new FS.ErrnoError(2)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(2)}if(!stream.stream_ops.mmap){throw new FS.ErrnoError(43)}if(!length){throw new FS.ErrnoError(28)}return stream.stream_ops.mmap(stream,length,position,prot,flags)},msync(stream,buffer,offset,length,mmapFlags){if(!stream.stream_ops.msync){return 0}return stream.stream_ops.msync(stream,buffer,offset,length,mmapFlags)},ioctl(stream,cmd,arg){if(!stream.stream_ops.ioctl){throw new FS.ErrnoError(59)}return stream.stream_ops.ioctl(stream,cmd,arg)},readFile(path,opts={}){opts.flags=opts.flags||0;opts.encoding=opts.encoding||"binary";if(opts.encoding!=="utf8"&&opts.encoding!=="binary"){throw new Error(`Invalid encoding type "${opts.encoding}"`)}var stream=FS.open(path,opts.flags);var stat=FS.stat(path);var length=stat.size;var buf=new Uint8Array(length);FS.read(stream,buf,0,length,0);if(opts.encoding==="utf8"){buf=UTF8ArrayToString(buf)}FS.close(stream);return buf},writeFile(path,data,opts={}){opts.flags=opts.flags||577;var stream=FS.open(path,opts.flags,opts.mode);if(typeof data=="string"){data=new Uint8Array(intArrayFromString(data,true))}if(ArrayBuffer.isView(data)){FS.write(stream,data,0,data.byteLength,undefined,opts.canOwn)}else{throw new Error("Unsupported data type")}FS.close(stream)},cwd:()=>FS.currentPath,chdir(path){var lookup=FS.lookupPath(path,{follow:true});if(lookup.node===null){throw new FS.ErrnoError(44)}if(!FS.isDir(lookup.node.mode)){throw new FS.ErrnoError(54)}var errCode=FS.nodePermissions(lookup.node,"x");if(errCode){throw new FS.ErrnoError(errCode)}FS.currentPath=lookup.path},createDefaultDirectories(){FS.mkdir("/tmp");FS.mkdir("/home");FS.mkdir("/home/web_user")},createDefaultDevices(){FS.mkdir("/dev");FS.registerDevice(FS.makedev(1,3),{read:()=>0,write:(stream,buffer,offset,length,pos)=>length,llseek:()=>0});FS.mkdev("/dev/null",FS.makedev(1,3));TTY.register(FS.makedev(5,0),TTY.default_tty_ops);TTY.register(FS.makedev(6,0),TTY.default_tty1_ops);FS.mkdev("/dev/tty",FS.makedev(5,0));FS.mkdev("/dev/tty1",FS.makedev(6,0));var randomBuffer=new Uint8Array(1024),randomLeft=0;var randomByte=()=>{if(randomLeft===0){randomFill(randomBuffer);randomLeft=randomBuffer.byteLength}return randomBuffer[--randomLeft]};FS.createDevice("/dev","random",randomByte);FS.createDevice("/dev","urandom",randomByte);FS.mkdir("/dev/shm");FS.mkdir("/dev/shm/tmp")},createSpecialDirectories(){FS.mkdir("/proc");var proc_self=FS.mkdir("/proc/self");FS.mkdir("/proc/self/fd");FS.mount({mount(){var node=FS.createNode(proc_self,"fd",16895,73);node.stream_ops={llseek:MEMFS.stream_ops.llseek};node.node_ops={lookup(parent,name){var fd=+name;var stream=FS.getStreamChecked(fd);var ret={parent:null,mount:{mountpoint:"fake"},node_ops:{readlink:()=>stream.path},id:fd+1};ret.parent=ret;return ret},readdir(){return Array.from(FS.streams.entries()).filter(([k,v])=>v).map(([k,v])=>k.toString())}};return node}},{},"/proc/self/fd")},createStandardStreams(input,output,error){if(input){FS.createDevice("/dev","stdin",input)}else{FS.symlink("/dev/tty","/dev/stdin")}if(output){FS.createDevice("/dev","stdout",null,output)}else{FS.symlink("/dev/tty","/dev/stdout")}if(error){FS.createDevice("/dev","stderr",null,error)}else{FS.symlink("/dev/tty1","/dev/stderr")}var stdin=FS.open("/dev/stdin",0);var stdout=FS.open("/dev/stdout",1);var stderr=FS.open("/dev/stderr",1)},staticInit(){FS.nameTable=new Array(4096);FS.mount(MEMFS,{},"/");FS.createDefaultDirectories();FS.createDefaultDevices();FS.createSpecialDirectories();FS.filesystems={MEMFS}},init(input,output,error){FS.initialized=true;input??=Module["stdin"];output??=Module["stdout"];error??=Module["stderr"];FS.createStandardStreams(input,output,error)},quit(){FS.initialized=false;for(var stream of FS.streams){if(stream){FS.close(stream)}}},findObject(path,dontResolveLastLink){var ret=FS.analyzePath(path,dontResolveLastLink);if(!ret.exists){return null}return ret.object},analyzePath(path,dontResolveLastLink){try{var lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});path=lookup.path}catch(e){}var ret={isRoot:false,exists:false,error:0,name:null,path:null,object:null,parentExists:false,parentPath:null,parentObject:null};try{var lookup=FS.lookupPath(path,{parent:true});ret.parentExists=true;ret.parentPath=lookup.path;ret.parentObject=lookup.node;ret.name=PATH.basename(path);lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});ret.exists=true;ret.path=lookup.path;ret.object=lookup.node;ret.name=lookup.node.name;ret.isRoot=lookup.path==="/"}catch(e){ret.error=e.errno}return ret},createPath(parent,path,canRead,canWrite){parent=typeof parent=="string"?parent:FS.getPath(parent);var parts=path.split("/").reverse();while(parts.length){var part=parts.pop();if(!part)continue;var current=PATH.join2(parent,part);try{FS.mkdir(current)}catch(e){if(e.errno!=20)throw e}parent=current}return current},createFile(parent,name,properties,canRead,canWrite){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(canRead,canWrite);return FS.create(path,mode)},createDataFile(parent,name,data,canRead,canWrite,canOwn){var path=name;if(parent){parent=typeof parent=="string"?parent:FS.getPath(parent);path=name?PATH.join2(parent,name):parent}var mode=FS_getMode(canRead,canWrite);var node=FS.create(path,mode);if(data){if(typeof data=="string"){var arr=new Array(data.length);for(var i=0,len=data.length;i<len;++i)arr[i]=data.charCodeAt(i);data=arr}FS.chmod(node,mode|146);var stream=FS.open(node,577);FS.write(stream,data,0,data.length,0,canOwn);FS.close(stream);FS.chmod(node,mode)}},createDevice(parent,name,input,output){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(!!input,!!output);FS.createDevice.major??=64;var dev=FS.makedev(FS.createDevice.major++,0);FS.registerDevice(dev,{open(stream){stream.seekable=false},close(stream){if(output?.buffer?.length){output(10)}},read(stream,buffer,offset,length,pos){var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=input()}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){for(var i=0;i<length;i++){try{output(buffer[offset+i])}catch(e){throw new FS.ErrnoError(29)}}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}});return FS.mkdev(path,mode,dev)},forceLoadFile(obj){if(obj.isDevice||obj.isFolder||obj.link||obj.contents)return true;if(typeof XMLHttpRequest!="undefined"){throw new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.")}else{try{obj.contents=readBinary(obj.url);obj.usedBytes=obj.contents.length}catch(e){throw new FS.ErrnoError(29)}}},createLazyFile(parent,name,url,canRead,canWrite){class LazyUint8Array{lengthKnown=false;chunks=[];get(idx){if(idx>this.length-1||idx<0){return undefined}var chunkOffset=idx%this.chunkSize;var chunkNum=idx/this.chunkSize|0;return this.getter(chunkNum)[chunkOffset]}setDataGetter(getter){this.getter=getter}cacheLength(){var xhr=new XMLHttpRequest;xhr.open("HEAD",url,false);xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))throw new Error("Couldn't load "+url+". Status: "+xhr.status);var datalength=Number(xhr.getResponseHeader("Content-length"));var header;var hasByteServing=(header=xhr.getResponseHeader("Accept-Ranges"))&&header==="bytes";var usesGzip=(header=xhr.getResponseHeader("Content-Encoding"))&&header==="gzip";var chunkSize=1024*1024;if(!hasByteServing)chunkSize=datalength;var doXHR=(from,to)=>{if(from>to)throw new Error("invalid range ("+from+", "+to+") or no bytes requested!");if(to>datalength-1)throw new Error("only "+datalength+" bytes available! programmer error!");var xhr=new XMLHttpRequest;xhr.open("GET",url,false);if(datalength!==chunkSize)xhr.setRequestHeader("Range","bytes="+from+"-"+to);xhr.responseType="arraybuffer";if(xhr.overrideMimeType){xhr.overrideMimeType("text/plain; charset=x-user-defined")}xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))throw new Error("Couldn't load "+url+". Status: "+xhr.status);if(xhr.response!==undefined){return new Uint8Array(xhr.response||[])}return intArrayFromString(xhr.responseText||"",true)};var lazyArray=this;lazyArray.setDataGetter(chunkNum=>{var start=chunkNum*chunkSize;var end=(chunkNum+1)*chunkSize-1;end=Math.min(end,datalength-1);if(typeof lazyArray.chunks[chunkNum]=="undefined"){lazyArray.chunks[chunkNum]=doXHR(start,end)}if(typeof lazyArray.chunks[chunkNum]=="undefined")throw new Error("doXHR failed!");return lazyArray.chunks[chunkNum]});if(usesGzip||!datalength){chunkSize=datalength=1;datalength=this.getter(0).length;chunkSize=datalength;out("LazyFiles on gzip forces download of the whole file when length is accessed")}this._length=datalength;this._chunkSize=chunkSize;this.lengthKnown=true}get length(){if(!this.lengthKnown){this.cacheLength()}return this._length}get chunkSize(){if(!this.lengthKnown){this.cacheLength()}return this._chunkSize}}if(typeof XMLHttpRequest!="undefined"){if(!ENVIRONMENT_IS_WORKER)throw"Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc";var lazyArray=new LazyUint8Array;var properties={isDevice:false,contents:lazyArray}}else{var properties={isDevice:false,url}}var node=FS.createFile(parent,name,properties,canRead,canWrite);if(properties.contents){node.contents=properties.contents}else if(properties.url){node.contents=null;node.url=properties.url}Object.defineProperties(node,{usedBytes:{get:function(){return this.contents.length}}});var stream_ops={};var keys=Object.keys(node.stream_ops);keys.forEach(key=>{var fn=node.stream_ops[key];stream_ops[key]=(...args)=>{FS.forceLoadFile(node);return fn(...args)}});function writeChunks(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=contents.length)return 0;var size=Math.min(contents.length-position,length);if(contents.slice){for(var i=0;i<size;i++){buffer[offset+i]=contents[position+i]}}else{for(var i=0;i<size;i++){buffer[offset+i]=contents.get(position+i)}}return size}stream_ops.read=(stream,buffer,offset,length,position)=>{FS.forceLoadFile(node);return writeChunks(stream,buffer,offset,length,position)};stream_ops.mmap=(stream,length,position,prot,flags)=>{FS.forceLoadFile(node);var ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}writeChunks(stream,HEAP8,ptr,length,position);return{ptr,allocated:true}};node.stream_ops=stream_ops;return node}};var SYSCALLS={DEFAULT_POLLMASK:5,calculateAt(dirfd,path,allowEmpty){if(PATH.isAbs(path)){return path}var dir;if(dirfd===-100){dir=FS.cwd()}else{var dirstream=SYSCALLS.getStreamFromFD(dirfd);dir=dirstream.path}if(path.length==0){if(!allowEmpty){throw new FS.ErrnoError(44)}return dir}return dir+"/"+path},writeStat(buf,stat){HEAP32[buf>>>2>>>0]=stat.dev;HEAP32[buf+4>>>2>>>0]=stat.mode;HEAPU32[buf+8>>>2>>>0]=stat.nlink;HEAP32[buf+12>>>2>>>0]=stat.uid;HEAP32[buf+16>>>2>>>0]=stat.gid;HEAP32[buf+20>>>2>>>0]=stat.rdev;HEAP64[buf+24>>>3>>>0]=BigInt(stat.size);HEAP32[buf+32>>>2>>>0]=4096;HEAP32[buf+36>>>2>>>0]=stat.blocks;var atime=stat.atime.getTime();var mtime=stat.mtime.getTime();var ctime=stat.ctime.getTime();HEAP64[buf+40>>>3>>>0]=BigInt(Math.floor(atime/1e3));HEAPU32[buf+48>>>2>>>0]=atime%1e3*1e3*1e3;HEAP64[buf+56>>>3>>>0]=BigInt(Math.floor(mtime/1e3));HEAPU32[buf+64>>>2>>>0]=mtime%1e3*1e3*1e3;HEAP64[buf+72>>>3>>>0]=BigInt(Math.floor(ctime/1e3));HEAPU32[buf+80>>>2>>>0]=ctime%1e3*1e3*1e3;HEAP64[buf+88>>>3>>>0]=BigInt(stat.ino);return 0},writeStatFs(buf,stats){HEAP32[buf+4>>>2>>>0]=stats.bsize;HEAP32[buf+40>>>2>>>0]=stats.bsize;HEAP32[buf+8>>>2>>>0]=stats.blocks;HEAP32[buf+12>>>2>>>0]=stats.bfree;HEAP32[buf+16>>>2>>>0]=stats.bavail;HEAP32[buf+20>>>2>>>0]=stats.files;HEAP32[buf+24>>>2>>>0]=stats.ffree;HEAP32[buf+28>>>2>>>0]=stats.fsid;HEAP32[buf+44>>>2>>>0]=stats.flags;HEAP32[buf+36>>>2>>>0]=stats.namelen},doMsync(addr,stream,len,flags,offset){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}if(flags&2){return 0}var buffer=HEAPU8.slice(addr,addr+len);FS.msync(stream,buffer,offset,len,flags)},getStreamFromFD(fd){var stream=FS.getStreamChecked(fd);return stream},varargs:undefined,getStr(ptr){var ret=UTF8ToString(ptr);return ret}};function _fd_close(fd){try{var stream=SYSCALLS.getStreamFromFD(fd);FS.close(stream);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doReadv=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>>2>>>0];var len=HEAPU32[iov+4>>>2>>>0];iov+=8;var curr=FS.read(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len)break;if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_read(fd,iov,iovcnt,pnum){iov>>>=0;iovcnt>>>=0;pnum>>>=0;try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doReadv(stream,iov,iovcnt);HEAPU32[pnum>>>2>>>0]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}function _fd_seek(fd,offset,whence,newOffset){offset=bigintToI53Checked(offset);newOffset>>>=0;try{if(isNaN(offset))return 61;var stream=SYSCALLS.getStreamFromFD(fd);FS.llseek(stream,offset,whence);HEAP64[newOffset>>>3>>>0]=BigInt(stream.position);if(stream.getdents&&offset===0&&whence===0)stream.getdents=null;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doWritev=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>>2>>>0];var len=HEAPU32[iov+4>>>2>>>0];iov+=8;var curr=FS.write(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len){break}if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_write(fd,iov,iovcnt,pnum){iov>>>=0;iovcnt>>>=0;pnum>>>=0;try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doWritev(stream,iov,iovcnt);HEAPU32[pnum>>>2>>>0]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}function _random_get(buffer,size){buffer>>>=0;size>>>=0;try{randomFill(HEAPU8.subarray(buffer>>>0,buffer+size>>>0));return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}function _wgpuAdapterGetLimits(adapterPtr,limitsOutPtr){adapterPtr>>>=0;limitsOutPtr>>>=0;var adapter=WebGPU.getJsObject(adapterPtr);WebGPU.fillLimitStruct(adapter.limits,limitsOutPtr);return 1}function _wgpuCommandEncoderBeginComputePass(encoderPtr,descriptor){encoderPtr>>>=0;descriptor>>>=0;var desc;if(descriptor){desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),timestampWrites:WebGPU.makePassTimestampWrites(HEAPU32[descriptor+12>>>2>>>0])}}var commandEncoder=WebGPU.getJsObject(encoderPtr);var ptr=_emwgpuCreateComputePassEncoder(0);WebGPU.Internals.jsObjectInsert(ptr,commandEncoder.beginComputePass(desc));return ptr}function _wgpuCommandEncoderCopyBufferToBuffer(encoderPtr,srcPtr,srcOffset,dstPtr,dstOffset,size){encoderPtr>>>=0;srcPtr>>>=0;srcOffset=bigintToI53Checked(srcOffset);dstPtr>>>=0;dstOffset=bigintToI53Checked(dstOffset);size=bigintToI53Checked(size);var commandEncoder=WebGPU.getJsObject(encoderPtr);var src=WebGPU.getJsObject(srcPtr);var dst=WebGPU.getJsObject(dstPtr);commandEncoder.copyBufferToBuffer(src,srcOffset,dst,dstOffset,size)}function _wgpuCommandEncoderCopyTextureToBuffer(encoderPtr,srcPtr,dstPtr,copySizePtr){encoderPtr>>>=0;srcPtr>>>=0;dstPtr>>>=0;copySizePtr>>>=0;var commandEncoder=WebGPU.getJsObject(encoderPtr);var copySize=WebGPU.makeExtent3D(copySizePtr);commandEncoder.copyTextureToBuffer(WebGPU.makeTexelCopyTextureInfo(srcPtr),WebGPU.makeTexelCopyBufferInfo(dstPtr),copySize)}function _wgpuCommandEncoderFinish(encoderPtr,descriptor){encoderPtr>>>=0;descriptor>>>=0;var commandEncoder=WebGPU.getJsObject(encoderPtr);var ptr=_emwgpuCreateCommandBuffer(0);WebGPU.Internals.jsObjectInsert(ptr,commandEncoder.finish());return ptr}function _wgpuComputePassEncoderDispatchWorkgroups(passPtr,x,y,z){passPtr>>>=0;var pass=WebGPU.getJsObject(passPtr);pass.dispatchWorkgroups(x,y,z)}function _wgpuComputePassEncoderEnd(passPtr){passPtr>>>=0;var pass=WebGPU.getJsObject(passPtr);pass.end()}function _wgpuComputePassEncoderSetBindGroup(passPtr,groupIndex,groupPtr,dynamicOffsetCount,dynamicOffsetsPtr){passPtr>>>=0;groupPtr>>>=0;dynamicOffsetCount>>>=0;dynamicOffsetsPtr>>>=0;var pass=WebGPU.getJsObject(passPtr);var group=WebGPU.getJsObject(groupPtr);if(dynamicOffsetCount==0){pass.setBindGroup(groupIndex,group)}else{pass.setBindGroup(groupIndex,group,HEAPU32,dynamicOffsetsPtr>>>2,dynamicOffsetCount)}}function _wgpuComputePassEncoderSetPipeline(passPtr,pipelinePtr){passPtr>>>=0;pipelinePtr>>>=0;var pass=WebGPU.getJsObject(passPtr);var pipeline=WebGPU.getJsObject(pipelinePtr);pass.setPipeline(pipeline)}function _wgpuComputePipelineGetBindGroupLayout(pipelinePtr,groupIndex){pipelinePtr>>>=0;var pipeline=WebGPU.getJsObject(pipelinePtr);var ptr=_emwgpuCreateBindGroupLayout(0);WebGPU.Internals.jsObjectInsert(ptr,pipeline.getBindGroupLayout(groupIndex));return ptr}var readI53FromI64=ptr=>HEAPU32[ptr>>>2>>>0]+HEAP32[ptr+4>>>2>>>0]*4294967296;function _wgpuDeviceCreateBindGroup(devicePtr,descriptor){devicePtr>>>=0;descriptor>>>=0;function makeEntry(entryPtr){var bufferPtr=HEAPU32[entryPtr+8>>>2>>>0];var samplerPtr=HEAPU32[entryPtr+32>>>2>>>0];var textureViewPtr=HEAPU32[entryPtr+36>>>2>>>0];var binding=HEAPU32[entryPtr+4>>>2>>>0];if(bufferPtr){var size=readI53FromI64(entryPtr+24);if(size==-1)size=undefined;return{binding,resource:{buffer:WebGPU.getJsObject(bufferPtr),offset:HEAPU32[entryPtr+4+16>>>2>>>0]*4294967296+HEAPU32[entryPtr+16>>>2>>>0],size}}}else if(samplerPtr){return{binding,resource:WebGPU.getJsObject(samplerPtr)}}else{return{binding,resource:WebGPU.getJsObject(textureViewPtr)}}}function makeEntries(count,entriesPtrs){var entries=[];for(var i=0;i<count;++i){entries.push(makeEntry(entriesPtrs+40*i))}return entries}var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),layout:WebGPU.getJsObject(HEAPU32[descriptor+12>>>2>>>0]),entries:makeEntries(HEAPU32[descriptor+16>>>2>>>0],HEAPU32[descriptor+20>>>2>>>0])};var device=WebGPU.getJsObject(devicePtr);var ptr=_emwgpuCreateBindGroup(0);WebGPU.Internals.jsObjectInsert(ptr,device.createBindGroup(desc));return ptr}function _wgpuDeviceCreateCommandEncoder(devicePtr,descriptor){devicePtr>>>=0;descriptor>>>=0;var desc;if(descriptor){desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4)}}var device=WebGPU.getJsObject(devicePtr);var ptr=_emwgpuCreateCommandEncoder(0);WebGPU.Internals.jsObjectInsert(ptr,device.createCommandEncoder(desc));return ptr}function _wgpuDeviceCreateComputePipeline(devicePtr,descriptor){devicePtr>>>=0;descriptor>>>=0;var desc=WebGPU.makeComputePipelineDesc(descriptor);var device=WebGPU.getJsObject(devicePtr);var ptr=_emwgpuCreateComputePipeline(0);WebGPU.Internals.jsObjectInsert(ptr,device.createComputePipeline(desc));return ptr}function _wgpuDeviceCreateTexture(devicePtr,descriptor){devicePtr>>>=0;descriptor>>>=0;var desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),size:WebGPU.makeExtent3D(descriptor+28),mipLevelCount:HEAPU32[descriptor+44>>>2>>>0],sampleCount:HEAPU32[descriptor+48>>>2>>>0],dimension:WebGPU.TextureDimension[HEAPU32[descriptor+24>>>2>>>0]],format:WebGPU.TextureFormat[HEAPU32[descriptor+40>>>2>>>0]],usage:HEAPU32[descriptor+16>>>2>>>0]};var viewFormatCount=HEAPU32[descriptor+52>>>2>>>0];if(viewFormatCount){var viewFormatsPtr=HEAPU32[descriptor+56>>>2>>>0];desc["viewFormats"]=Array.from(HEAP32.subarray(viewFormatsPtr>>>2>>>0,viewFormatsPtr+viewFormatCount*4>>>2>>>0),format=>WebGPU.TextureFormat[format])}var device=WebGPU.getJsObject(devicePtr);var ptr=_emwgpuCreateTexture(0);WebGPU.Internals.jsObjectInsert(ptr,device.createTexture(desc));return ptr}var _wgpuQueueSubmit=function(queuePtr,commandCount,commands){queuePtr>>>=0;commandCount>>>=0;commands>>>=0;var queue=WebGPU.getJsObject(queuePtr);var cmds=Array.from(HEAP32.subarray(commands>>>2>>>0,commands+commandCount*4>>>2>>>0),id=>WebGPU.getJsObject(id));queue.submit(cmds)};function _wgpuQueueWriteBuffer(queuePtr,bufferPtr,bufferOffset,data,size){queuePtr>>>=0;bufferPtr>>>=0;bufferOffset=bigintToI53Checked(bufferOffset);data>>>=0;size>>>=0;var queue=WebGPU.getJsObject(queuePtr);var buffer=WebGPU.getJsObject(bufferPtr);var subarray=HEAPU8.subarray(data>>>0,data+size>>>0);queue.writeBuffer(buffer,bufferOffset,subarray,0,size)}function _wgpuQueueWriteTexture(queuePtr,destinationPtr,data,dataSize,dataLayoutPtr,writeSizePtr){queuePtr>>>=0;destinationPtr>>>=0;data>>>=0;dataSize>>>=0;dataLayoutPtr>>>=0;writeSizePtr>>>=0;var queue=WebGPU.getJsObject(queuePtr);var destination=WebGPU.makeTexelCopyTextureInfo(destinationPtr);var dataLayout=WebGPU.makeTexelCopyBufferLayout(dataLayoutPtr);var writeSize=WebGPU.makeExtent3D(writeSizePtr);var subarray=HEAPU8.subarray(data>>>0,data+dataSize>>>0);queue.writeTexture(destination,subarray,dataLayout,writeSize)}function _wgpuTextureCreateView(texturePtr,descriptor){texturePtr>>>=0;descriptor>>>=0;var desc;if(descriptor){var mipLevelCount=HEAPU32[descriptor+24>>>2>>>0];var arrayLayerCount=HEAPU32[descriptor+32>>>2>>>0];desc={label:WebGPU.makeStringFromOptionalStringView(descriptor+4),format:WebGPU.TextureFormat[HEAPU32[descriptor+12>>>2>>>0]],dimension:WebGPU.TextureViewDimension[HEAPU32[descriptor+16>>>2>>>0]],baseMipLevel:HEAPU32[descriptor+20>>>2>>>0],mipLevelCount:mipLevelCount===4294967295?undefined:mipLevelCount,baseArrayLayer:HEAPU32[descriptor+28>>>2>>>0],arrayLayerCount:arrayLayerCount===4294967295?undefined:arrayLayerCount,aspect:WebGPU.TextureAspect[HEAPU32[descriptor+36>>>2>>>0]]}}var texture=WebGPU.getJsObject(texturePtr);var ptr=_emwgpuCreateTextureView(0);WebGPU.Internals.jsObjectInsert(ptr,texture.createView(desc));return ptr}function _wgpuTextureDestroy(texturePtr){texturePtr>>>=0;WebGPU.getJsObject(texturePtr).destroy()}var wasmTable;var runAndAbortIfError=func=>{try{return func()}catch(e){abort(e)}};var runtimeKeepalivePush=()=>{runtimeKeepaliveCounter+=1};var runtimeKeepalivePop=()=>{runtimeKeepaliveCounter-=1};var Asyncify={instrumentWasmImports(imports){var importPattern=/^(invoke_.*|__asyncjs__.*)$/;for(let[x,original]of Object.entries(imports)){if(typeof original=="function"){let isAsyncifyImport=original.isAsync||importPattern.test(x)}}},instrumentFunction(original){var wrapper=(...args)=>{Asyncify.exportCallStack.push(original);try{return original(...args)}finally{if(!ABORT){var top=Asyncify.exportCallStack.pop();Asyncify.maybeStopUnwind()}}};Asyncify.funcWrappers.set(original,wrapper);return wrapper},instrumentWasmExports(exports){var ret={};for(let[x,original]of Object.entries(exports)){if(typeof original=="function"){var wrapper=Asyncify.instrumentFunction(original);ret[x]=wrapper}else{ret[x]=original}}return ret},State:{Normal:0,Unwinding:1,Rewinding:2,Disabled:3},state:0,StackSize:131272,currData:null,handleSleepReturnValue:0,exportCallStack:[],callstackFuncToId:new Map,callStackIdToFunc:new Map,funcWrappers:new Map,callStackId:0,asyncPromiseHandlers:null,sleepCallbacks:[],getCallStackId(func){if(!Asyncify.callstackFuncToId.has(func)){var id=Asyncify.callStackId++;Asyncify.callstackFuncToId.set(func,id);Asyncify.callStackIdToFunc.set(id,func)}return Asyncify.callstackFuncToId.get(func)},maybeStopUnwind(){if(Asyncify.currData&&Asyncify.state===Asyncify.State.Unwinding&&Asyncify.exportCallStack.length===0){Asyncify.state=Asyncify.State.Normal;runAndAbortIfError(_asyncify_stop_unwind);if(typeof Fibers!="undefined"){Fibers.trampoline()}}},whenDone(){return new Promise((resolve,reject)=>{Asyncify.asyncPromiseHandlers={resolve,reject}})},allocateData(){var ptr=_malloc(12+Asyncify.StackSize);Asyncify.setDataHeader(ptr,ptr+12,Asyncify.StackSize);Asyncify.setDataRewindFunc(ptr);return ptr},setDataHeader(ptr,stack,stackSize){HEAPU32[ptr>>>2>>>0]=stack;HEAPU32[ptr+4>>>2>>>0]=stack+stackSize},setDataRewindFunc(ptr){var bottomOfCallStack=Asyncify.exportCallStack[0];var rewindId=Asyncify.getCallStackId(bottomOfCallStack);HEAP32[ptr+8>>>2>>>0]=rewindId},getDataRewindFunc(ptr){var id=HEAP32[ptr+8>>>2>>>0];var func=Asyncify.callStackIdToFunc.get(id);return func},doRewind(ptr){var original=Asyncify.getDataRewindFunc(ptr);var func=Asyncify.funcWrappers.get(original);return func()},handleSleep(startAsync){if(ABORT)return;if(Asyncify.state===Asyncify.State.Normal){var reachedCallback=false;var reachedAfterCallback=false;startAsync((handleSleepReturnValue=0)=>{if(ABORT)return;Asyncify.handleSleepReturnValue=handleSleepReturnValue;reachedCallback=true;if(!reachedAfterCallback){return}Asyncify.state=Asyncify.State.Rewinding;runAndAbortIfError(()=>_asyncify_start_rewind(Asyncify.currData));if(typeof MainLoop!="undefined"&&MainLoop.func){MainLoop.resume()}var asyncWasmReturnValue,isError=false;try{asyncWasmReturnValue=Asyncify.doRewind(Asyncify.currData)}catch(err){asyncWasmReturnValue=err;isError=true}var handled=false;if(!Asyncify.currData){var asyncPromiseHandlers=Asyncify.asyncPromiseHandlers;if(asyncPromiseHandlers){Asyncify.asyncPromiseHandlers=null;(isError?asyncPromiseHandlers.reject:asyncPromiseHandlers.resolve)(asyncWasmReturnValue);handled=true}}if(isError&&!handled){throw asyncWasmReturnValue}});reachedAfterCallback=true;if(!reachedCallback){Asyncify.state=Asyncify.State.Unwinding;Asyncify.currData=Asyncify.allocateData();if(typeof MainLoop!="undefined"&&MainLoop.func){MainLoop.pause()}runAndAbortIfError(()=>_asyncify_start_unwind(Asyncify.currData))}}else if(Asyncify.state===Asyncify.State.Rewinding){Asyncify.state=Asyncify.State.Normal;runAndAbortIfError(_asyncify_stop_rewind);_free(Asyncify.currData);Asyncify.currData=null;Asyncify.sleepCallbacks.forEach(callUserCallback)}else{abort(`invalid state: ${Asyncify.state}`)}return Asyncify.handleSleepReturnValue},handleAsync:startAsync=>Asyncify.handleSleep(wakeUp=>{startAsync().then(wakeUp)})};var getCFunc=ident=>{var func=Module["_"+ident];return func};var writeArrayToMemory=(array,buffer)=>{HEAP8.set(array,buffer>>>0)};var ccall=(ident,returnType,argTypes,args,opts)=>{var toC={string:str=>{var ret=0;if(str!==null&&str!==undefined&&str!==0){ret=stringToUTF8OnStack(str)}return ret},array:arr=>{var ret=stackAlloc(arr.length);writeArrayToMemory(arr,ret);return ret}};function convertReturnValue(ret){if(returnType==="string"){return UTF8ToString(ret)}if(returnType==="boolean")return Boolean(ret);return ret}var func=getCFunc(ident);var cArgs=[];var stack=0;if(args){for(var i=0;i<args.length;i++){var converter=toC[argTypes[i]];if(converter){if(stack===0)stack=stackSave();cArgs[i]=converter(args[i])}else{cArgs[i]=args[i]}}}var previousAsync=Asyncify.currData;var ret=func(...cArgs);function onDone(ret){runtimeKeepalivePop();if(stack!==0)stackRestore(stack);return convertReturnValue(ret)}var asyncMode=opts?.async;runtimeKeepalivePush();if(Asyncify.currData!=previousAsync){return Asyncify.whenDone().then(onDone)}ret=onDone(ret);if(asyncMode)return Promise.resolve(ret);return ret};var cwrap=(ident,returnType,argTypes,opts)=>{var numericArgs=!argTypes||argTypes.every(type=>type==="number"||type==="boolean");var numericRet=returnType!=="string";if(numericRet&&numericArgs&&!opts){return getCFunc(ident)}return(...args)=>ccall(ident,returnType,argTypes,args,opts)};FS.createPreloadedFile=FS_createPreloadedFile;FS.staticInit();MEMFS.doesNotExistError=new FS.ErrnoError(44);MEMFS.doesNotExistError.stack="<generic error, no stack>";{if(Module["noExitRuntime"])noExitRuntime=Module["noExitRuntime"];if(Module["preloadPlugins"])preloadPlugins=Module["preloadPlugins"];if(Module["print"])out=Module["print"];if(Module["printErr"])err=Module["printErr"];if(Module["wasmBinary"])wasmBinary=Module["wasmBinary"];if(Module["arguments"])arguments_=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"]}Module["ccall"]=ccall;Module["cwrap"]=cwrap;Module["setValue"]=setValue;Module["getValue"]=getValue;Module["UTF8ToString"]=UTF8ToString;Module["stringToUTF8"]=stringToUTF8;Module["lengthBytesUTF8"]=lengthBytesUTF8;var _gaussian_blur_fft,_invert_image,_threshold_image,_black_threshold_image,_kmeans,_bilateral_filter,_labels_to_svg,_image_to_svg,_malloc,_emwgpuCreateBindGroup,_emwgpuCreateBindGroupLayout,_emwgpuCreateCommandBuffer,_emwgpuCreateCommandEncoder,_emwgpuCreateComputePassEncoder,_emwgpuCreateComputePipeline,_emwgpuCreatePipelineLayout,_emwgpuCreateQuerySet,_emwgpuCreateRenderBundle,_emwgpuCreateRenderBundleEncoder,_emwgpuCreateRenderPassEncoder,_emwgpuCreateRenderPipeline,_emwgpuCreateSampler,_emwgpuCreateSurface,_emwgpuCreateTexture,_emwgpuCreateTextureView,_emwgpuCreateAdapter,_emwgpuCreateBuffer,_emwgpuCreateDevice,_emwgpuCreateQueue,_emwgpuCreateShaderModule,_emwgpuOnDeviceLostCompleted,_emwgpuOnMapAsyncCompleted,_emwgpuOnRequestAdapterCompleted,_emwgpuOnRequestDeviceCompleted,_emwgpuOnUncapturedError,_free,_memalign,_setThrew,__emscripten_tempret_set,_emscripten_stack_init,_emscripten_stack_get_end,__emscripten_stack_restore,__emscripten_stack_alloc,_emscripten_stack_get_current,___cxa_decrement_exception_refcount,___cxa_increment_exception_refcount,___cxa_can_catch,___cxa_get_exception_ptr,dynCall_vi,dynCall_viiiii,dynCall_viii,dynCall_ii,dynCall_viiii,dynCall_vii,dynCall_iii,dynCall_viji,dynCall_v,dynCall_iiiiii,dynCall_iiiiij,dynCall_iiiiid,dynCall_iiii,dynCall_viijii,dynCall_jiji,dynCall_iidiiii,dynCall_iiiiiiii,dynCall_iiiiiiiiiii,dynCall_iiiii,dynCall_jiiii,dynCall_iiiiiiiiiiiii,dynCall_fiii,dynCall_diii,dynCall_i,dynCall_viiiiiii,dynCall_iiiiiii,dynCall_iiiiiiiiiiii,dynCall_viiiiiiiiii,dynCall_viiiiiiiiiiiiiii,dynCall_iiiiiiiii,dynCall_iiiiijj,dynCall_iiiiiijj,dynCall_viiiiii,_asyncify_start_unwind,_asyncify_stop_unwind,_asyncify_start_rewind,_asyncify_stop_rewind;function assignWasmExports(wasmExports){Module["_gaussian_blur_fft"]=_gaussian_blur_fft=wasmExports["ta"];Module["_invert_image"]=_invert_image=wasmExports["ua"];Module["_threshold_image"]=_threshold_image=wasmExports["va"];Module["_black_threshold_image"]=_black_threshold_image=wasmExports["wa"];Module["_kmeans"]=_kmeans=wasmExports["xa"];Module["_bilateral_filter"]=_bilateral_filter=wasmExports["ya"];Module["_labels_to_svg"]=_labels_to_svg=wasmExports["za"];Module["_image_to_svg"]=_image_to_svg=wasmExports["Aa"];Module["_malloc"]=_malloc=wasmExports["Ca"];_emwgpuCreateBindGroup=wasmExports["Da"];_emwgpuCreateBindGroupLayout=wasmExports["Ea"];_emwgpuCreateCommandBuffer=wasmExports["Fa"];_emwgpuCreateCommandEncoder=wasmExports["Ga"];_emwgpuCreateComputePassEncoder=wasmExports["Ha"];_emwgpuCreateComputePipeline=wasmExports["Ia"];_emwgpuCreatePipelineLayout=wasmExports["Ja"];_emwgpuCreateQuerySet=wasmExports["Ka"];_emwgpuCreateRenderBundle=wasmExports["La"];_emwgpuCreateRenderBundleEncoder=wasmExports["Ma"];_emwgpuCreateRenderPassEncoder=wasmExports["Na"];_emwgpuCreateRenderPipeline=wasmExports["Oa"];_emwgpuCreateSampler=wasmExports["Pa"];_emwgpuCreateSurface=wasmExports["Qa"];_emwgpuCreateTexture=wasmExports["Ra"];_emwgpuCreateTextureView=wasmExports["Sa"];_emwgpuCreateAdapter=wasmExports["Ta"];_emwgpuCreateBuffer=wasmExports["Ua"];_emwgpuCreateDevice=wasmExports["Va"];_emwgpuCreateQueue=wasmExports["Wa"];_emwgpuCreateShaderModule=wasmExports["Xa"];_emwgpuOnDeviceLostCompleted=wasmExports["Ya"];_emwgpuOnMapAsyncCompleted=wasmExports["Za"];_emwgpuOnRequestAdapterCompleted=wasmExports["_a"];_emwgpuOnRequestDeviceCompleted=wasmExports["$a"];_emwgpuOnUncapturedError=wasmExports["ab"];Module["_free"]=_free=wasmExports["bb"];_memalign=wasmExports["cb"];_setThrew=wasmExports["db"];__emscripten_tempret_set=wasmExports["eb"];_emscripten_stack_init=wasmExports["fb"];_emscripten_stack_get_end=wasmExports["gb"];__emscripten_stack_restore=wasmExports["hb"];__emscripten_stack_alloc=wasmExports["ib"];_emscripten_stack_get_current=wasmExports["jb"];___cxa_decrement_exception_refcount=wasmExports["kb"];___cxa_increment_exception_refcount=wasmExports["lb"];___cxa_can_catch=wasmExports["mb"];___cxa_get_exception_ptr=wasmExports["nb"];dynCalls["vi"]=dynCall_vi=wasmExports["ob"];dynCalls["viiiii"]=dynCall_viiiii=wasmExports["pb"];dynCalls["viii"]=dynCall_viii=wasmExports["qb"];dynCalls["ii"]=dynCall_ii=wasmExports["rb"];dynCalls["viiii"]=dynCall_viiii=wasmExports["sb"];dynCalls["vii"]=dynCall_vii=wasmExports["tb"];dynCalls["iii"]=dynCall_iii=wasmExports["ub"];dynCalls["viji"]=dynCall_viji=wasmExports["vb"];dynCalls["v"]=dynCall_v=wasmExports["wb"];dynCalls["iiiiii"]=dynCall_iiiiii=wasmExports["xb"];dynCalls["iiiiij"]=dynCall_iiiiij=wasmExports["yb"];dynCalls["iiiiid"]=dynCall_iiiiid=wasmExports["zb"];dynCalls["iiii"]=dynCall_iiii=wasmExports["Ab"];dynCalls["viijii"]=dynCall_viijii=wasmExports["Bb"];dynCalls["jiji"]=dynCall_jiji=wasmExports["Cb"];dynCalls["iidiiii"]=dynCall_iidiiii=wasmExports["Db"];dynCalls["iiiiiiii"]=dynCall_iiiiiiii=wasmExports["Eb"];dynCalls["iiiiiiiiiii"]=dynCall_iiiiiiiiiii=wasmExports["Fb"];dynCalls["iiiii"]=dynCall_iiiii=wasmExports["Gb"];dynCalls["jiiii"]=dynCall_jiiii=wasmExports["Hb"];dynCalls["iiiiiiiiiiiii"]=dynCall_iiiiiiiiiiiii=wasmExports["Ib"];dynCalls["fiii"]=dynCall_fiii=wasmExports["Jb"];dynCalls["diii"]=dynCall_diii=wasmExports["Kb"];dynCalls["i"]=dynCall_i=wasmExports["Lb"];dynCalls["viiiiiii"]=dynCall_viiiiiii=wasmExports["Mb"];dynCalls["iiiiiii"]=dynCall_iiiiiii=wasmExports["Nb"];dynCalls["iiiiiiiiiiii"]=dynCall_iiiiiiiiiiii=wasmExports["Ob"];dynCalls["viiiiiiiiii"]=dynCall_viiiiiiiiii=wasmExports["Pb"];dynCalls["viiiiiiiiiiiiiii"]=dynCall_viiiiiiiiiiiiiii=wasmExports["Qb"];dynCalls["iiiiiiiii"]=dynCall_iiiiiiiii=wasmExports["Rb"];dynCalls["iiiiijj"]=dynCall_iiiiijj=wasmExports["Sb"];dynCalls["iiiiiijj"]=dynCall_iiiiiijj=wasmExports["Tb"];dynCalls["viiiiii"]=dynCall_viiiiii=wasmExports["Ub"];_asyncify_start_unwind=wasmExports["Vb"];_asyncify_stop_unwind=wasmExports["Wb"];_asyncify_start_rewind=wasmExports["Xb"];_asyncify_stop_rewind=wasmExports["Yb"]}var wasmImports={q:___cxa_begin_catch,s:___cxa_end_catch,a:___cxa_find_matching_catch_2,f:___cxa_find_matching_catch_3,ca:___cxa_rethrow,n:___cxa_throw,S:___cxa_uncaught_exceptions,d:___resumeException,ea:__abort_js,W:__tzset_js,ja:_emscripten_has_asyncify,da:_emscripten_resize_heap,r:_emscripten_sleep,ia:_emwgpuAdapterRequestDevice,oa:_emwgpuBufferDestroy,na:_emwgpuBufferGetConstMappedRange,ma:_emwgpuBufferMapAsync,la:_emwgpuBufferUnmap,i:_emwgpuDelete,ha:_emwgpuDeviceCreateBuffer,ga:_emwgpuDeviceCreateShaderModule,ka:_emwgpuDeviceDestroy,fa:_emwgpuInstanceRequestAdapter,X:_environ_get,Y:_environ_sizes_get,Z:_fd_close,_:_fd_read,$:_fd_seek,R:_fd_write,O:invoke_diii,P:invoke_fiii,j:invoke_i,b:invoke_ii,e:invoke_iii,o:invoke_iiii,h:invoke_iiiii,aa:invoke_iiiiid,v:invoke_iiiiii,w:invoke_iiiiiii,Q:invoke_iiiiiiii,J:invoke_iiiiiiiiiiii,ba:invoke_iiiiij,K:invoke_jiiii,g:invoke_v,p:invoke_vi,c:invoke_vii,k:invoke_viii,T:invoke_viiii,m:invoke_viiiiiii,F:invoke_viiiiiiiiii,I:invoke_viiiiiiiiiiiiiii,V:_random_get,qa:_wgpuAdapterGetLimits,E:_wgpuCommandEncoderBeginComputePass,U:_wgpuCommandEncoderCopyBufferToBuffer,N:_wgpuCommandEncoderCopyTextureToBuffer,M:_wgpuCommandEncoderFinish,B:_wgpuComputePassEncoderDispatchWorkgroups,A:_wgpuComputePassEncoderEnd,C:_wgpuComputePassEncoderSetBindGroup,D:_wgpuComputePassEncoderSetPipeline,y:_wgpuComputePipelineGetBindGroupLayout,x:_wgpuDeviceCreateBindGroup,G:_wgpuDeviceCreateCommandEncoder,pa:_wgpuDeviceCreateComputePipeline,t:_wgpuDeviceCreateTexture,L:_wgpuQueueSubmit,z:_wgpuQueueWriteBuffer,H:_wgpuQueueWriteTexture,l:_wgpuTextureCreateView,u:_wgpuTextureDestroy};var wasmExports=await createWasm();function invoke_iii(index,a1,a2){var sp=stackSave();try{return dynCall_iii(index,a1,a2)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_ii(index,a1){var sp=stackSave();try{return dynCall_ii(index,a1)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_vii(index,a1,a2){var sp=stackSave();try{dynCall_vii(index,a1,a2)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_vi(index,a1){var sp=stackSave();try{dynCall_vi(index,a1)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_v(index){var sp=stackSave();try{dynCall_v(index)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiiii(index,a1,a2,a3,a4,a5,a6){var sp=stackSave();try{return dynCall_iiiiiii(index,a1,a2,a3,a4,a5,a6)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiii(index,a1,a2,a3){var sp=stackSave();try{return dynCall_iiii(index,a1,a2,a3)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_viiii(index,a1,a2,a3,a4){var sp=stackSave();try{dynCall_viiii(index,a1,a2,a3,a4)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiii(index,a1,a2,a3,a4,a5){var sp=stackSave();try{return dynCall_iiiiii(index,a1,a2,a3,a4,a5)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiij(index,a1,a2,a3,a4,a5){var sp=stackSave();try{return dynCall_iiiiij(index,a1,a2,a3,a4,a5)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiid(index,a1,a2,a3,a4,a5){var sp=stackSave();try{return dynCall_iiiiid(index,a1,a2,a3,a4,a5)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_viii(index,a1,a2,a3){var sp=stackSave();try{dynCall_viii(index,a1,a2,a3)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiiiii(index,a1,a2,a3,a4,a5,a6,a7){var sp=stackSave();try{return dynCall_iiiiiiii(index,a1,a2,a3,a4,a5,a6,a7)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiii(index,a1,a2,a3,a4){var sp=stackSave();try{return dynCall_iiiii(index,a1,a2,a3,a4)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_jiiii(index,a1,a2,a3,a4){var sp=stackSave();try{return dynCall_jiiii(index,a1,a2,a3,a4)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0);return 0n}}function invoke_fiii(index,a1,a2,a3){var sp=stackSave();try{return dynCall_fiii(index,a1,a2,a3)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_diii(index,a1,a2,a3){var sp=stackSave();try{return dynCall_diii(index,a1,a2,a3)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_i(index){var sp=stackSave();try{return dynCall_i(index)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_viiiiiii(index,a1,a2,a3,a4,a5,a6,a7){var sp=stackSave();try{dynCall_viiiiiii(index,a1,a2,a3,a4,a5,a6,a7)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_iiiiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11){var sp=stackSave();try{return dynCall_iiiiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_viiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10){var sp=stackSave();try{dynCall_viiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function invoke_viiiiiiiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15){var sp=stackSave();try{dynCall_viiiiiiiiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15)}catch(e){stackRestore(sp);if(e!==e+0)throw e;_setThrew(1,0)}}function applySignatureConversions(wasmExports){wasmExports=Object.assign({},wasmExports);var makeWrapper_pp=f=>a0=>f(a0)>>>0;var makeWrapper_ppp=f=>(a0,a1)=>f(a0,a1)>>>0;var makeWrapper_p=f=>()=>f()>>>0;wasmExports["Ca"]=makeWrapper_pp(wasmExports["Ca"]);wasmExports["cb"]=makeWrapper_ppp(wasmExports["cb"]);wasmExports["emscripten_stack_get_base"]=makeWrapper_p(wasmExports["emscripten_stack_get_base"]);wasmExports["gb"]=makeWrapper_p(wasmExports["gb"]);wasmExports["ib"]=makeWrapper_pp(wasmExports["ib"]);wasmExports["jb"]=makeWrapper_p(wasmExports["jb"]);wasmExports["nb"]=makeWrapper_pp(wasmExports["nb"]);return wasmExports}function stackCheckInit(){_emscripten_stack_init();writeStackCookie()}function run(){if(runDependencies>0){dependenciesFulfilled=run;return}stackCheckInit();preRun();if(runDependencies>0){dependenciesFulfilled=run;return}function doRun(){Module["calledRun"]=true;if(ABORT)return;initRuntime();readyPromiseResolve?.(Module);Module["onRuntimeInitialized"]?.();postRun()}if(Module["setStatus"]){Module["setStatus"]("Running...");setTimeout(()=>{setTimeout(()=>Module["setStatus"](""),1);doRun()},1)}else{doRun()}checkStackCookie()}function preInit(){if(Module["preInit"]){if(typeof Module["preInit"]=="function")Module["preInit"]=[Module["preInit"]];while(Module["preInit"].length>0){Module["preInit"].shift()()}}}preInit();run();if(runtimeInitialized){moduleRtn=Module}else{moduleRtn=new Promise((resolve,reject)=>{readyPromiseResolve=resolve;readyPromiseReject=reject})}
8
+
9
+
10
+ return moduleRtn;
11
+ }
12
+ );
13
+ })();
14
+ export default createImg2NumModule;
Binary file
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Convenience image conversion utility to ensure type compatibility with the library.
4
+ *
5
+ * @file Convenience utility function.
6
+ *
7
+ * @module image-utils
8
+ * @license MIT
9
+ * @copyright Ryan Millard 2026
10
+ * @author Ryan Millard
11
+ * @since 0.0.0
12
+ *
13
+ * @exports imageToUint8ClampedArray
14
+ */
15
+
16
+ /**
17
+ * @summary Convert an image file into a `Uint8ClampedArray` of pixel data (RGBA).
18
+ *
19
+ * @function imageToUint8ClampedArray
20
+ * @async
21
+ * @description
22
+ * Reads an image file (PNG, JPEG, etc.) and returns its pixel data as a `Uint8ClampedArray`.
23
+ * Each pixel consists of four consecutive values: red, green, blue, and alpha (RGBA).
24
+ * Also returns the image's original width and height. Useful for canvas operations,
25
+ * image processing, WebGL textures, or computer vision tasks.
26
+ *
27
+ *
28
+ * @param {File} file - The image file to process. Must be a valid `File` object, e.g., from an `<input type="file">` element.
29
+ *
30
+ * @returns {Promise<{pixels: Uint8ClampedArray, width: number, height: number}>}
31
+ * A Promise resolving to an object containing:
32
+ * - `pixels`: A `Uint8ClampedArray` of RGBA pixel values.
33
+ * - `width`: Width of the image in pixels.
34
+ * - `height`: Height of the image in pixels.
35
+ *
36
+ * @throws {Error} Will not throw in current implementation, but could reject if the image fails to load.
37
+ *
38
+ * @example
39
+ * const fileInput = document.querySelector("#fileInput");
40
+ * fileInput.addEventListener("change", async (event) => {
41
+ * const file = event.target.files[0];
42
+ * const { pixels, width, height } = await imageToUint8ClampedArray(file);
43
+ * console.log("Width:", width, "Height:", height);
44
+ * console.log("Pixels:", pixels);
45
+ * });
46
+ *
47
+ * @todo Add error handling for invalid or corrupt image files.
48
+ * @variation Standard image file input
49
+ */
50
+ export function imageToUint8ClampedArray(file) {
51
+ return new Promise((resolve) => {
52
+ const img = new Image();
53
+ img.onload = () => {
54
+ const canvas = document.createElement("canvas");
55
+ canvas.width = img.width;
56
+ canvas.height = img.height;
57
+
58
+ const ctx = canvas.getContext("2d");
59
+ ctx.drawImage(img, 0, 0);
60
+ const { data } = ctx.getImageData(0, 0, img.width, img.height); // RGBA
61
+
62
+ resolve({ pixels: data, width: img.width, height: img.height });
63
+ };
64
+ img.src = URL.createObjectURL(file);
65
+ });
66
+ }
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Public entry point for the WASM image library
2
+
3
+ // Convenience
4
+ export * from "./imageToUint8ClampedArray.js";
5
+
6
+ // High-level image operations
7
+ export * from "./safeWasmWrappers.js";
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "img2num",
3
+ "version": "0.0.0",
4
+ "description": "Img2Num is a raster vectorization library - it converts images to SVGs",
5
+ "keywords": [
6
+ "img2num",
7
+ "computer-vision",
8
+ "computer-vision",
9
+ "vectorization",
10
+ "image-to-svg"
11
+ ],
12
+ "homepage": "https://github.com/Ryan-Millard/Img2Num#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/Ryan-Millard/Img2Num/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Ryan-Millard/Img2Num.git"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Ryan-Millard",
22
+ "type": "module",
23
+ "main": "./index.js",
24
+ "engines": {
25
+ "node": ">=14"
26
+ },
27
+ "files": [
28
+ "build-wasm/index.js",
29
+ "build-wasm/index.wasm",
30
+ "index.js",
31
+ "safeWasmWrappers.js",
32
+ "wasmClient.js",
33
+ "wasmWorker.js",
34
+ "imageToUint8ClampedArray.js"
35
+ ],
36
+ "dependencies": {},
37
+ "scripts": {
38
+ "test": "echo \"Error: no test specified\" && exit 1"
39
+ }
40
+ }
@@ -0,0 +1,240 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * High-level image operations exposed via WASM.
4
+ *
5
+ * The exports defined here abstract away the manual memory management required
6
+ * when importing raw WASM functions, making them more JavaScript-friendly.
7
+ *
8
+ * @file Safely wraps unsafe WASM (C++) function calls.
9
+ *
10
+ * @module image-wasm
11
+ * @license MIT
12
+ * @copyright Ryan Millard 2026
13
+ * @author Ryan Millard
14
+ * @since 0.0.0
15
+ * @description This module provides high-level image processing functions using WASM.
16
+ * Each function handles memory management and exposes a JavaScript-friendly API.
17
+ */
18
+
19
+ import { initWasmWorker, callWasm } from "./wasmClient.js";
20
+
21
+ // Ensure worker is ready as soon as this module is imported
22
+ initWasmWorker();
23
+
24
+ /**
25
+ * @summary Apply a Gaussian blur to an image using FFT in WASM.
26
+ *
27
+ * @description
28
+ * Takes a Uint8ClampedArray and its dimensions and applies a Gaussian blur on the Uint8ClampedArray image.
29
+ * The `sigma_pixels` parameter determines the blur radius and has a dynamic default value equal to 5% of the image's width.
30
+ * Useful for denoising images by applying a low-pass filter. Sped up by a 2-D FFT.
31
+ *
32
+ * @async
33
+ * @function gaussianBlur
34
+ * @param {Object} options - The input options.
35
+ * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array).
36
+ * @param {number} options.width - The width of the image.
37
+ * @param {number} options.height - The height of the image.
38
+ * @param {number} [options.sigma_pixels=width*0.005] - Standard deviation of the Gaussian blur (default=width*0.005; 5% of width).
39
+ * @returns {Promise<Uint8ClampedArray>} The blurred image pixels.
40
+ * @throws {Error} If the WASM function fails or memory allocation fails.
41
+ * @example
42
+ * const blurred = await gaussianBlur({ pixels, width, height });
43
+ * @todo Fix FFT zero-padding bug around edges of the image.
44
+ * @variation Standard Gaussian blur using FFT
45
+ * @since 0.0.0
46
+ */
47
+ export const gaussianBlur = async ({ pixels, width, height, sigma_pixels = width * 0.005 }) => {
48
+ const result = await callWasm({
49
+ funcName: "gaussian_blur_fft",
50
+ args: { pixels, width, height, sigma_pixels },
51
+ bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }],
52
+ });
53
+ return result.output.pixels;
54
+ };
55
+
56
+ /**
57
+ * @summary Apply a bilateral filter to an image using WASM.
58
+ *
59
+ * @description
60
+ * Takes a Uint8ClampedArray and its dimensions and applies a bilateral filter on the Uint8ClampedArray image.
61
+ * The `sigma_spatial` and `sigma_range` set weights to the respective Gaussian kernels applied to spatial (x, y) and range (color) data -
62
+ * they both have recommended default values applied.
63
+ * The default `color_space` is 0, which is CIE LAB, but sRGB can be chosen by setting `color_space` = 1. CIE LAB is more
64
+ * accurate, but sRGB is slightly faster.
65
+ *
66
+ * @async
67
+ * @function bilateralFilter
68
+ * @param {Object} options - The input options.
69
+ * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array).
70
+ * @param {number} options.width - The width of the image.
71
+ * @param {number} options.height - The height of the image.
72
+ * @param {number} [options.sigma_spatial=3] - Spatial standard deviation.
73
+ * @param {number} [options.sigma_range=50] - Range (color) standard deviation.
74
+ * @param {number} [options.color_space=0] - Color space mode (0: CIE LAB; 1: sRGB).
75
+ * @returns {Promise<Uint8ClampedArray>} The filtered image pixels.
76
+ * @throws {Error} If the WASM function fails.
77
+ * @example
78
+ * const filtered = await bilateralFilter({ pixels, width, height });
79
+ * @variation Standard bilateral filter with default parameters
80
+ * @since 0.0.0
81
+ */
82
+ export const bilateralFilter = async ({ pixels, width, height, sigma_spatial = 3, sigma_range = 50, color_space = 0 }) => {
83
+ const result = await callWasm({
84
+ funcName: "bilateral_filter",
85
+ args: { pixels, width, height, sigma_spatial, sigma_range, color_space },
86
+ bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }],
87
+ });
88
+ return result.output.pixels;
89
+ };
90
+
91
+ /**
92
+ * @summary Apply a black-biased threshold filter to reduce colors in an image.
93
+ *
94
+ * @description
95
+ * Apply a simple sRGB bin-based threshold on the Uint8ClampedArray image.
96
+ * The bins in this function are determined by the `num_colors` parameter.
97
+ *
98
+ * @async
99
+ * @function blackThreshold
100
+ * @param {Object} options - The input options.
101
+ * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array).
102
+ * @param {number} options.width - The width of the image.
103
+ * @param {number} options.height - The height of the image.
104
+ * @param {number} options.num_colors - Number of colors to reduce the image to.
105
+ * @returns {Promise<Uint8ClampedArray>} The thresholded image pixels.
106
+ * @throws {Error} If the WASM function fails.
107
+ * @example
108
+ * const thresholded = await blackThreshold({ pixels, width, height, num_colors: 16 });
109
+ * @see {@link https://en.wikipedia.org/wiki/Color_quantization|Color Quantization Wiki}
110
+ * @todo Support different bias levels for black/white thresholds.
111
+ * @variation Black-biased threshold with customizable number of colors
112
+ * @since 0.0.0
113
+ */
114
+ export const blackThreshold = async ({ pixels, width, height, num_colors }) => {
115
+ const result = await callWasm({
116
+ funcName: "black_threshold_image",
117
+ args: { pixels, width, height, num_colors },
118
+ bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }],
119
+ });
120
+ return result.output.pixels;
121
+ };
122
+
123
+ /**
124
+ * @summary Cluster pixels using the K-Means algorithm in WASM.
125
+ *
126
+ * @description
127
+ * Apply a standard K-Means clustering algorithm to the input image in the specified `color_space`
128
+ * (default is 0: CIE LAB, but 1: sRGB can be use) using pre-specified maximum color and iteration counts.
129
+ * You can provide the `out_pixels` and `out_labels` arrays,
130
+ * however this is atypical in JavaScript (since it is modified in-place and you will need to allocate a sufficiently large array),
131
+ * so it is recommended to use the default arguments and returns.
132
+ *
133
+ * @async
134
+ * @function kmeans
135
+ * @param {Object} options - The input options.
136
+ * @param {Uint8ClampedArray} options.pixels - Original image pixels.
137
+ * @param {Uint8ClampedArray} [options.out_pixels=new Uint8ClampedArray(pixels.length)] - Output pixels array.
138
+ * @param {Int32Array} [options.out_labels=new Int32Array(pixels.length/4)] - Output labels array.
139
+ * @param {number} options.width - Image width.
140
+ * @param {number} options.height - Image height.
141
+ * @param {number} options.num_colors - Number of color clusters.
142
+ * @param {number} [options.max_iter=100] - Maximum number of iterations.
143
+ * @param {number} [options.color_space=0] - Color space mode.
144
+ * @returns {Promise<{pixels: Uint8ClampedArray, labels: Int32Array}>} Clustered pixels and labels.
145
+ * @throws {Error} If the WASM function fails or iterations do not converge.
146
+ * @example
147
+ * const { pixels: clusteredPixels, labels } = await kmeans({ pixels, width, height, num_colors: 8 });
148
+ * @variation K-means clustering with default color space
149
+ * @since 0.0.0
150
+ */
151
+ export const kmeans = async ({
152
+ pixels,
153
+ out_pixels = new Uint8ClampedArray(pixels.length),
154
+ out_labels = new Int32Array(pixels.length / 4),
155
+ width,
156
+ height,
157
+ num_colors,
158
+ max_iter = 100,
159
+ color_space = 0,
160
+ }) => {
161
+ const result = await callWasm({
162
+ funcName: "kmeans",
163
+ args: { pixels, out_pixels, out_labels, width, height, num_colors, max_iter, color_space },
164
+ bufferKeys: [
165
+ { key: "pixels", type: "Uint8ClampedArray" },
166
+ { key: "out_pixels", type: "Uint8ClampedArray" },
167
+ { key: "out_labels", type: "Int32Array" },
168
+ ],
169
+ });
170
+ return { pixels: result.output.out_pixels, labels: result.output.out_labels };
171
+ };
172
+
173
+ /**
174
+ * @summary Convert labeled regions to SVG contours.
175
+ *
176
+ * @description
177
+ * Convert an input image and its labeled regions into an SVG.
178
+ *
179
+ * @async
180
+ * @function findContours
181
+ * @param {Object} options - The input options.
182
+ * @param {Uint8ClampedArray} options.pixels - Original image pixels.
183
+ * @param {Int32Array} options.labels - Label array from clustering (e.g., K-Means) or segmentation.
184
+ * @param {number} options.width - Image width.
185
+ * @param {number} options.height - Image height.
186
+ * @param {number} [options.min_area=100] - Minimum area of a region to be considered a contour.
187
+ * @returns {Promise<{svg: string>} Generated SVG.
188
+ * @throws {Error} If the WASM function fails or input labels are invalid.
189
+ * @example
190
+ * const { svg } = await findContours({ pixels, labels, width, height });
191
+ * @variation Converts labeled (from a clustering algorithm, e.g. K-Means) image into an SVG.
192
+ * @since 0.0.0
193
+ */
194
+ export const findContours = async ({ pixels, labels, width, height, min_area = 100 }) => {
195
+ const result = await callWasm({
196
+ funcName: "labels_to_svg",
197
+ args: { pixels, labels, width, height, min_area },
198
+ bufferKeys: [
199
+ { key: "pixels", type: "Uint8ClampedArray" },
200
+ { key: "labels", type: "Int32Array" },
201
+ ],
202
+ returnType: "string",
203
+ });
204
+ return { svg: result.returnValue };
205
+ };
206
+
207
+ /**
208
+ * @summary Convert raster images (e.g., JPEG, PNG) to SVGs.
209
+ *
210
+ * @description
211
+ * Convert an input raster image into an SVG. A unification of `bilateralFilter`, `kmeans`, and `findContours`.
212
+ *
213
+ * @async
214
+ * @function imageToSvg
215
+ * @param {Object} options - The input options.
216
+ * @param {Uint8ClampedArray} options.pixels - Original image pixels.
217
+ * @param {number} options.width - Image width.
218
+ * @param {number} options.height - Image height.
219
+ * @param {number} [options.sigma_spatial=3] - Spatial standard deviation.
220
+ * @param {number} [options.sigma_range=50] - Range (color) standard deviation.
221
+ * @param {number} [options.num_colors=16] - Number of color clusters.
222
+ * @param {number} [options.max_iter=100] - Maximum number of iterations.
223
+ * @param {number} [options.min_area=100] - Minimum area of a region to be considered a contour.
224
+ * @param {number} [options.color_space=0] - Color space mode.
225
+ * @returns {Promise<{svg: string}>} Generated SVG.
226
+ * @throws {Error} If the WASM function fails or input labels are invalid.
227
+ * @example
228
+ * const { svg } = await findContours({ pixels, labels, width, height });
229
+ * @variation Convert a raster image (e.g., PNG, JPG) into an SVG.
230
+ * @since 0.0.0
231
+ */
232
+ export const imageToSvg = async ({ pixels, width, height, sigma_spatial = 3, sigma_range = 50, num_colors = 16, max_iter = 100, min_area = 100, color_space = 0 }) => {
233
+ const result = await callWasm({
234
+ funcName: "image_to_svg",
235
+ args: { pixels, width, height, sigma_spatial, sigma_range, num_colors, max_iter, min_area, color_space },
236
+ bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }],
237
+ returnType: "string",
238
+ });
239
+ return { svg: result.returnValue };
240
+ };
package/wasmClient.js ADDED
@@ -0,0 +1,158 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Advanced low-level interface for communicating with the WASM worker.
4
+ * Provides granular control over calling WASM functions asynchronously,
5
+ * handling memory transfers, and managing worker lifecycle.
6
+ *
7
+ * @file Manages function call requests to the WASM worker.
8
+ * @internal
9
+ *
10
+ * @module wasm-client
11
+ * @license MIT
12
+ * @copyright Ryan Millard 2026
13
+ * @author Ryan Millard
14
+ * @since 0.0.0
15
+ * @description This module provides low-level image processing functions using WASM.
16
+ * You must specify exact C++ function signatures and manage the input data carefully.
17
+ * This allows more granular control.
18
+ */
19
+
20
+ /**
21
+ * Worker instance that handles communication with the WASM module.
22
+ * @type {Worker | null}
23
+ * @private
24
+ */
25
+ let worker;
26
+ /**
27
+ * Incremental ID counter for correlating requests and responses.
28
+ * @type {number}
29
+ * @private
30
+ */
31
+ let idCounter = 0;
32
+ /**
33
+ * Maps request IDs to their corresponding promise callbacks.
34
+ * @type {Map<number, {resolve: Function, reject: Function}>}
35
+ * @private
36
+ */
37
+ const callbacks = new Map();
38
+ /**
39
+ * Flag to ensure the WASM worker is initialized only once.
40
+ * @type {boolean}
41
+ * @private
42
+ */
43
+ let initialized = false;
44
+
45
+ /**
46
+ * @summary Initialize the WASM worker.
47
+ *
48
+ * @description
49
+ * Sets up message and error handlers. Safe to call multiple times;
50
+ * subsequent calls are no-ops. After initialization, functions can be called
51
+ * via {@link callWasm}.
52
+ *
53
+ * @function initWasmWorker
54
+ *
55
+ * @example
56
+ * import { initWasmWorker } from "./wasmClient.js";
57
+ *
58
+ * initWasmWorker();
59
+ *
60
+ * @since 0.0.0
61
+ */
62
+ export function initWasmWorker() {
63
+ if (initialized) return;
64
+
65
+ worker = new Worker(new URL("./wasmWorker.js", import.meta.url), { type: "module" });
66
+
67
+ worker.onmessage = ({ data }) => {
68
+ const { id, error, output, returnValue } = data;
69
+ const cb = callbacks.get(id);
70
+ if (!cb) return;
71
+
72
+ error ? cb.reject(new Error(error)) : cb.resolve({ output, returnValue });
73
+ callbacks.delete(id);
74
+ };
75
+
76
+ worker.onerror = (event) => {
77
+ const err = new Error(event.message || "WASM worker error");
78
+ for (const [_id, cb] of callbacks) {
79
+ cb.reject(err);
80
+ }
81
+ callbacks.clear();
82
+ };
83
+
84
+ initialized = true;
85
+ }
86
+
87
+ /**
88
+ * @summary Call a function in the WASM worker.
89
+ *
90
+ * @description
91
+ * Directly send a request to the WASM worker to call the specified function,
92
+ * passing specific buffers and and arguments.
93
+ *
94
+ * @async
95
+ * @function callWasm
96
+ * @param {Object} __named_parameters - Options for the WASM call.
97
+ * @property {string} __named_parameters.funcName - The name of the WASM function to invoke.
98
+ * @property {Object} [__named_parameters.args={}] - Named arguments to pass to the WASM function.
99
+ * @property {string[]} [__named_parameters.bufferKeys=[]] - Keys of arguments that should be transferred as ArrayBuffers.
100
+ * @property {string} [__named_parameters.returnType="void"] - Expected return type.
101
+ *
102
+ * @returns {Promise<{output: any, returnValue: any}>} Resolves with the result of the WASM function call.
103
+ *
104
+ * @throws {Error} If the worker has not been initialized.
105
+ *
106
+ * @example
107
+ * import { callWasm, initWasmWorker } from "./wasmClient.js";
108
+ *
109
+ * initWasmWorker();
110
+ *
111
+ * const result = await callWasm({
112
+ * funcName: "gaussian_blur_fft",
113
+ * args: { pixels, width, height, sigma_pixels: 5 },
114
+ * bufferKeys: ["pixels"],
115
+ * returnType: "Uint8ClampedArray"
116
+ * });
117
+ * console.log(result.output);
118
+ *
119
+ * @since 0.0.0
120
+ */
121
+ export async function callWasm({ funcName, args = {}, bufferKeys = [], returnType = "void" }) {
122
+ if (!initialized) throw new Error("WASM worker not initialized. Call initWasmWorker() first.");
123
+ const id = idCounter++;
124
+ return new Promise((resolve, reject) => {
125
+ callbacks.set(id, { resolve, reject });
126
+ worker.postMessage({ id, funcName, args, bufferKeys, returnType }); // <-- send it
127
+ });
128
+ }
129
+
130
+ /**
131
+ * @summary Terminate the WASM worker.
132
+ *
133
+ * @description
134
+ * Terminate the WASM worker used to call WASM functions.
135
+ * Rejects any pending calls before termination and resets internal state
136
+ * so the worker can be reinitialized later.
137
+ * The worker **must be reinitialized** if it is to be used again.
138
+ *
139
+ * @function terminateWasmWorker
140
+ *
141
+ * @example
142
+ * import { terminateWasmWorker } from "./wasmClient.js";
143
+ *
144
+ * terminateWasmWorker();
145
+ *
146
+ * @since 0.0.0
147
+ */
148
+ export function terminateWasmWorker() {
149
+ if (!worker) return;
150
+ worker.terminate();
151
+ // Reject any pending calls before clearing
152
+ for (const [_id, cb] of callbacks) {
153
+ cb.reject(new Error("WASM worker terminated"));
154
+ }
155
+ worker = null;
156
+ callbacks.clear();
157
+ initialized = false;
158
+ }
package/wasmWorker.js ADDED
@@ -0,0 +1,217 @@
1
+ /**
2
+ * @file wasmWorker.js
3
+ * @description
4
+ * Worker for calling WASM functions (Img2Num module) with proper memory handling.
5
+ *
6
+ * Notes:
7
+ * - WASM exposes its linear memory via typed array views such as HEAP32 (Int32) or HEAPU8 (byte).
8
+ * - When adding a new TypedArray type:
9
+ * 1. Add the corresponding HEAP view to EXPORTED_RUNTIME_METHODS in the Emscripten CMakeLists.txt.
10
+ * 2. Allocate memory with `_malloc`.
11
+ * 3. Copy data into the appropriate HEAP view. HEAP views are important because they allow the raw data to be read correctly.
12
+ * 4. After the call, read data back from the same HEAP view.
13
+ * 5. Free the allocated memory. This is important! We are using C-style memory since the code interfaces with the C ABI, so there is no GC.
14
+ * - This ensures JavaScript arrays correctly map to WASM memory.
15
+ *
16
+ * Important:
17
+ * - The `args` object passed to all convenience wrapper functions must match the order
18
+ * of the C++ function parameters exactly. For example:
19
+ * ```js
20
+ * args = { a: 1, b: 2 };
21
+ * ```
22
+ * corresponds to
23
+ * ```cpp
24
+ * int add(int a, int b);
25
+ * ```
26
+ *
27
+ * - Async functions:
28
+ * WebGPU operations inside WASM can be asynchronous. Use Emscripten Asyncify
29
+ * (via `ccall` with `{ async: true }`) to properly pause and resume execution.
30
+ *
31
+ * @example
32
+ * self.postMessage({
33
+ * id: 1,
34
+ * funcName: "bilateral_filter_gpu",
35
+ * args: { input: myArray },
36
+ * bufferKeys: [{ key: "input", type: "Uint8Array" }],
37
+ * returnType: "void"
38
+ * });
39
+ */
40
+
41
+ import createImg2NumModule from "./build-wasm/index.js";
42
+
43
+ let wasmModule;
44
+
45
+ /**
46
+ * Promise that resolves when WASM module is ready.
47
+ * @type {Promise<void>}
48
+ */
49
+ const readyPromise = createImg2NumModule().then((mod) => {
50
+ wasmModule = mod;
51
+ });
52
+
53
+ // --------------------
54
+ // WASM Type Handlers
55
+ // --------------------
56
+ /**
57
+ * Handlers for allocating, reading, and freeing different WASM types.
58
+ * @type {Record<string, {alloc: Function, read: Function}>}
59
+ */
60
+ const WASM_TYPES = {
61
+ void: {
62
+ alloc: () => null,
63
+ read: () => undefined,
64
+ },
65
+
66
+ Int32Array: {
67
+ /**
68
+ * Allocate an Int32Array in WASM memory.
69
+ * @param {Int32Array} arr
70
+ * @returns {number} Pointer to allocated memory
71
+ */
72
+ alloc: (arr) => {
73
+ const ptr = wasmModule._malloc(arr.byteLength);
74
+ wasmModule.HEAP32.set(arr, ptr >> 2);
75
+ return ptr;
76
+ },
77
+
78
+ /**
79
+ * Read an Int32Array from WASM memory.
80
+ * @param {number} ptr
81
+ * @param {number} len
82
+ * @returns {Int32Array}
83
+ */
84
+ read: (ptr, len) => new Int32Array(wasmModule.HEAP32.buffer, ptr, len).slice(),
85
+ },
86
+
87
+ Uint8Array: {
88
+ alloc: (arr) => {
89
+ const ptr = wasmModule._malloc(arr.byteLength);
90
+ wasmModule.HEAPU8.set(arr, ptr);
91
+ return ptr;
92
+ },
93
+ read: (ptr, len) => wasmModule.HEAPU8.slice(ptr, ptr + len),
94
+ },
95
+
96
+ Uint8ClampedArray: {
97
+ alloc: (arr) => {
98
+ const ptr = wasmModule._malloc(arr.byteLength);
99
+ wasmModule.HEAPU8.set(arr, ptr);
100
+ return ptr;
101
+ },
102
+ read: (ptr, len) => new Uint8ClampedArray(wasmModule.HEAPU8.slice(ptr, ptr + len)),
103
+ },
104
+
105
+ string: {
106
+ /**
107
+ * Allocate a string in WASM memory.
108
+ * @param {string} str
109
+ * @returns {number} Pointer to allocated memory
110
+ */
111
+ alloc: (str) => {
112
+ const len = wasmModule.lengthBytesUTF8(str) + 1;
113
+ const ptr = wasmModule._malloc(len);
114
+ wasmModule.stringToUTF8(str, ptr, len);
115
+ return ptr;
116
+ },
117
+
118
+ /**
119
+ * Read a string from WASM memory.
120
+ * @param {number} ptr
121
+ * @returns {string|null}
122
+ */
123
+ read: (ptr) => (ptr ? wasmModule.UTF8ToString(ptr) : null),
124
+ },
125
+ };
126
+
127
+ // --------------------
128
+ // Internal helper
129
+ // --------------------
130
+ /**
131
+ * Call a WASM function via ccall, handling asyncify automatically.
132
+ * @param {string} funcName
133
+ * @param {Map<string, number>} argsMap - Map of argument names to WASM pointers or numbers
134
+ * @param {string} returnType - WASM return type (e.g., 'void', 'Int32Array', 'string')
135
+ * @returns {Promise<number>} Result pointer or numeric return value
136
+ */
137
+ async function callWasm(funcName, argsMap, returnType) {
138
+ const argTypes = Array(argsMap.size).fill("number");
139
+ const retType = returnType !== "void" ? "number" : null;
140
+
141
+ return await wasmModule.ccall(funcName, retType, argTypes, [...argsMap.values()], { async: true });
142
+ }
143
+
144
+ // --------------------
145
+ // Worker message handler
146
+ // --------------------
147
+ /**
148
+ * Handle messages from main thread.
149
+ * Expects `data` to contain:
150
+ * - id: unique message ID
151
+ * - funcName: WASM export to call
152
+ * - args: object of input arguments to WASM export
153
+ * - bufferKeys: array of {key, type} defining memory buffers for WASM export args - JS doesn't have pointers, so we must do this
154
+ * - returnType: expected return type of the WASM export
155
+ * @param {MessageEvent} event
156
+ */
157
+ self.onmessage = async ({ data }) => {
158
+ await readyPromise;
159
+
160
+ const { id, funcName, args, bufferKeys, returnType } = data;
161
+
162
+ // These are freed in the finally block
163
+ const pointers = new Map();
164
+
165
+ try {
166
+ // -------- Validation --------
167
+ if (!funcName) throw new Error("Missing funcName");
168
+ if (!args) throw new Error("Missing args");
169
+ if (!bufferKeys) throw new Error("Missing bufferKeys");
170
+ if (!returnType) throw new Error("Missing returnType");
171
+
172
+ const argsMap = new Map(Object.entries(args));
173
+
174
+ // -------- Allocate buffers --------
175
+ for (const { key, type } of bufferKeys) {
176
+ const handler = WASM_TYPES[type];
177
+ if (!handler) throw new Error(`Unsupported type: ${type}`);
178
+
179
+ const val = argsMap.get(key);
180
+ const ptr = handler.alloc(val);
181
+
182
+ pointers.set(key, { ptr, type, length: val?.length });
183
+ argsMap.set(key, ptr);
184
+ }
185
+
186
+ // -------- Call WASM --------
187
+ let result = await callWasm(funcName, argsMap, returnType);
188
+
189
+ // -------- Read outputs --------
190
+ /** @type {Record<string, any>} */
191
+ const output = Object.create(null);
192
+ for (const { key, type } of bufferKeys) {
193
+ const { ptr, length } = pointers.get(key);
194
+ output[key] = WASM_TYPES[type].read(ptr, length);
195
+ }
196
+
197
+ // -------- Handle return --------
198
+ let returnValue = result;
199
+
200
+ if (returnType !== "void") {
201
+ returnValue = WASM_TYPES[returnType].read(result);
202
+ }
203
+
204
+ if (returnType === "string" && result) {
205
+ wasmModule._free(result);
206
+ }
207
+
208
+ self.postMessage({ id, output, returnValue });
209
+ } catch (error) {
210
+ self.postMessage({ id, error: error.message });
211
+ } finally {
212
+ // -------- Cleanup --------
213
+ for (const { ptr } of pointers.values()) {
214
+ wasmModule._free(ptr);
215
+ }
216
+ }
217
+ };