Skip to content

Commit c44f0f6

Browse files
committed
ffi: use type errors for invalid signatures
Signed-off-by: HoonDongKang <d159123@naver.com>
1 parent 3d85c94 commit c44f0f6

2 files changed

Lines changed: 24 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,27 @@ 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+
message: 'Return value type of function add_i32 must be a string',
440+
});
441+
442+
assert.throws(() => {
443+
lib.getFunction('add_i32', { return: 'i32', arguments: 'i32' });
444+
}, {
445+
code: 'ERR_INVALID_ARG_TYPE',
446+
message: 'Arguments list of function add_i32 must be an array',
447+
});
448+
449+
assert.throws(() => {
450+
lib.getFunction('add_i32', { return: 'i32', arguments: [1] });
451+
}, {
452+
code: 'ERR_INVALID_ARG_TYPE',
453+
message: 'Argument 0 of function add_i32 must be a string',
454+
});
455+
435456
assert.throws(() => {
436457
lib.getFunctions('not an object');
437458
}, {

0 commit comments

Comments
 (0)