|
| 1 | +{% callout %} |
| 2 | + |
| 3 | +This document is for maintainers and contributors to Bun, and describes internal implementation details. |
| 4 | + |
| 5 | +{% /callout %} |
| 6 | + |
| 7 | +The new bindings generator, introduced to the codebase in Dec 2024, scans for |
| 8 | +`*.bind.ts` to find function and class definition, and generates glue code to |
| 9 | +interop between JavaScript and native code. |
| 10 | + |
| 11 | +There are currently other code generators and systems that achieve similar |
| 12 | +purposes. The following will all eventually be completely phased out in favor of |
| 13 | +this one: |
| 14 | + |
| 15 | +- "Classes generator", converting `*.classes.ts` for custom classes. |
| 16 | +- "JS2Native", allowing ad-hoc calls from `src/js` to native code. |
| 17 | + |
| 18 | +## Creating JS Functions in Zig |
| 19 | + |
| 20 | +Given a file implementing a simple function, such as `add` |
| 21 | + |
| 22 | +```zig#src/bun.js/math.zig |
| 23 | +pub fn add(global: *JSC.JSGlobalObject, a: i32, b: i32) !i32 { |
| 24 | + return std.math.add(i32, a, b) catch { |
| 25 | + // Binding functions can return `error.OutOfMemory` and `error.JSError`. |
| 26 | + // Others like `error.Overflow` from `std.math.add` must be converted. |
| 27 | + // Remember to be descriptive. |
| 28 | + return global.throwPretty("Integer overflow while adding", .{}); |
| 29 | + }; |
| 30 | +} |
| 31 | +
|
| 32 | +const gen = bun.gen.math; // "math" being this file's basename |
| 33 | +
|
| 34 | +const std = @import("std"); |
| 35 | +const bun = @import("root").bun; |
| 36 | +const JSC = bun.JSC; |
| 37 | +``` |
| 38 | + |
| 39 | +Then describe the API schema using a `.bind.ts` function. The binding file goes next to the Zig file. |
| 40 | + |
| 41 | +```ts#src/bun.js/math.bind.ts |
| 42 | +import { t, fn } from 'bindgen'; |
| 43 | + |
| 44 | +export const add = fn({ |
| 45 | + args: { |
| 46 | + global: t.globalObject, |
| 47 | + a: t.i32, |
| 48 | + b: t.i32.default(1), |
| 49 | + }, |
| 50 | + ret: t.i32, |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +This function declaration is equivalent to: |
| 55 | + |
| 56 | +```ts |
| 57 | +/** |
| 58 | + * Throws if zero arguments are provided. |
| 59 | + * Wraps out of range numbers using modulo. |
| 60 | + */ |
| 61 | +declare function add(a: number, b: number = 1): number; |
| 62 | +``` |
| 63 | + |
| 64 | +The code generator will provide `bun.gen.math.jsAdd`, which is the native function implementation. To pass to JavaScript, use `bun.gen.math.createAddCallback(global)` |
| 65 | + |
| 66 | +## Strings |
| 67 | + |
| 68 | +The type for receiving strings is one of [`t.DOMString`](https://webidl.spec.whatwg.org/#idl-DOMString), [`t.ByteString`](https://webidl.spec.whatwg.org/#idl-ByteString), and [`t.USVString`](https://webidl.spec.whatwg.org/#idl-USVString). These map directly to their WebIDL counterparts, and have slightly different conversion logic. Bindgen will pass BunString to native code in all cases. |
| 69 | + |
| 70 | +When in doubt, use DOMString. |
| 71 | + |
| 72 | +`t.UTF8String` can be used in place of `t.DOMString`, but will call `bun.String.toUTF8`. The native callback gets `[]const u8` (WTF-8 data) passed to native code, freeing it after the function returns. |
| 73 | + |
| 74 | +TLDRs from WebIDL spec: |
| 75 | + |
| 76 | +- ByteString can only contain valid latin1 characters. It is not safe to assume bun.String is already in 8-bit format, but it is extremely likely. |
| 77 | +- USVString will not contain invalid surrogate pairs, aka text that can be represented correctly in UTF-8. |
| 78 | +- DOMString is the loosest but also most recommended strategy. |
| 79 | + |
| 80 | +## Function Variants |
| 81 | + |
| 82 | +A `variants` can specify multiple variants (also known as overloads). |
| 83 | + |
| 84 | +```ts#src/bun.js/math.bind.ts |
| 85 | +import { t, fn } from 'bindgen'; |
| 86 | + |
| 87 | +export const action = fn({ |
| 88 | + variants: [ |
| 89 | + { |
| 90 | + args: { |
| 91 | + a: t.i32, |
| 92 | + }, |
| 93 | + ret: t.i32, |
| 94 | + }, |
| 95 | + { |
| 96 | + args: { |
| 97 | + a: t.DOMString, |
| 98 | + }, |
| 99 | + ret: t.DOMString, |
| 100 | + }, |
| 101 | + ] |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +In Zig, each variant gets a number, based on the order the schema defines. |
| 106 | + |
| 107 | +``` |
| 108 | +fn action1(a: i32) i32 { |
| 109 | + return a; |
| 110 | +} |
| 111 | +
|
| 112 | +fn action2(a: bun.String) bun.String { |
| 113 | + return a; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## `t.dictionary` |
| 118 | + |
| 119 | +A `dictionary` is a definition for a JavaScript object, typically as a function inputs. For function outputs, it is usually a smarter idea to declare a class type to add functions and destructuring. |
| 120 | + |
| 121 | +## Enumerations |
| 122 | + |
| 123 | +To use [WebIDL's enumeration](https://webidl.spec.whatwg.org/#idl-enums) type, use either: |
| 124 | + |
| 125 | +- `t.stringEnum`: Create and codegen a new enum type. |
| 126 | +- `t.zigEnum`: Derive a bindgen type off of an existing enum in the codebase. |
| 127 | + |
| 128 | +An example of `stringEnum` as used in `fmt.zig` / `bun:internal-for-testing` |
| 129 | + |
| 130 | +```ts |
| 131 | +export const Formatter = t.stringEnum( |
| 132 | + "highlight-javascript", |
| 133 | + "escape-powershell", |
| 134 | +); |
| 135 | + |
| 136 | +export const fmtString = fn({ |
| 137 | + args: { |
| 138 | + global: t.globalObject, |
| 139 | + code: t.UTF8String, |
| 140 | + formatter: Formatter, |
| 141 | + }, |
| 142 | + ret: t.DOMString, |
| 143 | +}); |
| 144 | +``` |
| 145 | + |
| 146 | +WebIDL strongly encourages using kebab case for enumeration values, to be consistent with existing Web APIs. |
| 147 | + |
| 148 | +### Deriving enums from Zig code |
| 149 | + |
| 150 | +TODO: zigEnum |
| 151 | + |
| 152 | +## `t.oneOf` |
| 153 | + |
| 154 | +A `oneOf` is a union between two or more types. It is represented by `union(enum)` in Zig. |
| 155 | + |
| 156 | +TODO: |
| 157 | + |
| 158 | +## Attributes |
| 159 | + |
| 160 | +There are set of attributes that can be chained onto `t.*` types. On all types there are: |
| 161 | + |
| 162 | +- `.required`, in dictionary parameters only |
| 163 | +- `.optional`, in function arguments only |
| 164 | +- `.default(T)` |
| 165 | + |
| 166 | +When a value is optional, it is lowered to a Zig optional. |
| 167 | + |
| 168 | +Depending on the type, there are more attributes available. See the type definitions in auto-complete for more details. Note that one of the above three can only be applied, and they must be applied at the end. |
| 169 | + |
| 170 | +### Integer Attributes |
| 171 | + |
| 172 | +Integer types allow customizing the overflow behavior with `clamp` or `enforceRange` |
| 173 | + |
| 174 | +```ts |
| 175 | +import { t, fn } from "bindgen"; |
| 176 | + |
| 177 | +export const add = fn({ |
| 178 | + args: { |
| 179 | + global: t.globalObject, |
| 180 | + // enforce in i32 range |
| 181 | + a: t.i32.enforceRange(), |
| 182 | + // clamp to u16 range |
| 183 | + c: t.u16, |
| 184 | + // enforce in arbitrary range, with a default if not provided |
| 185 | + b: t.i32.enforceRange(0, 1000).default(5), |
| 186 | + // clamp to arbitrary range, or null |
| 187 | + d: t.u16.clamp(0, 10).optional, |
| 188 | + }, |
| 189 | + ret: t.i32, |
| 190 | +}); |
| 191 | +``` |
| 192 | + |
| 193 | +## Callbacks |
| 194 | + |
| 195 | +TODO |
| 196 | + |
| 197 | +## Classes |
| 198 | + |
| 199 | +TODO |
0 commit comments