FFI
源代码: lib/ffi.js
node:ffi 模块提供了一个实验性的外部函数接口,用于从 JavaScript 加载动态库并调用本地符号。
🌐 The node:ffi module provides an experimental foreign function interface for
loading dynamic libraries and calling native symbols from JavaScript.
此 API 不安全。传递无效指针、使用错误的符号签名或在内存被释放后访问内存可能会导致进程崩溃或内存损坏。
🌐 This API is unsafe. Passing invalid pointers, using an incorrect symbol signature, or accessing memory after it has been freed can crash the process or corrupt memory.
要访问它:
🌐 To access it:
import ffi from 'node:ffi';const ffi = require('node:ffi');该模块仅在具有 FFI 支持的构建中以 node: 方案可用,并受 --experimental-ffi 标志控制。
🌐 This module is only available under the node: scheme in builds with FFI
support and is gated by the --experimental-ffi flag.
在 libffi 提供兼容静态后端的平台上,通过打包的 libffi 可以构建支持 node:ffi 的 Node.js,或者使用 --shared-ffi 配置标志通过共享的 libffi 实现。
非官方的 GN 构建不支持 node:ffi。
🌐 Building Node.js with node:ffi support is available via the bundled libffi on
platforms where libffi provides a compatible static backend, or via a
shared libffi using the --shared-ffi configure flag.
The unofficial GN build does not support node:ffi.
打包的 libffi 不支持以下目标:
🌐 The following targets are not supported by bundled libffi:
s390x.mips、mipsel和mips64el在除 FreeBSD、Linux 和 OpenBSD 之外的目标上。ppc64在 Android、CloudABI、iOS、OpenHarmony、OS/400、Solaris 和 Windows 上。
使用 权限模型 时,除非提供 --allow-ffi 标志,否则 FFI API 会受到限制。
🌐 When using the Permission Model, FFI APIs are
restricted unless the --allow-ffi flag is provided.