Skip to content

Commit 2f5d7e7

Browse files
authored
module: exclude node:ffi from builtinModules when not enabled
`Module.builtinModules` is supposed to only list modules that are accessible to user code. `node:ffi` requires `--experimental-ffi` to be required, so filter it out when the flag is not set, mirroring the existing handling for `--experimental-quic`. Refs: #63137 (comment) Signed-off-by: Jordan Harband <ljharb@gmail.com> PR-URL: #63158 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent dc99d18 commit 2f5d7e7

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ function initializeCJS() {
490490
if (!getOptionValue('--experimental-quic')) {
491491
modules = modules.filter((i) => i !== 'node:quic');
492492
}
493+
if (!getOptionValue('--experimental-ffi')) {
494+
modules = modules.filter((i) => i !== 'node:ffi');
495+
}
493496
Module.builtinModules = ObjectFreeze(modules);
494497

495498
initializeCjsConditions();

test/ffi/test-ffi-module.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Flags: --experimental-ffi
22
'use strict';
33
const common = require('../common');
4+
const { spawnSyncAndAssert } = require('../common/child_process');
45
const assert = require('node:assert');
56
const { spawnSync } = require('node:child_process');
67
const { test } = require('node:test');
@@ -32,17 +33,16 @@ test('ffi builtin is unavailable when disabled', () => {
3233
});
3334

3435
test('ffi builtin is listed', () => {
35-
const { stdout, stderr, status, signal } = spawnSync(process.execPath, [
36-
'-p',
37-
'require("node:module").builtinModules.includes("node:ffi")',
38-
], {
39-
encoding: 'utf8',
40-
});
41-
42-
assert.strictEqual(stdout.trim(), 'true');
43-
assert.strictEqual(stderr, '');
44-
assert.strictEqual(status, 0);
45-
assert.strictEqual(signal, null);
36+
for (const [flag, stdout] of Object.entries({
37+
'--experimental-ffi': 'true\n',
38+
'--no-experimental-ffi': 'false\n',
39+
})) {
40+
spawnSyncAndAssert(process.execPath, [
41+
flag,
42+
'-p',
43+
'require("node:module").builtinModules.includes("node:ffi")',
44+
], { stdout });
45+
}
4646
});
4747

4848
test('ffi can be imported from ESM', () => {

0 commit comments

Comments
 (0)