Bun supports native code integration via N-API compatibility and its own FFI (Foreign Function Interface) via Bun.FFI.dlopen. Learn vocabulary for symbols definition, FFIType constants (ptr/i32/cstring), CString memory reading, N-API ABI stability across runtimes, and zero-copy ArrayBuffer data sharing.
0 / 5 completed
1 / 5
A developer writes a Bun native module (Napi addon). Which build system does Bun use to compile it?
Bun supports Node.js native addons (N-API) and uses node-gyp compatibility for existing native modules. Bun also provides Bun.FFI for calling native code directly without building a full Node addon. The bun build command with --compile can bundle JavaScript into a standalone executable but is distinct from native C++ addon compilation.
2 / 5
A developer uses Bun.FFI.dlopen to call a C function. What must they provide to define the function's signature?
Bun.FFI.dlopen requires a symbols object that describes each native function: its argument types (args array) and return type (returns) using Bun's FFI type constants (e.g., FFIType.i32, FFIType.ptr, FFIType.cstring). Bun uses this metadata to generate optimized JIT-compiled call stubs.
3 / 5
Bun's Bun.FFI.CString type is used to handle C strings returned from native functions. What does it do that a plain pointer number would not?
CString is a wrapper that reads a null-terminated C string from a native memory address and converts it to a JavaScript string. Without it, dlopen would return a raw integer pointer that JavaScript cannot directly use as a string. Memory management (freeing the C string) must still be handled manually if the native code allocates it.
4 / 5
A Node.js native addon uses N-API (node:napi). Does it require recompilation to run on Bun?
N-API was designed for ABI stability across Node.js versions and compatible runtimes. Bun implements N-API, so most existing Node.js native addons (compiled as .node files) work on Bun without recompilation. Modules that use Node.js internals beyond the N-API surface (libuv, V8 internals) may require adjustments.
5 / 5
A developer wants to pass a JavaScript ArrayBuffer to a native function via Bun FFI. Which FFI type represents a pointer to the buffer's underlying memory?
FFIType.ptr is used for pointer arguments, including pointers to ArrayBuffer data. Bun automatically passes the buffer's underlying memory address when a Buffer, TypedArray, or ArrayBuffer is passed to a function argument typed as ptr. This enables zero-copy data sharing between JavaScript and native code.