From bc088fdba5a7818c9fdde72f7f0217575da9f8bc Mon Sep 17 00:00:00 2001 From: Chloe <228616821+akiramusic000@users.noreply.github.com> Date: Wed, 18 Mar 2026 18:35:02 -0500 Subject: [PATCH 1/3] Add wabtHandler --- package.json | 1 + src/handlers/index.ts | 2 + src/handlers/wabt.ts | 89 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/handlers/wabt.ts diff --git a/package.json b/package.json index 0982dc3b..b356eaaa 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "verovio": "^6.0.1", "vexflow": "^5.0.0", "vite-plugin-static-copy": "^3.1.6", + "wabt": "^1.0.39", "wavefile": "^11.0.0", "woff2-encoder": "^2.0.0", "yaml": "^2.8.2" diff --git a/src/handlers/index.ts b/src/handlers/index.ts index 8515fa47..bb609965 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -66,6 +66,7 @@ import mclangHandler from "./minecraftLangfileHandler.ts"; import celariaMapHandler from "./celariaMap.ts"; import cybergrindHandler from "./cybergrindHandler.ts"; import textToSourceHandler from "./textToSource.ts"; +import wabtHandler from "./wabt.ts"; const handlers: FormatHandler[] = []; try { handlers.push(new svgTraceHandler()) } catch (_) { }; @@ -140,5 +141,6 @@ try { handlers.push(new mclangHandler()) } catch (_) { }; try { handlers.push(new celariaMapHandler()) } catch (_) { }; try { handlers.push(new cybergrindHandler()) } catch (_) { }; try { handlers.push(new textToSourceHandler()) } catch (_) { }; +try { handlers.push(new wabtHandler()) } catch (_) { }; export default handlers; diff --git a/src/handlers/wabt.ts b/src/handlers/wabt.ts new file mode 100644 index 00000000..984a9ed5 --- /dev/null +++ b/src/handlers/wabt.ts @@ -0,0 +1,89 @@ +import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; +import CommonFormats, { Category } from "src/CommonFormats.ts"; +import wabt from "wabt"; + +const wabtModule = await wabt(); + +function wasm2wat(bytes: Uint8Array): Uint8Array { + const wasmModule = wabtModule.readWasm(bytes, {}); + const str = wasmModule.toText({}); + const encoder = new TextEncoder(); + return encoder.encode(str); +} + +function wat2wasm(filename: string, bytes: Uint8Array): Uint8Array { + const wasmModule = wabtModule.parseWat(filename, bytes); + const outBytes = wasmModule.toBinary({}); + return outBytes.buffer; +} + +export default class wabtHandler implements FormatHandler { + public name: string = "wabt"; + public supportedFormats?: FileFormat[]; + public ready: boolean = false; + + async init() { + this.supportedFormats = [ + { + name: "WebAssembly Binary (Wasm)", + format: "wasm", + extension: "wasm", + mime: "application/wasm", + from: true, + to: true, + internal: "wasm", + category: Category.CODE, + lossless: true, + }, + { + name: "WebAssembly Text Format (WAT)", + format: "wat", + extension: "wat", + // https://github.com/WebAssembly/spec/issues/1347 + mime: "text/plain", + from: true, + to: true, + internal: "wat", + category: Category.CODE, + lossless: true, + }, + ]; + this.ready = true; + } + + async doConvert( + inputFiles: FileData[], + inputFormat: FileFormat, + outputFormat: FileFormat, + ): Promise { + const outputFiles: FileData[] = []; + + if (inputFormat.internal == "wasm" && outputFormat.internal == "wat") { + for (const file of inputFiles) { + outputFiles.push({ + name: + file.name.split(".").slice(0, -1).join(".") + + `.${outputFormat.extension}`, + bytes: wasm2wat(file.bytes), + }); + } + return outputFiles; + } + + if (inputFormat.internal == "wat" && outputFormat.internal == "wasm") { + for (const file of inputFiles) { + outputFiles.push({ + name: + file.name.split(".").slice(0, -1).join(".") + + `.${outputFormat.extension}`, + bytes: wat2wasm(file.name, file.bytes), + }); + } + return outputFiles; + } + + throw new Error( + `wabtHandler does not support route: ${inputFormat.internal} -> ${outputFormat.internal}`, + ); + } +} From 35c7a777689067e96aa4b94d45ad4c25f4cc9aef Mon Sep 17 00:00:00 2001 From: Chloe <228616821+akiramusic000@users.noreply.github.com> Date: Thu, 19 Mar 2026 10:36:23 -0500 Subject: [PATCH 2/3] Remove pointless import --- src/handlers/wabt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/wabt.ts b/src/handlers/wabt.ts index 984a9ed5..bedc20c3 100644 --- a/src/handlers/wabt.ts +++ b/src/handlers/wabt.ts @@ -1,5 +1,5 @@ import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; -import CommonFormats, { Category } from "src/CommonFormats.ts"; +import { Category } from "src/CommonFormats.ts"; import wabt from "wabt"; const wabtModule = await wabt(); From 2c942d3e7fefb2958e857211b21632677a94ba08 Mon Sep 17 00:00:00 2001 From: Chloe <228616821+akiramusic000@users.noreply.github.com> Date: Thu, 19 Mar 2026 10:36:52 -0500 Subject: [PATCH 3/3] Update handler file name --- src/handlers/index.ts | 2 +- src/handlers/{wabt.ts => wabtHandler.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/handlers/{wabt.ts => wabtHandler.ts} (100%) diff --git a/src/handlers/index.ts b/src/handlers/index.ts index bb609965..07bb756e 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -66,7 +66,7 @@ import mclangHandler from "./minecraftLangfileHandler.ts"; import celariaMapHandler from "./celariaMap.ts"; import cybergrindHandler from "./cybergrindHandler.ts"; import textToSourceHandler from "./textToSource.ts"; -import wabtHandler from "./wabt.ts"; +import wabtHandler from "./wabtHandler.ts"; const handlers: FormatHandler[] = []; try { handlers.push(new svgTraceHandler()) } catch (_) { }; diff --git a/src/handlers/wabt.ts b/src/handlers/wabtHandler.ts similarity index 100% rename from src/handlers/wabt.ts rename to src/handlers/wabtHandler.ts