🐈
【Bun】FFI で C 言語の printf を呼び出す
bun:ffi
モジュールを読み込む
// https://bun.sh/docs/api/ffi
import { dlopen, FFIType, suffix } from "bun:ffi";
// Linux
const path = `libc.${suffix}.6`;
// macOS
// const = path = `libSystem.B.$[suffix]`;
const {
symbols: { printf }
} = dlopen(
path,
{
printf: { args: [FFIType.cstring, FFIType.cstring] }
}
);
// https://bun.sh/docs/api/binary-data
const format = new TextEncoder().encode("%s\n");
const str = new TextEncoder().encode("hello World");
// const str = Buffer.from("Hello World");
printf(format, str);
Discussion