Skip to content

lib,src,test,doc: add node:ffi module#62072

Open
cjihrig wants to merge 12 commits intonodejs:mainfrom
cjihrig:ffi
Open

lib,src,test,doc: add node:ffi module#62072
cjihrig wants to merge 12 commits intonodejs:mainfrom
cjihrig:ffi

Conversation

@cjihrig
Copy link
Copy Markdown
Contributor

@cjihrig cjihrig commented Mar 2, 2026

This is not ready for review yet. Just opening to see if this is something the project is still interested in.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added build Issues and PRs related to build files or the CI. dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run. labels Mar 2, 2026
@anonrig
Copy link
Copy Markdown
Member

anonrig commented Mar 2, 2026

Yes

Copy link
Copy Markdown
Member

@Qard Qard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very cool! I'm glad to see someone has picked this back up. 🙂

I did a quick review and it all looks good. My only comment is there seems to be a bunch of static strings and a private symbol which we might want to define in env_properties.h.

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@cjihrig cjihrig added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 18, 2026
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Mar 18, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Failed to start CI
   ⚠  No approving reviews found
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/23272564448

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

anonrig
anonrig previously approved these changes Mar 18, 2026
@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Mar 18, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 18, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@cjihrig
Copy link
Copy Markdown
Contributor Author

cjihrig commented Mar 18, 2026

@anonrig I cancelled that CI run to save resources. I started https://ci.nodejs.org/job/node-test-pull-request/71877/ manually, and it's currently running.

@jasnell jasnell dismissed anonrig’s stale review March 19, 2026 00:26

given that this is still draft and @cjihrig indicated it's not ready for review, a sign off is not yet appropriate.

mcollina
mcollina previously approved these changes Mar 19, 2026
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Mar 19, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@jasnell jasnell dismissed mcollina’s stale review March 19, 2026 03:40

given that this is still draft and @cjihrig indicated it's not ready for review, a sign off is not yet appropriate.

@jasnell
Copy link
Copy Markdown
Member

jasnell commented Mar 19, 2026

@cjihrig ... definitely interested in seeing ffi land. I was hoping @bengl's prior efforts would make progress. Once you feel this is ready to go, I'll be happy to review.

* src,deps,test: various improvements

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* deps,test: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* deps,test: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* deps,test: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* deps,test: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* doc,lib: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* deps: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed failures

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

---------

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: removed useless file

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

---------

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@cjihrig cjihrig marked this pull request as ready for review April 1, 2026 14:41
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Copy Markdown
Member

mcollina commented Apr 1, 2026

@cjihrig @ShogunPanda this now conflicts, can you rebase?

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.18%. Comparing base (a9ac9b1) to head (281152e).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62072      +/-   ##
==========================================
- Coverage   89.70%   89.18%   -0.53%     
==========================================
  Files         695      702       +7     
  Lines      214213   216018    +1805     
  Branches    41021    41395     +374     
==========================================
+ Hits       192167   192657     +490     
- Misses      14113    15415    +1302     
- Partials     7933     7946      +13     
Files with missing lines Coverage Δ
lib/ffi.js 95.73% <ø> (ø)
lib/internal/bootstrap/realm.js 96.21% <ø> (+<0.01%) ⬆️
lib/internal/process/permission.js 100.00% <ø> (ø)
lib/internal/process/pre_execution.js 98.63% <ø> (+1.40%) ⬆️
src/env.cc 85.53% <ø> (+0.34%) ⬆️
src/ffi/data.cc 5.40% <ø> (ø)
src/ffi/types.cc 0.00% <ø> (ø)
src/node_binding.cc 82.74% <ø> (ø)
src/node_builtins.cc 76.09% <ø> (+0.04%) ⬆️
src/node_ffi.cc 12.43% <ø> (ø)
... and 8 more

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


bool ValidateBufferLength(Environment* env, size_t len) {
if (len > Buffer::kMaxLength) {
env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does THROW_ERR_BUFFER_TOO_LARGE(env) not work here?

} else if (value->IsNumber()) {
uint64_t validated;
if (!GetValidatedUnsignedInt(
env, value, 9007199254740991ULL, "uint64", &validated)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a constant to avoid the inline magic number?

return;
}

converted = static_cast<T>(number.ToLocalChecked()->Value());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid using ToLocalChecked

THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kFFI, "");

if (args.Length() < 1 || !args[0]->IsBigInt()) {
env->ThrowTypeError("The first argument must be a bigint");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be proper ERR_* errors.

Local<External> data = External::New(isolate, info);
Local<Function> ret =
Function::New(env->context(), DynamicLibrary::InvokeFunction, data)
.ToLocalChecked();
Copy link
Copy Markdown
Member

@jasnell jasnell Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few ToLocalChecked and Check uses that should be cleaned up.

Local<Object> object,
Local<String> key,
bool* out) {
v8::Maybe<bool> has = object->Has(context, key);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v8::Maybe in the using list above

Local<String> parameters_key =
String::NewFromUtf8Literal(env->isolate(), "parameters");
Local<String> arguments_key =
String::NewFromUtf8Literal(env->isolate(), "arguments");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be added to env-properties.h


*static_cast<double*>(ret) = arg->NumberValue(context).FromJust();
} else if (type == &ffi_type_pointer) {
if (arg->IsNull() || arg->IsUndefined()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsNullOrUndefined?

String::NewFromUtf8(isolate, name.c_str(), NewStringType::kNormal)
.ToLocalChecked());
ret->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "pointer"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env-properties?

info, DynamicLibrary::CleanupFunctionInfo, WeakCallbackType::kParameter);
ret->SetName(
String::NewFromUtf8(isolate, name.c_str(), NewStringType::kNormal)
.ToLocalChecked());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToV8Value I think?

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@ShogunPanda
Copy link
Copy Markdown
Contributor

@jasnell Thanks for the review, tomorrow (CEST) I'll address all of them.
Does the architecture and API look fine to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Issues and PRs related to build files or the CI. dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants