MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at type-hints-typescript 38 lines 909 B view raw
1import { dlopen, suffix, FFIType } from 'ant:ffi'; 2 3let libcName: string; 4if (process.platform === 'darwin') { 5 libcName = `libSystem.${suffix}`; 6} else if (process.platform === 'linux') { 7 libcName = `libc.${suffix}`; 8} else if (process.platform === 'win32') { 9 libcName = `msvcrt.${suffix}`; 10} else throw new Error(`Unsupported platform: ${process.platform}`); 11 12interface libC { 13 putchar(c: number): number; 14 printf(format: string, ...args: unknown[]): number; 15} 16 17const libc = dlopen<libC>(libcName); 18 19libc.define('putchar', { 20 args: [FFIType.int], 21 returns: FFIType.int 22}); 23 24libc.define('printf', { 25 args: [FFIType.string, FFIType.spread], 26 returns: FFIType.int 27}); 28 29console.log(libc); 30 31console.log('calling putchar(65):'); 32libc.putchar(65); // 'A' 33 34console.log('\ncalling printf:'); 35libc.printf('Hello FFI! I see %d\n', 42); 36 37console.log('calling putchar(66):'); 38libc.call('putchar', 66); // 'B'