forked from oven-sh/bun-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi.d.ts
763 lines (730 loc) · 17.5 KB
/
ffi.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
453
454
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
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
762
763
/**
* `bun:ffi` lets you efficiently call C functions & FFI functions from JavaScript
* without writing bindings yourself.
*
* ```js
* import {dlopen, CString, ptr} from 'bun:ffi';
*
* const lib = dlopen('libsqlite3', {
* });
* ```
*
* This is powered by just-in-time compiling C wrappers
* that convert JavaScript types to C types and back. Internally,
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
*
*/
declare module "bun:ffi" {
export enum FFIType {
char = 0,
/**
* 8-bit signed integer
*
* Must be a value between -127 and 127
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* signed char
* char // on x64 & aarch64 macOS
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
int8_t = 1,
/**
* 8-bit signed integer
*
* Must be a value between -127 and 127
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* signed char
* char // on x64 & aarch64 macOS
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
i8 = 1,
/**
* 8-bit unsigned integer
*
* Must be a value between 0 and 255
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* unsigned char
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
uint8_t = 2,
/**
* 8-bit unsigned integer
*
* Must be a value between 0 and 255
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* unsigned char
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
u8 = 2,
/**
* 16-bit signed integer
*
* Must be a value between -32768 and 32767
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* in16_t
* short // on arm64 & x64
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
int16_t = 3,
/**
* 16-bit signed integer
*
* Must be a value between -32768 and 32767
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* in16_t
* short // on arm64 & x64
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
i16 = 3,
/**
* 16-bit unsigned integer
*
* Must be a value between 0 and 65535, inclusive.
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* uint16_t
* unsigned short // on arm64 & x64
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
uint16_t = 4,
/**
* 16-bit unsigned integer
*
* Must be a value between 0 and 65535, inclusive.
*
* When passing to a FFI function (C ABI), type coercion is not performed.
*
* In C:
* ```c
* uint16_t
* unsigned short // on arm64 & x64
* ```
*
* In JavaScript:
* ```js
* var num = 0;
* ```
*/
u16 = 4,
/**
* 32-bit signed integer
*
*/
int32_t = 5,
/**
* 32-bit signed integer
*
* Alias of {@link FFIType.int32_t}
*/
i32 = 5,
/**
* 32-bit signed integer
*
* The same as `int` in C
*
* ```c
* int
* ```
*/
int = 5,
/**
* 32-bit unsigned integer
*
* The same as `unsigned int` in C (on x64 & arm64)
*
* C:
* ```c
* unsigned int
* ```
* JavaScript:
* ```js
* ptr(new Uint32Array(1))
* ```
*/
uint32_t = 6,
/**
* 32-bit unsigned integer
*
* Alias of {@link FFIType.uint32_t}
*/
u32 = 6,
/**
* int64 is a 64-bit signed integer
*
* This is not implemented yet!
*/
int64_t = 7,
/**
* i64 is a 64-bit signed integer
*
* This is not implemented yet!
*/
i64 = 7,
/**
* 64-bit unsigned integer
*
* This is not implemented yet!
*/
uint64_t = 8,
/**
* 64-bit unsigned integer
*
* This is not implemented yet!
*/
u64 = 8,
/**
* Doubles are not supported yet!
*/
double = 9,
/**
* Doubles are not supported yet!
*/
f64 = 9,
/**
* Floats are not supported yet!
*/
float = 10,
/**
* Floats are not supported yet!
*/
f32 = 10,
/**
* Booelan value
*
* Must be `true` or `false`. `0` and `1` type coercion is not supported.
*
* In C, this corresponds to:
* ```c
* bool
* _Bool
* ```
*
*
*/
bool = 11,
/**
* Pointer value
*
* See {@link Bun.FFI.ptr} for more information
*
* In C:
* ```c
* void*
* ```
*
* In JavaScript:
* ```js
* ptr(new Uint8Array(1))
* ```
*/
ptr = 12,
/**
* Pointer value
*
* alias of {@link FFIType.ptr}
*/
pointer = 12,
/**
* void value
*
* void arguments are not supported
*
* void return type is the default return type
*
* In C:
* ```c
* void
* ```
*
*/
void = 13,
/**
* When used as a `returns`, this will automatically become a {@link CString}.
*
* When used in `args` it is equivalent to {@link FFIType.pointer}
*
*/
cstring = 14,
/**
* Attempt to coerce `BigInt` into a `Number` if it fits. This improves performance
* but means you might get a `BigInt` or you might get a `number`.
*
* In C, this always becomes `int64_t`
*
* In JavaScript, this could be number or it could be BigInt, depending on what
* value is passed in.
*
*/
i64_fast = 15,
/**
* Attempt to coerce `BigInt` into a `Number` if it fits. This improves performance
* but means you might get a `BigInt` or you might get a `number`.
*
* In C, this always becomes `uint64_t`
*
* In JavaScript, this could be number or it could be BigInt, depending on what
* value is passed in.
*
*/
u64_fast = 16,
}
export type FFITypeOrString =
| FFIType
| "char"
| "int8_t"
| "i8"
| "uint8_t"
| "u8"
| "int16_t"
| "i16"
| "uint16_t"
| "u16"
| "int32_t"
| "i32"
| "int"
| "uint32_t"
| "u32"
| "int64_t"
| "i64"
| "uint64_t"
| "u64"
| "double"
| "f64"
| "float"
| "f32"
| "bool"
| "ptr"
| "pointer"
| "void"
| "cstring";
interface FFIFunction {
/**
* Arguments to a FFI function (C ABI)
*
* Defaults to an empty array, which means no arguments.
*
* To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
*
* @example
* From JavaScript:
* ```js
* const lib = dlopen('add', {
* // FFIType can be used or you can pass string labels.
* args: [FFIType.i32, "i32"],
* returns: "i32",
* });
* lib.symbols.add(1, 2)
* ```
* In C:
* ```c
* int add(int a, int b) {
* return a + b;
* }
* ```
*/
args?: FFITypeOrString[];
/**
* Return type to a FFI function (C ABI)
*
* Defaults to {@link FFIType.void}
*
* To pass a pointer, use "ptr" or "pointer" as the type name. To get a pointer, see {@link ptr}.
*
* @example
* From JavaScript:
* ```js
* const lib = dlopen('z', {
* version: {
* returns: "ptr",
* }
* });
* console.log(new CString(lib.symbols.version()));
* ```
* In C:
* ```c
* char* version()
* {
* return "1.0.0";
* }
* ```
*/
returns?: FFITypeOrString;
/**
* Function pointer to the native function
*
* If provided, instead of using dlsym() to lookup the function, Bun will use this instead.
* This pointer should not be null (0).
*
* This is useful if the library has already been loaded
* or if the module is also using Node-API.
*/
ptr?: number | bigint;
}
type Symbols = Record<string, FFIFunction>;
// /**
// * Compile a callback function
// *
// * Returns a function pointer
// *
// */
// export function callback(ffi: FFIFunction, cb: Function): number;
export interface Library {
symbols: Record<
string,
CallableFunction & {
/**
* The function without a wrapper
*/
native: CallableFunction;
}
>;
/**
* `dlclose` the library, unloading the symbols and freeing allocated memory.
*
* Once called, the library is no longer usable.
*
* Calling a function from a library that has been closed is undefined behavior.
*/
close(): void;
}
/**
* Open a library using `"bun:ffi"`
*
* @param name The name of the library or file path. This will be passed to `dlopen()`
* @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
*
* @example
*
* ```js
* import {dlopen} from 'bun:ffi';
*
* const lib = dlopen("duckdb.dylib", {
* get_version: {
* returns: "cstring",
* args: [],
* },
* });
* lib.symbols.get_version();
* // "1.0.0"
* ```
*
* This is powered by just-in-time compiling C wrappers
* that convert JavaScript types to C types and back. Internally,
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
*
*/
export function dlopen(name: string, symbols: Symbols): Library;
/**
* Turn a native library's function pointer into a JavaScript function
*
* Libraries using Node-API & bun:ffi in the same module could use this to skip an extra dlopen() step.
*
* @param fn {@link FFIFunction} declaration. `ptr` is required
*
* @example
*
* ```js
* import {CFunction} from 'bun:ffi';
*
* const getVersion = new CFunction({
* returns: "cstring",
* args: [],
* ptr: myNativeLibraryGetVersion,
* });
* getVersion();
* getVersion.close();
* ```
*
* This is powered by just-in-time compiling C wrappers
* that convert JavaScript types to C types and back. Internally,
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
*
*/
export function CFunction(
fn: FFIFunction & { ptr: number | bigint }
): CallableFunction & {
/**
* Free the memory allocated by the wrapping function
*/
close(): void;
};
/**
* Link a map of symbols to JavaScript functions
*
* This lets you use native libraries that were already loaded somehow. You usually will want {@link dlopen} instead.
*
* You could use this with Node-API to skip loading a second time.
*
* @param symbols Map of symbols to load where the key is the symbol name and the value is the {@link FFIFunction}
*
* @example
*
* ```js
* import { linkSymbols } from "bun:ffi";
*
* const [majorPtr, minorPtr, patchPtr] = getVersionPtrs();
*
* const lib = linkSymbols({
* // Unlike with dlopen(), the names here can be whatever you want
* getMajor: {
* returns: "cstring",
* args: [],
*
* // Since this doesn't use dlsym(), you have to provide a valid ptr
* // That ptr could be a number or a bigint
* // An invalid pointer will crash your program.
* ptr: majorPtr,
* },
* getMinor: {
* returns: "cstring",
* args: [],
* ptr: minorPtr,
* },
* getPatch: {
* returns: "cstring",
* args: [],
* ptr: patchPtr,
* },
* });
*
* const [major, minor, patch] = [
* lib.symbols.getMajor(),
* lib.symbols.getMinor(),
* lib.symbols.getPatch(),
* ];
* ```
*
* This is powered by just-in-time compiling C wrappers
* that convert JavaScript types to C types and back. Internally,
* bun uses [tinycc](https://github.com/TinyCC/tinycc), so a big thanks
* goes to Fabrice Bellard and TinyCC maintainers for making this possible.
*
*/
export function linkSymbols(symbols: Symbols): Library;
/**
* Read a pointer as a {@link Buffer}
*
* If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
*
* @param ptr The memory address to read
* @param byteOffset bytes to skip before reading
* @param byteLength bytes to read
*
* While there are some checks to catch invalid pointers, this is a difficult
* thing to do safely. Passing an invalid pointer can crash the program and
* reading beyond the bounds of the pointer will crash the program or cause
* undefined behavior. Use with care!
*
*/
export function toBuffer(
ptr: number,
byteOffset?: number,
byteLength?: number
): Buffer;
/**
* Read a pointer as an {@link ArrayBuffer}
*
* If `byteLength` is not provided, the pointer is assumed to be 0-terminated.
*
* @param ptr The memory address to read
* @param byteOffset bytes to skip before reading
* @param byteLength bytes to read
*
* While there are some checks to catch invalid pointers, this is a difficult
* thing to do safely. Passing an invalid pointer can crash the program and
* reading beyond the bounds of the pointer will crash the program or cause
* undefined behavior. Use with care!
*/
export function toArrayBuffer(
ptr: number,
byteOffset?: number,
byteLength?: number
): ArrayBuffer;
/**
* Get the pointer backing a {@link TypedArray} or {@link ArrayBuffer}
*
* Use this to pass {@link TypedArray} or {@link ArrayBuffer} to C functions.
*
* This is for use with FFI functions. For performance reasons, FFI will
* not automatically convert typed arrays to C pointers.
*
* @param {TypedArray|ArrayBuffer|DataView} view the typed array or array buffer to get the pointer for
* @param {number} byteOffset optional offset into the view in bytes
*
* @example
*
* From JavaScript:
* ```js
* const array = new Uint8Array(10);
* const rawPtr = ptr(array);
* myFFIFunction(rawPtr);
* ```
* To C:
* ```c
* void myFFIFunction(char* rawPtr) {
* // Do something with rawPtr
* }
* ```
*
*/
export function ptr(
view: TypedArray | ArrayBufferLike | DataView,
byteOffset?: number
): number;
/**
* Get a string from a UTF-8 encoded C string
* If `byteLength` is not provided, the string is assumed to be null-terminated.
*
* @example
* ```js
* var ptr = lib.symbols.getVersion();
* console.log(new CString(ptr));
* ```
*
* @example
* ```js
* var ptr = lib.symbols.getVersion();
* // print the first 4 characters
* console.log(new CString(ptr, 0, 4));
* ```
*
* While there are some checks to catch invalid pointers, this is a difficult
* thing to do safely. Passing an invalid pointer can crash the program and
* reading beyond the bounds of the pointer will crash the program or cause
* undefined behavior. Use with care!
*/
export class CString extends String {
/**
* Get a string from a UTF-8 encoded C string
* If `byteLength` is not provided, the string is assumed to be null-terminated.
*
* @param ptr The pointer to the C string
* @param byteOffset bytes to skip before reading
* @param byteLength bytes to read
*
*
* @example
* ```js
* var ptr = lib.symbols.getVersion();
* console.log(new CString(ptr));
* ```
*
* @example
* ```js
* var ptr = lib.symbols.getVersion();
* // print the first 4 characters
* console.log(new CString(ptr, 0, 4));
* ```
*
* While there are some checks to catch invalid pointers, this is a difficult
* thing to do safely. Passing an invalid pointer can crash the program and
* reading beyond the bounds of the pointer will crash the program or cause
* undefined behavior. Use with care!
*/
constructor(ptr: number, byteOffset?: number, byteLength?: number);
/**
* The ptr to the C string
*
* This `CString` instance is a clone of the string, so it
* is safe to continue using this instance after the `ptr` has been
* freed.
*/
ptr: number;
byteOffset?: number;
byteLength?: number;
/**
* Get the {@link ptr} as an `ArrayBuffer`
*
* `null` or empty ptrs returns an `ArrayBuffer` with `byteLength` 0
*/
get arrayBuffer(): ArrayBuffer;
}
/**
* View the generated C code for FFI bindings
*
* You probably won't need this unless there's a bug in the FFI bindings
* generator or you're just curious.
*/
export function viewSource(symbols: Symbols, is_callback?: false): string[];
export function viewSource(callback: FFIFunction, is_callback: true): string;
/**
* Platform-specific file extension name for dynamic libraries
*
* "." is not included
*
* @example
* ```js
* "dylib" // macOS
* ```
*
* @example
* ```js
* "so" // linux
* ```
*/
export const suffix: string;
}