Skip to content

Commit ad63e9e

Browse files
authored
ffi: use type errors for invalid signatures
Signed-off-by: HoonDongKang <d159123@naver.com> PR-URL: #66222 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent e76358e commit ad63e9e

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎src/ffi/types.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Maybe<FunctionSignature> ParseFunctionSignature(Environment* env,
107107
}
108108

109109
if (!return_type_val->IsString()) {
110-
THROW_ERR_INVALID_ARG_VALUE(
110+
THROW_ERR_INVALID_ARG_TYPE(
111111
env, "Return value type of function %s must be a string", name);
112112
return {};
113113
}
@@ -132,7 +132,7 @@ Maybe<FunctionSignature> ParseFunctionSignature(Environment* env,
132132
}
133133

134134
if (!arguments_val->IsArray()) {
135-
THROW_ERR_INVALID_ARG_VALUE(
135+
THROW_ERR_INVALID_ARG_TYPE(
136136
env, "Arguments list of function %s must be an array", name);
137137
return {};
138138
}
@@ -148,7 +148,7 @@ Maybe<FunctionSignature> ParseFunctionSignature(Environment* env,
148148
}
149149

150150
if (!arg->IsString()) {
151-
THROW_ERR_INVALID_ARG_VALUE(
151+
THROW_ERR_INVALID_ARG_TYPE(
152152
env, "Argument %u of function %s must be a string", i, name);
153153
return {};
154154
}

‎test/ffi/test-ffi-dynamic-library.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,24 @@ test('dynamic library APIs validate failures and bad signatures', () => {
432432
lib.getFunction('add_i32', { return: 'i32', arguments: ['i32\0bad'] });
433433
}, /Argument 0 of function add_i32 must not contain null bytes/);
434434

435+
assert.throws(() => {
436+
lib.getFunction('add_i32', { return: 1, arguments: [] });
437+
}, {
438+
code: 'ERR_INVALID_ARG_TYPE',
439+
});
440+
441+
assert.throws(() => {
442+
lib.getFunction('add_i32', { return: 'i32', arguments: 'i32' });
443+
}, {
444+
code: 'ERR_INVALID_ARG_TYPE',
445+
});
446+
447+
assert.throws(() => {
448+
lib.getFunction('add_i32', { return: 'i32', arguments: [1] });
449+
}, {
450+
code: 'ERR_INVALID_ARG_TYPE',
451+
});
452+
435453
assert.throws(() => {
436454
lib.getFunctions('not an object');
437455
}, {

0 commit comments

Comments
 (0)