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.js
hello.c
hello.js
, it will print:
terminal
cc
uses TinyCC to compile the C code and then link it with the JavaScript runtime, efficiently converting types in-place.
Primitive types
The sameFFIType
values in dlopen
are supported in cc
.
FFIType | C Type | Aliases |
---|---|---|
cstring | char* | |
function | (void*)(*)() | fn , callback |
ptr | void* | pointer , void* , char* |
i8 | int8_t | int8_t |
i16 | int16_t | int16_t |
i32 | int32_t | int32_t , int |
i64 | int64_t | int64_t |
i64_fast | int64_t | |
u8 | uint8_t | uint8_t |
u16 | uint16_t | uint16_t |
u32 | uint32_t | uint32_t |
u64 | uint64_t | uint64_t |
u64_fast | uint64_t | |
f32 | float | float |
f64 | double | double |
bool | bool | |
char | char | |
napi_env | napi_env | |
napi_value | napi_value |
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.js
hello.c
hello.c
cc
Reference
library: string[]
The library
array is used to specify the libraries that should be linked with the C code.
symbols
The symbols
object is used to specify the functions and variables that should be exposed 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.
-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.