diff --git a/src/ffi/types.cc b/src/ffi/types.cc index 5a910e63a3e3..971ca8b6adbb 100644 --- a/src/ffi/types.cc +++ b/src/ffi/types.cc @@ -634,19 +634,22 @@ Maybe ToFFIArgument(Environment* env, *static_cast(ret) = static_cast(value); } else if (type == &ffi_type_sint32) { - if (!arg->IsInt32()) { + int64_t value; + if (!GetValidatedSignedInt(env, arg, INT32_MIN, INT32_MAX, "int32") + .To(&value)) { THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int32", index); return {}; } - *static_cast(ret) = arg->Int32Value(context).FromJust(); + *static_cast(ret) = static_cast(value); } else if (type == &ffi_type_uint32) { - if (!arg->IsUint32()) { + uint64_t value; + if (!GetValidatedUnsignedInt(env, arg, UINT32_MAX, "uint32").To(&value)) { THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint32", index); return {}; } - *static_cast(ret) = arg->Uint32Value(context).FromJust(); + *static_cast(ret) = static_cast(value); } else if (type == &ffi_type_sint64) { if (arg->IsBigInt()) { bool lossless; diff --git a/test/ffi/test-ffi-calls.js b/test/ffi/test-ffi-calls.js index 0dbddb244b77..338a728bd227 100644 --- a/test/ffi/test-ffi-calls.js +++ b/test/ffi/test-ffi-calls.js @@ -20,6 +20,8 @@ test('ffi calls support integer arithmetic and char semantics', () => { assert.strictEqual(symbols.add_u16(65_530, 10), 4); assert.strictEqual(symbols.add_i32(-10, 52), 42); assert.strictEqual(symbols.add_u32(0xFFFFFFFF, 1), 0); + assert.strictEqual(symbols.add_i32(-0, 1), 1); + assert.strictEqual(symbols.add_u32(-0, 1), 1); assert.strictEqual(symbols.add_i64(20n, 22n), 42n); assert.strictEqual(symbols.add_u64(20n, 22n), 42n); assert.strictEqual(symbols.add_i64(20, 22), symbols.add_i64(20n, 22n));