As the name implies this plugin allows you to import C files into typescript when using Bun.
This is mostly a proof of concept so no t all C code is supported — use at your own risk.
-
install
bunx jsr add @mastermakrela/bun-plugin-c
-
add
bun-plugin-c
to yourbunfig.toml
preload = ["@mastermakrela/bun-plugin-c"]
-
now in your typescript code you can import C files
import _lib from "./lib.c"; interface lib { // this interface will be printed in console when the import is resolved } const lib = _lib as lib; lib.hello();
The plugin analyzes the C to find all function declarations, then compiles the file and returns a module with all the functions.
It also generates interface based on the C* functions signatures.
They are printed in the console (for easy copy-pasting),
and are also available in the lib.__types
variable.
All functions from the C file are available both as direct imports and as default exports:
import { hello } from "./lib.c";
import lib from "./lib.c";
// both are the same
hello();
lib.hello();
// lib.c
int add(int a, float b) {
return a + (int)b; // Add the integer and truncated float
}
void double_number(int *x) {
*x = 2 * *x;
}
import lib, { double_number } from "./lib.c";
const sum = lib.add(1, 2.5); // sum is 3
const x = new Uint32Array(1);
x[0] = sum;
double_number(x); // sum is 6
- replace parser with native one using
onBeforeParse
- figure out better way to handle ts types