From 28bdadcbca3b6c0f8a49bf5624b3404eabc75e44 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Mon, 12 May 2025 18:43:36 +0000 Subject: [PATCH] Bufferized printf --- build/wasmPlugin.ts | 17 ++++++++++------- src/games/playground/awoo.cpp | 2 +- src/games/playground/index.tsx | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/build/wasmPlugin.ts b/build/wasmPlugin.ts index 4ec48c9..c64d1f0 100644 --- a/build/wasmPlugin.ts +++ b/build/wasmPlugin.ts @@ -26,20 +26,25 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin return decoder.decode(memory.buffer.slice(start, end)); }; + let buf = ''; const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { env: { memory, log(format, argsPtr) { format = getString(format); - let buf = ''; let isFormat = false; let w = 4; const align = (a) => argsPtr += (argsPtr % a); for (const c of format) { - if (!isFormat && c !== '%') { - buf += c; - } else if (!isFormat && c === '%') { - isFormat = true; + if (!isFormat) { + if (c === '%') { + isFormat = true; + } else if (c === '\\n') { + console.log('[wasm]', buf); + buf = ''; + } else { + buf += c; + } } else switch(c) { case '%': buf += '%'; isFormat = false; break; case 's': align(4); buf += getString(data.getInt32(argsPtr, true)); argsPtr += 4; isFormat = false; break; @@ -53,8 +58,6 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin default: buf += '%' + c; isFormat = false; break; } } - - console.log('[wasm]', buf); }, grow(blocks) { if (blocks > 0) { diff --git a/src/games/playground/awoo.cpp b/src/games/playground/awoo.cpp index 7737947..8f78c97 100644 --- a/src/games/playground/awoo.cpp +++ b/src/games/playground/awoo.cpp @@ -1,5 +1,5 @@ #include EXPORT(cpptest) void cpptest() { - printf("Awoo! %t%%"); + printf("Awoo! %t%%\n"); } \ No newline at end of file diff --git a/src/games/playground/index.tsx b/src/games/playground/index.tsx index a2591a4..c7d65ad 100644 --- a/src/games/playground/index.tsx +++ b/src/games/playground/index.tsx @@ -2,5 +2,5 @@ import awoo from "./awoo.cpp"; export default function main() { awoo.cpptest(); - console.log(awoo.memory.grow); + console.log(awoo); } \ No newline at end of file