From b5475d518fc9d629407c4c45e2e3dd5ba58dad82 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Wed, 17 Jul 2024 22:59:18 +0800 Subject: [PATCH] fix(es/typescript): patch for wasm-bindgen --- .../binding_typescript_wasm/scripts/patch.mjs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bindings/binding_typescript_wasm/scripts/patch.mjs b/bindings/binding_typescript_wasm/scripts/patch.mjs index 7ce3b7fcafa7..1f18112d6c7d 100755 --- a/bindings/binding_typescript_wasm/scripts/patch.mjs +++ b/bindings/binding_typescript_wasm/scripts/patch.mjs @@ -8,6 +8,23 @@ const base64 = rawWasmFile.toString('base64'); const patchedJsFile = origJsFile .replace(`const path = require('path').join(__dirname, 'wasm_bg.wasm');`, '') + .replaceAll(/=\s*getInt32Memory0\(\)\[(\w+)\s*\/\s*(\d+)\s*\+\s*(\d+)\]/g, `= getDataView().getInt32($1 + $2 * $3, true)`) + .replaceAll(/getInt32Memory0\(\)\[(\w+)\s*\/\s*(\d+)\s*\+\s*(\d+)\]\s*=\s*(.*);/g, `getDataView().setInt32($1 + $2 * $3, $4, true);`) + .replaceAll(/getFloat64Memory0\(\)\[(\w+)\s*\/\s*(\d+)\s*\+\s*(\d+)\]\s*=\s*(.*);/g, `getDataView().setFloat64($1 + $2 * $3, $4, true);`) + .replace(', fatal: true', '') + .replace('let cachedFloat64Memory0 = null;', + ` +let cachedDataView = null; +/** @returns {DataView} */ +function getDataView() { + if (cachedDataView === null || cachedDataView.buffer !== wasm.memory.buffer) { + cachedDataView = new DataView(wasm.memory.buffer); + } + return cachedDataView; +} + +let cachedFloat64Memory0 = null; +`) .replace(`const bytes = require('fs').readFileSync(path);`, ` const { Buffer } = require('node:buffer'); const bytes = Buffer.from('${base64}', 'base64');`)