From ad63e9ef29d3f70081dda871da46f9fa187d38fd Mon Sep 17 00:00:00 2001 From: Donghoon Kang Date: Sat, 26 Sep 2026 03:58:21 +0900 Subject: [PATCH] ffi: use type errors for invalid signatures Signed-off-by: HoonDongKang PR-URL: https://github.com/nodejs/node/pull/66222 Reviewed-By: Daeyeon Jeong Reviewed-By: Matteo Collina --- src/ffi/types.cc | 6 +++--- test/ffi/test-ffi-dynamic-library.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ffi/types.cc b/src/ffi/types.cc index 83be0f48ecc..bf3e7e675ed 100644 --- a/src/ffi/types.cc +++ b/src/ffi/types.cc @@ -107,7 +107,7 @@ Maybe ParseFunctionSignature(Environment* env, } if (!return_type_val->IsString()) { - THROW_ERR_INVALID_ARG_VALUE( + THROW_ERR_INVALID_ARG_TYPE( env, "Return value type of function %s must be a string", name); return {}; } @@ -132,7 +132,7 @@ Maybe ParseFunctionSignature(Environment* env, } if (!arguments_val->IsArray()) { - THROW_ERR_INVALID_ARG_VALUE( + THROW_ERR_INVALID_ARG_TYPE( env, "Arguments list of function %s must be an array", name); return {}; } @@ -148,7 +148,7 @@ Maybe ParseFunctionSignature(Environment* env, } if (!arg->IsString()) { - THROW_ERR_INVALID_ARG_VALUE( + THROW_ERR_INVALID_ARG_TYPE( env, "Argument %u of function %s must be a string", i, name); return {}; } diff --git a/test/ffi/test-ffi-dynamic-library.js b/test/ffi/test-ffi-dynamic-library.js index c7741a9eed5..3240f194972 100644 --- a/test/ffi/test-ffi-dynamic-library.js +++ b/test/ffi/test-ffi-dynamic-library.js @@ -432,6 +432,24 @@ test('dynamic library APIs validate failures and bad signatures', () => { lib.getFunction('add_i32', { return: 'i32', arguments: ['i32\0bad'] }); }, /Argument 0 of function add_i32 must not contain null bytes/); + assert.throws(() => { + lib.getFunction('add_i32', { return: 1, arguments: [] }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }); + + assert.throws(() => { + lib.getFunction('add_i32', { return: 'i32', arguments: 'i32' }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }); + + assert.throws(() => { + lib.getFunction('add_i32', { return: 'i32', arguments: [1] }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }); + assert.throws(() => { lib.getFunctions('not an object'); }, {