Skip to content

Commit 29fb725

Browse files
committed
module: exclude node:ffi from builtinModules when flag is disabled
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>
1 parent 1b04f16 commit 29fb725

2 files changed

Lines changed: 20 additions & 1 deletion

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ test('ffi builtin is unavailable when disabled', () => {
3131
assert.strictEqual(signal, null);
3232
});
3333

34-
test('ffi builtin is listed', () => {
34+
test('ffi builtin is listed when enabled', () => {
3535
const { stdout, stderr, status, signal } = spawnSync(process.execPath, [
36+
'--experimental-ffi',
3637
'-p',
3738
'require("node:module").builtinModules.includes("node:ffi")',
3839
], {
@@ -45,6 +46,21 @@ test('ffi builtin is listed', () => {
4546
assert.strictEqual(signal, null);
4647
});
4748

49+
test('ffi builtin is not listed when disabled', () => {
50+
const { stdout, stderr, status, signal } = spawnSync(process.execPath, [
51+
'--no-experimental-ffi',
52+
'-p',
53+
'require("node:module").builtinModules.includes("node:ffi")',
54+
], {
55+
encoding: 'utf8',
56+
});
57+
58+
assert.strictEqual(stdout.trim(), 'false');
59+
assert.strictEqual(stderr, '');
60+
assert.strictEqual(status, 0);
61+
assert.strictEqual(signal, null);
62+
});
63+
4864
test('ffi can be imported from ESM', () => {
4965
const { stdout, stderr, status, signal } = spawnSync(process.execPath, [
5066
'--experimental-ffi',

0 commit comments

Comments
 (0)