Skip to content

mastermakrela/bun-plugin-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published