stdlib: printf-like logging
This commit is contained in:
parent
4cdc19b590
commit
5f4b19bdca
|
|
@ -6,6 +6,7 @@ extern "C" {
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
extern unsigned char __heap_base;
|
extern unsigned char __heap_base;
|
||||||
|
|
||||||
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
||||||
|
|
@ -13,8 +14,6 @@ extern unsigned char __heap_base;
|
||||||
|
|
||||||
#define NULL ((void*)0)
|
#define NULL ((void*)0)
|
||||||
|
|
||||||
typedef uint32_t size_t;
|
|
||||||
|
|
||||||
EXPORT(malloc) void* malloc(size_t);
|
EXPORT(malloc) void* malloc(size_t);
|
||||||
EXPORT(free) void free(void*);
|
EXPORT(free) void free(void*);
|
||||||
EXPORT(realloc) void* realloc(void*, size_t);
|
EXPORT(realloc) void* realloc(void*, size_t);
|
||||||
|
|
@ -23,7 +22,7 @@ void* memset(void* s, uint8_t c, uint32_t n);
|
||||||
void* memcpy(void* dest, const void* src, uint32_t n);
|
void* memcpy(void* dest, const void* src, uint32_t n);
|
||||||
int memcmp(const void* s1, const void* s2, uint32_t n);
|
int memcmp(const void* s1, const void* s2, uint32_t n);
|
||||||
|
|
||||||
IMPORT(log) void print_int(int64_t);
|
IMPORT(log) void printf(const char* format, ...);
|
||||||
|
|
||||||
EXPORT(__srand) void srand(uint64_t seed);
|
EXPORT(__srand) void srand(uint64_t seed);
|
||||||
uint64_t rand(void);
|
uint64_t rand(void);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ const filePlugin: BunPlugin = {
|
||||||
name: "File loader",
|
name: "File loader",
|
||||||
async setup(build) {
|
async setup(build) {
|
||||||
build.onLoad({ filter: /\.glsl$/ }, async (args) => {
|
build.onLoad({ filter: /\.glsl$/ }, async (args) => {
|
||||||
console.log(args);
|
|
||||||
const text = await Bun.file(args.path).text();
|
const text = await Bun.file(args.path).text();
|
||||||
return {
|
return {
|
||||||
contents: `
|
contents: `
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,42 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
|
||||||
initial: 32,
|
initial: 32,
|
||||||
});
|
});
|
||||||
let data = new DataView(memory.buffer);
|
let data = new DataView(memory.buffer);
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const getString = (ptr) => {
|
||||||
|
const view = new Uint8Array(memory.buffer, ptr);
|
||||||
|
const start = ptr;
|
||||||
|
const end = ptr + view.indexOf(0);
|
||||||
|
|
||||||
|
return decoder.decode(memory.buffer.slice(start, end));
|
||||||
|
};
|
||||||
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
|
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
|
||||||
env: {
|
env: {
|
||||||
memory,
|
memory,
|
||||||
log(...args) {
|
log(format, argsPtr) {
|
||||||
console.log('[wasm]', ...args);
|
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 (c === '%') {
|
||||||
|
isFormat = true;
|
||||||
|
} else switch(c) {
|
||||||
|
case '%': buf += '%'; isFormat = false; break;
|
||||||
|
case 's': align(4); buf += getString(data.getInt32(argsPtr, true)); argsPtr += 4; isFormat = false; break;
|
||||||
|
case 'd': align(w); buf += data[w === 4 ? 'getInt32': 'getBigInt64'](argsPtr, true); argsPtr += w; isFormat = false; break;
|
||||||
|
case 'x': align(w); buf += data[w === 4 ? 'getInt32': 'getBigInt64'](argsPtr, true).toString(16); argsPtr += w; isFormat = false; break;
|
||||||
|
case 'o': align(w); buf += data[w === 4 ? 'getInt32': 'getBigInt64'](argsPtr, true).toString(8); argsPtr += w; isFormat = false; break;
|
||||||
|
case 'u': align(w); buf += data[w === 4 ? 'getUint32': 'getBigUint64'](argsPtr, true); argsPtr += w; isFormat = false; break;
|
||||||
|
case 'f': align(8); buf += data.getFloat64(argsPtr, true); argsPtr += 8; isFormat = false; break;
|
||||||
|
case 'c': align(4); buf += String.fromCharCode(data.getInt32(argsPtr, true)); argsPtr += 4; isFormat = false; break;
|
||||||
|
case 'l': w = 8; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[wasm] ' + buf);
|
||||||
},
|
},
|
||||||
grow(blocks) {
|
grow(blocks) {
|
||||||
if (blocks > 0) {
|
if (blocks > 0) {
|
||||||
|
|
@ -49,7 +80,7 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
|
||||||
} else {
|
} else {
|
||||||
const buildAssets = path.resolve(import.meta.dir, 'assets');
|
const buildAssets = path.resolve(import.meta.dir, 'assets');
|
||||||
const stdlib = `${buildAssets}/stdlib.c`;
|
const stdlib = `${buildAssets}/stdlib.c`;
|
||||||
const opt = production ? '-O3': '-O0';
|
const opt = production ? '-O3' : '-O0';
|
||||||
const result = await $`clang --target=wasm32 ${opt} -flto -fno-builtin --no-standard-libraries -I ${buildAssets} -Wall -Wextra -Wpedantic -Werror -Wl,--lto-O3 -Wl,--no-entry -Wl,--import-memory -o ${wasmPath} ${args.path} ${stdlib}`;
|
const result = await $`clang --target=wasm32 ${opt} -flto -fno-builtin --no-standard-libraries -I ${buildAssets} -Wall -Wextra -Wpedantic -Werror -Wl,--lto-O3 -Wl,--no-entry -Wl,--import-memory -o ${wasmPath} ${args.path} ${stdlib}`;
|
||||||
|
|
||||||
if (result.exitCode !== 0) {
|
if (result.exitCode !== 0) {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,5 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct awoo {
|
EXPORT(cpptest) void cpptest() {
|
||||||
int a;
|
printf("Awoo d=%d d2=%d", 69llu, 42ll);
|
||||||
|
|
||||||
awoo operator+(const awoo& other) const { return awoo{a + other.a}; }
|
|
||||||
|
|
||||||
operator int() const { return a * 2; }
|
|
||||||
};
|
|
||||||
|
|
||||||
EXPORT(cpptest) uint32_t cpptest() {
|
|
||||||
awoo a{42};
|
|
||||||
awoo b{69};
|
|
||||||
|
|
||||||
return a + b;
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,7 @@
|
||||||
import { render } from "preact";
|
|
||||||
import awoo from "./awoo.cpp";
|
import awoo from "./awoo.cpp";
|
||||||
import shader from './assets/shader.glsl';
|
|
||||||
|
|
||||||
export default function main() {
|
export default function main() {
|
||||||
console.log(shader);
|
awoo.cpptest();
|
||||||
render(
|
// awoo.data.getBig
|
||||||
<button onClick={() => console.log(awoo.cpptest())}>
|
console.log(awoo.memory);
|
||||||
Attack
|
|
||||||
</button>,
|
|
||||||
document.body
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
@ -103,8 +103,7 @@ void patterns_init(uint32_t* pixel_data, uint16_t image_width, uint16_t image_he
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_int(num_patterns);
|
log("num=%u, max=%u", num_patterns, max_patterns);
|
||||||
print_int(max_patterns);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void superposition_init(void) {
|
void superposition_init(void) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue