iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐈

[Bun] Calling C's printf using FFI

に公開

Importing the bun:ffi module

// 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