类型名称


🌐 Type names

FFI 签名使用字符串类型名称。

🌐 FFI signatures use string type names.

支持的类型名称:

🌐 Supported type names:

  • void
  • char
  • int8
  • uint8
  • int16
  • uint16
  • int32
  • uint32
  • int64
  • uint64
  • float32
  • float64
  • pointer
  • string
  • buffer
  • arraybuffer
  • function
其他拼写
  • i8 给 int8
  • u8 和 bool 用于 uint8
  • i16 给 int16
  • u16 给 uint16
  • i32 给 int32
  • u32 给 uint32
  • i64 给 int64
  • u64 给 uint64
  • f32 和 float 用于 float32
  • f64 和 double 用于 float64
  • ptr 给 pointer
  • str 给 string

这些类型名称也作为常量暴露在 ffi.types 上:

🌐 These type names are also exposed as constants on ffi.types:

  • ffi.types.VOID = 'void'
  • ffi.types.POINTER = 'pointer'
  • 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'
  • ffi.types.UINT_16 = 'uint16'
  • ffi.types.INT_32 = 'int32'
  • ffi.types.UINT_32 = 'uint32'
  • ffi.types.INT_64 = 'int64'
  • ffi.types.UINT_64 = 'uint64'
  • ffi.types.FLOAT_32 = 'float32'
  • ffi.types.FLOAT_64 = 'float64'

类似指针的类型(pointer、string、buffer、arraybuffer 和 function)都会作为指针通过本地层传递。

🌐 Pointer-like types (pointer, string, buffer, arraybuffer, and function) are all passed through the native layer as pointers.

当将 Buffer、ArrayBuffer 或类型化数组的值作为类似指针的参数传递时,Node.js 会在本地调用期间借用它们底层内存的原始指针。调用者必须确保底层存储在整个调用期间保持有效和稳定。

🌐 When Buffer, ArrayBuffer, or typed array values are passed as pointer-like arguments, Node.js borrows a raw pointer to their backing memory for the duration of the native call. The caller must ensure that backing store remains valid and stable for the entire call.

在原生调用处于活动状态时调整大小、传输、分离或以其他方式使该后备存储无效是不受支持且危险的,包括通过可重入的 JavaScript(例如 FFI 回调)进行操作。这样做可能会导致进程崩溃、产生错误输出或损坏内存。

🌐 It is unsupported and dangerous to resize, transfer, detach, or otherwise invalidate that backing store while the native call is active, including through reentrant JavaScript such as FFI callbacks. Doing so may crash the process, produce incorrect output, or corrupt memory.

char 类型遵循平台的 C ABI。在普通 C char 为有符号的的平台上,它的行为像 int8;否则,它的行为像 uint8。

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

bool 类型被编组为 8 位无符号整数。传递诸如 0 和 1 的数值;不接受 JavaScript 的 true 和 false。

🌐 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.

在优化的快速 FFI 调用中,pointer 和 function 参数接受原始指针 bigint 值。对于类似指针的参数 null、undefined、字符串、Buffer、类型数组、DataView 和 ArrayBuffer 的值,会在调用优化后的本地封装器之前,在 JavaScript 端进行转换。

🌐 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.

当函数的参数或返回类型不适合特定平台的快速跳板时,优化的快速 FFI 调用会退回到另一个 调用路径。快速 FFI 调用最多支持 8 个参数,而且不同架构的寄存器和参数限制也不同:

🌐 Optimized Fast FFI calls fall back to another call path when a function's arguments or return type do not fit the platform-specific fast trampoline. Fast FFI calls support at most 8 total arguments, and the register and argument limits differ per architecture:

架构最大整数/指针参数最大浮点参数支持缓冲区形状参数支持缓冲区形状+浮点一起窄(8/16位)返回值
AArch647(如果有缓冲区形状参数则为6)8支持不支持支持
x86-64, Linux/macOS(SysV)6(如果有缓冲区形状参数则为4)8支持不支持支持
x86-64, Windows(Win64)3(总参数也限制为3)3不支持不适用支持
s390x44不支持不适用不支持
PPC64LE78不支持不适用不支持
LoongArch6478不支持不适用不支持
RISC-V(64位)78不支持不适用不支持

PPC64BE 没有快速调用 trampoline,总是使用通用调用路径。 “缓冲区形状的参数”是指作为指针类参数传递的 Buffer、类型化数组、DataView 或 ArrayBuffer 值。函数的参数或返回类型超过当前平台限制时,会使用其他的 调用路径 之一。

🌐 PPC64BE has no fast-call trampoline and always uses the generic call path. "Buffer-shaped args" means Buffer, typed array, DataView, or ArrayBuffer values passed as pointer-like arguments. Functions whose argument or return types exceed the limits for the current platform use one of the other call paths instead.