Skip to content

Bun's Foreign Function Interface (FFI) lets you call native C/C++ code directly from JavaScript with near-zero overhead. This is perfect when you need performance-critical operations or want to leverage existing C++ libraries.

Notifications You must be signed in to change notification settings

calumk/example-repo-bun-ffi-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bun FFI Experiment

A minimal example of calling C++ functions from Bun using FFI.

Structure

  • cpp/cpp_math.cpp - C++ source with a simple add() function
  • build_cpp.sh - Compiles C++ to a shared library
  • bun/index.js - Bun script that calls the C++ function via FFI
  • build_bun.sh - Compiles the Bun script into a standalone executable, including the C++ shared library

Usage

  1. Build the C++ library:
./build_cpp.sh
  1. Run the Bun script:
bun run bun/index.js
  1. Build the Bun executable:
./build_bun.sh

Run the Bun executable:

./dist/bun_binary

Expected output:

C++ Add: 10 + 20 = 30
C++ Multiply: 10 * 20 = 200
JS Add: 10 + 20 = 30
JS Multiply: 10 * 20 = 200

About

Bun's Foreign Function Interface (FFI) lets you call native C/C++ code directly from JavaScript with near-zero overhead. This is perfect when you need performance-critical operations or want to leverage existing C++ libraries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published