类型名称


🌐 Type names

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

🌐 FFI signatures use string type names.

支持的类型名称:

🌐 Supported type names:

  • void
  • i8, int8
  • u8uint8boolchar
  • i16, int16
  • u16, uint16
  • i32int32
  • u32, uint32
  • i64, int64
  • u64uint64
  • f32float
  • f64, double
  • pointerptr
  • string, str
  • buffer
  • arraybuffer
  • function

这些类型名称也作为常量暴露在 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'

类似指针的类型(pointerstringbufferarraybufferfunction)都会作为指针通过本地层传递。

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

当将 BufferArrayBuffer 或类型化数组的值作为类似指针的参数传递时,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 为有符号的平 台上,它的行为类似于 i8;否则,它的行为类似于 u8

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

bool 类型被编组为 8 位无符号整数。传递诸如 01 的数值;不接受 JavaScript 的 truefalse

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