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 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 = {
cc: 'clang',
flags: [
@ -113,17 +113,12 @@ async function instantiate(url: string) {
let errBuf = '';
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
env: { memory, __indirect_function_table: table },
wasi_snapshot_preview1: {
wasi_snapshot_preview1: new Proxy({
random_get: (ptr: number, length: number) => {
for (let i = 0; i < length; i++) {
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) => {
const iovs = new Uint32Array(memory.buffer, iovsPtr, iovsLength * 2);
if (fd === 1 || fd === 2) {
@ -149,11 +144,15 @@ async function instantiate(url: string) {
}
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; },
fd_prestat_get: (...args: any[]) => { console.debug(`[fd_prestat_get]`, args); return 0; },
fd_prestat_dir_name: (...args: any[]) => { console.debug(`[fd_prestat_dir_name]`, args); return 0; },
}
}, {
get(target, p) {
if (p in target) {
console.debug(`[${String(p)}] exists`);
return target[p as keyof typeof target];
}
return (...args: any[]) => { console.debug(`[${String(p)}]`, args); return 0; }
},
}),
});
return {
@ -169,7 +168,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
name: "WASM loader",
async setup(build) {
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 = `
${instantiate}
const module = await instantiate(new URL($WASM$));
@ -179,7 +178,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
if (args.path.endsWith('.wasm')) {
wasmPath = args.path;
} else {
const buildAssets = path.resolve(import.meta.dir, 'assets');
const buildAssets = path.resolve(import.meta.dir, '..', 'assets');
const include = `${buildAssets}/include`;
const glob = new Bun.Glob(`${buildAssets}/lib/**/*.c`);
const stdlib = await Array.fromAsync(glob.scan());

View File

@ -1,5 +1,6 @@
import { createCanvas } from "@common/display/canvas";
import { gameLoop } from "@common/game";
import { bresenhamCircleGen } from "@common/navigation/bresenham";
import Physics from "@common/physics";
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) {
// because default circle fill is antialiased
// https://stackoverflow.com/a/45745753
xc = xc | 0;
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
for (const { x, y } of bresenhamCircleGen(xc, yc, r, { fill: true })) {
ctx.fillRect(x, y, 1, 1);
}
}