Map of symbols to define where the key is the symbol name and the value is the symbol value
Equivalent to -D
option in gcc/clang.
Module
The 'bun:ffi'
module enables high-performance calls to native libraries from JavaScript. It works with languages that support the C ABI (Zig, Rust, C/C++, C#, Nim, Kotlin, etc).
Bun generates and just-in-time compiles C bindings that efficiently convert values between JavaScript types and native types, using embedded TinyCC (a small and fast C compiler). According to benchmarks, bun:ffi is roughly 2-6x faster than Node.js FFI via Node-API.
⚠️ Warning — bun:ffi is experimental, with known bugs and limitations, and should not be relied on in production. The most stable way to interact with native code from Bun is to write a Node-API module.
Map of symbols to define where the key is the symbol name and the value is the symbol value
Equivalent to -D
option in gcc/clang.
Flags to pass to the compiler. Note: we do not make gurantees about which specific version of the compiler is used.
Include directories to pass to the compiler
Equivalent to -I
option in gcc/clang.
Library names to link against
Equivalent to -l
option in gcc/clang.
Map of symbols to load where the key is the symbol name and the value is the FFIFunction
Experimental: Compile ISO C11 source code using TinyCC, and make symbols available as functions to JavaScript.
Library<Fns>
Free the memory allocated by the wrapping function
Turn a native library's function pointer into a JavaScript function
Libraries using Node-API & bun:ffi in the same module could use this to skip an extra dlopen() step.
FFIFunction declaration. ptr
is required
Get a string from a UTF-8 encoded C string If byteLength
is not provided, the string is assumed to be null-terminated.
Return the String value whose elements are, in order, the elements in the List elements. If length is 0, the empty string is returned.
String.raw is usually used as a tag function of a Tagged Template String. When called as such, the first argument will be a well formed template call site object and the rest parameter will contain the substitution values. It can also be called directly, for example, to interleave strings and values from your own tag function, and in this case the only thing it needs from the first argument is the raw property.
A well-formed template string call site representation.
A set of substitution values.
Get a string from a UTF-8 encoded C string If byteLength
is not provided, the string is assumed to be null-terminated.
The pointer to the C string
bytes to skip before reading
bytes to read
Returns the length of a String object.
The ptr to the C string
This CString
instance is a clone of the string, so it is safe to continue using this instance after the ptr
has been freed.
Returns a new String consisting of the single UTF-16 code unit located at the specified index.
The zero-based index of the desired code unit. A negative index will count back from the last item.
Returns the character at the specified index.
The zero-based index of the desired character.
Returns the Unicode value of the character at the specified location.
The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
Returns a nonnegative integer Number less than 1114112 (0x110000) that is the code point value of the UTF-16 encoded code point starting at the string element at position pos in the String resulting from converting this object to a String. If there is no element at that position, the result is undefined. If a valid UTF-16 surrogate pair does not begin at pos, the result is the code unit at pos.
Returns a string that contains the concatenation of two or more strings.
The strings to append to the end of the string.
Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at endPosition – length(this). Otherwise returns false.
Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.
search string
If position is undefined, 0 is assumed, so as to search all of the String.
Returns the position of the first occurrence of a substring.
The substring to search for in the string
The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.
Returns true if all leading surrogates and trailing surrogates appear paired and in order.
Returns the last occurrence of a substring in the string.
The substring to search for.
The index at which to begin searching. If omitted, the search begins at the end of the string.
Determines whether two strings are equivalent in the current locale.
String to compare to target string
Matches a string with a regular expression, and returns an array containing the results of that search.
A variable name or string literal containing the regular expression pattern and flags.
Matches a string with a regular expression, and returns an iterable of matches containing the results of that search.
A variable name or string literal containing the regular expression pattern and flags.
Returns the String value result of normalizing the string into the normalization form named by form as specified in Unicode Standard Annex #15, Unicode Normalization Forms.
Applicable values: "NFC", "NFD", "NFKC", or "NFKD", If not specified default is "NFC"
Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string will be returned as it is.
The string to pad the current string with. If this string is too long, it will be truncated and the left-most part will be applied. The default value for this parameter is " " (U+0020).
Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string will be returned as it is.
The string to pad the current string with. If this string is too long, it will be truncated and the left-most part will be applied. The default value for this parameter is " " (U+0020).
Returns a String value that is made from count copies appended together. If count is 0, the empty string is returned.
number of copies to append
Replaces text in a string, using a regular expression or search string.
A string or regular expression to search for.
A string containing the text to replace. When the searchValue is a RegExp
, all matches are replaced if the g
flag is set (or only those matches at the beginning, if the y
flag is also present). Otherwise, only the first match of searchValue is replaced.
Replace all instances of a substring in a string, using a regular expression or search string.
A string to search for.
A string containing the text to replace for every successful match of searchValue in this string.
Finds the first substring match in a regular expression search.
The regular expression pattern and applicable flags.
Returns a section of a string.
The index to the beginning of the specified portion of stringObj.
The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. If this value is not specified, the substring continues to the end of stringObj.
Split a string into substrings using the specified separator and return them as an array.
A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
A value used to limit the number of elements returned in the array.
Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.
Returns the substring at the specified location within a String object.
The zero-based index number indicating the beginning of the substring.
Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end. If end is omitted, the characters from start through the end of the original string are returned.
Converts all alphabetic characters to lowercase, taking into account the host environment's current locale.
Returns a string where all alphabetic characters have been converted to uppercase, taking into account the host environment's current locale.
Returns a string where all lone or out-of-order surrogates have been replaced by the Unicode replacement character (U+FFFD).
Removes the leading and trailing white space and line terminator characters from a string.
Open a library using "bun:ffi"
The name of the library or file path. This will be passed to dlopen()
Map of symbols to load where the key is the symbol name and the value is the FFIFunction
Pass a JavaScript function to FFI (Foreign Function Interface)
Enable a JavaScript callback function to be passed to C with bun:ffi
The JavaScript function to be called
The C function definition
The pointer to the C function
Becomes null
once JSCallback.prototype.close is called
Can the callback be called from a different thread?
Free the memory allocated for the callback
If called multiple times, does nothing after the first call.
Link a map of symbols to JavaScript functions
This lets you use native libraries that were already loaded somehow. You usually will want dlopen instead.
You could use this with Node-API to skip loading a second time.
Map of symbols to load where the key is the symbol name and the value is the FFIFunction
Get the pointer backing a TypedArray or ArrayBuffer
Use this to pass TypedArray or ArrayBuffer to C functions.
This is for use with FFI functions. For performance reasons, FFI will not automatically convert typed arrays to C pointers.
the typed array or array buffer to get the pointer for
optional offset into the view in bytes
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
The read function behaves similarly to DataView, but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
The memory address to read
bytes to skip before reading
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
Platform-specific file extension name for dynamic libraries
"." is not included
Read a pointer as an ArrayBuffer
If byteLength
is not provided, the pointer is assumed to be 0-terminated.
The memory address to read
bytes to skip before reading
bytes to read
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
Read a pointer as a Buffer
If byteLength
is not provided, the pointer is assumed to be 0-terminated.
The memory address to read
bytes to skip before reading
bytes to read
While there are some checks to catch invalid pointers, this is a difficult thing to do safely. Passing an invalid pointer can crash the program and reading beyond the bounds of the pointer will crash the program or cause undefined behavior. Use with care!
View the generated C code for FFI bindings
You probably won't need this unless there's a bug in the FFI bindings generator or you're just curious.
Arguments to a FFI function (C ABI)
Defaults to an empty array, which means no arguments.
To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see ptr.
Function pointer to the native function
If provided, instead of using dlsym() to lookup the function, Bun will use this instead. This pointer should not be null (0).
This is useful if the library has already been loaded or if the module is also using Node-API.
Return type to a FFI function (C ABI)
Defaults to FFIType.void
To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see ptr.
Can C/FFI code call this function from a separate thread?
Only supported with JSCallback.
This does not make the function run in a separate thread. It is still up to the application/library to run their code in a separate thread.
By default, JSCallback calls are not thread-safe. Turning this on incurs a small performance penalty for every function call. That small performance penalty needs to be less than the performance gain from running the function in a separate thread.