Skip to content

Commit 5e33eeb

Browse files
committed
fixup! src,lib: initial ffi implementation
1 parent 1eb617d commit 5e33eeb

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

doc/api/ffi.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Foreign Function Interface (FFI)
2+
3+
<!--introduced_in=REPLACEME-->
4+
5+
> Stability: 1 - Experimental
6+
7+
<!-- source_link=lib/ffi.js -->
8+
9+
The `node:ffi` module provides a foreign function interface (FFI). It can be
10+
used to make native function calls without the use of native addons. To access
11+
it:
12+
13+
```mjs
14+
import ffi from 'node:ffi';
15+
```
16+
17+
```cjs
18+
const ffi = require('node:ffi');
19+
```
20+
21+
This module is only available under the `node:` scheme. The following will not
22+
work:
23+
24+
```mjs
25+
import ffi from 'ffi';
26+
```
27+
28+
```cjs
29+
const ffi = require('ffi');
30+
```
31+
32+
### Supported Types
33+
34+
The following types are supported.
35+
36+
* `char`
37+
* `signed char`
38+
* `unsigned char`
39+
* `short`
40+
* `short int`
41+
* `signed short`
42+
* `signed short int`
43+
* `unsigned short`
44+
* `unsigned short int`
45+
* `int`
46+
* `signed`
47+
* `signed int`
48+
* `unsigned`
49+
* `unsigned int`
50+
* `long`
51+
* `long int`
52+
* `signed long`
53+
* `signed long int`
54+
* `unsigned long`
55+
* `unsigned long int`
56+
* `long long`
57+
* `long long int`
58+
* `signed long long`
59+
* `signed long long int`
60+
* `unsigned long long`
61+
* `unsigned long long int`
62+
* `float`
63+
* `double`
64+
* `uint8_t`
65+
* `int8_t`
66+
* `uint16_t`
67+
* `int16_t`
68+
* `uint32_t`
69+
* `int32_t`
70+
* `uint64_t`
71+
* `int64_t`
72+
73+
For functions with pointer arguments or return values, use the type `pointer`.
74+
Any type provided that contains an asterisk (`*`) will be treated as `pointer`.
75+
76+
All types map to `number` in JavaScript, except:
77+
78+
* 64-bit integer types, which are always mapped to `bigint`
79+
* Pointers, which are mapped to `bigint` on systems with 64-bit pointers.
80+
81+
Currently, no conversion is done between `number` and `bigint`, so be careful
82+
when calling functions returned from `getNativeFunction`.
83+
84+
Function pointers are not currently supported.
85+
86+
Structs are not supported as arguments or return values, but pointers to them
87+
are. Structs can be built up in or read from `Buffer`s, and `getBufferPointer`
88+
can be used from there.
89+
90+
## `ffi.getBufferPointer(buffer)`
91+
92+
<!-- YAML
93+
added: REPLACEME
94+
-->
95+
96+
* `buffer` {Buffer|TypedArray|ArrayBuffer} A buffer to get the pointer of.
97+
* Returns: {bigint} A pointer, represented as a `bigint`.
98+
99+
Gets the memory address of the first byte of `buffer`. If there's an offset,
100+
that's added to the result.
101+
102+
## `ffi.getNativeFunction(library, func, retType, argTypes)`
103+
104+
<!-- YAML
105+
added: REPLACEME
106+
-->
107+
108+
* `library` {string|null} The path to the shared library.
109+
* `func` {string} The function name inside the shared library.
110+
* `retType` {string} The return value's type.
111+
* `argTypes` {Array} The types of the arguments.
112+
* Returns: {function} A JavaScript wrapper for the native function.
113+
114+
Retrieves a native function from a given shared library (i.e. `.so`, `.dylib`,
115+
or `.dll`), and returns a JavaScript wrapper function. The wrapper function does
116+
the appropriate conversion from JavaScript `number`s and `bigints` to the
117+
appropriate types as per the `argTypes` array, and similarly for return values.
118+
119+
If `null` is provided as the `library`, function symbols will be searched for in
120+
the current process.
121+
122+
## `ffi.sizeof(type)`
123+
124+
<!-- YAML
125+
added: REPLACEME
126+
-->
127+
128+
* `type` {string} The name of the type to get the size of.
129+
* Returns: {number} The size of the the type in bytes.
130+
131+
A passthrough to the C `sizeof` operator. The `type` must be one of the
132+
supported types, or else the return value will be `undefined`.

0 commit comments

Comments
 (0)