Skip to content

Commit 4d36946

Browse files
committed
src: export FFI types as constants
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
1 parent bb30b89 commit 4d36946

5 files changed

Lines changed: 89 additions & 2 deletions

File tree

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',

deps/libffi/preprocess_asm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ def preprocess(args):
7272
sys.stderr.write(result.stderr)
7373
raise RuntimeError(f'Preprocessing failed: {" ".join(command)}')
7474

75-
output.write_text(result.stdout, encoding='utf-8')
75+
processed_lines = []
76+
for line in result.stdout.splitlines(keepends=True):
77+
if line.lstrip().startswith('#'):
78+
continue
79+
processed_lines.append(line)
80+
81+
output.write_text(''.join(processed_lines), encoding='utf-8')
7682

7783

7884
def main(argv=None):

doc/api/ffi.md

Lines changed: 23 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

lib/ffi.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
validateInteger,
1818
validateString,
1919
} = require('internal/validators');
20+
const { __proto__ } = require('console');
2021

2122
emitExperimentalWarning('FFI');
2223

@@ -131,6 +132,31 @@ function exportBuffer(buffer, data, len) {
131132
buffer.copy(targetBuffer, 0, 0, buffer.length);
132133
}
133134

135+
const types = ObjectFreeze({
136+
__proto__: null,
137+
VOID: 'void',
138+
POINTER: 'pointer',
139+
BUFFER: 'buffer',
140+
ARRAY_BUFFER: 'arraybuffer',
141+
FUNCTION: 'function',
142+
BOOL: 'bool',
143+
CHAR: 'char',
144+
STRING: 'string',
145+
FLOAT: 'float',
146+
DOUBLE: 'double',
147+
INT_8: 'int8',
148+
UINT_8: 'uint8',
149+
INT_16: 'int16',
150+
UINT_16: 'uint16',
151+
INT_32: 'int32',
152+
UINT_32: 'uint32',
153+
INT_64: 'int64',
154+
UINT_64: 'uint64',
155+
FLOAT_32: 'float32',
156+
FLOAT_64: 'float64',
157+
});
158+
159+
134160
module.exports = {
135161
DynamicLibrary,
136162
dlopen,
@@ -161,4 +187,5 @@ module.exports = {
161187
toString,
162188
toArrayBuffer,
163189
toBuffer,
190+
types,
164191
};

test/ffi/test-ffi-module.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ test('ffi exports expected API surface', () => {
108108
'toArrayBuffer',
109109
'toBuffer',
110110
'toString',
111+
'types',
111112
];
112113

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

0 commit comments

Comments
 (0)