1
0
Fork 0

Fix wasm plugin & proxy the env

This commit is contained in:
Pabloader 2026-05-04 14:26:24 +00:00
parent 85a264eed3
commit 96d85d3164
2 changed files with 16 additions and 30 deletions

View File

@ -16,7 +16,7 @@ const wasiArchiveURL = 'https://github.com/WebAssembly/wasi-sdk/releases/downloa
const rtURL = 'https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/libclang_rt.builtins-wasm32-wasi-25.0.tar.gz'; const rtURL = 'https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/libclang_rt.builtins-wasm32-wasi-25.0.tar.gz';
const getCompiler = async (): Promise<CompilerWithFlags> => { const getCompiler = async (): Promise<CompilerWithFlags> => {
const wasiDir = path.resolve(import.meta.dir, '..', 'dist', 'wasi'); const wasiDir = path.resolve(import.meta.dir, '..', '..', 'dist', 'wasi');
const cc: CompilerWithFlags = { const cc: CompilerWithFlags = {
cc: 'clang', cc: 'clang',
flags: [ flags: [
@ -113,17 +113,12 @@ async function instantiate(url: string) {
let errBuf = ''; let errBuf = '';
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
env: { memory, __indirect_function_table: table }, env: { memory, __indirect_function_table: table },
wasi_snapshot_preview1: { wasi_snapshot_preview1: new Proxy({
random_get: (ptr: number, length: number) => { random_get: (ptr: number, length: number) => {
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
data.setUint8(ptr + i, Math.random() * 256); data.setUint8(ptr + i, Math.random() * 256);
} }
}, },
environ_sizes_get: (...args: any[]) => { console.debug(`[environ_sizes_get]`, args); return 0; },
environ_get: (...args: any[]) => { console.debug(`[environ_get]`, args); return 0; },
proc_exit: (...args: any[]) => { console.debug(`[proc_exit]`, args); return 0; },
fd_close: (...args: any[]) => { console.debug(`[fd_close]`, args); return 0; },
fd_seek: (...args: any[]) => { console.debug(`[fd_seek]`, args); return 0; },
fd_write: (fd: number, iovsPtr: number, iovsLength: number, bytesWrittenPtr: number) => { fd_write: (fd: number, iovsPtr: number, iovsLength: number, bytesWrittenPtr: number) => {
const iovs = new Uint32Array(memory.buffer, iovsPtr, iovsLength * 2); const iovs = new Uint32Array(memory.buffer, iovsPtr, iovsLength * 2);
if (fd === 1 || fd === 2) { if (fd === 1 || fd === 2) {
@ -149,11 +144,15 @@ async function instantiate(url: string) {
} }
return 0; return 0;
}, },
fd_read: (...args: any[]) => { console.debug(`[fd_read]`, args); return 0; }, }, {
fd_fdstat_get: (fd: number, fdstatPtr: number) => { console.debug(`[fd_fdstat_get] fd=${fd}, ptr=${fdstatPtr}`); return 0; }, get(target, p) {
fd_prestat_get: (...args: any[]) => { console.debug(`[fd_prestat_get]`, args); return 0; }, if (p in target) {
fd_prestat_dir_name: (...args: any[]) => { console.debug(`[fd_prestat_dir_name]`, args); return 0; }, console.debug(`[${String(p)}] exists`);
return target[p as keyof typeof target];
} }
return (...args: any[]) => { console.debug(`[${String(p)}]`, args); return 0; }
},
}),
}); });
return { return {
@ -169,7 +168,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
name: "WASM loader", name: "WASM loader",
async setup(build) { async setup(build) {
build.onLoad({ filter: /\.(c(pp)?|wasm)$/ }, async (args) => { build.onLoad({ filter: /\.(c(pp)?|wasm)$/ }, async (args) => {
let wasmPath = path.resolve(import.meta.dir, '..', 'dist', 'tmp.wasm'); let wasmPath = path.resolve(import.meta.dir, '..', '..', 'dist', 'tmp.wasm');
let jsContent: string = ` let jsContent: string = `
${instantiate} ${instantiate}
const module = await instantiate(new URL($WASM$)); const module = await instantiate(new URL($WASM$));
@ -179,7 +178,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
if (args.path.endsWith('.wasm')) { if (args.path.endsWith('.wasm')) {
wasmPath = args.path; wasmPath = args.path;
} else { } else {
const buildAssets = path.resolve(import.meta.dir, 'assets'); const buildAssets = path.resolve(import.meta.dir, '..', 'assets');
const include = `${buildAssets}/include`; const include = `${buildAssets}/include`;
const glob = new Bun.Glob(`${buildAssets}/lib/**/*.c`); const glob = new Bun.Glob(`${buildAssets}/lib/**/*.c`);
const stdlib = await Array.fromAsync(glob.scan()); const stdlib = await Array.fromAsync(glob.scan());

View File

@ -1,5 +1,6 @@
import { createCanvas } from "@common/display/canvas"; import { createCanvas } from "@common/display/canvas";
import { gameLoop } from "@common/game"; import { gameLoop } from "@common/game";
import { bresenhamCircleGen } from "@common/navigation/bresenham";
import Physics from "@common/physics"; import Physics from "@common/physics";
import { mapNumber } from "@common/utils"; import { mapNumber } from "@common/utils";
@ -161,22 +162,8 @@ const frame = (dt: number, state: State) => {
function fillCircle(ctx: CanvasRenderingContext2D, xc: number, yc: number, r: number) { function fillCircle(ctx: CanvasRenderingContext2D, xc: number, yc: number, r: number) {
// because default circle fill is antialiased // because default circle fill is antialiased
// https://stackoverflow.com/a/45745753 for (const { x, y } of bresenhamCircleGen(xc, yc, r, { fill: true })) {
xc = xc | 0; ctx.fillRect(x, y, 1, 1);
yc = yc | 0;
r = r | 0;
var x = r, y = 0, cd = 0;
// middle line
ctx.fillRect(xc - x, yc, r << 1, 1);
while (x > y) {
cd -= (--x) - (++y);
if (cd < 0) cd += x++;
ctx.fillRect(xc - y, yc - x, y << 1, 1); // upper 1/4
ctx.fillRect(xc - x, yc - y, x << 1, 1); // upper 2/4
ctx.fillRect(xc - x, yc + y, x << 1, 1); // lower 3/4
ctx.fillRect(xc - y, yc + x, y << 1, 1); // lower 4/4
} }
} }