@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.fc4afc2 → 0.0.0-insiders.fc63ce7
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/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +177 -131
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +58 -2
- package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +58 -2
- package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +58 -2
- package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +212 -133
- package/node_modules/@emnapi/core/dist/emnapi-core.js +269 -163
- package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +58 -2
- package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -1
- package/node_modules/@emnapi/core/dist/emnapi-core.mjs +177 -131
- package/node_modules/@emnapi/core/package.json +2 -2
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +19 -12
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +4 -4
- package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +4 -4
- package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +4 -4
- package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +19 -12
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +4 -4
- package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +19 -12
- package/node_modules/@emnapi/runtime/dist/emnapi.js +19 -12
- package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +4 -4
- package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -1
- package/node_modules/@emnapi/runtime/dist/emnapi.mjs +19 -12
- package/node_modules/@emnapi/runtime/package.json +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +45 -29
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +57 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +57 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +57 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +45 -29
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +45 -29
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +57 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -1
- package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +45 -29
- package/node_modules/@emnapi/wasi-threads/package.json +1 -1
- package/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +264 -0
- package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +14861 -13920
- package/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +259 -0
- package/node_modules/@napi-rs/wasm-runtime/package.json +15 -13
- package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +1 -1
- package/node_modules/@napi-rs/wasm-runtime/runtime.js +8 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +6 -0
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +51 -31
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +51 -31
- package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -1
- package/node_modules/@tybys/wasm-util/dist/wasm-util.js +51 -31
- package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -1
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +12 -14
- package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +39 -17
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +12 -14
- package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +39 -17
- package/node_modules/@tybys/wasm-util/package.json +1 -1
- package/node_modules/tslib/package.json +1 -1
- package/node_modules/tslib/tslib.es6.js +10 -1
- package/node_modules/tslib/tslib.es6.mjs +10 -1
- package/node_modules/tslib/tslib.js +42 -33
- package/package.json +7 -7
- package/tailwindcss-oxide.wasi-browser.js +6 -3
- package/tailwindcss-oxide.wasi.cjs +26 -3
- package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
- package/node_modules/@napi-rs/wasm-runtime/LICENSE +0 -43
- package/node_modules/@napi-rs/wasm-runtime/dist/runtime.js +0 -12269
- package/node_modules/@napi-rs/wasm-runtime/fs-proxy.cjs +0 -193
|
@@ -1 +1 @@
|
|
|
1
|
-
const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0,t="object"==typeof process&&null!==process&&"object"==typeof process.versions&&null!==process.versions&&"string"==typeof process.versions.node;function r(e){return"function"==typeof(null==e?void 0:e.postMessage)?e.postMessage:"function"==typeof postMessage?postMessage:void 0}function s(e){return"function"==typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer||"[object SharedArrayBuffer]"===Object.prototype.toString.call(e.constructor)}function o(t){try{return t instanceof e.RuntimeError}catch(e){return!1}}function n(e,t){return{__emnapi__:{type:e,payload:t}}}function i(e){if(e){if(!s(e.buffer))throw new Error("Multithread features require shared wasm memory. Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, then create WebAssembly.Memory with `shared: true` option")}else if("undefined"==typeof SharedArrayBuffer)throw new Error("Current environment does not support SharedArrayBuffer, threads are not available!")}let a=0;class d{get nextWorkerID(){return a}constructor(e){var t;if(this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.wasmModule=null,this.wasmMemory=null,this.messageEvents=new WeakMap,!e)throw new TypeError("ThreadManager(): options is not provided");this._childThread="childThread"in e&&Boolean(e.childThread),this._childThread?(this._onCreateWorker=void 0,this._reuseWorker=!1,this._beforeLoad=void 0):(this._onCreateWorker=e.onCreateWorker,this._reuseWorker=function(e){var t;if("boolean"==typeof e)return!!e&&{size:0,strict:!1};if("number"==typeof e){if(!(e>=0))throw new RangeError("reuseWorker: size must be a non-negative integer");return{size:e,strict:!1}}if(!e)return!1;const r=null!==(t=Number(e.size))&&void 0!==t?t:0,s=Boolean(e.strict);if(!(r>0)&&s)throw new RangeError("reuseWorker: size must be set to positive integer if strict is set to true");return{size:r,strict:s}}(e.reuseWorker),this._beforeLoad=e.beforeLoad),this.printErr=null!==(t=e.printErr)&&void 0!==t?t:console.error.bind(console)}init(){this._childThread||this.initMainThread()}initMainThread(){this.preparePool()}preparePool(){if(this._reuseWorker&&this._reuseWorker.size){let e=this._reuseWorker.size;for(;e--;){const e=this.allocateUnusedWorker();t&&(e.once("message",(()=>{})),e.unref())}}}shouldPreloadWorkers(){return!this._childThread&&this._reuseWorker&&this._reuseWorker.size>0}loadWasmModuleToAllWorkers(){const e=Array(this.unusedWorkers.length);for(let r=0;r<this.unusedWorkers.length;++r){const s=this.unusedWorkers[r];t&&s.ref(),e[r]=this.loadWasmModuleToWorker(s).then((e=>(t&&s.unref(),e)),(e=>{throw t&&s.unref(),e}))}return Promise.all(e).catch((e=>{throw this.terminateAllThreads(),e}))}preloadWorkers(){return this.shouldPreloadWorkers()?this.loadWasmModuleToAllWorkers():Promise.resolve([])}setup(e,t){this.wasmModule=e,this.wasmMemory=t}markId(e){if(e.__emnapi_tid)return e.__emnapi_tid;const t=a+43;return a=(a+1)%536870869,this.pthreads[t]=e,e.__emnapi_tid=t,t}returnWorkerToPool(e){var r=e.__emnapi_tid;void 0!==r&&delete this.pthreads[r],this.unusedWorkers.push(e),this.runningWorkers.splice(this.runningWorkers.indexOf(e),1),delete e.__emnapi_tid,t&&e.unref()}loadWasmModuleToWorker(e,r){if(e.whenLoaded)return e.whenLoaded;const s=this.printErr,o=this._beforeLoad,a=this;return e.whenLoaded=new Promise(((d,h)=>{const l=r=>{if(r.__emnapi__){const s=r.__emnapi__.type,o=r.__emnapi__.payload;"loaded"===s?(e.loaded=!0,t&&!e.__emnapi_tid&&e.unref(),d(e)):"cleanup-thread"===s&&o.tid in this.pthreads&&this.cleanThread(e,o.tid)}};e.onmessage=t=>{l(t.data),this.fireMessageEvent(e,t)},e.onerror=function(t){let r="worker sent an error!";if(void 0!==e.__emnapi_tid&&(r="worker (tid = "+e.__emnapi_tid+") sent an error!"),s(r+" "+t.message),-1!==t.message.indexOf("RuntimeError")||-1!==t.message.indexOf("unreachable"))try{a.terminateAllThreads()}catch(e){}throw h(t),t},t&&(e.on("message",(function(t){var r,s;null===(s=(r=e).onmessage)||void 0===s||s.call(r,{data:t})})),e.on("error",(function(t){var r,s;null===(s=(r=e).onerror)||void 0===s||s.call(r,t)})),e.on("detachedExit",(function(){}))),"function"==typeof o&&o(e);try{e.postMessage(n("load",{wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,sab:r}))}catch(e){throw i(this.wasmMemory),e}})),e.whenLoaded}allocateUnusedWorker(){const e=this._onCreateWorker;if("function"!=typeof e)throw new TypeError("`options.onCreateWorker` is not provided");const t=e({type:"thread",name:"emnapi-pthread"});return this.unusedWorkers.push(t),t}getNewWorker(e){if(this._reuseWorker){if(0===this.unusedWorkers.length){if(this._reuseWorker.strict&&!t){return void(0,this.printErr)("Tried to spawn a new thread, but the thread pool is exhausted.\nThis might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.")}const r=this.allocateUnusedWorker();this.loadWasmModuleToWorker(r,e)}return this.unusedWorkers.pop()}const r=this.allocateUnusedWorker();return this.loadWasmModuleToWorker(r,e),this.unusedWorkers.pop()}cleanThread(e,t,r){if(!r&&this._reuseWorker)this.returnWorkerToPool(e);else{delete this.pthreads[t];const r=this.runningWorkers.indexOf(e);-1!==r&&this.runningWorkers.splice(r,1),this.terminateWorker(e),delete e.__emnapi_tid}}terminateWorker(e){var t;const r=e.__emnapi_tid;e.terminate(),null===(t=this.messageEvents.get(e))||void 0===t||t.clear(),this.messageEvents.delete(e),e.onmessage=e=>{if(e.data.__emnapi__){(0,this.printErr)('received "'+e.data.__emnapi__.type+'" command from terminated worker: '+r)}}}terminateAllThreads(){for(let e=0;e<this.runningWorkers.length;++e)this.terminateWorker(this.runningWorkers[e]);for(let e=0;e<this.unusedWorkers.length;++e)this.terminateWorker(this.unusedWorkers[e]);this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.preparePool()}addMessageEventListener(e,t){let r=this.messageEvents.get(e);return r||(r=new Set,this.messageEvents.set(e,r)),r.add(t),()=>{null==r||r.delete(t)}}fireMessageEvent(e,t){const r=this.messageEvents.get(e);if(!r)return;const s=this.printErr;r.forEach((e=>{try{e(t)}catch(e){s(e.stack)}}))}}const h=Symbol("kIsProxy");function l(e,t){if(e[h])return e;const r=e.exports,s=function(e){const t=["apply","construct","defineProperty","deleteProperty","get","getOwnPropertyDescriptor","getPrototypeOf","has","isExtensible","ownKeys","preventExtensions","set","setPrototypeOf"],r={};for(let s=0;s<t.length;s++){const o=t[s];r[o]=function(){const t=Array.prototype.slice.call(arguments,1);return t.unshift(e),Reflect[o].apply(Reflect,t)}}return r}(r),o=()=>{},n=()=>0;s.get=function(e,s,i){var a;return"memory"===s?null!==(a="function"==typeof t?t():t)&&void 0!==a?a:Reflect.get(r,s,i):"_initialize"===s?s in r?o:void 0:"_start"===s?s in r?n:void 0:Reflect.get(r,s,i)},s.has=function(e,t){return"memory"===t||Reflect.has(r,t)};const i=new Proxy(Object.create(null),s);return new Proxy(e,{get:(e,t,r)=>"exports"===t?i:t===h||Reflect.get(e,t,r)})}const c=new WeakMap;class u{constructor(s){if(!s)throw new TypeError("WASIThreads(): options is not provided");if(!s.wasi)throw new TypeError("WASIThreads(): options.wasi is not provided");c.set(this,new WeakSet);const a=s.wasi;!function(e,t){const r=c.get(e);if(r.has(t))return;const s=e,n=t.wasiImport;if(n){const e=n.proc_exit;n.proc_exit=function(t){return s.terminateAllThreads(),e.call(this,t)}}const i=t.start;"function"==typeof i&&(t.start=function(e){try{return i.call(this,e)}catch(e){throw o(e)&&s.terminateAllThreads(),e}});r.add(t)}(this,a),this.wasi=a,this.childThread="childThread"in s&&Boolean(s.childThread),this.PThread=void 0,"threadManager"in s?"function"==typeof s.threadManager?this.PThread=s.threadManager():this.PThread=s.threadManager:this.childThread||(this.PThread=new d(s),this.PThread.init());let h=!1;"waitThreadStart"in s&&(h="number"==typeof s.waitThreadStart?s.waitThreadStart:Boolean(s.waitThreadStart));const l=r(s);if(this.childThread&&"function"!=typeof l)throw new TypeError("options.postMessage is not a function");this.postMessage=l;const u=Boolean(s.wasm64),f=e=>{if(e.data.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;"spawn-thread"===t?p(r.startArg,r.errorOrTid):"terminate-all-threads"===t&&this.terminateAllThreads()}},p=(r,s)=>{var o;const a=void 0!==s;try{i(this.wasmMemory)}catch(e){if(null===(o=this.PThread)||void 0===o||o.printErr(e.stack),a){const e=new Int32Array(this.wasmMemory.buffer,s,2);return Atomics.store(e,0,1),Atomics.store(e,1,6),Atomics.notify(e,1),1}return-6}if(!a){const e=this.wasmInstance.exports.malloc;if(!(s=u?Number(e(BigInt(8))):e(8)))return-48}const d=this.wasmInstance.exports.free,c=u?e=>{d(BigInt(e))}:d,p=new Int32Array(this.wasmMemory.buffer,s,2);if(Atomics.store(p,0,0),Atomics.store(p,1,0),this.childThread){l(n("spawn-thread",{startArg:r,errorOrTid:s})),Atomics.wait(p,1,0);const e=Atomics.load(p,0),t=Atomics.load(p,1);return a?e:(c(s),e?-t:t)}const m=h||0===h;let w,y,_;m&&(w=new Int32Array(new SharedArrayBuffer(8208)),Atomics.store(w,0,0));const g=this.PThread;try{if(y=g.getNewWorker(w),!y)throw new Error("failed to get new worker");if(g.addMessageEventListener(y,f),_=g.markId(y),t&&y.ref(),y.postMessage(n("start",{tid:_,arg:r,sab:w})),m){if("number"==typeof h){if("timed-out"===Atomics.wait(w,0,0,h)){try{g.cleanThread(y,_,!0)}catch(e){}throw new Error("Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.")}}else Atomics.wait(w,0,0);if(Atomics.load(w,0)>1){try{g.cleanThread(y,_,!0)}catch(e){}throw function(t){var r,s;const o=new Int32Array(t);if(Atomics.load(o,0)<=1)return null;const n=Atomics.load(o,1),i=Atomics.load(o,2),a=Atomics.load(o,3),d=new Uint8Array(t),h=d.slice(16,16+n),l=d.slice(16+n,16+n+i),c=d.slice(16+n+i,16+n+i+a),u=(new TextDecoder).decode(h),f=(new TextDecoder).decode(l),p=(new TextDecoder).decode(c),m=new(null!==(r=globalThis[u])&&void 0!==r?r:"RuntimeError"===u&&null!==(s=e.RuntimeError)&&void 0!==s?s:Error)(f);return Object.defineProperty(m,"stack",{value:p,writable:!0,enumerable:!1,configurable:!0}),m}(w.buffer)}}}catch(e){return Atomics.store(p,0,1),Atomics.store(p,1,6),Atomics.notify(p,1),null==g||g.printErr(e.stack),a?1:(c(s),-6)}return Atomics.store(p,0,0),Atomics.store(p,1,_),Atomics.notify(p,1),g.runningWorkers.push(y),m||y.whenLoaded.catch((e=>{throw delete y.whenLoaded,g.cleanThread(y,_,!0),e})),a?0:(c(s),_)};this.threadSpawn=p}getImportObject(){return{wasi:{"thread-spawn":this.threadSpawn}}}setup(e,t,r){null!=r||(r=e.exports.memory),this.wasmInstance=e,this.wasmMemory=r,this.PThread&&this.PThread.setup(t,r)}preloadWorkers(){return this.PThread?this.PThread.preloadWorkers():Promise.resolve([])}initialize(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);const o=this.wasi;if("_start"in s&&"function"==typeof s._start)if(this.childThread){o.start(e);try{o[f(o,"kStarted")]=!1}catch(e){}}else!function(e,t){const[r,s]=f(e,["kInstance","kSetMemory"]);e[r]=t,e[s](t.exports.memory)}(o,e);else o.initialize(e);return e}start(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);return{exitCode:this.wasi.start(e),instance:e}}terminateAllThreads(){var e;this.childThread?this.postMessage(n("terminate-all-threads",{})):null===(e=this.PThread)||void 0===e||e.terminateAllThreads()}}function f(e,t){const r=Object.getOwnPropertySymbols(e),s=e=>t=>t.description?t.description===e:t.toString()===`Symbol(${e})`;return Array.isArray(t)?t.map((e=>r.filter(s(e))[0])):r.filter(s(t))[0]}class p{constructor(e){const t=r(e);if("function"!=typeof t)throw new TypeError("options.postMessage is not a function");this.postMessage=t,this.onLoad=null==e?void 0:e.onLoad,this.instance=void 0,this.messagesBeforeLoad=[]}instantiate(e){if("function"==typeof this.onLoad)return this.onLoad(e);throw new Error("ThreadMessageHandler.prototype.instantiate is not implemented")}handle(e){var t;if(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;"load"===t?this._load(r):"start"===t&&this.handleAfterLoad(e,(()=>{this._start(r)}))}}_load(e){if(void 0!==this.instance)return;let t;try{t=this.instantiate(e)}catch(t){return void this._loaded(t,null,e)}const r=t&&"then"in t?t.then:void 0;"function"==typeof r?r.call(t,(t=>{this._loaded(null,t,e)}),(t=>{this._loaded(t,null,e)})):this._loaded(null,t,e)}_start(e){if("function"!=typeof this.instance.exports.wasi_thread_start){const t=new TypeError("wasi_thread_start is not exported");throw m(e.sab,2,t),t}const t=this.postMessage,r=e.tid,s=e.arg;m(e.sab,1);try{this.instance.exports.wasi_thread_start(r,s)}catch(e){throw o(e)&&t(n("terminate-all-threads",{})),e}t(n("cleanup-thread",{tid:r}))}_loaded(e,t,r){if(e)throw m(r.sab,2,e),e;if(null==t){const e=new TypeError("onLoad should return an object");throw m(r.sab,2,e),e}const s=t.instance;if(!s){const e=new TypeError('onLoad should return an object which includes "instance"');throw m(r.sab,2,e),e}this.instance=s;(0,this.postMessage)(n("loaded",{}));const o=this.messagesBeforeLoad;this.messagesBeforeLoad=[];for(let e=0;e<o.length;e++){const t=o[e];this.handle({data:t})}}handleAfterLoad(e,t){void 0!==this.instance?t.call(this,e):this.messagesBeforeLoad.push(e.data)}}function m(e,t,r){e&&(!function(e,t,r){const s=new Int32Array(e);if(Atomics.store(s,0,t),t>1&&r){const t=r.name,o=r.message,n=r.stack,i=(new TextEncoder).encode(t),a=(new TextEncoder).encode(o),d=(new TextEncoder).encode(n);Atomics.store(s,1,i.length),Atomics.store(s,2,a.length),Atomics.store(s,3,d.length);const h=new Uint8Array(e);h.set(i,16),h.set(a,16+i.length),h.set(d,16+i.length+a.length)}}(e.buffer,t,r),Atomics.notify(e,0))}export{d as ThreadManager,p as ThreadMessageHandler,u as WASIThreads,l as createInstanceProxy,s as isSharedArrayBuffer,o as isTrapError};
|
|
1
|
+
const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0,t="object"==typeof process&&null!==process&&"object"==typeof process.versions&&null!==process.versions&&"string"==typeof process.versions.node;function r(e){return"function"==typeof(null==e?void 0:e.postMessage)?e.postMessage:"function"==typeof postMessage?postMessage:void 0}function s(e){return"function"==typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer||"[object SharedArrayBuffer]"===Object.prototype.toString.call(e)}function o(t){try{return t instanceof e.RuntimeError}catch(e){return!1}}function n(e,t){return{__emnapi__:{type:e,payload:t}}}function i(e){if(e){if(!s(e.buffer))throw new Error("Multithread features require shared wasm memory. Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, then create WebAssembly.Memory with `shared: true` option")}else if("undefined"==typeof SharedArrayBuffer)throw new Error("Current environment does not support SharedArrayBuffer, threads are not available!")}let a=0;class d{get nextWorkerID(){return a}constructor(e){var t;if(this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.wasmModule=null,this.wasmMemory=null,this.messageEvents=new WeakMap,!e)throw new TypeError("ThreadManager(): options is not provided");this._childThread="childThread"in e&&Boolean(e.childThread),this._childThread?(this._onCreateWorker=void 0,this._reuseWorker=!1,this._beforeLoad=void 0):(this._onCreateWorker=e.onCreateWorker,this._reuseWorker=function(e){var t;if("boolean"==typeof e)return!!e&&{size:0,strict:!1};if("number"==typeof e){if(!(e>=0))throw new RangeError("reuseWorker: size must be a non-negative integer");return{size:e,strict:!1}}if(!e)return!1;const r=null!==(t=Number(e.size))&&void 0!==t?t:0,s=Boolean(e.strict);if(!(r>0)&&s)throw new RangeError("reuseWorker: size must be set to positive integer if strict is set to true");return{size:r,strict:s}}(e.reuseWorker),this._beforeLoad=e.beforeLoad),this.printErr=null!==(t=e.printErr)&&void 0!==t?t:console.error.bind(console)}init(){this._childThread||this.initMainThread()}initMainThread(){this.preparePool()}preparePool(){if(this._reuseWorker&&this._reuseWorker.size){let e=this._reuseWorker.size;for(;e--;){const e=this.allocateUnusedWorker();t&&(e.once("message",()=>{}),e.unref())}}}shouldPreloadWorkers(){return!this._childThread&&this._reuseWorker&&this._reuseWorker.size>0}loadWasmModuleToAllWorkers(){const e=Array(this.unusedWorkers.length);for(let r=0;r<this.unusedWorkers.length;++r){const s=this.unusedWorkers[r];t&&s.ref(),e[r]=this.loadWasmModuleToWorker(s).then(e=>(t&&s.unref(),e),e=>{throw t&&s.unref(),e})}return Promise.all(e).catch(e=>{throw this.terminateAllThreads(),e})}preloadWorkers(){return this.shouldPreloadWorkers()?this.loadWasmModuleToAllWorkers():Promise.resolve([])}setup(e,t){this.wasmModule=e,this.wasmMemory=t}markId(e){if(e.__emnapi_tid)return e.__emnapi_tid;const t=a+43;return a=(a+1)%536870869,this.pthreads[t]=e,e.__emnapi_tid=t,t}returnWorkerToPool(e){var r=e.__emnapi_tid;void 0!==r&&delete this.pthreads[r],this.unusedWorkers.push(e),this.runningWorkers.splice(this.runningWorkers.indexOf(e),1),delete e.__emnapi_tid,t&&e.unref()}loadWasmModuleToWorker(e,r){if(e.whenLoaded)return e.whenLoaded;const s=this.printErr,o=this._beforeLoad,a=this;return e.whenLoaded=new Promise((d,h)=>{const l=r=>{if(r.__emnapi__){const s=r.__emnapi__.type,o=r.__emnapi__.payload;"loaded"===s?(e.loaded=!0,t&&!e.__emnapi_tid&&e.unref(),d(e)):"cleanup-thread"===s&&o.tid in this.pthreads&&this.cleanThread(e,o.tid)}};e.onmessage=t=>{l(t.data),this.fireMessageEvent(e,t)},e.onerror=function(t){let r="worker sent an error!";if(void 0!==e.__emnapi_tid&&(r="worker (tid = "+e.__emnapi_tid+") sent an error!"),"message"in t){if(s(r+" "+t.message),-1!==t.message.indexOf("RuntimeError")||-1!==t.message.indexOf("unreachable"))try{a.terminateAllThreads()}catch(e){}}else s(r);throw h(t),t},t&&(e.on("message",function(t){var r,s;null===(s=(r=e).onmessage)||void 0===s||s.call(r,{data:t})}),e.on("error",function(t){var r,s;null===(s=(r=e).onerror)||void 0===s||s.call(r,t)}),e.on("detachedExit",function(){})),"function"==typeof o&&o(e);try{e.postMessage(n("load",{wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,sab:r}))}catch(e){throw i(this.wasmMemory),e}}),e.whenLoaded}allocateUnusedWorker(){const e=this._onCreateWorker;if("function"!=typeof e)throw new TypeError("`options.onCreateWorker` is not provided");const t=e({type:"thread",name:"emnapi-pthread"});return this.unusedWorkers.push(t),t}getNewWorker(e){if(this._reuseWorker){if(0===this.unusedWorkers.length){if(this._reuseWorker.strict&&!t){return void(0,this.printErr)("Tried to spawn a new thread, but the thread pool is exhausted.\nThis might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.")}const r=this.allocateUnusedWorker();this.loadWasmModuleToWorker(r,e)}return this.unusedWorkers.pop()}const r=this.allocateUnusedWorker();return this.loadWasmModuleToWorker(r,e),this.unusedWorkers.pop()}cleanThread(e,t,r){if(!r&&this._reuseWorker)this.returnWorkerToPool(e);else{delete this.pthreads[t];const r=this.runningWorkers.indexOf(e);-1!==r&&this.runningWorkers.splice(r,1),this.terminateWorker(e),delete e.__emnapi_tid}}terminateWorker(e){var t;const r=e.__emnapi_tid;e.terminate(),null===(t=this.messageEvents.get(e))||void 0===t||t.clear(),this.messageEvents.delete(e),e.onmessage=e=>{if(e.data.__emnapi__){(0,this.printErr)('received "'+e.data.__emnapi__.type+'" command from terminated worker: '+r)}}}terminateAllThreads(){for(let e=0;e<this.runningWorkers.length;++e)this.terminateWorker(this.runningWorkers[e]);for(let e=0;e<this.unusedWorkers.length;++e)this.terminateWorker(this.unusedWorkers[e]);this.unusedWorkers=[],this.runningWorkers=[],this.pthreads=Object.create(null),this.preparePool()}addMessageEventListener(e,t){let r=this.messageEvents.get(e);return r||(r=new Set,this.messageEvents.set(e,r)),r.add(t),()=>{null==r||r.delete(t)}}fireMessageEvent(e,t){const r=this.messageEvents.get(e);if(!r)return;const s=this.printErr;r.forEach(e=>{try{e(t)}catch(e){s(e.stack)}})}}const h=Symbol("kIsProxy");function l(e,t){if(e[h])return e;const r=e.exports,s=function(e){const t=["apply","construct","defineProperty","deleteProperty","get","getOwnPropertyDescriptor","getPrototypeOf","has","isExtensible","ownKeys","preventExtensions","set","setPrototypeOf"],r={};for(let s=0;s<t.length;s++){const o=t[s];r[o]=function(){const t=Array.prototype.slice.call(arguments,1);return t.unshift(e),Reflect[o].apply(Reflect,t)}}return r}(r),o=()=>{},n=()=>0;s.get=function(e,s,i){var a;return"memory"===s?null!==(a="function"==typeof t?t():t)&&void 0!==a?a:Reflect.get(r,s,i):"_initialize"===s?s in r?o:void 0:"_start"===s?s in r?n:void 0:Reflect.get(r,s,i)},s.has=function(e,t){return"memory"===t||Reflect.has(r,t)};const i=new Proxy(Object.create(null),s);return new Proxy(e,{get:(e,t,r)=>"exports"===t?i:t===h||Reflect.get(e,t,r)})}const c=new WeakMap;class u{constructor(s){if(!s)throw new TypeError("WASIThreads(): options is not provided");if(!s.wasi)throw new TypeError("WASIThreads(): options.wasi is not provided");c.set(this,new WeakSet);const a=s.wasi;!function(e,t){const r=c.get(e);if(r.has(t))return;const s=e,n=t.wasiImport;if(n){const e=n.proc_exit;n.proc_exit=function(t){return s.terminateAllThreads(),e.call(this,t)}}if(!s.childThread){const e=t.start;"function"==typeof e&&(t.start=function(t){try{return e.call(this,t)}catch(e){throw o(e)&&s.terminateAllThreads(),e}})}r.add(t)}(this,a),this.wasi=a,this.childThread="childThread"in s&&Boolean(s.childThread),this.PThread=void 0,"threadManager"in s?"function"==typeof s.threadManager?this.PThread=s.threadManager():this.PThread=s.threadManager:this.childThread||(this.PThread=new d(s),this.PThread.init());let h=!1;"waitThreadStart"in s&&(h="number"==typeof s.waitThreadStart?s.waitThreadStart:Boolean(s.waitThreadStart));const l=r(s);if(this.childThread&&"function"!=typeof l)throw new TypeError("options.postMessage is not a function");this.postMessage=l;const u=Boolean(s.wasm64),f=e=>{if(e.data.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;"spawn-thread"===t?p(r.startArg,r.errorOrTid):"terminate-all-threads"===t&&this.terminateAllThreads()}},p=(r,s)=>{var o;const a=void 0!==s;try{i(this.wasmMemory)}catch(e){if(null===(o=this.PThread)||void 0===o||o.printErr(e.stack),a){const e=new Int32Array(this.wasmMemory.buffer,s,2);return Atomics.store(e,0,1),Atomics.store(e,1,6),Atomics.notify(e,1),1}return-6}if(!a){const e=this.wasmInstance.exports.malloc;if(!(s=u?Number(e(BigInt(8))):e(8)))return-48}const d=this.wasmInstance.exports.free,c=u?e=>{d(BigInt(e))}:d,p=new Int32Array(this.wasmMemory.buffer,s,2);if(Atomics.store(p,0,0),Atomics.store(p,1,0),this.childThread){l(n("spawn-thread",{startArg:r,errorOrTid:s})),Atomics.wait(p,1,0);const e=Atomics.load(p,0),t=Atomics.load(p,1);return a?e:(c(s),e?-t:t)}const m=h||0===h;let w,y,_;m&&(w=new Int32Array(new SharedArrayBuffer(8208)),Atomics.store(w,0,0));const g=this.PThread;try{if(y=g.getNewWorker(w),!y)throw new Error("failed to get new worker");if(g.addMessageEventListener(y,f),_=g.markId(y),t&&y.ref(),y.postMessage(n("start",{tid:_,arg:r,sab:w})),m){if("number"==typeof h){if("timed-out"===Atomics.wait(w,0,0,h)){try{g.cleanThread(y,_,!0)}catch(e){}throw new Error("Spawning thread timed out. Please check if the worker is created successfully and if message is handled properly in the worker.")}}else Atomics.wait(w,0,0);if(Atomics.load(w,0)>1){try{g.cleanThread(y,_,!0)}catch(e){}throw function(t){var r,s;const o=new Int32Array(t);if(Atomics.load(o,0)<=1)return null;const n=Atomics.load(o,1),i=Atomics.load(o,2),a=Atomics.load(o,3),d=new Uint8Array(t),h=d.slice(16,16+n),l=d.slice(16+n,16+n+i),c=d.slice(16+n+i,16+n+i+a),u=(new TextDecoder).decode(h),f=(new TextDecoder).decode(l),p=(new TextDecoder).decode(c),m=new(null!==(r=globalThis[u])&&void 0!==r?r:"RuntimeError"===u&&null!==(s=e.RuntimeError)&&void 0!==s?s:Error)(f);return Object.defineProperty(m,"stack",{value:p,writable:!0,enumerable:!1,configurable:!0}),m}(w.buffer)}}}catch(e){return Atomics.store(p,0,1),Atomics.store(p,1,6),Atomics.notify(p,1),null==g||g.printErr(e.stack),a?1:(c(s),-6)}return Atomics.store(p,0,0),Atomics.store(p,1,_),Atomics.notify(p,1),g.runningWorkers.push(y),m||y.whenLoaded.catch(e=>{throw delete y.whenLoaded,g.cleanThread(y,_,!0),e}),a?0:(c(s),_)};this.threadSpawn=p}getImportObject(){return{wasi:{"thread-spawn":this.threadSpawn}}}setup(e,t,r){null!=r||(r=e.exports.memory),this.wasmInstance=e,this.wasmMemory=r,this.PThread&&this.PThread.setup(t,r)}preloadWorkers(){return this.PThread?this.PThread.preloadWorkers():Promise.resolve([])}initialize(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);const o=this.wasi;if("_start"in s&&"function"==typeof s._start)if(this.childThread){o.start(e);try{o[f(o,"kStarted")]=!1}catch(e){}}else!function(e,t){const[r,s]=f(e,["kInstance","kSetMemory"]);e[r]=t,e[s](t.exports.memory)}(o,e);else o.initialize(e);return e}start(e,t,r){const s=e.exports;null!=r||(r=s.memory),this.childThread&&(e=l(e,r)),this.setup(e,t,r);return{exitCode:this.wasi.start(e),instance:e}}terminateAllThreads(){var e;this.childThread?this.postMessage(n("terminate-all-threads",{})):null===(e=this.PThread)||void 0===e||e.terminateAllThreads()}}function f(e,t){const r=Object.getOwnPropertySymbols(e),s=e=>t=>t.description?t.description===e:t.toString()===`Symbol(${e})`;return Array.isArray(t)?t.map(e=>r.filter(s(e))[0]):r.filter(s(t))[0]}class p{constructor(e){const t=r(e);if("function"!=typeof t)throw new TypeError("options.postMessage is not a function");this.postMessage=t,this.onLoad=null==e?void 0:e.onLoad,this.onError="function"==typeof(null==e?void 0:e.onError)?e.onError:(e,t)=>{throw t},this.instance=void 0,this.messagesBeforeLoad=[]}instantiate(e){if("function"==typeof this.onLoad)return this.onLoad(e);throw new Error("ThreadMessageHandler.prototype.instantiate is not implemented")}handle(e){var t;if(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.__emnapi__){const t=e.data.__emnapi__.type,r=e.data.__emnapi__.payload;try{"load"===t?this._load(r):"start"===t&&this.handleAfterLoad(e,()=>{this._start(r)})}catch(e){this.onError(e,t)}}}_load(e){if(void 0!==this.instance)return;let t;try{t=this.instantiate(e)}catch(t){return void this._loaded(t,null,e)}const r=t&&"then"in t?t.then:void 0;"function"==typeof r?r.call(t,t=>{this._loaded(null,t,e)},t=>{this._loaded(t,null,e)}):this._loaded(null,t,e)}_start(e){const t=this.instance.exports.wasi_thread_start;if("function"!=typeof t){const t=new TypeError("wasi_thread_start is not exported");throw m(e.sab,2,t),t}const r=this.postMessage,s=e.tid,o=e.arg;m(e.sab,1);try{t(s,o)}catch(e){if("unwind"!==e)throw e;return}r(n("cleanup-thread",{tid:s}))}_loaded(e,t,r){if(e)throw m(r.sab,2,e),e;if(null==t){const e=new TypeError("onLoad should return an object");throw m(r.sab,2,e),e}const s=t.instance;if(!s){const e=new TypeError('onLoad should return an object which includes "instance"');throw m(r.sab,2,e),e}this.instance=s;(0,this.postMessage)(n("loaded",{}));const o=this.messagesBeforeLoad;this.messagesBeforeLoad=[];for(let e=0;e<o.length;e++){const t=o[e];this.handle({data:t})}}handleAfterLoad(e,t){void 0!==this.instance?t.call(this,e):this.messagesBeforeLoad.push(e.data)}}function m(e,t,r){e&&(!function(e,t,r){const s=new Int32Array(e);if(Atomics.store(s,0,t),t>1&&r){const t=r.name,o=r.message,n=r.stack,i=(new TextEncoder).encode(t),a=(new TextEncoder).encode(o),d=(new TextEncoder).encode(n);Atomics.store(s,1,i.length),Atomics.store(s,2,a.length),Atomics.store(s,3,d.length);const h=new Uint8Array(e);h.set(i,16),h.set(a,16+i.length),h.set(d,16+i.length+a.length)}}(e.buffer,t,r),Atomics.notify(e,0))}export{d as ThreadManager,p as ThreadMessageHandler,u as WASIThreads,l as createInstanceProxy,s as isSharedArrayBuffer,o as isTrapError};
|
|
@@ -61,7 +61,7 @@ function deserizeErrorFromBuffer(sab) {
|
|
|
61
61
|
}
|
|
62
62
|
function isSharedArrayBuffer(value) {
|
|
63
63
|
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
|
64
|
-
(Object.prototype.toString.call(value
|
|
64
|
+
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
|
65
65
|
}
|
|
66
66
|
function isTrapError(e) {
|
|
67
67
|
try {
|
|
@@ -238,12 +238,17 @@ class ThreadManager {
|
|
|
238
238
|
if (worker.__emnapi_tid !== undefined) {
|
|
239
239
|
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
|
240
240
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
241
|
+
if ('message' in e) {
|
|
242
|
+
err(message + ' ' + e.message);
|
|
243
|
+
if (e.message.indexOf('RuntimeError') !== -1 || e.message.indexOf('unreachable') !== -1) {
|
|
244
|
+
try {
|
|
245
|
+
_this.terminateAllThreads();
|
|
246
|
+
}
|
|
247
|
+
catch (_) { }
|
|
245
248
|
}
|
|
246
|
-
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
err(message);
|
|
247
252
|
}
|
|
248
253
|
reject(e);
|
|
249
254
|
throw e;
|
|
@@ -724,19 +729,21 @@ function patchWasiInstance(wasiThreads, wasi) {
|
|
|
724
729
|
return proc_exit.call(this, code);
|
|
725
730
|
};
|
|
726
731
|
}
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
catch (err) {
|
|
734
|
-
if (isTrapError(err)) {
|
|
735
|
-
_this.terminateAllThreads();
|
|
732
|
+
if (!_this.childThread) {
|
|
733
|
+
const start = wasi.start;
|
|
734
|
+
if (typeof start === 'function') {
|
|
735
|
+
wasi.start = function (instance) {
|
|
736
|
+
try {
|
|
737
|
+
return start.call(this, instance);
|
|
736
738
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
739
|
+
catch (err) {
|
|
740
|
+
if (isTrapError(err)) {
|
|
741
|
+
_this.terminateAllThreads();
|
|
742
|
+
}
|
|
743
|
+
throw err;
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
}
|
|
740
747
|
}
|
|
741
748
|
patched.add(wasi);
|
|
742
749
|
}
|
|
@@ -767,6 +774,7 @@ class ThreadMessageHandler {
|
|
|
767
774
|
}
|
|
768
775
|
this.postMessage = postMsg;
|
|
769
776
|
this.onLoad = options === null || options === void 0 ? void 0 : options.onLoad;
|
|
777
|
+
this.onError = typeof (options === null || options === void 0 ? void 0 : options.onError) === 'function' ? options.onError : (_type, err) => { throw err; };
|
|
770
778
|
this.instance = undefined;
|
|
771
779
|
this.messagesBeforeLoad = [];
|
|
772
780
|
}
|
|
@@ -781,13 +789,18 @@ class ThreadMessageHandler {
|
|
|
781
789
|
if ((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.__emnapi__) {
|
|
782
790
|
const type = e.data.__emnapi__.type;
|
|
783
791
|
const payload = e.data.__emnapi__.payload;
|
|
784
|
-
|
|
785
|
-
|
|
792
|
+
try {
|
|
793
|
+
if (type === 'load') {
|
|
794
|
+
this._load(payload);
|
|
795
|
+
}
|
|
796
|
+
else if (type === 'start') {
|
|
797
|
+
this.handleAfterLoad(e, () => {
|
|
798
|
+
this._start(payload);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
786
801
|
}
|
|
787
|
-
|
|
788
|
-
this.
|
|
789
|
-
this._start(payload);
|
|
790
|
-
});
|
|
802
|
+
catch (err) {
|
|
803
|
+
this.onError(err, type);
|
|
791
804
|
}
|
|
792
805
|
}
|
|
793
806
|
}
|
|
@@ -811,7 +824,8 @@ class ThreadMessageHandler {
|
|
|
811
824
|
}
|
|
812
825
|
}
|
|
813
826
|
_start(payload) {
|
|
814
|
-
|
|
827
|
+
const wasi_thread_start = this.instance.exports.wasi_thread_start;
|
|
828
|
+
if (typeof wasi_thread_start !== 'function') {
|
|
815
829
|
const err = new TypeError('wasi_thread_start is not exported');
|
|
816
830
|
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
817
831
|
throw err;
|
|
@@ -821,13 +835,15 @@ class ThreadMessageHandler {
|
|
|
821
835
|
const startArg = payload.arg;
|
|
822
836
|
notifyPthreadCreateResult(payload.sab, 1);
|
|
823
837
|
try {
|
|
824
|
-
|
|
838
|
+
wasi_thread_start(tid, startArg);
|
|
825
839
|
}
|
|
826
840
|
catch (err) {
|
|
827
|
-
if (
|
|
828
|
-
|
|
841
|
+
if (err !== 'unwind') {
|
|
842
|
+
throw err;
|
|
843
|
+
}
|
|
844
|
+
else {
|
|
845
|
+
return;
|
|
829
846
|
}
|
|
830
|
-
throw err;
|
|
831
847
|
}
|
|
832
848
|
postMessage(createMessage('cleanup-thread', { tid }));
|
|
833
849
|
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// @ts-check
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {unknown} value
|
|
7
|
+
*/
|
|
8
|
+
const getType = (value) => {
|
|
9
|
+
if (value === undefined) return 0
|
|
10
|
+
if (value === null) return 1
|
|
11
|
+
const t = typeof value;
|
|
12
|
+
if (t === 'boolean') return 2
|
|
13
|
+
if (t === 'number') return 3
|
|
14
|
+
if (t === 'string') return 4
|
|
15
|
+
if (t === 'object') return 6
|
|
16
|
+
if (t === 'bigint') return 9
|
|
17
|
+
return -1
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('memfs').IFs} memfs
|
|
22
|
+
* @param {any} value
|
|
23
|
+
* @param {ReturnType<typeof getType>} type
|
|
24
|
+
* @returns {Uint8Array}
|
|
25
|
+
*/
|
|
26
|
+
const encodeValue = (memfs, value, type) => {
|
|
27
|
+
switch (type) {
|
|
28
|
+
case 0:
|
|
29
|
+
case 1:
|
|
30
|
+
return new Uint8Array(0)
|
|
31
|
+
case 2: {
|
|
32
|
+
const view = new Int32Array(1);
|
|
33
|
+
view[0] = value ? 1 : 0;
|
|
34
|
+
return new Uint8Array(view.buffer)
|
|
35
|
+
}
|
|
36
|
+
case 3: {
|
|
37
|
+
const view = new Float64Array(1);
|
|
38
|
+
view[0] = value;
|
|
39
|
+
return new Uint8Array(view.buffer)
|
|
40
|
+
}
|
|
41
|
+
case 4: {
|
|
42
|
+
const view = new TextEncoder().encode(value);
|
|
43
|
+
return view
|
|
44
|
+
}
|
|
45
|
+
case 6: {
|
|
46
|
+
function storeConstructor(obj, memfs, processed = new WeakSet()) {
|
|
47
|
+
if (!obj || typeof obj !== 'object') {
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (processed.has(obj)) {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
processed.add(obj);
|
|
56
|
+
|
|
57
|
+
const [entry] =
|
|
58
|
+
Object.entries(memfs).filter(([_, v]) => v === obj.constructor)[0] ??
|
|
59
|
+
[];
|
|
60
|
+
if (entry) {
|
|
61
|
+
Object.defineProperty(obj, '__constructor__', {
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
enumerable: true,
|
|
65
|
+
value: entry,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const value of Object.values(obj)) {
|
|
70
|
+
storeConstructor(value, memfs, processed);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
storeConstructor(value, memfs);
|
|
75
|
+
|
|
76
|
+
const json = JSON.stringify(value, (_, value) => {
|
|
77
|
+
if (typeof value === 'bigint') {
|
|
78
|
+
return `BigInt(${String(value)})`
|
|
79
|
+
}
|
|
80
|
+
if (value instanceof Error) {
|
|
81
|
+
return {
|
|
82
|
+
...value,
|
|
83
|
+
message: value.message,
|
|
84
|
+
stack: value.stack,
|
|
85
|
+
__error__: value.constructor.name,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return value
|
|
89
|
+
});
|
|
90
|
+
const view = new TextEncoder().encode(json);
|
|
91
|
+
return view
|
|
92
|
+
}
|
|
93
|
+
case 9: {
|
|
94
|
+
const view = new BigInt64Array(1);
|
|
95
|
+
view[0] = value;
|
|
96
|
+
return new Uint8Array(view.buffer)
|
|
97
|
+
}
|
|
98
|
+
case -1:
|
|
99
|
+
default:
|
|
100
|
+
throw new Error('unsupported data')
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {typeof import('memfs')} memfs
|
|
106
|
+
* @param {Uint8Array} payload
|
|
107
|
+
* @param {number} type
|
|
108
|
+
* @returns {any}
|
|
109
|
+
*/
|
|
110
|
+
const decodeValue = (memfs, payload, type) => {
|
|
111
|
+
if (type === 0) return undefined
|
|
112
|
+
if (type === 1) return null
|
|
113
|
+
if (type === 2)
|
|
114
|
+
return Boolean(new Int32Array(payload.buffer, payload.byteOffset, 1)[0])
|
|
115
|
+
if (type === 3)
|
|
116
|
+
return new Float64Array(payload.buffer, payload.byteOffset, 1)[0]
|
|
117
|
+
if (type === 4) return new TextDecoder().decode(payload.slice())
|
|
118
|
+
if (type === 6) {
|
|
119
|
+
const obj = JSON.parse(
|
|
120
|
+
new TextDecoder().decode(payload.slice()),
|
|
121
|
+
(_key, value) => {
|
|
122
|
+
if (typeof value === 'string') {
|
|
123
|
+
const matched = value.match(/^BigInt\((-?\d+)\)$/);
|
|
124
|
+
if (matched && matched[1]) {
|
|
125
|
+
return BigInt(matched[1])
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return value
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
function loadConstructor(obj, memfs, processed = new WeakSet()) {
|
|
133
|
+
if (!obj || typeof obj !== 'object') {
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (processed.has(obj)) {
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
processed.add(obj);
|
|
142
|
+
|
|
143
|
+
if (obj.__constructor__) {
|
|
144
|
+
const ctor = obj.__constructor__;
|
|
145
|
+
delete obj.__constructor__;
|
|
146
|
+
Object.setPrototypeOf(obj, memfs[ctor].prototype);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const value of Object.values(obj)) {
|
|
150
|
+
loadConstructor(value, memfs, processed);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
loadConstructor(obj, memfs);
|
|
155
|
+
|
|
156
|
+
if (obj.__error__) {
|
|
157
|
+
const name = obj.__error__;
|
|
158
|
+
const ErrorConstructor = globalThis[name] || Error;
|
|
159
|
+
delete obj.__error__;
|
|
160
|
+
const err = new ErrorConstructor(obj.message);
|
|
161
|
+
Object.defineProperty(err, 'stack', {
|
|
162
|
+
configurable: true,
|
|
163
|
+
enumerable: false,
|
|
164
|
+
writable: true,
|
|
165
|
+
value: err.stack,
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(err, Symbol.toStringTag, {
|
|
168
|
+
configurable: true,
|
|
169
|
+
enumerable: false,
|
|
170
|
+
writable: true,
|
|
171
|
+
value: name,
|
|
172
|
+
});
|
|
173
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
174
|
+
if (k === 'message' || k === 'stack') continue
|
|
175
|
+
err[k] = v;
|
|
176
|
+
}
|
|
177
|
+
return err
|
|
178
|
+
}
|
|
179
|
+
return obj
|
|
180
|
+
}
|
|
181
|
+
if (type === 9)
|
|
182
|
+
return new BigInt64Array(payload.buffer, payload.byteOffset, 1)[0]
|
|
183
|
+
throw new Error('unsupported data')
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {import('memfs').IFs} fs
|
|
188
|
+
* @returns {(e: { data: { __fs__: { sab: Int32Array, type: keyof import('memfs').IFs, payload: any[] } } }) => void}
|
|
189
|
+
*/
|
|
190
|
+
// oxlint-disable-next-line no-unused-vars -- fixed in an upcoming release
|
|
191
|
+
const createOnMessage = (fs) =>
|
|
192
|
+
function onMessage(e) {
|
|
193
|
+
if (e.data.__fs__) {
|
|
194
|
+
/**
|
|
195
|
+
* 0..4 status(int32_t): 21(waiting) 0(success) 1(error)
|
|
196
|
+
* 5..8 type(napi_valuetype): 0(undefined) 1(null) 2(boolean) 3(number) 4(string) 6(jsonstring) 9(bigint) -1(unsupported)
|
|
197
|
+
* 9..16 payload_size(uint32_t) <= 1024
|
|
198
|
+
* 16..16 + payload_size payload_content
|
|
199
|
+
*/
|
|
200
|
+
const { sab, type, payload } = e.data.__fs__;
|
|
201
|
+
const fn = fs[type];
|
|
202
|
+
try {
|
|
203
|
+
const ret = fn.apply(fs, payload);
|
|
204
|
+
const t = getType(ret);
|
|
205
|
+
Atomics.store(sab, 1, t);
|
|
206
|
+
const v = encodeValue(fs, ret, t);
|
|
207
|
+
Atomics.store(sab, 2, v.length);
|
|
208
|
+
new Uint8Array(sab.buffer).set(v, 16);
|
|
209
|
+
Atomics.store(sab, 0, 0); // success
|
|
210
|
+
} catch (/** @type {any} */ err) {
|
|
211
|
+
const t = getType(err);
|
|
212
|
+
Atomics.store(sab, 1, t);
|
|
213
|
+
const v = encodeValue(fs, err, t);
|
|
214
|
+
Atomics.store(sab, 2, v.length);
|
|
215
|
+
new Uint8Array(sab.buffer).set(v, 16);
|
|
216
|
+
Atomics.store(sab, 0, 1); // error
|
|
217
|
+
} finally {
|
|
218
|
+
Atomics.notify(sab, 0);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* @param {typeof import('memfs')} memfs
|
|
225
|
+
*/
|
|
226
|
+
const createFsProxy = (memfs) =>
|
|
227
|
+
new Proxy(
|
|
228
|
+
{},
|
|
229
|
+
{
|
|
230
|
+
get(_target, p, _receiver) {
|
|
231
|
+
/**
|
|
232
|
+
* @param {any[]} args
|
|
233
|
+
*/
|
|
234
|
+
return function (...args) {
|
|
235
|
+
const sab = new SharedArrayBuffer(16 + 10240);
|
|
236
|
+
const i32arr = new Int32Array(sab);
|
|
237
|
+
Atomics.store(i32arr, 0, 21);
|
|
238
|
+
|
|
239
|
+
postMessage({
|
|
240
|
+
__fs__: {
|
|
241
|
+
sab: i32arr,
|
|
242
|
+
type: p,
|
|
243
|
+
payload: args,
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
Atomics.wait(i32arr, 0, 21);
|
|
248
|
+
|
|
249
|
+
const status = Atomics.load(i32arr, 0);
|
|
250
|
+
const type = Atomics.load(i32arr, 1);
|
|
251
|
+
const size = Atomics.load(i32arr, 2);
|
|
252
|
+
const content = new Uint8Array(sab, 16, size);
|
|
253
|
+
const value = decodeValue(memfs, content, type);
|
|
254
|
+
if (status === 1) {
|
|
255
|
+
throw value
|
|
256
|
+
}
|
|
257
|
+
return value
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
exports.createFsProxy = createFsProxy;
|
|
264
|
+
exports.createOnMessage = createOnMessage;
|