1
0
Fork 0

Bufferized printf

This commit is contained in:
Pabloader 2025-05-12 18:43:36 +00:00
parent 4c17d32e4c
commit 28bdadcbca
3 changed files with 12 additions and 9 deletions

View File

@ -26,20 +26,25 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
return decoder.decode(memory.buffer.slice(start, end)); return decoder.decode(memory.buffer.slice(start, end));
}; };
let buf = '';
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
env: { env: {
memory, memory,
log(format, argsPtr) { log(format, argsPtr) {
format = getString(format); format = getString(format);
let buf = '';
let isFormat = false; let isFormat = false;
let w = 4; let w = 4;
const align = (a) => argsPtr += (argsPtr % a); const align = (a) => argsPtr += (argsPtr % a);
for (const c of format) { for (const c of format) {
if (!isFormat && c !== '%') { if (!isFormat) {
buf += c; if (c === '%') {
} else if (!isFormat && c === '%') { isFormat = true;
isFormat = true; } else if (c === '\\n') {
console.log('[wasm]', buf);
buf = '';
} else {
buf += c;
}
} else switch(c) { } else switch(c) {
case '%': buf += '%'; isFormat = false; break; case '%': buf += '%'; isFormat = false; break;
case 's': align(4); buf += getString(data.getInt32(argsPtr, true)); argsPtr += 4; 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; default: buf += '%' + c; isFormat = false; break;
} }
} }
console.log('[wasm]', buf);
}, },
grow(blocks) { grow(blocks) {
if (blocks > 0) { if (blocks > 0) {

View File

@ -1,5 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
EXPORT(cpptest) void cpptest() { EXPORT(cpptest) void cpptest() {
printf("Awoo! %t%%"); printf("Awoo! %t%%\n");
} }

View File

@ -2,5 +2,5 @@ import awoo from "./awoo.cpp";
export default function main() { export default function main() {
awoo.cpptest(); awoo.cpptest();
console.log(awoo.memory.grow); console.log(awoo);
} }