bun-types 1.0.25 → 1.0.26

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/ffi.d.ts ADDED
@@ -0,0 +1,1030 @@
1
+ /**
2
+ * `bun:ffi` lets you efficiently call C functions & FFI functions from JavaScript
3
+ * without writing bindings yourself.
4
+ *
5
+ * ```js
6
+ * import {dlopen, CString, ptr} from 'bun:ffi';
7
+ *
8
+ * const lib = dlopen('libsqlite3', {
9
+ * });
10
+ * ```
11
+ *
12
+ * This is powered by just-in-time compiling C wrappers
13
+ * that convert JavaScript types to C types and back. Internally,
14
+ * bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
15
+ * goes to Fabrice Bellard and TinyCC maintainers for making this possible.
16
+ */
17
+ declare module "bun:ffi" {
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
+
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
+
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
+
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
+
174
+ /**
175
+ * 32-bit signed integer
176
+ */
177
+ int32_t = 5,
178
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+ }
341
+
342
+ type Pointer = number & { __pointer__: null };
343
+
344
+ interface FFITypeToArgsType {
345
+ [FFIType.char]: number;
346
+ [FFIType.int8_t]: number;
347
+ [FFIType.i8]: number;
348
+ [FFIType.uint8_t]: number;
349
+ [FFIType.u8]: number;
350
+ [FFIType.int16_t]: number;
351
+ [FFIType.i16]: number;
352
+ [FFIType.uint16_t]: number;
353
+ [FFIType.u16]: number;
354
+ [FFIType.int32_t]: number;
355
+ [FFIType.i32]: number;
356
+ [FFIType.int]: number;
357
+ [FFIType.uint32_t]: number;
358
+ [FFIType.u32]: number;
359
+ [FFIType.int64_t]: number | bigint;
360
+ [FFIType.i64]: number | bigint;
361
+ [FFIType.uint64_t]: number | bigint;
362
+ [FFIType.u64]: number | bigint;
363
+ [FFIType.double]: number;
364
+ [FFIType.f64]: number;
365
+ [FFIType.float]: number;
366
+ [FFIType.f32]: number;
367
+ [FFIType.bool]: boolean;
368
+ [FFIType.ptr]: NodeJS.TypedArray | Pointer | CString | null;
369
+ [FFIType.pointer]: NodeJS.TypedArray | Pointer | CString | null;
370
+ [FFIType.void]: undefined;
371
+ [FFIType.cstring]: NodeJS.TypedArray | Pointer | CString | null;
372
+ [FFIType.i64_fast]: number | bigint;
373
+ [FFIType.u64_fast]: number | bigint;
374
+ [FFIType.function]: Pointer | JSCallback; // cannot be null
375
+ }
376
+ interface FFITypeToReturnsType {
377
+ [FFIType.char]: number;
378
+ [FFIType.int8_t]: number;
379
+ [FFIType.i8]: number;
380
+ [FFIType.uint8_t]: number;
381
+ [FFIType.u8]: number;
382
+ [FFIType.int16_t]: number;
383
+ [FFIType.i16]: number;
384
+ [FFIType.uint16_t]: number;
385
+ [FFIType.u16]: number;
386
+ [FFIType.int32_t]: number;
387
+ [FFIType.i32]: number;
388
+ [FFIType.int]: number;
389
+ [FFIType.uint32_t]: number;
390
+ [FFIType.u32]: number;
391
+ [FFIType.int64_t]: bigint;
392
+ [FFIType.i64]: bigint;
393
+ [FFIType.uint64_t]: bigint;
394
+ [FFIType.u64]: bigint;
395
+ [FFIType.double]: number;
396
+ [FFIType.f64]: number;
397
+ [FFIType.float]: number;
398
+ [FFIType.f32]: number;
399
+ [FFIType.bool]: boolean;
400
+ [FFIType.ptr]: Pointer | null;
401
+ [FFIType.pointer]: Pointer | null;
402
+ [FFIType.void]: undefined;
403
+ [FFIType.cstring]: CString;
404
+ [FFIType.i64_fast]: number | bigint;
405
+ [FFIType.u64_fast]: number | bigint;
406
+ [FFIType.function]: Pointer | null;
407
+ }
408
+ interface FFITypeStringToType {
409
+ ["char"]: FFIType.char;
410
+ ["int8_t"]: FFIType.int8_t;
411
+ ["i8"]: FFIType.i8;
412
+ ["uint8_t"]: FFIType.uint8_t;
413
+ ["u8"]: FFIType.u8;
414
+ ["int16_t"]: FFIType.int16_t;
415
+ ["i16"]: FFIType.i16;
416
+ ["uint16_t"]: FFIType.uint16_t;
417
+ ["u16"]: FFIType.u16;
418
+ ["int32_t"]: FFIType.int32_t;
419
+ ["i32"]: FFIType.i32;
420
+ ["int"]: FFIType.int;
421
+ ["uint32_t"]: FFIType.uint32_t;
422
+ ["u32"]: FFIType.u32;
423
+ ["int64_t"]: FFIType.int64_t;
424
+ ["i64"]: FFIType.i64;
425
+ ["uint64_t"]: FFIType.uint64_t;
426
+ ["u64"]: FFIType.u64;
427
+ ["double"]: FFIType.double;
428
+ ["f64"]: FFIType.f64;
429
+ ["float"]: FFIType.float;
430
+ ["f32"]: FFIType.f32;
431
+ ["bool"]: FFIType.bool;
432
+ ["ptr"]: FFIType.ptr;
433
+ ["pointer"]: FFIType.pointer;
434
+ ["void"]: FFIType.void;
435
+ ["cstring"]: FFIType.cstring;
436
+ ["function"]: FFIType.pointer; // for now
437
+ ["usize"]: FFIType.uint64_t; // for now
438
+ ["callback"]: FFIType.pointer; // for now
439
+ }
440
+
441
+ type FFITypeOrString = FFIType | keyof FFITypeStringToType;
442
+
443
+ interface FFIFunction {
444
+ /**
445
+ * Arguments to a FFI function (C ABI)
446
+ *
447
+ * Defaults to an empty array, which means no arguments.
448
+ *
449
+ * To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
450
+ *
451
+ * @example
452
+ * From JavaScript:
453
+ * ```ts
454
+ * import { dlopen, FFIType, suffix } from "bun:ffi"
455
+ *
456
+ * const lib = dlopen(`adder.${suffix}`, {
457
+ * add: {
458
+ * // FFIType can be used or you can pass string labels.
459
+ * args: [FFIType.i32, "i32"],
460
+ * returns: "i32",
461
+ * },
462
+ * })
463
+ * lib.symbols.add(1, 2)
464
+ * ```
465
+ * In C:
466
+ * ```c
467
+ * int add(int a, int b) {
468
+ * return a + b;
469
+ * }
470
+ * ```
471
+ */
472
+ readonly args?: readonly FFITypeOrString[];
473
+ /**
474
+ * Return type to a FFI function (C ABI)
475
+ *
476
+ * Defaults to {@link FFIType.void}
477
+ *
478
+ * To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
479
+ *
480
+ * @example
481
+ * From JavaScript:
482
+ * ```ts
483
+ * import { dlopen, CString } from "bun:ffi"
484
+ *
485
+ * const lib = dlopen('z', {
486
+ * version: {
487
+ * returns: "ptr",
488
+ * }
489
+ * });
490
+ * console.log(new CString(lib.symbols.version()));
491
+ * ```
492
+ * In C:
493
+ * ```c
494
+ * char* version()
495
+ * {
496
+ * return "1.0.0";
497
+ * }
498
+ * ```
499
+ */
500
+ readonly returns?: FFITypeOrString;
501
+
502
+ /**
503
+ * Function pointer to the native function
504
+ *
505
+ * If provided, instead of using dlsym() to lookup the function, Bun will use this instead.
506
+ * This pointer should not be null (0).
507
+ *
508
+ * This is useful if the library has already been loaded
509
+ * or if the module is also using Node-API.
510
+ */
511
+ readonly ptr?: Pointer | bigint;
512
+
513
+ /**
514
+ * Can C/FFI code call this function from a separate thread?
515
+ *
516
+ * Only supported with {@link JSCallback}.
517
+ *
518
+ * This does not make the function run in a separate thread. It is still up to the application/library
519
+ * to run their code in a separate thread.
520
+ *
521
+ * By default, {@link JSCallback} calls are not thread-safe. Turning this on
522
+ * incurs a small performance penalty for every function call. That small
523
+ * performance penalty needs to be less than the performance gain from
524
+ * running the function in a separate thread.
525
+ *
526
+ * @default false
527
+ */
528
+ readonly threadsafe?: boolean;
529
+ }
530
+
531
+ type Symbols = Readonly<Record<string, FFIFunction>>;
532
+
533
+ // /**
534
+ // * Compile a callback function
535
+ // *
536
+ // * Returns a function pointer
537
+ // *
538
+ // */
539
+ // export function callback(ffi: FFIFunction, cb: Function): number;
540
+
541
+ interface Library<Fns extends Symbols> {
542
+ symbols: ConvertFns<Fns>;
543
+
544
+ /**
545
+ * `dlclose` the library, unloading the symbols and freeing allocated memory.
546
+ *
547
+ * Once called, the library is no longer usable.
548
+ *
549
+ * Calling a function from a library that has been closed is undefined behavior.
550
+ */
551
+ close(): void;
552
+ }
553
+
554
+ type ToFFIType<T extends FFITypeOrString> = T extends FFIType ? T : T extends string ? FFITypeStringToType[T] : never;
555
+
556
+ type ConvertFns<Fns extends Symbols> = {
557
+ [K in keyof Fns]: (
558
+ ...args: Fns[K]["args"] extends infer A extends readonly FFITypeOrString[]
559
+ ? { [L in keyof A]: FFITypeToArgsType[ToFFIType<A[L]>] }
560
+ : // eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
561
+ [unknown] extends [Fns[K]["args"]]
562
+ ? []
563
+ : never
564
+ ) => [unknown] extends [Fns[K]["returns"]] // eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
565
+ ? undefined
566
+ : FFITypeToReturnsType[ToFFIType<NonNullable<Fns[K]["returns"]>>];
567
+ };
568
+
569
+ /**
570
+ * Open a library using `"bun:ffi"`
571
+ *
572
+ * @param name The name of the library or file path. This will be passed to `dlopen()`
573
+ * @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
574
+ *
575
+ * @example
576
+ *
577
+ * ```js
578
+ * import {dlopen} from 'bun:ffi';
579
+ *
580
+ * const lib = dlopen("duckdb.dylib", {
581
+ * get_version: {
582
+ * returns: "cstring",
583
+ * args: [],
584
+ * },
585
+ * });
586
+ * lib.symbols.get_version();
587
+ * // "1.0.0"
588
+ * ```
589
+ *
590
+ * This is powered by just-in-time compiling C wrappers
591
+ * that convert JavaScript types to C types and back. Internally,
592
+ * bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
593
+ * goes to Fabrice Bellard and TinyCC maintainers for making this possible.
594
+ */
595
+ function dlopen<Fns extends Record<string, FFIFunction>>(name: string, symbols: Fns): Library<Fns>;
596
+
597
+ /**
598
+ * Turn a native library's function pointer into a JavaScript function
599
+ *
600
+ * Libraries using Node-API & bun:ffi in the same module could use this to skip an extra dlopen() step.
601
+ *
602
+ * @param fn {@link FFIFunction} declaration. `ptr` is required
603
+ *
604
+ * @example
605
+ *
606
+ * ```js
607
+ * import {CFunction} from 'bun:ffi';
608
+ *
609
+ * const getVersion = new CFunction({
610
+ * returns: "cstring",
611
+ * args: [],
612
+ * ptr: myNativeLibraryGetVersion,
613
+ * });
614
+ * getVersion();
615
+ * getVersion.close();
616
+ * ```
617
+ *
618
+ * This is powered by just-in-time compiling C wrappers
619
+ * that convert JavaScript types to C types and back. Internally,
620
+ * bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
621
+ * goes to Fabrice Bellard and TinyCC maintainers for making this possible.
622
+ */
623
+ function CFunction(fn: FFIFunction & { ptr: Pointer }): CallableFunction & {
624
+ /**
625
+ * Free the memory allocated by the wrapping function
626
+ */
627
+ close(): void;
628
+ };
629
+
630
+ /**
631
+ * Link a map of symbols to JavaScript functions
632
+ *
633
+ * This lets you use native libraries that were already loaded somehow. You usually will want {@link dlopen} instead.
634
+ *
635
+ * You could use this with Node-API to skip loading a second time.
636
+ *
637
+ * @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
638
+ *
639
+ * @example
640
+ *
641
+ * ```js
642
+ * import { linkSymbols } from "bun:ffi";
643
+ *
644
+ * const [majorPtr, minorPtr, patchPtr] = getVersionPtrs();
645
+ *
646
+ * const lib = linkSymbols({
647
+ * // Unlike with dlopen(), the names here can be whatever you want
648
+ * getMajor: {
649
+ * returns: "cstring",
650
+ * args: [],
651
+ *
652
+ * // Since this doesn't use dlsym(), you have to provide a valid ptr
653
+ * // That ptr could be a number or a bigint
654
+ * // An invalid pointer will crash your program.
655
+ * ptr: majorPtr,
656
+ * },
657
+ * getMinor: {
658
+ * returns: "cstring",
659
+ * args: [],
660
+ * ptr: minorPtr,
661
+ * },
662
+ * getPatch: {
663
+ * returns: "cstring",
664
+ * args: [],
665
+ * ptr: patchPtr,
666
+ * },
667
+ * });
668
+ *
669
+ * const [major, minor, patch] = [
670
+ * lib.symbols.getMajor(),
671
+ * lib.symbols.getMinor(),
672
+ * lib.symbols.getPatch(),
673
+ * ];
674
+ * ```
675
+ *
676
+ * This is powered by just-in-time compiling C wrappers
677
+ * that convert JavaScript types to C types and back. Internally,
678
+ * bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
679
+ * goes to Fabrice Bellard and TinyCC maintainers for making this possible.
680
+ */
681
+ function linkSymbols<Fns extends Record<string, FFIFunction>>(symbols: Fns): Library<Fns>;
682
+
683
+ /**
684
+ * Read a pointer as a {@link Buffer}
685
+ *
686
+ * If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
687
+ *
688
+ * @param ptr The memory address to read
689
+ * @param byteOffset bytes to skip before reading
690
+ * @param byteLength bytes to read
691
+ *
692
+ * While there are some checks to catch invalid pointers, this is a difficult
693
+ * thing to do safely. Passing an invalid pointer can crash the program and
694
+ * reading beyond the bounds of the pointer will crash the program or cause
695
+ * undefined behavior. Use with care!
696
+ */
697
+ function toBuffer(ptr: Pointer, byteOffset?: number, byteLength?: number): Buffer;
698
+
699
+ /**
700
+ * Read a pointer as an {@link ArrayBuffer}
701
+ *
702
+ * If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
703
+ *
704
+ * @param ptr The memory address to read
705
+ * @param byteOffset bytes to skip before reading
706
+ * @param byteLength bytes to read
707
+ *
708
+ * While there are some checks to catch invalid pointers, this is a difficult
709
+ * thing to do safely. Passing an invalid pointer can crash the program and
710
+ * reading beyond the bounds of the pointer will crash the program or cause
711
+ * undefined behavior. Use with care!
712
+ */
713
+ function toArrayBuffer(ptr: Pointer, byteOffset?: number, byteLength?: number): ArrayBuffer;
714
+
715
+ namespace read {
716
+ /**
717
+ * The read function behaves similarly to DataView,
718
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
719
+ *
720
+ * @param ptr The memory address to read
721
+ * @param byteOffset bytes to skip before reading
722
+ *
723
+ * While there are some checks to catch invalid pointers, this is a difficult
724
+ * thing to do safely. Passing an invalid pointer can crash the program and
725
+ * reading beyond the bounds of the pointer will crash the program or cause
726
+ * undefined behavior. Use with care!
727
+ */
728
+ function u8(ptr: Pointer, byteOffset?: number): number;
729
+ /**
730
+ * The read function behaves similarly to DataView,
731
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
732
+ *
733
+ * @param ptr The memory address to read
734
+ * @param byteOffset bytes to skip before reading
735
+ *
736
+ * While there are some checks to catch invalid pointers, this is a difficult
737
+ * thing to do safely. Passing an invalid pointer can crash the program and
738
+ * reading beyond the bounds of the pointer will crash the program or cause
739
+ * undefined behavior. Use with care!
740
+ */
741
+ function i8(ptr: Pointer, byteOffset?: number): number;
742
+ /**
743
+ * The read function behaves similarly to DataView,
744
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
745
+ *
746
+ * @param ptr The memory address to read
747
+ * @param byteOffset bytes to skip before reading
748
+ *
749
+ * While there are some checks to catch invalid pointers, this is a difficult
750
+ * thing to do safely. Passing an invalid pointer can crash the program and
751
+ * reading beyond the bounds of the pointer will crash the program or cause
752
+ * undefined behavior. Use with care!
753
+ */
754
+ function u16(ptr: Pointer, byteOffset?: number): number;
755
+ /**
756
+ * The read function behaves similarly to DataView,
757
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
758
+ *
759
+ * @param ptr The memory address to read
760
+ * @param byteOffset bytes to skip before reading
761
+ *
762
+ * While there are some checks to catch invalid pointers, this is a difficult
763
+ * thing to do safely. Passing an invalid pointer can crash the program and
764
+ * reading beyond the bounds of the pointer will crash the program or cause
765
+ * undefined behavior. Use with care!
766
+ */
767
+ function i16(ptr: Pointer, byteOffset?: number): number;
768
+ /**
769
+ * The read function behaves similarly to DataView,
770
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
771
+ *
772
+ * @param ptr The memory address to read
773
+ * @param byteOffset bytes to skip before reading
774
+ *
775
+ * While there are some checks to catch invalid pointers, this is a difficult
776
+ * thing to do safely. Passing an invalid pointer can crash the program and
777
+ * reading beyond the bounds of the pointer will crash the program or cause
778
+ * undefined behavior. Use with care!
779
+ */
780
+ function u32(ptr: Pointer, byteOffset?: number): number;
781
+ /**
782
+ * The read function behaves similarly to DataView,
783
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
784
+ *
785
+ * @param ptr The memory address to read
786
+ * @param byteOffset bytes to skip before reading
787
+ *
788
+ * While there are some checks to catch invalid pointers, this is a difficult
789
+ * thing to do safely. Passing an invalid pointer can crash the program and
790
+ * reading beyond the bounds of the pointer will crash the program or cause
791
+ * undefined behavior. Use with care!
792
+ */
793
+ function i32(ptr: Pointer, byteOffset?: number): number;
794
+ /**
795
+ * The read function behaves similarly to DataView,
796
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
797
+ *
798
+ * @param ptr The memory address to read
799
+ * @param byteOffset bytes to skip before reading
800
+ *
801
+ * While there are some checks to catch invalid pointers, this is a difficult
802
+ * thing to do safely. Passing an invalid pointer can crash the program and
803
+ * reading beyond the bounds of the pointer will crash the program or cause
804
+ * undefined behavior. Use with care!
805
+ */
806
+ function f32(ptr: Pointer, byteOffset?: number): number;
807
+ /**
808
+ * The read function behaves similarly to DataView,
809
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
810
+ *
811
+ * @param ptr The memory address to read
812
+ * @param byteOffset bytes to skip before reading
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 u64(ptr: Pointer, byteOffset?: number): bigint;
820
+ /**
821
+ * The read function behaves similarly to DataView,
822
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
823
+ *
824
+ * @param ptr The memory address to read
825
+ * @param byteOffset bytes to skip before reading
826
+ *
827
+ * While there are some checks to catch invalid pointers, this is a difficult
828
+ * thing to do safely. Passing an invalid pointer can crash the program and
829
+ * reading beyond the bounds of the pointer will crash the program or cause
830
+ * undefined behavior. Use with care!
831
+ */
832
+ function i64(ptr: Pointer, byteOffset?: number): bigint;
833
+ /**
834
+ * The read function behaves similarly to DataView,
835
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
836
+ *
837
+ * @param ptr The memory address to read
838
+ * @param byteOffset bytes to skip before reading
839
+ *
840
+ * While there are some checks to catch invalid pointers, this is a difficult
841
+ * thing to do safely. Passing an invalid pointer can crash the program and
842
+ * reading beyond the bounds of the pointer will crash the program or cause
843
+ * undefined behavior. Use with care!
844
+ */
845
+ function f64(ptr: Pointer, byteOffset?: number): number;
846
+ /**
847
+ * The read function behaves similarly to DataView,
848
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
849
+ *
850
+ * @param ptr The memory address to read
851
+ * @param byteOffset bytes to skip before reading
852
+ *
853
+ * While there are some checks to catch invalid pointers, this is a difficult
854
+ * thing to do safely. Passing an invalid pointer can crash the program and
855
+ * reading beyond the bounds of the pointer will crash the program or cause
856
+ * undefined behavior. Use with care!
857
+ */
858
+ function ptr(ptr: Pointer, byteOffset?: number): number;
859
+ /**
860
+ * The read function behaves similarly to DataView,
861
+ * but it's usually faster because it doesn't need to create a DataView or ArrayBuffer.
862
+ *
863
+ * @param ptr The memory address to read
864
+ * @param byteOffset bytes to skip before reading
865
+ *
866
+ * While there are some checks to catch invalid pointers, this is a difficult
867
+ * thing to do safely. Passing an invalid pointer can crash the program and
868
+ * reading beyond the bounds of the pointer will crash the program or cause
869
+ * undefined behavior. Use with care!
870
+ */
871
+ function intptr(ptr: Pointer, byteOffset?: number): number;
872
+ }
873
+
874
+ /**
875
+ * Get the pointer backing a {@link TypedArray} or {@link ArrayBuffer}
876
+ *
877
+ * Use this to pass {@link TypedArray} or {@link ArrayBuffer} to C functions.
878
+ *
879
+ * This is for use with FFI functions. For performance reasons, FFI will
880
+ * not automatically convert typed arrays to C pointers.
881
+ *
882
+ * @param {TypedArray|ArrayBuffer|DataView} view the typed array or array buffer to get the pointer for
883
+ * @param {number} byteOffset optional offset into the view in bytes
884
+ *
885
+ * @example
886
+ *
887
+ * From JavaScript:
888
+ * ```js
889
+ * const array = new Uint8Array(10);
890
+ * const rawPtr = ptr(array);
891
+ * myFFIFunction(rawPtr);
892
+ * ```
893
+ * To C:
894
+ * ```c
895
+ * void myFFIFunction(char* rawPtr) {
896
+ * // Do something with rawPtr
897
+ * }
898
+ * ```
899
+ */
900
+ function ptr(view: NodeJS.TypedArray | ArrayBufferLike | DataView, byteOffset?: number): Pointer;
901
+
902
+ /**
903
+ * Get a string from a UTF-8 encoded C string
904
+ * If `byteLength` is not provided, the string is assumed to be null-terminated.
905
+ *
906
+ * @example
907
+ * ```js
908
+ * var ptr = lib.symbols.getVersion();
909
+ * console.log(new CString(ptr));
910
+ * ```
911
+ *
912
+ * @example
913
+ * ```js
914
+ * var ptr = lib.symbols.getVersion();
915
+ * // print the first 4 characters
916
+ * console.log(new CString(ptr, 0, 4));
917
+ * ```
918
+ *
919
+ * While there are some checks to catch invalid pointers, this is a difficult
920
+ * thing to do safely. Passing an invalid pointer can crash the program and
921
+ * reading beyond the bounds of the pointer will crash the program or cause
922
+ * undefined behavior. Use with care!
923
+ */
924
+
925
+ class CString extends String {
926
+ /**
927
+ * Get a string from a UTF-8 encoded C string
928
+ * If `byteLength` is not provided, the string is assumed to be null-terminated.
929
+ *
930
+ * @param ptr The pointer to the C string
931
+ * @param byteOffset bytes to skip before reading
932
+ * @param byteLength bytes to read
933
+ *
934
+ * @example
935
+ * ```js
936
+ * var ptr = lib.symbols.getVersion();
937
+ * console.log(new CString(ptr));
938
+ * ```
939
+ *
940
+ * @example
941
+ * ```js
942
+ * var ptr = lib.symbols.getVersion();
943
+ * // print the first 4 characters
944
+ * console.log(new CString(ptr, 0, 4));
945
+ * ```
946
+ *
947
+ * While there are some checks to catch invalid pointers, this is a difficult
948
+ * thing to do safely. Passing an invalid pointer can crash the program and
949
+ * reading beyond the bounds of the pointer will crash the program or cause
950
+ * undefined behavior. Use with care!
951
+ */
952
+ constructor(ptr: Pointer, byteOffset?: number, byteLength?: number);
953
+
954
+ /**
955
+ * The ptr to the C string
956
+ *
957
+ * This `CString` instance is a clone of the string, so it
958
+ * is safe to continue using this instance after the `ptr` has been
959
+ * freed.
960
+ */
961
+ ptr: Pointer;
962
+ byteOffset?: number;
963
+ byteLength?: number;
964
+
965
+ /**
966
+ * Get the {@link ptr} as an `ArrayBuffer`
967
+ *
968
+ * `null` or empty ptrs returns an `ArrayBuffer` with `byteLength` 0
969
+ */
970
+ get arrayBuffer(): ArrayBuffer;
971
+ }
972
+
973
+ /**
974
+ * Pass a JavaScript function to FFI (Foreign Function Interface)
975
+ */
976
+ class JSCallback {
977
+ /**
978
+ * Enable a JavaScript callback function to be passed to C with bun:ffi
979
+ *
980
+ * @param callback The JavaScript function to be called
981
+ * @param definition The C function definition
982
+ */
983
+ constructor(callback: (...args: any[]) => any, definition: FFIFunction);
984
+
985
+ /**
986
+ * The pointer to the C function
987
+ *
988
+ * Becomes `null` once {@link JSCallback.prototype.close} is called
989
+ */
990
+ readonly ptr: Pointer | null;
991
+
992
+ /**
993
+ * Can the callback be called from a different thread?
994
+ */
995
+ readonly threadsafe: boolean;
996
+
997
+ /**
998
+ * Free the memory allocated for the callback
999
+ *
1000
+ * If called multiple times, does nothing after the first call.
1001
+ */
1002
+ close(): void;
1003
+ }
1004
+
1005
+ /**
1006
+ * View the generated C code for FFI bindings
1007
+ *
1008
+ * You probably won't need this unless there's a bug in the FFI bindings
1009
+ * generator or you're just curious.
1010
+ */
1011
+ function viewSource(symbols: Symbols, is_callback?: false): string[];
1012
+ function viewSource(callback: FFIFunction, is_callback: true): string;
1013
+
1014
+ /**
1015
+ * Platform-specific file extension name for dynamic libraries
1016
+ *
1017
+ * "." is not included
1018
+ *
1019
+ * @example
1020
+ * ```js
1021
+ * "dylib" // macOS
1022
+ * ```
1023
+ *
1024
+ * @example
1025
+ * ```js
1026
+ * "so" // linux
1027
+ * ```
1028
+ */
1029
+ const suffix: string;
1030
+ }