bun-types 1.2.6 → 1.2.7
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/README.md +1 -3
- package/bun.d.ts +7186 -7604
- package/bun.ns.d.ts +7 -0
- package/deprecated.d.ts +53 -58
- package/devserver.d.ts +177 -183
- package/docs/api/cookie.md +449 -0
- package/docs/api/fetch.md +1 -1
- package/docs/api/http.md +78 -0
- package/docs/api/spawn.md +1 -1
- package/docs/cli/publish.md +1 -1
- package/docs/guides/ecosystem/nuxt.md +1 -1
- package/docs/guides/install/add-peer.md +2 -2
- package/docs/guides/install/from-npm-install-to-bun-install.md +1 -1
- package/docs/guides/runtime/typescript.md +7 -6
- package/docs/guides/test/run-tests.md +3 -3
- package/docs/guides/test/snapshot.md +3 -3
- package/docs/guides/test/update-snapshots.md +1 -1
- package/docs/guides/util/version.md +1 -1
- package/docs/installation.md +4 -4
- package/docs/runtime/debugger.md +3 -3
- package/docs/runtime/nodejs-apis.md +1 -1
- package/docs/test/dom.md +1 -1
- package/docs/typescript.md +7 -6
- package/extensions.d.ts +31 -0
- package/fetch.d.ts +62 -159
- package/ffi.d.ts +1084 -1108
- package/globals.d.ts +1721 -1949
- package/html-rewriter.d.ts +146 -151
- package/index.d.ts +13 -11
- package/jsc.d.ts +216 -230
- package/overrides.d.ts +106 -63
- package/package.json +39 -31
- package/s3.d.ts +825 -0
- package/sqlite.d.ts +1143 -1172
- package/test.d.ts +2130 -2241
- package/wasm.d.ts +190 -288
- package/ambient.d.ts +0 -31
package/ffi.d.ts
CHANGED
|
@@ -15,1162 +15,1138 @@
|
|
|
15
15
|
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
|
|
16
16
|
*/
|
|
17
17
|
declare module "bun:ffi" {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
18
|
+
enum FFIType {
|
|
19
|
+
char = 0,
|
|
20
|
+
/**
|
|
21
|
+
* 8-bit signed integer
|
|
22
|
+
*
|
|
23
|
+
* Must be a value between -127 and 127
|
|
24
|
+
*
|
|
25
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
26
|
+
*
|
|
27
|
+
* In C:
|
|
28
|
+
* ```c
|
|
29
|
+
* signed char
|
|
30
|
+
* char // on x64 & aarch64 macOS
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* In JavaScript:
|
|
34
|
+
* ```js
|
|
35
|
+
* var num = 0;
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
int8_t = 1,
|
|
39
|
+
/**
|
|
40
|
+
* 8-bit signed integer
|
|
41
|
+
*
|
|
42
|
+
* Must be a value between -127 and 127
|
|
43
|
+
*
|
|
44
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
45
|
+
*
|
|
46
|
+
* In C:
|
|
47
|
+
* ```c
|
|
48
|
+
* signed char
|
|
49
|
+
* char // on x64 & aarch64 macOS
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* In JavaScript:
|
|
53
|
+
* ```js
|
|
54
|
+
* var num = 0;
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
i8 = 1,
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
59
|
+
/**
|
|
60
|
+
* 8-bit unsigned integer
|
|
61
|
+
*
|
|
62
|
+
* Must be a value between 0 and 255
|
|
63
|
+
*
|
|
64
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
65
|
+
*
|
|
66
|
+
* In C:
|
|
67
|
+
* ```c
|
|
68
|
+
* unsigned char
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* In JavaScript:
|
|
72
|
+
* ```js
|
|
73
|
+
* var num = 0;
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
uint8_t = 2,
|
|
77
|
+
/**
|
|
78
|
+
* 8-bit unsigned integer
|
|
79
|
+
*
|
|
80
|
+
* Must be a value between 0 and 255
|
|
81
|
+
*
|
|
82
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
83
|
+
*
|
|
84
|
+
* In C:
|
|
85
|
+
* ```c
|
|
86
|
+
* unsigned char
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* In JavaScript:
|
|
90
|
+
* ```js
|
|
91
|
+
* var num = 0;
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
u8 = 2,
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
96
|
+
/**
|
|
97
|
+
* 16-bit signed integer
|
|
98
|
+
*
|
|
99
|
+
* Must be a value between -32768 and 32767
|
|
100
|
+
*
|
|
101
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
102
|
+
*
|
|
103
|
+
* In C:
|
|
104
|
+
* ```c
|
|
105
|
+
* in16_t
|
|
106
|
+
* short // on arm64 & x64
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* In JavaScript:
|
|
110
|
+
* ```js
|
|
111
|
+
* var num = 0;
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
int16_t = 3,
|
|
115
|
+
/**
|
|
116
|
+
* 16-bit signed integer
|
|
117
|
+
*
|
|
118
|
+
* Must be a value between -32768 and 32767
|
|
119
|
+
*
|
|
120
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
121
|
+
*
|
|
122
|
+
* In C:
|
|
123
|
+
* ```c
|
|
124
|
+
* in16_t
|
|
125
|
+
* short // on arm64 & x64
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* In JavaScript:
|
|
129
|
+
* ```js
|
|
130
|
+
* var num = 0;
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
i16 = 3,
|
|
134
134
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
135
|
+
/**
|
|
136
|
+
* 16-bit unsigned integer
|
|
137
|
+
*
|
|
138
|
+
* Must be a value between 0 and 65535, inclusive.
|
|
139
|
+
*
|
|
140
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
141
|
+
*
|
|
142
|
+
* In C:
|
|
143
|
+
* ```c
|
|
144
|
+
* uint16_t
|
|
145
|
+
* unsigned short // on arm64 & x64
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* In JavaScript:
|
|
149
|
+
* ```js
|
|
150
|
+
* var num = 0;
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
uint16_t = 4,
|
|
154
|
+
/**
|
|
155
|
+
* 16-bit unsigned integer
|
|
156
|
+
*
|
|
157
|
+
* Must be a value between 0 and 65535, inclusive.
|
|
158
|
+
*
|
|
159
|
+
* When passing to a FFI function (C ABI), type coercion is not performed.
|
|
160
|
+
*
|
|
161
|
+
* In C:
|
|
162
|
+
* ```c
|
|
163
|
+
* uint16_t
|
|
164
|
+
* unsigned short // on arm64 & x64
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* In JavaScript:
|
|
168
|
+
* ```js
|
|
169
|
+
* var num = 0;
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
u16 = 4,
|
|
173
173
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
/**
|
|
175
|
+
* 32-bit signed integer
|
|
176
|
+
*/
|
|
177
|
+
int32_t = 5,
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
179
|
+
/**
|
|
180
|
+
* 32-bit signed integer
|
|
181
|
+
*
|
|
182
|
+
* Alias of {@link FFIType.int32_t}
|
|
183
|
+
*/
|
|
184
|
+
i32 = 5,
|
|
185
|
+
/**
|
|
186
|
+
* 32-bit signed integer
|
|
187
|
+
*
|
|
188
|
+
* The same as `int` in C
|
|
189
|
+
*
|
|
190
|
+
* ```c
|
|
191
|
+
* int
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
int = 5,
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
196
|
+
/**
|
|
197
|
+
* 32-bit unsigned integer
|
|
198
|
+
*
|
|
199
|
+
* The same as `unsigned int` in C (on x64 & arm64)
|
|
200
|
+
*
|
|
201
|
+
* C:
|
|
202
|
+
* ```c
|
|
203
|
+
* unsigned int
|
|
204
|
+
* ```
|
|
205
|
+
* JavaScript:
|
|
206
|
+
* ```js
|
|
207
|
+
* ptr(new Uint32Array(1))
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
uint32_t = 6,
|
|
211
|
+
/**
|
|
212
|
+
* 32-bit unsigned integer
|
|
213
|
+
*
|
|
214
|
+
* Alias of {@link FFIType.uint32_t}
|
|
215
|
+
*/
|
|
216
|
+
u32 = 6,
|
|
217
217
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
218
|
+
/**
|
|
219
|
+
* int64 is a 64-bit signed integer
|
|
220
|
+
*
|
|
221
|
+
* This is not implemented yet!
|
|
222
|
+
*/
|
|
223
|
+
int64_t = 7,
|
|
224
|
+
/**
|
|
225
|
+
* i64 is a 64-bit signed integer
|
|
226
|
+
*
|
|
227
|
+
* This is not implemented yet!
|
|
228
|
+
*/
|
|
229
|
+
i64 = 7,
|
|
230
230
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
231
|
+
/**
|
|
232
|
+
* 64-bit unsigned integer
|
|
233
|
+
*
|
|
234
|
+
* This is not implemented yet!
|
|
235
|
+
*/
|
|
236
|
+
uint64_t = 8,
|
|
237
|
+
/**
|
|
238
|
+
* 64-bit unsigned integer
|
|
239
|
+
*
|
|
240
|
+
* This is not implemented yet!
|
|
241
|
+
*/
|
|
242
|
+
u64 = 8,
|
|
243
243
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
244
|
+
/**
|
|
245
|
+
* Doubles are not supported yet!
|
|
246
|
+
*/
|
|
247
|
+
double = 9,
|
|
248
|
+
/**
|
|
249
|
+
* Doubles are not supported yet!
|
|
250
|
+
*/
|
|
251
|
+
f64 = 9,
|
|
252
|
+
/**
|
|
253
|
+
* Floats are not supported yet!
|
|
254
|
+
*/
|
|
255
|
+
float = 10,
|
|
256
|
+
/**
|
|
257
|
+
* Floats are not supported yet!
|
|
258
|
+
*/
|
|
259
|
+
f32 = 10,
|
|
260
260
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Boolean value
|
|
263
|
+
*
|
|
264
|
+
* Must be `true` or `false`. `0` and `1` type coercion is not supported.
|
|
265
|
+
*
|
|
266
|
+
* In C, this corresponds to:
|
|
267
|
+
* ```c
|
|
268
|
+
* bool
|
|
269
|
+
* _Bool
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
bool = 11,
|
|
273
273
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Pointer value
|
|
276
|
+
*
|
|
277
|
+
* See {@link Bun.FFI.ptr} for more information
|
|
278
|
+
*
|
|
279
|
+
* In C:
|
|
280
|
+
* ```c
|
|
281
|
+
* void*
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* In JavaScript:
|
|
285
|
+
* ```js
|
|
286
|
+
* ptr(new Uint8Array(1))
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
ptr = 12,
|
|
290
|
+
/**
|
|
291
|
+
* Pointer value
|
|
292
|
+
*
|
|
293
|
+
* alias of {@link FFIType.ptr}
|
|
294
|
+
*/
|
|
295
|
+
pointer = 12,
|
|
296
296
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
297
|
+
/**
|
|
298
|
+
* void value
|
|
299
|
+
*
|
|
300
|
+
* void arguments are not supported
|
|
301
|
+
*
|
|
302
|
+
* void return type is the default return type
|
|
303
|
+
*
|
|
304
|
+
* In C:
|
|
305
|
+
* ```c
|
|
306
|
+
* void
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
void = 13,
|
|
310
310
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
311
|
+
/**
|
|
312
|
+
* When used as a `returns`, this will automatically become a {@link CString}.
|
|
313
|
+
*
|
|
314
|
+
* When used in `args` it is equivalent to {@link FFIType.pointer}
|
|
315
|
+
*/
|
|
316
|
+
cstring = 14,
|
|
317
317
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Attempt to coerce `BigInt` into a `Number` if it fits. This improves performance
|
|
320
|
+
* but means you might get a `BigInt` or you might get a `number`.
|
|
321
|
+
*
|
|
322
|
+
* In C, this always becomes `int64_t`
|
|
323
|
+
*
|
|
324
|
+
* In JavaScript, this could be number or it could be BigInt, depending on what
|
|
325
|
+
* value is passed in.
|
|
326
|
+
*/
|
|
327
|
+
i64_fast = 15,
|
|
328
328
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Attempt to coerce `BigInt` into a `Number` if it fits. This improves performance
|
|
331
|
+
* but means you might get a `BigInt` or you might get a `number`.
|
|
332
|
+
*
|
|
333
|
+
* In C, this always becomes `uint64_t`
|
|
334
|
+
*
|
|
335
|
+
* In JavaScript, this could be number or it could be BigInt, depending on what
|
|
336
|
+
* value is passed in.
|
|
337
|
+
*/
|
|
338
|
+
u64_fast = 16,
|
|
339
|
+
function = 17,
|
|
340
340
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
341
|
+
napi_env = 18,
|
|
342
|
+
napi_value = 19,
|
|
343
|
+
buffer = 20,
|
|
344
|
+
}
|
|
345
345
|
|
|
346
|
-
|
|
346
|
+
type Pointer = number & { __pointer__: null };
|
|
347
347
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
348
|
+
interface FFITypeToArgsType {
|
|
349
|
+
[FFIType.char]: number;
|
|
350
|
+
[FFIType.int8_t]: number;
|
|
351
|
+
[FFIType.i8]: number;
|
|
352
|
+
[FFIType.uint8_t]: number;
|
|
353
|
+
[FFIType.u8]: number;
|
|
354
|
+
[FFIType.int16_t]: number;
|
|
355
|
+
[FFIType.i16]: number;
|
|
356
|
+
[FFIType.uint16_t]: number;
|
|
357
|
+
[FFIType.u16]: number;
|
|
358
|
+
[FFIType.int32_t]: number;
|
|
359
|
+
[FFIType.i32]: number;
|
|
360
|
+
[FFIType.int]: number;
|
|
361
|
+
[FFIType.uint32_t]: number;
|
|
362
|
+
[FFIType.u32]: number;
|
|
363
|
+
[FFIType.int64_t]: number | bigint;
|
|
364
|
+
[FFIType.i64]: number | bigint;
|
|
365
|
+
[FFIType.uint64_t]: number | bigint;
|
|
366
|
+
[FFIType.u64]: number | bigint;
|
|
367
|
+
[FFIType.double]: number;
|
|
368
|
+
[FFIType.f64]: number;
|
|
369
|
+
[FFIType.float]: number;
|
|
370
|
+
[FFIType.f32]: number;
|
|
371
|
+
[FFIType.bool]: boolean;
|
|
372
|
+
[FFIType.ptr]: NodeJS.TypedArray | Pointer | CString | null;
|
|
373
|
+
[FFIType.pointer]: NodeJS.TypedArray | Pointer | CString | null;
|
|
374
|
+
[FFIType.void]: undefined;
|
|
375
|
+
[FFIType.cstring]: NodeJS.TypedArray | Pointer | CString | null;
|
|
376
|
+
[FFIType.i64_fast]: number | bigint;
|
|
377
|
+
[FFIType.u64_fast]: number | bigint;
|
|
378
|
+
[FFIType.function]: Pointer | JSCallback; // cannot be null
|
|
379
|
+
[FFIType.napi_env]: unknown;
|
|
380
|
+
[FFIType.napi_value]: unknown;
|
|
381
|
+
[FFIType.buffer]: NodeJS.TypedArray | DataView;
|
|
382
|
+
}
|
|
383
|
+
interface FFITypeToReturnsType {
|
|
384
|
+
[FFIType.char]: number;
|
|
385
|
+
[FFIType.int8_t]: number;
|
|
386
|
+
[FFIType.i8]: number;
|
|
387
|
+
[FFIType.uint8_t]: number;
|
|
388
|
+
[FFIType.u8]: number;
|
|
389
|
+
[FFIType.int16_t]: number;
|
|
390
|
+
[FFIType.i16]: number;
|
|
391
|
+
[FFIType.uint16_t]: number;
|
|
392
|
+
[FFIType.u16]: number;
|
|
393
|
+
[FFIType.int32_t]: number;
|
|
394
|
+
[FFIType.i32]: number;
|
|
395
|
+
[FFIType.int]: number;
|
|
396
|
+
[FFIType.uint32_t]: number;
|
|
397
|
+
[FFIType.u32]: number;
|
|
398
|
+
[FFIType.int64_t]: bigint;
|
|
399
|
+
[FFIType.i64]: bigint;
|
|
400
|
+
[FFIType.uint64_t]: bigint;
|
|
401
|
+
[FFIType.u64]: bigint;
|
|
402
|
+
[FFIType.double]: number;
|
|
403
|
+
[FFIType.f64]: number;
|
|
404
|
+
[FFIType.float]: number;
|
|
405
|
+
[FFIType.f32]: number;
|
|
406
|
+
[FFIType.bool]: boolean;
|
|
407
|
+
[FFIType.ptr]: Pointer | null;
|
|
408
|
+
[FFIType.pointer]: Pointer | null;
|
|
409
|
+
[FFIType.void]: undefined;
|
|
410
|
+
[FFIType.cstring]: CString;
|
|
411
|
+
[FFIType.i64_fast]: number | bigint;
|
|
412
|
+
[FFIType.u64_fast]: number | bigint;
|
|
413
|
+
[FFIType.function]: Pointer | null;
|
|
414
|
+
[FFIType.napi_env]: unknown;
|
|
415
|
+
[FFIType.napi_value]: unknown;
|
|
416
|
+
[FFIType.buffer]: NodeJS.TypedArray | DataView;
|
|
417
|
+
}
|
|
418
|
+
interface FFITypeStringToType {
|
|
419
|
+
["char"]: FFIType.char;
|
|
420
|
+
["int8_t"]: FFIType.int8_t;
|
|
421
|
+
["i8"]: FFIType.i8;
|
|
422
|
+
["uint8_t"]: FFIType.uint8_t;
|
|
423
|
+
["u8"]: FFIType.u8;
|
|
424
|
+
["int16_t"]: FFIType.int16_t;
|
|
425
|
+
["i16"]: FFIType.i16;
|
|
426
|
+
["uint16_t"]: FFIType.uint16_t;
|
|
427
|
+
["u16"]: FFIType.u16;
|
|
428
|
+
["int32_t"]: FFIType.int32_t;
|
|
429
|
+
["i32"]: FFIType.i32;
|
|
430
|
+
["int"]: FFIType.int;
|
|
431
|
+
["uint32_t"]: FFIType.uint32_t;
|
|
432
|
+
["u32"]: FFIType.u32;
|
|
433
|
+
["int64_t"]: FFIType.int64_t;
|
|
434
|
+
["i64"]: FFIType.i64;
|
|
435
|
+
["uint64_t"]: FFIType.uint64_t;
|
|
436
|
+
["u64"]: FFIType.u64;
|
|
437
|
+
["double"]: FFIType.double;
|
|
438
|
+
["f64"]: FFIType.f64;
|
|
439
|
+
["float"]: FFIType.float;
|
|
440
|
+
["f32"]: FFIType.f32;
|
|
441
|
+
["bool"]: FFIType.bool;
|
|
442
|
+
["ptr"]: FFIType.ptr;
|
|
443
|
+
["pointer"]: FFIType.pointer;
|
|
444
|
+
["void"]: FFIType.void;
|
|
445
|
+
["cstring"]: FFIType.cstring;
|
|
446
|
+
["function"]: FFIType.pointer; // for now
|
|
447
|
+
["usize"]: FFIType.uint64_t; // for now
|
|
448
|
+
["callback"]: FFIType.pointer; // for now
|
|
449
|
+
["napi_env"]: FFIType.napi_env;
|
|
450
|
+
["napi_value"]: FFIType.napi_value;
|
|
451
|
+
["buffer"]: FFIType.buffer;
|
|
452
|
+
}
|
|
453
453
|
|
|
454
|
-
|
|
454
|
+
type FFITypeOrString = FFIType | keyof FFITypeStringToType;
|
|
455
455
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
456
|
+
interface FFIFunction {
|
|
457
|
+
/**
|
|
458
|
+
* Arguments to a FFI function (C ABI)
|
|
459
|
+
*
|
|
460
|
+
* Defaults to an empty array, which means no arguments.
|
|
461
|
+
*
|
|
462
|
+
* To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* From JavaScript:
|
|
466
|
+
* ```ts
|
|
467
|
+
* import { dlopen, FFIType, suffix } from "bun:ffi"
|
|
468
|
+
*
|
|
469
|
+
* const lib = dlopen(`adder.${suffix}`, {
|
|
470
|
+
* add: {
|
|
471
|
+
* // FFIType can be used or you can pass string labels.
|
|
472
|
+
* args: [FFIType.i32, "i32"],
|
|
473
|
+
* returns: "i32",
|
|
474
|
+
* },
|
|
475
|
+
* })
|
|
476
|
+
* lib.symbols.add(1, 2)
|
|
477
|
+
* ```
|
|
478
|
+
* In C:
|
|
479
|
+
* ```c
|
|
480
|
+
* int add(int a, int b) {
|
|
481
|
+
* return a + b;
|
|
482
|
+
* }
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
readonly args?: readonly FFITypeOrString[];
|
|
486
|
+
/**
|
|
487
|
+
* Return type to a FFI function (C ABI)
|
|
488
|
+
*
|
|
489
|
+
* Defaults to {@link FFIType.void}
|
|
490
|
+
*
|
|
491
|
+
* To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* From JavaScript:
|
|
495
|
+
* ```ts
|
|
496
|
+
* import { dlopen, CString } from "bun:ffi"
|
|
497
|
+
*
|
|
498
|
+
* const lib = dlopen('z', {
|
|
499
|
+
* version: {
|
|
500
|
+
* returns: "ptr",
|
|
501
|
+
* }
|
|
502
|
+
* });
|
|
503
|
+
* console.log(new CString(lib.symbols.version()));
|
|
504
|
+
* ```
|
|
505
|
+
* In C:
|
|
506
|
+
* ```c
|
|
507
|
+
* char* version()
|
|
508
|
+
* {
|
|
509
|
+
* return "1.0.0";
|
|
510
|
+
* }
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
513
|
+
readonly returns?: FFITypeOrString;
|
|
514
514
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
515
|
+
/**
|
|
516
|
+
* Function pointer to the native function
|
|
517
|
+
*
|
|
518
|
+
* If provided, instead of using dlsym() to lookup the function, Bun will use this instead.
|
|
519
|
+
* This pointer should not be null (0).
|
|
520
|
+
*
|
|
521
|
+
* This is useful if the library has already been loaded
|
|
522
|
+
* or if the module is also using Node-API.
|
|
523
|
+
*/
|
|
524
|
+
readonly ptr?: Pointer | bigint;
|
|
525
525
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
526
|
+
/**
|
|
527
|
+
* Can C/FFI code call this function from a separate thread?
|
|
528
|
+
*
|
|
529
|
+
* Only supported with {@link JSCallback}.
|
|
530
|
+
*
|
|
531
|
+
* This does not make the function run in a separate thread. It is still up to the application/library
|
|
532
|
+
* to run their code in a separate thread.
|
|
533
|
+
*
|
|
534
|
+
* By default, {@link JSCallback} calls are not thread-safe. Turning this on
|
|
535
|
+
* incurs a small performance penalty for every function call. That small
|
|
536
|
+
* performance penalty needs to be less than the performance gain from
|
|
537
|
+
* running the function in a separate thread.
|
|
538
|
+
*
|
|
539
|
+
* @default false
|
|
540
|
+
*/
|
|
541
|
+
readonly threadsafe?: boolean;
|
|
542
|
+
}
|
|
543
543
|
|
|
544
|
-
|
|
544
|
+
type Symbols = Readonly<Record<string, FFIFunction>>;
|
|
545
545
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
// *
|
|
549
|
-
// * Returns a function pointer
|
|
550
|
-
// *
|
|
551
|
-
// */
|
|
552
|
-
// export function callback(ffi: FFIFunction, cb: Function): number;
|
|
546
|
+
interface Library<Fns extends Symbols> {
|
|
547
|
+
symbols: ConvertFns<Fns>;
|
|
553
548
|
|
|
554
|
-
|
|
555
|
-
|
|
549
|
+
/**
|
|
550
|
+
* `dlclose` the library, unloading the symbols and freeing allocated memory.
|
|
551
|
+
*
|
|
552
|
+
* Once called, the library is no longer usable.
|
|
553
|
+
*
|
|
554
|
+
* Calling a function from a library that has been closed is undefined behavior.
|
|
555
|
+
*/
|
|
556
|
+
close(): void;
|
|
557
|
+
}
|
|
556
558
|
|
|
557
|
-
|
|
558
|
-
* `dlclose` the library, unloading the symbols and freeing allocated memory.
|
|
559
|
-
*
|
|
560
|
-
* Once called, the library is no longer usable.
|
|
561
|
-
*
|
|
562
|
-
* Calling a function from a library that has been closed is undefined behavior.
|
|
563
|
-
*/
|
|
564
|
-
close(): void;
|
|
565
|
-
}
|
|
559
|
+
type ToFFIType<T extends FFITypeOrString> = T extends FFIType ? T : T extends string ? FFITypeStringToType[T] : never;
|
|
566
560
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
561
|
+
const FFIFunctionCallableSymbol: unique symbol;
|
|
562
|
+
type ConvertFns<Fns extends Symbols> = {
|
|
563
|
+
[K in keyof Fns]: {
|
|
564
|
+
(
|
|
565
|
+
...args: Fns[K]["args"] extends infer A extends readonly FFITypeOrString[]
|
|
566
|
+
? { [L in keyof A]: FFITypeToArgsType[ToFFIType<A[L]>] }
|
|
567
|
+
: // eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
|
|
568
|
+
[unknown] extends [Fns[K]["args"]]
|
|
569
|
+
? []
|
|
570
|
+
: never
|
|
571
|
+
): [unknown] extends [Fns[K]["returns"]] // eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
|
|
572
|
+
? undefined
|
|
573
|
+
: FFITypeToReturnsType[ToFFIType<NonNullable<Fns[K]["returns"]>>];
|
|
574
|
+
__ffi_function_callable: typeof FFIFunctionCallableSymbol;
|
|
575
|
+
};
|
|
576
|
+
};
|
|
572
577
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
578
|
+
/**
|
|
579
|
+
* Open a library using `"bun:ffi"`
|
|
580
|
+
*
|
|
581
|
+
* @param name The name of the library or file path. This will be passed to `dlopen()`
|
|
582
|
+
* @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
*
|
|
586
|
+
* ```js
|
|
587
|
+
* import {dlopen} from 'bun:ffi';
|
|
588
|
+
*
|
|
589
|
+
* const lib = dlopen("duckdb.dylib", {
|
|
590
|
+
* get_version: {
|
|
591
|
+
* returns: "cstring",
|
|
592
|
+
* args: [],
|
|
593
|
+
* },
|
|
594
|
+
* });
|
|
595
|
+
* lib.symbols.get_version();
|
|
596
|
+
* // "1.0.0"
|
|
597
|
+
* ```
|
|
598
|
+
*
|
|
599
|
+
* This is powered by just-in-time compiling C wrappers
|
|
600
|
+
* that convert JavaScript types to C types and back. Internally,
|
|
601
|
+
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
|
|
602
|
+
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
|
|
603
|
+
*/
|
|
604
|
+
function dlopen<Fns extends Record<string, FFIFunction>>(
|
|
605
|
+
name: string | import("bun").BunFile | URL,
|
|
606
|
+
symbols: Fns,
|
|
607
|
+
): Library<Fns>;
|
|
590
608
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
609
|
+
/**
|
|
610
|
+
* **Experimental:** Compile ISO C11 source code using TinyCC, and make {@link symbols} available as functions to JavaScript.
|
|
611
|
+
*
|
|
612
|
+
* @param options
|
|
613
|
+
* @returns Library<Fns>
|
|
614
|
+
*
|
|
615
|
+
* @example
|
|
616
|
+
* ## Hello, World!
|
|
617
|
+
*
|
|
618
|
+
* JavaScript:
|
|
619
|
+
* ```js
|
|
620
|
+
* import { cc } from "bun:ffi";
|
|
621
|
+
* import source from "./hello.c" with {type: "file"};
|
|
622
|
+
* const {symbols: {hello}} = cc({
|
|
623
|
+
* source,
|
|
624
|
+
* symbols: {
|
|
625
|
+
* hello: {
|
|
626
|
+
* returns: "cstring",
|
|
627
|
+
* args: [],
|
|
628
|
+
* },
|
|
629
|
+
* },
|
|
630
|
+
* });
|
|
631
|
+
* // "Hello, World!"
|
|
632
|
+
* console.log(hello());
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* `./hello.c`:
|
|
636
|
+
* ```c
|
|
637
|
+
* #include <stdio.h>
|
|
638
|
+
* const char* hello() {
|
|
639
|
+
* return "Hello, World!";
|
|
640
|
+
* }
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
function cc<Fns extends Record<string, FFIFunction>>(options: {
|
|
644
|
+
/**
|
|
645
|
+
* File path to an ISO C11 source file to compile and link
|
|
646
|
+
*/
|
|
647
|
+
source: string | import("bun").BunFile | URL;
|
|
621
648
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
* @example
|
|
629
|
-
* ## Hello, World!
|
|
630
|
-
*
|
|
631
|
-
* JavaScript:
|
|
632
|
-
* ```js
|
|
633
|
-
* import { cc } from "bun:ffi";
|
|
634
|
-
* import hello from "./hello.c" with {type: "file"};
|
|
635
|
-
* const {symbols: {hello}} = cc({
|
|
636
|
-
* source: hello,
|
|
637
|
-
* symbols: {
|
|
638
|
-
* hello: {
|
|
639
|
-
* returns: "cstring",
|
|
640
|
-
* args: [],
|
|
641
|
-
* },
|
|
642
|
-
* },
|
|
643
|
-
* });
|
|
644
|
-
* // "Hello, World!"
|
|
645
|
-
* console.log(hello());
|
|
646
|
-
* ```
|
|
647
|
-
*
|
|
648
|
-
* `./hello.c`:
|
|
649
|
-
* ```c
|
|
650
|
-
* #include <stdio.h>
|
|
651
|
-
* const char* hello() {
|
|
652
|
-
* return "Hello, World!";
|
|
653
|
-
* }
|
|
654
|
-
* ```
|
|
655
|
-
*/
|
|
656
|
-
function cc<Fns extends Record<string, FFIFunction>>(options: {
|
|
657
|
-
/**
|
|
658
|
-
* File path to an ISO C11 source file to compile and link
|
|
659
|
-
*/
|
|
660
|
-
source: string | import("bun").BunFile | URL;
|
|
649
|
+
/**
|
|
650
|
+
* Library names to link against
|
|
651
|
+
*
|
|
652
|
+
* Equivalent to `-l` option in gcc/clang.
|
|
653
|
+
*/
|
|
654
|
+
library?: string[] | string;
|
|
661
655
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
656
|
+
/**
|
|
657
|
+
* Include directories to pass to the compiler
|
|
658
|
+
*
|
|
659
|
+
* Equivalent to `-I` option in gcc/clang.
|
|
660
|
+
*/
|
|
661
|
+
include?: string[] | string;
|
|
668
662
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
*/
|
|
674
|
-
include?: string[] | string;
|
|
663
|
+
/**
|
|
664
|
+
* Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
|
|
665
|
+
*/
|
|
666
|
+
symbols: Fns;
|
|
675
667
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
668
|
+
/**
|
|
669
|
+
* Map of symbols to define where the key is the symbol name and the value is the symbol value
|
|
670
|
+
*
|
|
671
|
+
* Equivalent to `-D` option in gcc/clang.
|
|
672
|
+
*
|
|
673
|
+
* @example
|
|
674
|
+
* ```js
|
|
675
|
+
* import { cc } from "bun:ffi";
|
|
676
|
+
* import source from "./hello.c" with {type: "file"};
|
|
677
|
+
* const {symbols: {hello}} = cc({
|
|
678
|
+
* source,
|
|
679
|
+
* define: {
|
|
680
|
+
* "NDEBUG": "1",
|
|
681
|
+
* },
|
|
682
|
+
* symbols: {
|
|
683
|
+
* hello: {
|
|
684
|
+
* returns: "cstring",
|
|
685
|
+
* args: [],
|
|
686
|
+
* },
|
|
687
|
+
* },
|
|
688
|
+
* });
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
define?: Record<string, string>;
|
|
680
692
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
693
|
+
/**
|
|
694
|
+
* Flags to pass to the compiler. Note: we do not make gurantees about which specific version of the compiler is used.
|
|
695
|
+
*
|
|
696
|
+
* @default "-std=c11 -Wl,--export-all-symbols -g -O2"
|
|
697
|
+
*
|
|
698
|
+
* This is useful for passing macOS frameworks to link against. Or if there are other options you want to pass to the compiler.
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```js
|
|
702
|
+
* import { cc } from "bun:ffi";
|
|
703
|
+
* import source from "./hello.c" with {type: "file"};
|
|
704
|
+
* const {symbols: {hello}} = cc({
|
|
705
|
+
* source,
|
|
706
|
+
* flags: ["-framework CoreFoundation", "-framework Security"],
|
|
707
|
+
* symbols: {
|
|
708
|
+
* hello: {
|
|
709
|
+
* returns: "cstring",
|
|
710
|
+
* args: [],
|
|
711
|
+
* },
|
|
712
|
+
* },
|
|
713
|
+
* });
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
flags?: string | string[];
|
|
717
|
+
}): Library<Fns>;
|
|
704
718
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
719
|
+
/**
|
|
720
|
+
* Turn a native library's function pointer into a JavaScript function
|
|
721
|
+
*
|
|
722
|
+
* Libraries using Node-API & bun:ffi in the same module could use this to skip an extra dlopen() step.
|
|
723
|
+
*
|
|
724
|
+
* @param fn {@link FFIFunction} declaration. `ptr` is required
|
|
725
|
+
*
|
|
726
|
+
* @example
|
|
727
|
+
*
|
|
728
|
+
* ```js
|
|
729
|
+
* import {CFunction} from 'bun:ffi';
|
|
730
|
+
*
|
|
731
|
+
* const getVersion = new CFunction({
|
|
732
|
+
* returns: "cstring",
|
|
733
|
+
* args: [],
|
|
734
|
+
* ptr: myNativeLibraryGetVersion,
|
|
735
|
+
* });
|
|
736
|
+
* getVersion();
|
|
737
|
+
* getVersion.close();
|
|
738
|
+
* ```
|
|
739
|
+
*
|
|
740
|
+
* This is powered by just-in-time compiling C wrappers
|
|
741
|
+
* that convert JavaScript types to C types and back. Internally,
|
|
742
|
+
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
|
|
743
|
+
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
|
|
744
|
+
*/
|
|
745
|
+
function CFunction(fn: FFIFunction & { ptr: Pointer }): CallableFunction & {
|
|
746
|
+
/**
|
|
747
|
+
* Free the memory allocated by the wrapping function
|
|
748
|
+
*/
|
|
749
|
+
close(): void;
|
|
750
|
+
};
|
|
729
751
|
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
752
|
+
/**
|
|
753
|
+
* Link a map of symbols to JavaScript functions
|
|
754
|
+
*
|
|
755
|
+
* This lets you use native libraries that were already loaded somehow. You usually will want {@link dlopen} instead.
|
|
756
|
+
*
|
|
757
|
+
* You could use this with Node-API to skip loading a second time.
|
|
758
|
+
*
|
|
759
|
+
* @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
|
|
760
|
+
*
|
|
761
|
+
* @example
|
|
762
|
+
*
|
|
763
|
+
* ```js
|
|
764
|
+
* import { linkSymbols } from "bun:ffi";
|
|
765
|
+
*
|
|
766
|
+
* const [majorPtr, minorPtr, patchPtr] = getVersionPtrs();
|
|
767
|
+
*
|
|
768
|
+
* const lib = linkSymbols({
|
|
769
|
+
* // Unlike with dlopen(), the names here can be whatever you want
|
|
770
|
+
* getMajor: {
|
|
771
|
+
* returns: "cstring",
|
|
772
|
+
* args: [],
|
|
773
|
+
*
|
|
774
|
+
* // Since this doesn't use dlsym(), you have to provide a valid ptr
|
|
775
|
+
* // That ptr could be a number or a bigint
|
|
776
|
+
* // An invalid pointer will crash your program.
|
|
777
|
+
* ptr: majorPtr,
|
|
778
|
+
* },
|
|
779
|
+
* getMinor: {
|
|
780
|
+
* returns: "cstring",
|
|
781
|
+
* args: [],
|
|
782
|
+
* ptr: minorPtr,
|
|
783
|
+
* },
|
|
784
|
+
* getPatch: {
|
|
785
|
+
* returns: "cstring",
|
|
786
|
+
* args: [],
|
|
787
|
+
* ptr: patchPtr,
|
|
788
|
+
* },
|
|
789
|
+
* });
|
|
790
|
+
*
|
|
791
|
+
* const [major, minor, patch] = [
|
|
792
|
+
* lib.symbols.getMajor(),
|
|
793
|
+
* lib.symbols.getMinor(),
|
|
794
|
+
* lib.symbols.getPatch(),
|
|
795
|
+
* ];
|
|
796
|
+
* ```
|
|
797
|
+
*
|
|
798
|
+
* This is powered by just-in-time compiling C wrappers
|
|
799
|
+
* that convert JavaScript types to C types and back. Internally,
|
|
800
|
+
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
|
|
801
|
+
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
|
|
802
|
+
*/
|
|
803
|
+
function linkSymbols<Fns extends Record<string, FFIFunction>>(symbols: Fns): Library<Fns>;
|
|
762
804
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
*
|
|
779
|
-
* const lib = linkSymbols({
|
|
780
|
-
* // Unlike with dlopen(), the names here can be whatever you want
|
|
781
|
-
* getMajor: {
|
|
782
|
-
* returns: "cstring",
|
|
783
|
-
* args: [],
|
|
784
|
-
*
|
|
785
|
-
* // Since this doesn't use dlsym(), you have to provide a valid ptr
|
|
786
|
-
* // That ptr could be a number or a bigint
|
|
787
|
-
* // An invalid pointer will crash your program.
|
|
788
|
-
* ptr: majorPtr,
|
|
789
|
-
* },
|
|
790
|
-
* getMinor: {
|
|
791
|
-
* returns: "cstring",
|
|
792
|
-
* args: [],
|
|
793
|
-
* ptr: minorPtr,
|
|
794
|
-
* },
|
|
795
|
-
* getPatch: {
|
|
796
|
-
* returns: "cstring",
|
|
797
|
-
* args: [],
|
|
798
|
-
* ptr: patchPtr,
|
|
799
|
-
* },
|
|
800
|
-
* });
|
|
801
|
-
*
|
|
802
|
-
* const [major, minor, patch] = [
|
|
803
|
-
* lib.symbols.getMajor(),
|
|
804
|
-
* lib.symbols.getMinor(),
|
|
805
|
-
* lib.symbols.getPatch(),
|
|
806
|
-
* ];
|
|
807
|
-
* ```
|
|
808
|
-
*
|
|
809
|
-
* This is powered by just-in-time compiling C wrappers
|
|
810
|
-
* that convert JavaScript types to C types and back. Internally,
|
|
811
|
-
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
|
|
812
|
-
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
|
|
813
|
-
*/
|
|
814
|
-
function linkSymbols<Fns extends Record<string, FFIFunction>>(
|
|
815
|
-
symbols: Fns,
|
|
816
|
-
): Library<Fns>;
|
|
805
|
+
/**
|
|
806
|
+
* Read a pointer as a {@link Buffer}
|
|
807
|
+
*
|
|
808
|
+
* If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
|
|
809
|
+
*
|
|
810
|
+
* @param ptr The memory address to read
|
|
811
|
+
* @param byteOffset bytes to skip before reading
|
|
812
|
+
* @param byteLength bytes to read
|
|
813
|
+
*
|
|
814
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
815
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
816
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
817
|
+
* undefined behavior. Use with care!
|
|
818
|
+
*/
|
|
819
|
+
function toBuffer(ptr: Pointer, byteOffset?: number, byteLength?: number): Buffer;
|
|
817
820
|
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
ptr: Pointer,
|
|
834
|
-
byteOffset?: number,
|
|
835
|
-
byteLength?: number,
|
|
836
|
-
): Buffer;
|
|
821
|
+
/**
|
|
822
|
+
* Read a pointer as an {@link ArrayBuffer}
|
|
823
|
+
*
|
|
824
|
+
* If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
|
|
825
|
+
*
|
|
826
|
+
* @param ptr The memory address to read
|
|
827
|
+
* @param byteOffset bytes to skip before reading
|
|
828
|
+
* @param byteLength bytes to read
|
|
829
|
+
*
|
|
830
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
831
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
832
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
833
|
+
* undefined behavior. Use with care!
|
|
834
|
+
*/
|
|
835
|
+
function toArrayBuffer(ptr: Pointer, byteOffset?: number, byteLength?: number): ArrayBuffer;
|
|
837
836
|
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
837
|
+
namespace read {
|
|
838
|
+
/**
|
|
839
|
+
* The read function behaves similarly to DataView,
|
|
840
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
841
|
+
*
|
|
842
|
+
* @param ptr The memory address to read
|
|
843
|
+
* @param byteOffset bytes to skip before reading
|
|
844
|
+
*
|
|
845
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
846
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
847
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
848
|
+
* undefined behavior. Use with care!
|
|
849
|
+
*/
|
|
850
|
+
function u8(ptr: Pointer, byteOffset?: number): number;
|
|
851
|
+
/**
|
|
852
|
+
* The read function behaves similarly to DataView,
|
|
853
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
854
|
+
*
|
|
855
|
+
* @param ptr The memory address to read
|
|
856
|
+
* @param byteOffset bytes to skip before reading
|
|
857
|
+
*
|
|
858
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
859
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
860
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
861
|
+
* undefined behavior. Use with care!
|
|
862
|
+
*/
|
|
863
|
+
function i8(ptr: Pointer, byteOffset?: number): number;
|
|
864
|
+
/**
|
|
865
|
+
* The read function behaves similarly to DataView,
|
|
866
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
867
|
+
*
|
|
868
|
+
* @param ptr The memory address to read
|
|
869
|
+
* @param byteOffset bytes to skip before reading
|
|
870
|
+
*
|
|
871
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
872
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
873
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
874
|
+
* undefined behavior. Use with care!
|
|
875
|
+
*/
|
|
876
|
+
function u16(ptr: Pointer, byteOffset?: number): number;
|
|
877
|
+
/**
|
|
878
|
+
* The read function behaves similarly to DataView,
|
|
879
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
880
|
+
*
|
|
881
|
+
* @param ptr The memory address to read
|
|
882
|
+
* @param byteOffset bytes to skip before reading
|
|
883
|
+
*
|
|
884
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
885
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
886
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
887
|
+
* undefined behavior. Use with care!
|
|
888
|
+
*/
|
|
889
|
+
function i16(ptr: Pointer, byteOffset?: number): number;
|
|
890
|
+
/**
|
|
891
|
+
* The read function behaves similarly to DataView,
|
|
892
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
893
|
+
*
|
|
894
|
+
* @param ptr The memory address to read
|
|
895
|
+
* @param byteOffset bytes to skip before reading
|
|
896
|
+
*
|
|
897
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
898
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
899
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
900
|
+
* undefined behavior. Use with care!
|
|
901
|
+
*/
|
|
902
|
+
function u32(ptr: Pointer, byteOffset?: number): number;
|
|
903
|
+
/**
|
|
904
|
+
* The read function behaves similarly to DataView,
|
|
905
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
906
|
+
*
|
|
907
|
+
* @param ptr The memory address to read
|
|
908
|
+
* @param byteOffset bytes to skip before reading
|
|
909
|
+
*
|
|
910
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
911
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
912
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
913
|
+
* undefined behavior. Use with care!
|
|
914
|
+
*/
|
|
915
|
+
function i32(ptr: Pointer, byteOffset?: number): number;
|
|
916
|
+
/**
|
|
917
|
+
* The read function behaves similarly to DataView,
|
|
918
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
919
|
+
*
|
|
920
|
+
* @param ptr The memory address to read
|
|
921
|
+
* @param byteOffset bytes to skip before reading
|
|
922
|
+
*
|
|
923
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
924
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
925
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
926
|
+
* undefined behavior. Use with care!
|
|
927
|
+
*/
|
|
928
|
+
function f32(ptr: Pointer, byteOffset?: number): number;
|
|
929
|
+
/**
|
|
930
|
+
* The read function behaves similarly to DataView,
|
|
931
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
932
|
+
*
|
|
933
|
+
* @param ptr The memory address to read
|
|
934
|
+
* @param byteOffset bytes to skip before reading
|
|
935
|
+
*
|
|
936
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
937
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
938
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
939
|
+
* undefined behavior. Use with care!
|
|
940
|
+
*/
|
|
941
|
+
function u64(ptr: Pointer, byteOffset?: number): bigint;
|
|
942
|
+
/**
|
|
943
|
+
* The read function behaves similarly to DataView,
|
|
944
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
945
|
+
*
|
|
946
|
+
* @param ptr The memory address to read
|
|
947
|
+
* @param byteOffset bytes to skip before reading
|
|
948
|
+
*
|
|
949
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
950
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
951
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
952
|
+
* undefined behavior. Use with care!
|
|
953
|
+
*/
|
|
954
|
+
function i64(ptr: Pointer, byteOffset?: number): bigint;
|
|
955
|
+
/**
|
|
956
|
+
* The read function behaves similarly to DataView,
|
|
957
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
958
|
+
*
|
|
959
|
+
* @param ptr The memory address to read
|
|
960
|
+
* @param byteOffset bytes to skip before reading
|
|
961
|
+
*
|
|
962
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
963
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
964
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
965
|
+
* undefined behavior. Use with care!
|
|
966
|
+
*/
|
|
967
|
+
function f64(ptr: Pointer, byteOffset?: number): number;
|
|
968
|
+
/**
|
|
969
|
+
* The read function behaves similarly to DataView,
|
|
970
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
971
|
+
*
|
|
972
|
+
* @param ptr The memory address to read
|
|
973
|
+
* @param byteOffset bytes to skip before reading
|
|
974
|
+
*
|
|
975
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
976
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
977
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
978
|
+
* undefined behavior. Use with care!
|
|
979
|
+
*/
|
|
980
|
+
function ptr(ptr: Pointer, byteOffset?: number): number;
|
|
981
|
+
/**
|
|
982
|
+
* The read function behaves similarly to DataView,
|
|
983
|
+
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
984
|
+
*
|
|
985
|
+
* @param ptr The memory address to read
|
|
986
|
+
* @param byteOffset bytes to skip before reading
|
|
987
|
+
*
|
|
988
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
989
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
990
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
991
|
+
* undefined behavior. Use with care!
|
|
992
|
+
*/
|
|
993
|
+
function intptr(ptr: Pointer, byteOffset?: number): number;
|
|
994
|
+
}
|
|
857
995
|
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
/**
|
|
886
|
-
* The read function behaves similarly to DataView,
|
|
887
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
888
|
-
*
|
|
889
|
-
* @param ptr The memory address to read
|
|
890
|
-
* @param byteOffset bytes to skip before reading
|
|
891
|
-
*
|
|
892
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
893
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
894
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
895
|
-
* undefined behavior. Use with care!
|
|
896
|
-
*/
|
|
897
|
-
function u16(ptr: Pointer, byteOffset?: number): number;
|
|
898
|
-
/**
|
|
899
|
-
* The read function behaves similarly to DataView,
|
|
900
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
901
|
-
*
|
|
902
|
-
* @param ptr The memory address to read
|
|
903
|
-
* @param byteOffset bytes to skip before reading
|
|
904
|
-
*
|
|
905
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
906
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
907
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
908
|
-
* undefined behavior. Use with care!
|
|
909
|
-
*/
|
|
910
|
-
function i16(ptr: Pointer, byteOffset?: number): number;
|
|
911
|
-
/**
|
|
912
|
-
* The read function behaves similarly to DataView,
|
|
913
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
914
|
-
*
|
|
915
|
-
* @param ptr The memory address to read
|
|
916
|
-
* @param byteOffset bytes to skip before reading
|
|
917
|
-
*
|
|
918
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
919
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
920
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
921
|
-
* undefined behavior. Use with care!
|
|
922
|
-
*/
|
|
923
|
-
function u32(ptr: Pointer, byteOffset?: number): number;
|
|
924
|
-
/**
|
|
925
|
-
* The read function behaves similarly to DataView,
|
|
926
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
927
|
-
*
|
|
928
|
-
* @param ptr The memory address to read
|
|
929
|
-
* @param byteOffset bytes to skip before reading
|
|
930
|
-
*
|
|
931
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
932
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
933
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
934
|
-
* undefined behavior. Use with care!
|
|
935
|
-
*/
|
|
936
|
-
function i32(ptr: Pointer, byteOffset?: number): number;
|
|
937
|
-
/**
|
|
938
|
-
* The read function behaves similarly to DataView,
|
|
939
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
940
|
-
*
|
|
941
|
-
* @param ptr The memory address to read
|
|
942
|
-
* @param byteOffset bytes to skip before reading
|
|
943
|
-
*
|
|
944
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
945
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
946
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
947
|
-
* undefined behavior. Use with care!
|
|
948
|
-
*/
|
|
949
|
-
function f32(ptr: Pointer, byteOffset?: number): number;
|
|
950
|
-
/**
|
|
951
|
-
* The read function behaves similarly to DataView,
|
|
952
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
953
|
-
*
|
|
954
|
-
* @param ptr The memory address to read
|
|
955
|
-
* @param byteOffset bytes to skip before reading
|
|
956
|
-
*
|
|
957
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
958
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
959
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
960
|
-
* undefined behavior. Use with care!
|
|
961
|
-
*/
|
|
962
|
-
function u64(ptr: Pointer, byteOffset?: number): bigint;
|
|
963
|
-
/**
|
|
964
|
-
* The read function behaves similarly to DataView,
|
|
965
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
966
|
-
*
|
|
967
|
-
* @param ptr The memory address to read
|
|
968
|
-
* @param byteOffset bytes to skip before reading
|
|
969
|
-
*
|
|
970
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
971
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
972
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
973
|
-
* undefined behavior. Use with care!
|
|
974
|
-
*/
|
|
975
|
-
function i64(ptr: Pointer, byteOffset?: number): bigint;
|
|
976
|
-
/**
|
|
977
|
-
* The read function behaves similarly to DataView,
|
|
978
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
979
|
-
*
|
|
980
|
-
* @param ptr The memory address to read
|
|
981
|
-
* @param byteOffset bytes to skip before reading
|
|
982
|
-
*
|
|
983
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
984
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
985
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
986
|
-
* undefined behavior. Use with care!
|
|
987
|
-
*/
|
|
988
|
-
function f64(ptr: Pointer, byteOffset?: number): number;
|
|
989
|
-
/**
|
|
990
|
-
* The read function behaves similarly to DataView,
|
|
991
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
992
|
-
*
|
|
993
|
-
* @param ptr The memory address to read
|
|
994
|
-
* @param byteOffset bytes to skip before reading
|
|
995
|
-
*
|
|
996
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
997
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
998
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
999
|
-
* undefined behavior. Use with care!
|
|
1000
|
-
*/
|
|
1001
|
-
function ptr(ptr: Pointer, byteOffset?: number): number;
|
|
1002
|
-
/**
|
|
1003
|
-
* The read function behaves similarly to DataView,
|
|
1004
|
-
* but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
|
|
1005
|
-
*
|
|
1006
|
-
* @param ptr The memory address to read
|
|
1007
|
-
* @param byteOffset bytes to skip before reading
|
|
1008
|
-
*
|
|
1009
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
1010
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
1011
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
1012
|
-
* undefined behavior. Use with care!
|
|
1013
|
-
*/
|
|
1014
|
-
function intptr(ptr: Pointer, byteOffset?: number): number;
|
|
1015
|
-
}
|
|
996
|
+
/**
|
|
997
|
+
* Get the pointer backing a {@link TypedArray} or {@link ArrayBuffer}
|
|
998
|
+
*
|
|
999
|
+
* Use this to pass {@link TypedArray} or {@link ArrayBuffer} to C functions.
|
|
1000
|
+
*
|
|
1001
|
+
* This is for use with FFI functions. For performance reasons, FFI will
|
|
1002
|
+
* not automatically convert typed arrays to C pointers.
|
|
1003
|
+
*
|
|
1004
|
+
* @param {TypedArray|ArrayBuffer|DataView} view the typed array or array buffer to get the pointer for
|
|
1005
|
+
* @param {number} byteOffset optional offset into the view in bytes
|
|
1006
|
+
*
|
|
1007
|
+
* @example
|
|
1008
|
+
*
|
|
1009
|
+
* From JavaScript:
|
|
1010
|
+
* ```js
|
|
1011
|
+
* const array = new Uint8Array(10);
|
|
1012
|
+
* const rawPtr = ptr(array);
|
|
1013
|
+
* myFFIFunction(rawPtr);
|
|
1014
|
+
* ```
|
|
1015
|
+
* To C:
|
|
1016
|
+
* ```c
|
|
1017
|
+
* void myFFIFunction(char* rawPtr) {
|
|
1018
|
+
* // Do something with rawPtr
|
|
1019
|
+
* }
|
|
1020
|
+
* ```
|
|
1021
|
+
*/
|
|
1022
|
+
function ptr(view: NodeJS.TypedArray | ArrayBufferLike | DataView, byteOffset?: number): Pointer;
|
|
1016
1023
|
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
* // Do something with rawPtr
|
|
1040
|
-
* }
|
|
1041
|
-
* ```
|
|
1042
|
-
*/
|
|
1043
|
-
function ptr(
|
|
1044
|
-
view: NodeJS.TypedArray | ArrayBufferLike | DataView,
|
|
1045
|
-
byteOffset?: number,
|
|
1046
|
-
): Pointer;
|
|
1024
|
+
/**
|
|
1025
|
+
* Get a string from a UTF-8 encoded C string
|
|
1026
|
+
* If `byteLength` is not provided, the string is assumed to be null-terminated.
|
|
1027
|
+
*
|
|
1028
|
+
* @example
|
|
1029
|
+
* ```js
|
|
1030
|
+
* var ptr = lib.symbols.getVersion();
|
|
1031
|
+
* console.log(new CString(ptr));
|
|
1032
|
+
* ```
|
|
1033
|
+
*
|
|
1034
|
+
* @example
|
|
1035
|
+
* ```js
|
|
1036
|
+
* var ptr = lib.symbols.getVersion();
|
|
1037
|
+
* // print the first 4 characters
|
|
1038
|
+
* console.log(new CString(ptr, 0, 4));
|
|
1039
|
+
* ```
|
|
1040
|
+
*
|
|
1041
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
1042
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
1043
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
1044
|
+
* undefined behavior. Use with care!
|
|
1045
|
+
*/
|
|
1047
1046
|
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1047
|
+
class CString extends String {
|
|
1048
|
+
/**
|
|
1049
|
+
* Get a string from a UTF-8 encoded C string
|
|
1050
|
+
* If `byteLength` is not provided, the string is assumed to be null-terminated.
|
|
1051
|
+
*
|
|
1052
|
+
* @param ptr The pointer to the C string
|
|
1053
|
+
* @param byteOffset bytes to skip before reading
|
|
1054
|
+
* @param byteLength bytes to read
|
|
1055
|
+
*
|
|
1056
|
+
* @example
|
|
1057
|
+
* ```js
|
|
1058
|
+
* var ptr = lib.symbols.getVersion();
|
|
1059
|
+
* console.log(new CString(ptr));
|
|
1060
|
+
* ```
|
|
1061
|
+
*
|
|
1062
|
+
* @example
|
|
1063
|
+
* ```js
|
|
1064
|
+
* var ptr = lib.symbols.getVersion();
|
|
1065
|
+
* // print the first 4 characters
|
|
1066
|
+
* console.log(new CString(ptr, 0, 4));
|
|
1067
|
+
* ```
|
|
1068
|
+
*
|
|
1069
|
+
* While there are some checks to catch invalid pointers, this is a difficult
|
|
1070
|
+
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
1071
|
+
* reading beyond the bounds of the pointer will crash the program or cause
|
|
1072
|
+
* undefined behavior. Use with care!
|
|
1073
|
+
*/
|
|
1074
|
+
constructor(ptr: Pointer, byteOffset?: number, byteLength?: number);
|
|
1070
1075
|
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
* ```js
|
|
1082
|
-
* var ptr = lib.symbols.getVersion();
|
|
1083
|
-
* console.log(new CString(ptr));
|
|
1084
|
-
* ```
|
|
1085
|
-
*
|
|
1086
|
-
* @example
|
|
1087
|
-
* ```js
|
|
1088
|
-
* var ptr = lib.symbols.getVersion();
|
|
1089
|
-
* // print the first 4 characters
|
|
1090
|
-
* console.log(new CString(ptr, 0, 4));
|
|
1091
|
-
* ```
|
|
1092
|
-
*
|
|
1093
|
-
* While there are some checks to catch invalid pointers, this is a difficult
|
|
1094
|
-
* thing to do safely. Passing an invalid pointer can crash the program and
|
|
1095
|
-
* reading beyond the bounds of the pointer will crash the program or cause
|
|
1096
|
-
* undefined behavior. Use with care!
|
|
1097
|
-
*/
|
|
1098
|
-
constructor(ptr: Pointer, byteOffset?: number, byteLength?: number);
|
|
1076
|
+
/**
|
|
1077
|
+
* The ptr to the C string
|
|
1078
|
+
*
|
|
1079
|
+
* This `CString` instance is a clone of the string, so it
|
|
1080
|
+
* is safe to continue using this instance after the `ptr` has been
|
|
1081
|
+
* freed.
|
|
1082
|
+
*/
|
|
1083
|
+
ptr: Pointer;
|
|
1084
|
+
byteOffset?: number;
|
|
1085
|
+
byteLength?: number;
|
|
1099
1086
|
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
ptr: Pointer;
|
|
1108
|
-
byteOffset?: number;
|
|
1109
|
-
byteLength?: number;
|
|
1087
|
+
/**
|
|
1088
|
+
* Get the {@link ptr} as an `ArrayBuffer`
|
|
1089
|
+
*
|
|
1090
|
+
* `null` or empty ptrs returns an `ArrayBuffer` with `byteLength` 0
|
|
1091
|
+
*/
|
|
1092
|
+
get arrayBuffer(): ArrayBuffer;
|
|
1093
|
+
}
|
|
1110
1094
|
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1095
|
+
/**
|
|
1096
|
+
* Pass a JavaScript function to FFI (Foreign Function Interface)
|
|
1097
|
+
*/
|
|
1098
|
+
class JSCallback {
|
|
1099
|
+
/**
|
|
1100
|
+
* Enable a JavaScript callback function to be passed to C with bun:ffi
|
|
1101
|
+
*
|
|
1102
|
+
* @param callback The JavaScript function to be called
|
|
1103
|
+
* @param definition The C function definition
|
|
1104
|
+
*/
|
|
1105
|
+
constructor(callback: (...args: any[]) => any, definition: FFIFunction);
|
|
1118
1106
|
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
*
|
|
1126
|
-
* @param callback The JavaScript function to be called
|
|
1127
|
-
* @param definition The C function definition
|
|
1128
|
-
*/
|
|
1129
|
-
constructor(callback: (...args: any[]) => any, definition: FFIFunction);
|
|
1107
|
+
/**
|
|
1108
|
+
* The pointer to the C function
|
|
1109
|
+
*
|
|
1110
|
+
* Becomes `null` once {@link JSCallback.prototype.close} is called
|
|
1111
|
+
*/
|
|
1112
|
+
readonly ptr: Pointer | null;
|
|
1130
1113
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
*/
|
|
1136
|
-
readonly ptr: Pointer | null;
|
|
1114
|
+
/**
|
|
1115
|
+
* Can the callback be called from a different thread?
|
|
1116
|
+
*/
|
|
1117
|
+
readonly threadsafe: boolean;
|
|
1137
1118
|
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1119
|
+
/**
|
|
1120
|
+
* Free the memory allocated for the callback
|
|
1121
|
+
*
|
|
1122
|
+
* If called multiple times, does nothing after the first call.
|
|
1123
|
+
*/
|
|
1124
|
+
close(): void;
|
|
1125
|
+
}
|
|
1142
1126
|
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1127
|
+
/**
|
|
1128
|
+
* View the generated C code for FFI bindings
|
|
1129
|
+
*
|
|
1130
|
+
* You probably won't need this unless there's a bug in the FFI bindings
|
|
1131
|
+
* generator or you're just curious.
|
|
1132
|
+
*/
|
|
1133
|
+
function viewSource(symbols: Symbols, is_callback?: false): string[];
|
|
1134
|
+
function viewSource(callback: FFIFunction, is_callback: true): string;
|
|
1150
1135
|
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
* "dylib" // macOS
|
|
1168
|
-
* ```
|
|
1169
|
-
*
|
|
1170
|
-
* @example
|
|
1171
|
-
* ```js
|
|
1172
|
-
* "so" // linux
|
|
1173
|
-
* ```
|
|
1174
|
-
*/
|
|
1175
|
-
const suffix: string;
|
|
1136
|
+
/**
|
|
1137
|
+
* Platform-specific file extension name for dynamic libraries
|
|
1138
|
+
*
|
|
1139
|
+
* "." is not included
|
|
1140
|
+
*
|
|
1141
|
+
* @example
|
|
1142
|
+
* ```js
|
|
1143
|
+
* "dylib" // macOS
|
|
1144
|
+
* ```
|
|
1145
|
+
*
|
|
1146
|
+
* @example
|
|
1147
|
+
* ```js
|
|
1148
|
+
* "so" // linux
|
|
1149
|
+
* ```
|
|
1150
|
+
*/
|
|
1151
|
+
const suffix: string;
|
|
1176
1152
|
}
|