Skip to content

Commit 1432c97

Browse files
committed
src: add suffix and types
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
1 parent bb30b89 commit 1432c97

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

deps/libffi/libffi.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@
178178
'preprocess_asm.py',
179179
'include/ffi_cfi.h',
180180
'src/aarch64/internal.h',
181-
'src/aarch64/ksarm64.h',
182181
'src/aarch64/win64_armasm.S',
183182
'<(INTERMEDIATE_DIR)/ffi.h',
184183
'<(INTERMEDIATE_DIR)/fficonfig.h',

doc/api/ffi.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ Supported type names:
7676
* `arraybuffer`
7777
* `function`
7878

79+
These type names are also exposed as constants on `ffi.types`:
80+
81+
* `ffi.types.VOID` = `'void'`
82+
* `ffi.types.POINTER` = `'pointer'`
83+
* `ffi.types.BUFFER` = `'buffer'`
84+
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
85+
* `ffi.types.FUNCTION` = `'function'`
86+
* `ffi.types.BOOL` = `'bool'`
87+
* `ffi.types.CHAR` = `'char'`
88+
* `ffi.types.STRING` = `'string'`
89+
* `ffi.types.FLOAT` = `'float'`
90+
* `ffi.types.DOUBLE` = `'double'`
91+
* `ffi.types.INT_8` = `'int8'`
92+
* `ffi.types.UINT_8` = `'uint8'`
93+
* `ffi.types.INT_16` = `'int16'`
94+
* `ffi.types.UINT_16` = `'uint16'`
95+
* `ffi.types.INT_32` = `'int32'`
96+
* `ffi.types.UINT_32` = `'uint32'`
97+
* `ffi.types.INT_64` = `'int64'`
98+
* `ffi.types.UINT_64` = `'uint64'`
99+
* `ffi.types.FLOAT_32` = `'float32'`
100+
* `ffi.types.FLOAT_64` = `'float64'`
101+
79102
Pointer-like types (`pointer`, `string`, `buffer`, `arraybuffer`, and
80103
`function`) are all passed through the native layer as pointers.
81104

@@ -114,6 +137,28 @@ const signature = {
114137
};
115138
```
116139

140+
## `ffi.suffix`
141+
142+
<!-- YAML
143+
added: REPLACEME
144+
-->
145+
146+
* {string}
147+
148+
The native shared library suffix for the current platform:
149+
150+
* `'dylib'` on macOS
151+
* `'so'` on Unix-like platforms
152+
* `'dll'` on Windows
153+
154+
This can be used to build portable library paths:
155+
156+
```cjs
157+
const { suffix } = require('node:ffi');
158+
159+
const path = `libsqlite3.${suffix}`;
160+
```
161+
117162
## `ffi.dlopen(path[, definitions])`
118163

119164
<!-- YAML

lib/ffi.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@ function exportBuffer(buffer, data, len) {
131131
buffer.copy(targetBuffer, 0, 0, buffer.length);
132132
}
133133

134+
const suffix = process.platform === 'win32' ? 'dll' : process.platform === 'darwin' ? 'dylib' : 'so';
135+
136+
const types = ObjectFreeze({
137+
__proto__: null,
138+
VOID: 'void',
139+
POINTER: 'pointer',
140+
BUFFER: 'buffer',
141+
ARRAY_BUFFER: 'arraybuffer',
142+
FUNCTION: 'function',
143+
BOOL: 'bool',
144+
CHAR: 'char',
145+
STRING: 'string',
146+
FLOAT: 'float',
147+
DOUBLE: 'double',
148+
INT_8: 'int8',
149+
UINT_8: 'uint8',
150+
INT_16: 'int16',
151+
UINT_16: 'uint16',
152+
INT_32: 'int32',
153+
UINT_32: 'uint32',
154+
INT_64: 'int64',
155+
UINT_64: 'uint64',
156+
FLOAT_32: 'float32',
157+
FLOAT_64: 'float64',
158+
});
159+
160+
134161
module.exports = {
135162
DynamicLibrary,
136163
dlopen,
@@ -158,7 +185,9 @@ module.exports = {
158185
setUint64,
159186
setFloat32,
160187
setFloat64,
188+
suffix,
161189
toString,
162190
toArrayBuffer,
163191
toBuffer,
192+
types,
164193
};

test/ffi/test-ffi-module.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ test('ffi exports expected API surface', () => {
105105
'setUint32',
106106
'setUint64',
107107
'setUint8',
108+
'suffix',
108109
'toArrayBuffer',
109110
'toBuffer',
110111
'toString',
112+
'types',
111113
];
112114

113115
assert.deepStrictEqual(Object.keys(ffi).sort(), expected);
@@ -140,4 +142,35 @@ test('ffi exports expected API surface', () => {
140142
assert.strictEqual(typeof ffi.toString, 'function');
141143
assert.strictEqual(typeof ffi.toBuffer, 'function');
142144
assert.strictEqual(typeof ffi.toArrayBuffer, 'function');
145+
assert.strictEqual(typeof ffi.types, 'object');
146+
});
147+
148+
test('ffi.types exports canonical type constants', () => {
149+
const ffi = require('node:ffi');
150+
const expected = {
151+
__proto__: null,
152+
VOID: 'void',
153+
POINTER: 'pointer',
154+
BUFFER: 'buffer',
155+
ARRAY_BUFFER: 'arraybuffer',
156+
FUNCTION: 'function',
157+
BOOL: 'bool',
158+
CHAR: 'char',
159+
STRING: 'string',
160+
FLOAT: 'float',
161+
DOUBLE: 'double',
162+
INT_8: 'int8',
163+
UINT_8: 'uint8',
164+
INT_16: 'int16',
165+
UINT_16: 'uint16',
166+
INT_32: 'int32',
167+
UINT_32: 'uint32',
168+
INT_64: 'int64',
169+
UINT_64: 'uint64',
170+
FLOAT_32: 'float32',
171+
FLOAT_64: 'float64',
172+
};
173+
174+
assert.deepStrictEqual(ffi.types, expected);
175+
assert.strictEqual(Object.isFrozen(ffi.types), true);
143176
});

tools/nix/non-v8-deps-mock.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ symlinkJoin (finalAttrs: {
5656
configureFlags = [
5757
"--without-lief"
5858
"--without-sqlite"
59+
"--without-ffi"
5960
"--without-ssl"
6061
]
6162
++ (lib.concatMap (sharedLibName: [

0 commit comments

Comments
 (0)