Skip to content

Commit 1eb617d

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

4 files changed

Lines changed: 57 additions & 24 deletions

File tree

lib/ffi.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ const typesToFfiTypes = {
4949
'signed int': 'int',
5050
'unsigned': 'uint',
5151
'unsigned int': 'uint',
52-
// 'long': '', // TODO verify this next bunch before supporting it
53-
// 'long int': ,
54-
// 'signed long': ,
55-
// 'signed long int': ,
56-
// 'unsigned long': ,
57-
// 'unsigned long int': ,
52+
'long': 'long',
53+
'long int': 'long',
54+
'signed long': 'long',
55+
'signed long int': 'long',
56+
'unsigned long': 'ulong',
57+
'unsigned long int': 'ulong',
5858
// 'long long': ,
5959
// 'long long int': ,
6060
// 'signed long long': ,
@@ -70,6 +70,11 @@ const typesToFfiTypes = {
7070
function normalizeTypeToFfiTypes(type) {
7171
if (type.includes('*')) return typesToFfiTypes.pointer;
7272
if (/^u?int\d\d?_t/.test(type)) return type.replace(/_t$/, '');
73+
if (type.includes('long long')) {
74+
const size = sizes[type];
75+
const signed = type.includes('unsigned');
76+
return `${signed ? 'u' : ''}int${size * 8}`;
77+
}
7378
if (typesToFfiTypes[type]) return typesToFfiTypes[type];
7479
throw new TypeError(`Type "${type}" unsupported`);
7580
}

src/node_ffi.cc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ void Initialize(
177177
NODE_FFI_SET_TYPE_CONSTANT(short, ffi_type_sshort); // NOLINT(runtime/int)
178178
NODE_FFI_SET_TYPE_CONSTANT(uint, ffi_type_uint);
179179
NODE_FFI_SET_TYPE_CONSTANT(int, ffi_type_sint);
180-
// NODE_FFI_SET_TYPE_CONSTANT(ulong, ffi_type_ulong);
181-
// NODE_FFI_SET_TYPE_CONSTANT(longlong, ffi_type_slong);
180+
NODE_FFI_SET_TYPE_CONSTANT(ulong, ffi_type_ulong);
181+
NODE_FFI_SET_TYPE_CONSTANT(long, ffi_type_slong);
182182
// NODE_FFI_SET_TYPE_CONSTANT(longdouble, ffi_type_longdouble);
183183
NODE_FFI_SET_TYPE_CONSTANT(pointer, ffi_type_pointer);
184184
target->Set(context, OneByteString(isolate, "types"), types).Check();
@@ -201,21 +201,21 @@ void Initialize(
201201
NODE_FFI_SET_SIZE_CONSTANT(signed int);
202202
NODE_FFI_SET_SIZE_CONSTANT(unsigned);
203203
NODE_FFI_SET_SIZE_CONSTANT(unsigned int);
204-
// NODE_FFI_SET_SIZE_CONSTANT(long);
205-
// NODE_FFI_SET_SIZE_CONSTANT(long int);
206-
// NODE_FFI_SET_SIZE_CONSTANT(signed long);
207-
// NODE_FFI_SET_SIZE_CONSTANT(signed long int);
208-
// NODE_FFI_SET_SIZE_CONSTANT(unsigned long);
209-
// NODE_FFI_SET_SIZE_CONSTANT(unsigned long int);
210-
// NODE_FFI_SET_SIZE_CONSTANT(long long);
211-
// NODE_FFI_SET_SIZE_CONSTANT(long long int);
212-
// NODE_FFI_SET_SIZE_CONSTANT(signed long long);
213-
// NODE_FFI_SET_SIZE_CONSTANT(signed long long int);
214-
// NODE_FFI_SET_SIZE_CONSTANT(unsigned long long);
215-
// NODE_FFI_SET_SIZE_CONSTANT(unsigned long long int);
204+
NODE_FFI_SET_SIZE_CONSTANT(long);
205+
NODE_FFI_SET_SIZE_CONSTANT(long int);
206+
NODE_FFI_SET_SIZE_CONSTANT(signed long);
207+
NODE_FFI_SET_SIZE_CONSTANT(signed long int);
208+
NODE_FFI_SET_SIZE_CONSTANT(unsigned long);
209+
NODE_FFI_SET_SIZE_CONSTANT(unsigned long int);
210+
NODE_FFI_SET_SIZE_CONSTANT(long long);
211+
NODE_FFI_SET_SIZE_CONSTANT(long long int);
212+
NODE_FFI_SET_SIZE_CONSTANT(signed long long);
213+
NODE_FFI_SET_SIZE_CONSTANT(signed long long int);
214+
NODE_FFI_SET_SIZE_CONSTANT(unsigned long long);
215+
NODE_FFI_SET_SIZE_CONSTANT(unsigned long long int);
216216
NODE_FFI_SET_SIZE_CONSTANT(float);
217217
NODE_FFI_SET_SIZE_CONSTANT(double);
218-
NODE_FFI_SET_SIZE_CONSTANT(long double);
218+
// NODE_FFI_SET_SIZE_CONSTANT(long double);
219219
NODE_FFI_SET_SIZE_CONSTANT(char *);
220220
NODE_FFI_SET_SIZE_CONSTANT(uint8_t);
221221
NODE_FFI_SET_SIZE_CONSTANT(int8_t);

test/ffi/types/test.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ const basicTypes = [
2424
'signed int',
2525
'unsigned',
2626
'unsigned int',
27+
'long',
28+
'long int',
29+
'signed long',
30+
'signed long int',
31+
'unsigned long',
32+
'unsigned long int',
33+
'long long',
34+
'long long int',
35+
'signed long long',
36+
'signed long long int',
37+
'unsigned long long',
38+
'unsigned long long int',
2739
'float',
2840
'double',
2941
'uint8_t',
@@ -45,12 +57,16 @@ function getRWName(type, size) {
4557
return `${u}${big}Int${size * 8}${end}`;
4658
}
4759

60+
function isBigInt(type) {
61+
return type !== 'double' && ffi.sizeof(type) === 8;
62+
}
63+
4864
for (const type of basicTypes) {
4965
const fnName = `test_add_${type.replace(/ /g, '_')}`;
5066
const fn = ffi.getNativeFunction(shared, fnName, type, [type, type]);
51-
const three = type.includes('64') ? 3n : 3;
52-
const four = type.includes('64') ? 4n : 4;
53-
const seven = type.includes('64') ? 7n : 7;
67+
const three = isBigInt(type) ? 3n : 3;
68+
const four = isBigInt(type) ? 4n : 4;
69+
const seven = isBigInt(type) ? 7n : 7;
5470
assert.strictEqual(fn(three, four), seven);
5571

5672
const ptrFnName = `test_add_ptr_${type.replace(/ /g, '_')}`;

test/ffi/types/types.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ test(signed, signed);
1515
test(signed int, signed_int);
1616
test(unsigned, unsigned);
1717
test(unsigned int, unsigned_int);
18+
test(long, long);
19+
test(long int, long_int);
20+
test(signed long, signed_long);
21+
test(signed long int, signed_long_int);
22+
test(unsigned long, unsigned_long);
23+
test(unsigned long int, unsigned_long_int);
24+
test(long long, long_long);
25+
test(long long int, long_long_int);
26+
test(signed long long, signed_long_long);
27+
test(signed long long int, signed_long_long_int);
28+
test(unsigned long long, unsigned_long_long);
29+
test(unsigned long long int, unsigned_long_long_int);
1830
test(float, float);
1931
test(double, double);
2032
test(uint8_t, uint8_t);

0 commit comments

Comments
 (0)