bun-types 1.3.2-canary.20251106T140813 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FFI
|
|
3
|
+
description: Use Bun's FFI module to efficiently call native libraries from JavaScript
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Warning>
|
|
7
|
+
`bun:ffi` is **experimental**, with known bugs and limitations, and should not be relied on in production. The most
|
|
8
|
+
stable way to interact with native code from Bun is to write a [Node-API module](/runtime/node-api).
|
|
9
|
+
</Warning>
|
|
10
|
+
|
|
11
|
+
Use the built-in `bun:ffi` module to efficiently call native libraries from JavaScript. It works with languages that support the C ABI (Zig, Rust, C/C++, C#, Nim, Kotlin, etc).
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## dlopen usage (`bun:ffi`)
|
|
16
|
+
|
|
17
|
+
To print the version number of `sqlite3`:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { dlopen, FFIType, suffix } from "bun:ffi";
|
|
21
|
+
|
|
22
|
+
// `suffix` is either "dylib", "so", or "dll" depending on the platform
|
|
23
|
+
// you don't have to use "suffix", it's just there for convenience
|
|
24
|
+
const path = `libsqlite3.${suffix}`;
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
symbols: {
|
|
28
|
+
sqlite3_libversion, // the function to call
|
|
29
|
+
},
|
|
30
|
+
} = dlopen(
|
|
31
|
+
path, // a library name or file path
|
|
32
|
+
{
|
|
33
|
+
sqlite3_libversion: {
|
|
34
|
+
// no arguments, returns a string
|
|
35
|
+
args: [],
|
|
36
|
+
returns: FFIType.cstring,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
console.log(`SQLite 3 version: ${sqlite3_libversion()}`);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Performance
|
|
47
|
+
|
|
48
|
+
According to [our benchmark](https://github.com/oven-sh/bun/tree/main/bench/ffi), `bun:ffi` is roughly 2-6x faster than Node.js FFI via `Node-API`.
|
|
49
|
+
|
|
50
|
+
<Image src="/images/ffi.png" height="400" />
|
|
51
|
+
|
|
52
|
+
Bun generates & just-in-time compiles C bindings that efficiently convert values between JavaScript types and native types. To compile C, Bun embeds [TinyCC](https://github.com/TinyCC/tinycc), a small and fast C compiler.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
### Zig
|
|
59
|
+
|
|
60
|
+
```zig add.zig icon="file-code"
|
|
61
|
+
pub export fn add(a: i32, b: i32) i32 {
|
|
62
|
+
return a + b;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To compile:
|
|
67
|
+
|
|
68
|
+
```bash terminal icon="terminal"
|
|
69
|
+
zig build-lib add.zig -dynamic -OReleaseFast
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Pass a path to the shared library and a map of symbols to import into `dlopen`:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { dlopen, FFIType, suffix } from "bun:ffi";
|
|
76
|
+
const { i32 } = FFIType;
|
|
77
|
+
|
|
78
|
+
const path = `libadd.${suffix}`;
|
|
79
|
+
|
|
80
|
+
const lib = dlopen(path, {
|
|
81
|
+
add: {
|
|
82
|
+
args: [i32, i32],
|
|
83
|
+
returns: i32,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
console.log(lib.symbols.add(1, 2));
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Rust
|
|
91
|
+
|
|
92
|
+
```rust
|
|
93
|
+
// add.rs
|
|
94
|
+
#[no_mangle]
|
|
95
|
+
pub extern "C" fn add(a: i32, b: i32) -> i32 {
|
|
96
|
+
a + b
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
To compile:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
rustc --crate-type cdylib add.rs
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### C++
|
|
107
|
+
|
|
108
|
+
```c
|
|
109
|
+
#include <cstdint>
|
|
110
|
+
|
|
111
|
+
extern "C" int32_t add(int32_t a, int32_t b) {
|
|
112
|
+
return a + b;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
To compile:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
zig build-lib add.cpp -dynamic -lc -lc++
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## FFI types
|
|
125
|
+
|
|
126
|
+
The following `FFIType` values are supported.
|
|
127
|
+
|
|
128
|
+
| `FFIType` | C Type | Aliases |
|
|
129
|
+
| ---------- | -------------- | --------------------------- |
|
|
130
|
+
| buffer | `char*` | |
|
|
131
|
+
| cstring | `char*` | |
|
|
132
|
+
| function | `(void*)(*)()` | `fn`, `callback` |
|
|
133
|
+
| ptr | `void*` | `pointer`, `void*`, `char*` |
|
|
134
|
+
| i8 | `int8_t` | `int8_t` |
|
|
135
|
+
| i16 | `int16_t` | `int16_t` |
|
|
136
|
+
| i32 | `int32_t` | `int32_t`, `int` |
|
|
137
|
+
| i64 | `int64_t` | `int64_t` |
|
|
138
|
+
| i64_fast | `int64_t` | |
|
|
139
|
+
| u8 | `uint8_t` | `uint8_t` |
|
|
140
|
+
| u16 | `uint16_t` | `uint16_t` |
|
|
141
|
+
| u32 | `uint32_t` | `uint32_t` |
|
|
142
|
+
| u64 | `uint64_t` | `uint64_t` |
|
|
143
|
+
| u64_fast | `uint64_t` | |
|
|
144
|
+
| f32 | `float` | `float` |
|
|
145
|
+
| f64 | `double` | `double` |
|
|
146
|
+
| bool | `bool` | |
|
|
147
|
+
| char | `char` | |
|
|
148
|
+
| napi_env | `napi_env` | |
|
|
149
|
+
| napi_value | `napi_value` | |
|
|
150
|
+
|
|
151
|
+
Note: `buffer` arguments must be a `TypedArray` or `DataView`.
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Strings
|
|
156
|
+
|
|
157
|
+
JavaScript strings and C-like strings are different, and that complicates using strings with native libraries.
|
|
158
|
+
|
|
159
|
+
<Accordion title="How are JavaScript strings and C strings different?">
|
|
160
|
+
JavaScript strings:
|
|
161
|
+
|
|
162
|
+
- UTF16 (2 bytes per letter) or potentially latin1, depending on the JavaScript engine & what characters are used
|
|
163
|
+
- `length` stored separately
|
|
164
|
+
- Immutable
|
|
165
|
+
|
|
166
|
+
C strings:
|
|
167
|
+
|
|
168
|
+
- UTF8 (1 byte per letter), usually
|
|
169
|
+
- The length is not stored. Instead, the string is null-terminated which means the length is the index of the first `\0` it finds
|
|
170
|
+
- Mutable
|
|
171
|
+
|
|
172
|
+
</Accordion>
|
|
173
|
+
|
|
174
|
+
To solve this, `bun:ffi` exports `CString` which extends JavaScript's built-in `String` to support null-terminated strings and add a few extras:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
class CString extends String {
|
|
178
|
+
/**
|
|
179
|
+
* Given a `ptr`, this will automatically search for the closing `\0` character and transcode from UTF-8 to UTF-16 if necessary.
|
|
180
|
+
*/
|
|
181
|
+
constructor(ptr: number, byteOffset?: number, byteLength?: number): string;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The ptr to the C string
|
|
185
|
+
*
|
|
186
|
+
* This `CString` instance is a clone of the string, so it
|
|
187
|
+
* is safe to continue using this instance after the `ptr` has been
|
|
188
|
+
* freed.
|
|
189
|
+
*/
|
|
190
|
+
ptr: number;
|
|
191
|
+
byteOffset?: number;
|
|
192
|
+
byteLength?: number;
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
To convert from a null-terminated string pointer to a JavaScript string:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
const myString = new CString(ptr);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
To convert from a pointer with a known length to a JavaScript string:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
const myString = new CString(ptr, 0, byteLength);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The `new CString()` constructor clones the C string, so it is safe to continue using `myString` after `ptr` has been freed.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
my_library_free(myString.ptr);
|
|
212
|
+
|
|
213
|
+
// this is safe because myString is a clone
|
|
214
|
+
console.log(myString);
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
When used in `returns`, `FFIType.cstring` coerces the pointer to a JavaScript `string`. When used in `args`, `FFIType.cstring` is identical to `ptr`.
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## Function pointers
|
|
222
|
+
|
|
223
|
+
<Note>Async functions are not yet supported</Note>
|
|
224
|
+
|
|
225
|
+
To call a function pointer from JavaScript, use `CFunction`. This is useful if using Node-API (napi) with Bun, and you've already loaded some symbols.
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import { CFunction } from "bun:ffi";
|
|
229
|
+
|
|
230
|
+
let myNativeLibraryGetVersion = /* somehow, you got this pointer */
|
|
231
|
+
|
|
232
|
+
const getVersion = new CFunction({
|
|
233
|
+
returns: "cstring",
|
|
234
|
+
args: [],
|
|
235
|
+
ptr: myNativeLibraryGetVersion,
|
|
236
|
+
});
|
|
237
|
+
getVersion();
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
If you have multiple function pointers, you can define them all at once with `linkSymbols`:
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
import { linkSymbols } from "bun:ffi";
|
|
244
|
+
|
|
245
|
+
// getVersionPtrs defined elsewhere
|
|
246
|
+
const [majorPtr, minorPtr, patchPtr] = getVersionPtrs();
|
|
247
|
+
|
|
248
|
+
const lib = linkSymbols({
|
|
249
|
+
// Unlike with dlopen(), the names here can be whatever you want
|
|
250
|
+
getMajor: {
|
|
251
|
+
returns: "cstring",
|
|
252
|
+
args: [],
|
|
253
|
+
|
|
254
|
+
// Since this doesn't use dlsym(), you have to provide a valid ptr
|
|
255
|
+
// That ptr could be a number or a bigint
|
|
256
|
+
// An invalid pointer will crash your program.
|
|
257
|
+
ptr: majorPtr,
|
|
258
|
+
},
|
|
259
|
+
getMinor: {
|
|
260
|
+
returns: "cstring",
|
|
261
|
+
args: [],
|
|
262
|
+
ptr: minorPtr,
|
|
263
|
+
},
|
|
264
|
+
getPatch: {
|
|
265
|
+
returns: "cstring",
|
|
266
|
+
args: [],
|
|
267
|
+
ptr: patchPtr,
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const [major, minor, patch] = [lib.symbols.getMajor(), lib.symbols.getMinor(), lib.symbols.getPatch()];
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Callbacks
|
|
277
|
+
|
|
278
|
+
Use `JSCallback` to create JavaScript callback functions that can be passed to C/FFI functions. The C/FFI function can call into the JavaScript/TypeScript code. This is useful for asynchronous code or whenever you want to call into JavaScript code from C.
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
import { dlopen, JSCallback, ptr, CString } from "bun:ffi";
|
|
282
|
+
|
|
283
|
+
const {
|
|
284
|
+
symbols: { search },
|
|
285
|
+
close,
|
|
286
|
+
} = dlopen("libmylib", {
|
|
287
|
+
search: {
|
|
288
|
+
returns: "usize",
|
|
289
|
+
args: ["cstring", "callback"],
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const searchIterator = new JSCallback((ptr, length) => /hello/.test(new CString(ptr, length)), {
|
|
294
|
+
returns: "bool",
|
|
295
|
+
args: ["ptr", "usize"],
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const str = Buffer.from("wwutwutwutwutwutwutwutwutwutwutut\0", "utf8");
|
|
299
|
+
if (search(ptr(str), searchIterator)) {
|
|
300
|
+
// found a match!
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Sometime later:
|
|
304
|
+
setTimeout(() => {
|
|
305
|
+
searchIterator.close();
|
|
306
|
+
close();
|
|
307
|
+
}, 5000);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
When you're done with a JSCallback, you should call `close()` to free the memory.
|
|
311
|
+
|
|
312
|
+
### Experimental thread-safe callbacks
|
|
313
|
+
|
|
314
|
+
`JSCallback` has experimental support for thread-safe callbacks. This will be needed if you pass a callback function into a different thread from its instantiation context. You can enable it with the optional `threadsafe` parameter.
|
|
315
|
+
|
|
316
|
+
Currently, thread-safe callbacks work best when run from another thread that is running JavaScript code, i.e. a [`Worker`](/runtime/workers). A future version of Bun will enable them to be called from any thread (such as new threads spawned by your native library that Bun is not aware of).
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
const searchIterator = new JSCallback((ptr, length) => /hello/.test(new CString(ptr, length)), {
|
|
320
|
+
returns: "bool",
|
|
321
|
+
args: ["ptr", "usize"],
|
|
322
|
+
threadsafe: true, // Optional. Defaults to `false`
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
<Note>
|
|
327
|
+
**⚡️ Performance tip** — For a slight performance boost, directly pass `JSCallback.prototype.ptr` instead of the `JSCallback` object:
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
const onResolve = new JSCallback(arg => arg === 42, {
|
|
331
|
+
returns: "bool",
|
|
332
|
+
args: ["i32"],
|
|
333
|
+
});
|
|
334
|
+
const setOnResolve = new CFunction({
|
|
335
|
+
returns: "bool",
|
|
336
|
+
args: ["function"],
|
|
337
|
+
ptr: myNativeLibrarySetOnResolve,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// This code runs slightly faster:
|
|
341
|
+
setOnResolve(onResolve.ptr);
|
|
342
|
+
|
|
343
|
+
// Compared to this:
|
|
344
|
+
setOnResolve(onResolve);
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
</Note>
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## Pointers
|
|
352
|
+
|
|
353
|
+
Bun represents [pointers](<https://en.wikipedia.org/wiki/Pointer_(computer_programming)>) as a `number` in JavaScript.
|
|
354
|
+
|
|
355
|
+
<Accordion title="How does a 64 bit pointer fit in a JavaScript number?">
|
|
356
|
+
|
|
357
|
+
64-bit processors support up to [52 bits of addressable space](https://en.wikipedia.org/wiki/64-bit_computing#Limits_of_processors). [JavaScript numbers](https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64) support 53 bits of usable space, so that leaves us with about 11 bits of extra space.
|
|
358
|
+
|
|
359
|
+
**Why not `BigInt`?** `BigInt` is slower. JavaScript engines allocate a separate `BigInt` which means they can't fit into a regular JavaScript value. If you pass a `BigInt` to a function, it will be converted to a `number`
|
|
360
|
+
|
|
361
|
+
</Accordion>
|
|
362
|
+
|
|
363
|
+
To convert from a `TypedArray` to a pointer:
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
import { ptr } from "bun:ffi";
|
|
367
|
+
let myTypedArray = new Uint8Array(32);
|
|
368
|
+
const myPtr = ptr(myTypedArray);
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
To convert from a pointer to an `ArrayBuffer`:
|
|
372
|
+
|
|
373
|
+
```ts
|
|
374
|
+
import { ptr, toArrayBuffer } from "bun:ffi";
|
|
375
|
+
let myTypedArray = new Uint8Array(32);
|
|
376
|
+
const myPtr = ptr(myTypedArray);
|
|
377
|
+
|
|
378
|
+
// toArrayBuffer accepts a `byteOffset` and `byteLength`
|
|
379
|
+
// if `byteLength` is not provided, it is assumed to be a null-terminated pointer
|
|
380
|
+
myTypedArray = new Uint8Array(toArrayBuffer(myPtr, 0, 32), 0, 32);
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
To read data from a pointer, you have two options. For long-lived pointers, use a `DataView`:
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
import { toArrayBuffer } from "bun:ffi";
|
|
387
|
+
let myDataView = new DataView(toArrayBuffer(myPtr, 0, 32));
|
|
388
|
+
|
|
389
|
+
console.log(
|
|
390
|
+
myDataView.getUint8(0, true),
|
|
391
|
+
myDataView.getUint8(1, true),
|
|
392
|
+
myDataView.getUint8(2, true),
|
|
393
|
+
myDataView.getUint8(3, true),
|
|
394
|
+
);
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
For short-lived pointers, use `read`:
|
|
398
|
+
|
|
399
|
+
```ts
|
|
400
|
+
import { read } from "bun:ffi";
|
|
401
|
+
|
|
402
|
+
console.log(
|
|
403
|
+
// ptr, byteOffset
|
|
404
|
+
read.u8(myPtr, 0),
|
|
405
|
+
read.u8(myPtr, 1),
|
|
406
|
+
read.u8(myPtr, 2),
|
|
407
|
+
read.u8(myPtr, 3),
|
|
408
|
+
);
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
The `read` function behaves similarly to `DataView`, but it's usually faster because it doesn't need to create a `DataView` or `ArrayBuffer`.
|
|
412
|
+
|
|
413
|
+
| `FFIType` | `read` function |
|
|
414
|
+
| --------- | --------------- |
|
|
415
|
+
| ptr | `read.ptr` |
|
|
416
|
+
| i8 | `read.i8` |
|
|
417
|
+
| i16 | `read.i16` |
|
|
418
|
+
| i32 | `read.i32` |
|
|
419
|
+
| i64 | `read.i64` |
|
|
420
|
+
| u8 | `read.u8` |
|
|
421
|
+
| u16 | `read.u16` |
|
|
422
|
+
| u32 | `read.u32` |
|
|
423
|
+
| u64 | `read.u64` |
|
|
424
|
+
| f32 | `read.f32` |
|
|
425
|
+
| f64 | `read.f64` |
|
|
426
|
+
|
|
427
|
+
### Memory management
|
|
428
|
+
|
|
429
|
+
`bun:ffi` does not manage memory for you. You must free the memory when you're done with it.
|
|
430
|
+
|
|
431
|
+
#### From JavaScript
|
|
432
|
+
|
|
433
|
+
If you want to track when a `TypedArray` is no longer in use from JavaScript, you can use a [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry).
|
|
434
|
+
|
|
435
|
+
#### From C, Rust, Zig, etc
|
|
436
|
+
|
|
437
|
+
If you want to track when a `TypedArray` is no longer in use from C or FFI, you can pass a callback and an optional context pointer to `toArrayBuffer` or `toBuffer`. This function is called at some point later, once the garbage collector frees the underlying `ArrayBuffer` JavaScript object.
|
|
438
|
+
|
|
439
|
+
The expected signature is the same as in [JavaScriptCore's C API](https://developer.apple.com/documentation/javascriptcore/jstypedarraybytesdeallocator?language=objc):
|
|
440
|
+
|
|
441
|
+
```c
|
|
442
|
+
typedef void (*JSTypedArrayBytesDeallocator)(void *bytes, void *deallocatorContext);
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
```ts
|
|
446
|
+
import { toArrayBuffer } from "bun:ffi";
|
|
447
|
+
|
|
448
|
+
// with a deallocatorContext:
|
|
449
|
+
toArrayBuffer(
|
|
450
|
+
bytes,
|
|
451
|
+
byteOffset,
|
|
452
|
+
|
|
453
|
+
byteLength,
|
|
454
|
+
|
|
455
|
+
// this is an optional pointer to a callback
|
|
456
|
+
deallocatorContext,
|
|
457
|
+
|
|
458
|
+
// this is a pointer to a function
|
|
459
|
+
jsTypedArrayBytesDeallocator,
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
// without a deallocatorContext:
|
|
463
|
+
toArrayBuffer(
|
|
464
|
+
bytes,
|
|
465
|
+
byteOffset,
|
|
466
|
+
|
|
467
|
+
byteLength,
|
|
468
|
+
|
|
469
|
+
// this is a pointer to a function
|
|
470
|
+
jsTypedArrayBytesDeallocator,
|
|
471
|
+
);
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
### Memory safety
|
|
475
|
+
|
|
476
|
+
Using raw pointers outside of FFI is extremely not recommended. A future version of Bun may add a CLI flag to disable `bun:ffi`.
|
|
477
|
+
|
|
478
|
+
### Pointer alignment
|
|
479
|
+
|
|
480
|
+
If an API expects a pointer sized to something other than `char` or `u8`, make sure the `TypedArray` is also that size. A `u64*` is not exactly the same as `[8]u8*` due to alignment.
|
|
481
|
+
|
|
482
|
+
### Passing a pointer
|
|
483
|
+
|
|
484
|
+
Where FFI functions expect a pointer, pass a `TypedArray` of equivalent size:
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
import { dlopen, FFIType } from "bun:ffi";
|
|
488
|
+
|
|
489
|
+
const {
|
|
490
|
+
symbols: { encode_png },
|
|
491
|
+
} = dlopen(myLibraryPath, {
|
|
492
|
+
encode_png: {
|
|
493
|
+
// FFIType's can be specified as strings too
|
|
494
|
+
args: ["ptr", "u32", "u32"],
|
|
495
|
+
returns: FFIType.ptr,
|
|
496
|
+
},
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
const pixels = new Uint8ClampedArray(128 * 128 * 4);
|
|
500
|
+
pixels.fill(254);
|
|
501
|
+
pixels.subarray(0, 32 * 32 * 2).fill(0);
|
|
502
|
+
|
|
503
|
+
const out = encode_png(
|
|
504
|
+
// pixels will be passed as a pointer
|
|
505
|
+
pixels,
|
|
506
|
+
|
|
507
|
+
128,
|
|
508
|
+
128,
|
|
509
|
+
);
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
The [auto-generated wrapper](https://github.com/oven-sh/bun/blob/6a65631cbdcae75bfa1e64323a6ad613a922cd1a/src/bun.js/ffi.exports.js#L180-L182) converts the pointer to a `TypedArray`.
|
|
513
|
+
|
|
514
|
+
<Accordion title="Hardmode">
|
|
515
|
+
|
|
516
|
+
If you don't want the automatic conversion or you want a pointer to a specific byte offset within the `TypedArray`, you can also directly get the pointer to the `TypedArray`:
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
import { dlopen, FFIType, ptr } from "bun:ffi";
|
|
520
|
+
|
|
521
|
+
const {
|
|
522
|
+
symbols: { encode_png },
|
|
523
|
+
} = dlopen(myLibraryPath, {
|
|
524
|
+
encode_png: {
|
|
525
|
+
// FFIType's can be specified as strings too
|
|
526
|
+
args: ["ptr", "u32", "u32"],
|
|
527
|
+
returns: FFIType.ptr,
|
|
528
|
+
},
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
const pixels = new Uint8ClampedArray(128 * 128 * 4);
|
|
532
|
+
pixels.fill(254);
|
|
533
|
+
|
|
534
|
+
// this returns a number! not a BigInt!
|
|
535
|
+
const myPtr = ptr(pixels);
|
|
536
|
+
|
|
537
|
+
const out = encode_png(
|
|
538
|
+
myPtr,
|
|
539
|
+
|
|
540
|
+
// dimensions:
|
|
541
|
+
128,
|
|
542
|
+
128,
|
|
543
|
+
);
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
</Accordion>
|
|
547
|
+
|
|
548
|
+
### Reading pointers
|
|
549
|
+
|
|
550
|
+
```ts
|
|
551
|
+
const out = encode_png(
|
|
552
|
+
// pixels will be passed as a pointer
|
|
553
|
+
pixels,
|
|
554
|
+
|
|
555
|
+
// dimensions:
|
|
556
|
+
128,
|
|
557
|
+
128,
|
|
558
|
+
);
|
|
559
|
+
|
|
560
|
+
// assuming it is 0-terminated, it can be read like this:
|
|
561
|
+
let png = new Uint8Array(toArrayBuffer(out));
|
|
562
|
+
|
|
563
|
+
// save it to disk:
|
|
564
|
+
await Bun.write("out.png", png);
|
|
565
|
+
```
|