Skip to content

Commit 1f63811

Browse files
trivikraduh95
authored andcommitted
ffi: accept -0 for int32 and uint32 arguments
ToFFIArgument validated int32/uint32 with IsInt32()/IsUint32(), which reject -0, while every other integer width and the Fast API and SharedBuffer paths accept it. Use the same validators as the 8- and 16-bit branches so behavior no longer depends on the JIT tier. Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Assisted-by: claude:opus-5.5 PR-URL: #67035 Fixes: #67033 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 555bcfb commit 1f63811

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎src/ffi/types.cc‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,19 +634,22 @@ Maybe<FFIArgumentCategory> ToFFIArgument(Environment* env,
634634

635635
*static_cast<uint16_t*>(ret) = static_cast<uint16_t>(value);
636636
} else if (type == &ffi_type_sint32) {
637-
if (!arg->IsInt32()) {
637+
int64_t value;
638+
if (!GetValidatedSignedInt(env, arg, INT32_MIN, INT32_MAX, "int32")
639+
.To(&value)) {
638640
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be an int32", index);
639641
return {};
640642
}
641643

642-
*static_cast<int32_t*>(ret) = arg->Int32Value(context).FromJust();
644+
*static_cast<int32_t*>(ret) = static_cast<int32_t>(value);
643645
} else if (type == &ffi_type_uint32) {
644-
if (!arg->IsUint32()) {
646+
uint64_t value;
647+
if (!GetValidatedUnsignedInt(env, arg, UINT32_MAX, "uint32").To(&value)) {
645648
THROW_ERR_INVALID_ARG_VALUE(env, "Argument %u must be a uint32", index);
646649
return {};
647650
}
648651

649-
*static_cast<uint32_t*>(ret) = arg->Uint32Value(context).FromJust();
652+
*static_cast<uint32_t*>(ret) = static_cast<uint32_t>(value);
650653
} else if (type == &ffi_type_sint64) {
651654
if (arg->IsBigInt()) {
652655
bool lossless;

‎test/ffi/test-ffi-calls.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ test('ffi calls support integer arithmetic and char semantics', () => {
2020
assert.strictEqual(symbols.add_u16(65_530, 10), 4);
2121
assert.strictEqual(symbols.add_i32(-10, 52), 42);
2222
assert.strictEqual(symbols.add_u32(0xFFFFFFFF, 1), 0);
23+
assert.strictEqual(symbols.add_i32(-0, 1), 1);
24+
assert.strictEqual(symbols.add_u32(-0, 1), 1);
2325
assert.strictEqual(symbols.add_i64(20n, 22n), 42n);
2426
assert.strictEqual(symbols.add_u64(20n, 22n), 42n);
2527
assert.strictEqual(symbols.add_i64(20, 22), symbols.add_i64(20n, 22n));

0 commit comments

Comments
 (0)