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));
};
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) {

View File

@ -1,5 +1,5 @@
#include <stdlib.h>
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() {
awoo.cpptest();
console.log(awoo.memory.grow);
console.log(awoo);
}