@napi-rs/wasm-runtime
Advanced tools
+77
-61
@@ -58,2 +58,10 @@ // @ts-check | ||
| } | ||
| if (value instanceof Error) { | ||
| return { | ||
| ...value, | ||
| message: value.message, | ||
| stack: value.stack, | ||
| __error__: value.constructor.name, | ||
| } | ||
| } | ||
| return value | ||
@@ -76,2 +84,58 @@ }) | ||
| /** | ||
| * @param {typeof import('memfs')} memfs | ||
| * @param {Uint8Array} payload | ||
| * @param {number} type | ||
| * @returns {any} | ||
| */ | ||
| const decodeValue = (memfs, payload, type) => { | ||
| if (type === 0) return undefined | ||
| if (type === 1) return null | ||
| if (type === 2) return Boolean(payload[0]) | ||
| if (type === 3) return new Float64Array(payload.buffer, payload.byteOffset, 1)[0] | ||
| if (type === 4) return new TextDecoder().decode(payload.slice()) | ||
| if (type === 6) { | ||
| const obj = JSON.parse(new TextDecoder().decode(payload.slice()), (_key, value) => { | ||
| if (typeof value === 'string') { | ||
| const matched = value.match(/^BigInt\((-?\d+)\)$/) | ||
| if (matched && matched[1]) { | ||
| return BigInt(matched[1]) | ||
| } | ||
| } | ||
| return value | ||
| }) | ||
| if (obj.__constructor__) { | ||
| const ctor = obj.__constructor__ | ||
| delete obj.__constructor__ | ||
| Object.setPrototypeOf(obj, memfs[ctor].prototype) | ||
| } | ||
| if (obj.__error__) { | ||
| const name = obj.__error__ | ||
| const ErrorConstructor = globalThis[name] || Error | ||
| delete obj.__error__ | ||
| const err = new ErrorConstructor(obj.message) | ||
| Object.defineProperty(err, 'stack', { | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: true, | ||
| value: err.stack | ||
| }) | ||
| Object.defineProperty(err, Symbol.toStringTag, { | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: true, | ||
| value: name | ||
| }) | ||
| for (const [k, v] of Object.entries(obj)) { | ||
| if (k === 'message' || k === 'stack') continue | ||
| err[k] = v | ||
| } | ||
| return err | ||
| } | ||
| return obj | ||
| } | ||
| if (type === 9) return new BigInt64Array(payload.buffer, payload.byteOffset, 1)[0] | ||
| throw new Error('unsupported data') | ||
| } | ||
| /** | ||
| * @param {import('memfs').IFs} fs | ||
@@ -91,16 +155,8 @@ * @returns {(e: { data: { __fs__: { sab: Int32Array, type: keyof import('memfs').IFs, payload: any[] } } }) => void} | ||
| const fn = fs[type] | ||
| const args = payload ? payload.map((value) => { | ||
| if (value instanceof Uint8Array) { | ||
| // buffer polyfill bug | ||
| // @ts-expect-error | ||
| value._isBuffer = true | ||
| } | ||
| return value | ||
| }) : payload | ||
| try { | ||
| const ret = fn.apply(fs, args) | ||
| const ret = fn.apply(fs, payload) | ||
| Atomics.store(sab, 0, 0) | ||
| const t = getType(ret) | ||
| Atomics.store(sab, 1, t) | ||
| const v = encodeValue(fs, ret, t) | ||
| Atomics.store(sab, 0, 0) | ||
| Atomics.store(sab, 1, t) | ||
| Atomics.store(sab, 2, v.length) | ||
@@ -111,10 +167,7 @@ new Uint8Array(sab.buffer).set(v, 16) | ||
| Atomics.store(sab, 0, 1) | ||
| Atomics.store(sab, 1, 6) | ||
| const payloadContent = new TextEncoder().encode(JSON.stringify({ | ||
| ...err, | ||
| message: err.message, | ||
| stack: err.stack | ||
| })) | ||
| Atomics.store(sab, 2, payloadContent.length) | ||
| new Uint8Array(sab.buffer).set(payloadContent, 16) | ||
| const t = getType(err) | ||
| Atomics.store(sab, 1, t) | ||
| const v = encodeValue(fs, err, t) | ||
| Atomics.store(sab, 2, v.length) | ||
| new Uint8Array(sab.buffer).set(v, 16) | ||
| } finally { | ||
@@ -127,3 +180,3 @@ Atomics.notify(sab, 0) | ||
| /** | ||
| * @param {import('memfs').IFs} memfs | ||
| * @param {typeof import('memfs')} memfs | ||
| */ | ||
@@ -136,7 +189,6 @@ module.exports.createFsProxy = (memfs) => new Proxy({}, { | ||
| return function (...args) { | ||
| const sab = new SharedArrayBuffer(16 + 1024) | ||
| const sab = new SharedArrayBuffer(16 + 10240) | ||
| const i32arr = new Int32Array(sab) | ||
| Atomics.store(i32arr, 0, 21) | ||
| // @ts-expect-error | ||
| postMessage({ | ||
@@ -156,45 +208,9 @@ __fs__: { | ||
| const content = new Uint8Array(sab, 16, size) | ||
| const value = decodeValue(memfs, content, type) | ||
| if (status === 1) { | ||
| const errobj = JSON.parse(new TextDecoder().decode(content.slice())) | ||
| const err = new Error(errobj.message) | ||
| Object.defineProperty(err, 'stack', { | ||
| configurable: true, | ||
| enumerable: false, | ||
| writable: true, | ||
| value: errobj.stack | ||
| }) | ||
| for (const [k, v] of Object.entries(errobj)) { | ||
| if (k === 'message' || k === 'stack') continue | ||
| // @ts-expect-error | ||
| err[k] = v | ||
| } | ||
| throw err | ||
| throw value | ||
| } | ||
| if (type === 0) return undefined | ||
| if (type === 1) return null | ||
| if (type === 2) return Boolean(content[0]) | ||
| if (type === 3) return new Float64Array(sab, 16, 1)[0] | ||
| if (type === 4) return new TextDecoder().decode(content.slice()) | ||
| if (type === 6) { | ||
| const obj = JSON.parse(new TextDecoder().decode(content.slice()), (_key, value) => { | ||
| if (typeof value === 'string') { | ||
| const matched = value.match(/^BigInt\((-?\d+)\)$/) | ||
| if (matched && matched[1]) { | ||
| return BigInt(matched[1]) | ||
| } | ||
| } | ||
| return value | ||
| }) | ||
| if (obj.__constructor__) { | ||
| const ctor = obj.__constructor__ | ||
| delete obj.__constructor__ | ||
| // @ts-expect-error | ||
| Object.setPrototypeOf(obj, memfs[ctor].prototype) | ||
| } | ||
| return obj | ||
| } | ||
| if (type === 9) return new BigInt64Array(sab, 16, 1)[0] | ||
| throw new Error('unsupported data') | ||
| return value | ||
| } | ||
| } | ||
| }) |
+4
-3
| { | ||
| "name": "@napi-rs/wasm-runtime", | ||
| "version": "0.2.8", | ||
| "version": "0.2.9", | ||
| "type": "module", | ||
@@ -48,3 +48,4 @@ "description": "Runtime and polyfill for wasm targets", | ||
| "scripts": { | ||
| "build": "rollup -c rollup.config.js" | ||
| "build": "rollup -c rollup.config.js", | ||
| "test": "node --test" | ||
| }, | ||
@@ -60,3 +61,3 @@ "exports": { | ||
| }, | ||
| "gitHead": "6ce13931967f9413ed3312a210a20418b8d6773f" | ||
| "gitHead": "84209fc82931e8c6ef93e61e4a0e1d1a0ffaf511" | ||
| } |
Sorry, the diff of this file is too big to display
8025348
0.02%18977
0.18%