Skip to main content
bun:ffi has experimental support for compiling and running C from JavaScript with low overhead.

Usage (cc in bun:ffi)

See the introduction blog post for more information. JavaScript:
hello.ts
C source:
hello.c
When you run hello.js, it will print:
terminal
Under the hood, cc uses TinyCC to compile the C code and then link it with the JavaScript runtime, efficiently converting types in-place.

Primitive types

The same FFIType values in dlopen are supported in cc.

Strings, objects, and non-primitive types

To make it easier to work with strings, objects, and other non-primitive types that don’t map 1:1 to C types, cc supports N-API. To pass or receive a JavaScript values without any type conversions from a C function, you can use napi_value. You can also pass a napi_env to receive the N-API environment used to call the JavaScript function.

Returning a C string to JavaScript

For example, if you have a string in C, you can return it to JavaScript like this:
hello.ts
And in C:
hello.c
You can also use this to return other types like objects and arrays:
hello.c

cc Reference

library: string[]

Use the library array to specify the libraries to link with the C code.

symbols

Use the symbols object to specify the functions and variables to expose to JavaScript.

source

The source is a file path to the C code that should be compiled and linked with the JavaScript runtime.

flags: string | string[]

The flags is an optional array of strings that should be passed to the TinyCC compiler.
These are flags like -I for include directories and -D for preprocessor definitions.

define: Record<string, string>

The define is an optional object that should be passed to the TinyCC compiler.
These are preprocessor definitions passed to the TinyCC compiler.