Something went wrong. Try again.
MIRROR: javascript for 🐜's, a tiny runtime with big ambitions ant.themackabu.dev
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839import { dlopen, suffix, FFIType } from 'ant:ffi';
let libcName: string;if (process.platform === 'darwin') { libcName = `libSystem.${suffix}`;} else if (process.platform === 'linux') { libcName = `libc.${suffix}`;} else if (process.platform === 'win32') { libcName = `msvcrt.${suffix}`;} else throw new Error(`Unsupported platform: ${process.platform}`);
interface libC { putchar(c: number): number; printf(format: string, ...args: unknown[]): number;}
const libc = dlopen<libC>(libcName);
libc.define('putchar', { args: [FFIType.int], returns: FFIType.int});
libc.define('printf', { args: [FFIType.string, FFIType.spread], returns: FFIType.int});
console.log(libc);
console.log('calling putchar(65):');libc.putchar(65); // 'A'
console.log('\ncalling printf:');libc.printf('Hello FFI! I see %d\n', 42);
console.log('calling putchar(66):');libc.call('putchar', 66); // 'B'