Skip to main content
Home

Built and signed on GitHub Actions

It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
29%
Published
10 months ago (0.1.0)

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

  1. install bunx jsr add @mastermakrela/bun-plugin-c

  2. add bun-plugin-c to your bunfig.toml

    preload = ["@mastermakrela/bun-plugin-c"]
    
  3. 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
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@mastermakrela/bun-plugin-c

Import symbol

import * as bun_plugin_c from "@mastermakrela/bun-plugin-c";
or

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
or (using pnpm 10.8 or older)
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
or (using Yarn 4.8 or older)
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";