@mastermakrela/bun-plugin-c@0.1.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Bun C Plugin
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.
Usage
-
install
bunx jsr add @mastermakrela/bun-plugin-c -
add
bun-plugin-cto yourbunfig.tomlpreload = ["@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();
How it works
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();
Simple Example
// 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
TODOs
- replace parser with native one using
onBeforeParse - figure out better way to handle ts types
Add Package
deno add jsr:@mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
Import directly with a jsr specifier
import * as bun_plugin_c from "jsr:@mastermakrela/bun-plugin-c";
Add Package
pnpm i jsr:@mastermakrela/bun-plugin-c
pnpm dlx jsr add @mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
Add Package
yarn add jsr:@mastermakrela/bun-plugin-c
yarn dlx jsr add @mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
Add Package
vlt install jsr:@mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
Add Package
npx jsr add @mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
Add Package
bunx jsr add @mastermakrela/bun-plugin-c
Import symbol
import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";