Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All @@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All @@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand Down Expand Up @@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand Down Expand Up @@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All @@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All @@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand Down Expand Up @@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand Down Expand Up @@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand Down Expand Up @@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand Down Expand Up @@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All @@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand Down Expand Up @@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All @@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer` and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading