bun-types 1.1.39-canary.20241210T140601 → 1.1.39-canary.20241212T140641

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/ambient.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ declare module "*.txt" {
2
+ var text: string;
3
+ export = text;
4
+ }
5
+
6
+ declare module "*.toml" {
7
+ var contents: any;
8
+ export = contents;
9
+ }
@@ -0,0 +1,199 @@
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
package/globals.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- export {};
2
-
3
1
  type _ReadableStream<T> = typeof globalThis extends {
4
2
  onerror: any;
5
3
  ReadableStream: infer T;
@@ -146,16 +144,6 @@ import type {
146
144
  import type { MessagePort } from "worker_threads";
147
145
  import type { WebSocket as _WebSocket } from "ws";
148
146
 
149
- declare module "*.txt" {
150
- var text: string;
151
- export = text;
152
- }
153
-
154
- declare module "*.toml" {
155
- var contents: any;
156
- export = contents;
157
- }
158
-
159
147
  declare global {
160
148
  var Bun: typeof import("bun");
161
149
 
@@ -1944,10 +1932,10 @@ declare global {
1944
1932
  readonly main: boolean;
1945
1933
 
1946
1934
  /** Alias of `import.meta.dir`. Exists for Node.js compatibility */
1947
- readonly dirname: string;
1935
+ dirname: string;
1948
1936
 
1949
1937
  /** Alias of `import.meta.path`. Exists for Node.js compatibility */
1950
- readonly filename: string;
1938
+ filename: string;
1951
1939
  }
1952
1940
 
1953
1941
  /**
package/index.d.ts CHANGED
@@ -20,3 +20,4 @@
20
20
  /// <reference path="./sqlite.d.ts" />
21
21
  /// <reference path="./wasm.d.ts" />
22
22
  /// <reference path="./deprecated.d.ts" />
23
+ /// <reference path="./ambient.d.ts" />
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.39-canary.20241210T140601",
2
+ "version": "1.1.39-canary.20241212T140641",
3
3
  "name": "bun-types",
4
4
  "license": "MIT",
5
5
  "main": "",
package/sqlite.d.ts CHANGED
@@ -1152,7 +1152,7 @@ declare module "bun:sqlite" {
1152
1152
  *
1153
1153
  * @since Bun v1.1.14
1154
1154
  */
1155
- interface Changes {
1155
+ export interface Changes {
1156
1156
  /**
1157
1157
  * The number of rows changed by the last `run` or `exec` call.
1158
1158
  */