Skip to content

Commit ae0ca8d

Browse files
committed
ffi: preserve uint8 semantics for bool fast calls
Normalize bool to kUint8 when creating Fast API metadata. This keeps optimized calls consistent with generic FFI behavior, including numeric return values and rejection of JavaScript Boolean values. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.6-sol
1 parent f2ba101 commit ae0ca8d

10 files changed

Lines changed: 14 additions & 18 deletions

File tree

doc/contributing/ffi-fast-api-internals.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ surface. It models the ABI categories that the generated trampoline knows how to
200200
marshal directly:
201201

202202
* `kVoid`
203-
* `kBool`
204203
* signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers
205204
* `kFloat32`
206205
* `kFloat64`
207206
* `kPointer`
208207
* `kBuffer`
209208

210209
Public aliases are normalized in `FastScalarTypeFromName()` and
211-
`FastArgTypeFromName()`.
210+
`FastArgTypeFromName()`. In particular, `bool` is normalized to `kUint8` to
211+
match its documented 8-bit unsigned integer semantics.
212212

213213
`pointer`, `ptr`, `string`, `str`, `buffer`, and `arraybuffer` all represent
214214
pointer-sized native values at the target ABI boundary. They differ in how

src/ffi/fast.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
3737
if (type == "void") {
3838
*out = FastFFIType::kVoid;
3939
} else if (type == "bool") {
40-
*out = FastFFIType::kBool;
40+
*out = FastFFIType::kUint8;
4141
} else if (IsTypeName(type, {"i8", "int8"})) {
4242
*out = FastFFIType::kInt8;
4343
} else if (IsTypeName(type, {"u8", "uint8"})) {
@@ -96,8 +96,6 @@ CTypeInfo::Type ToV8Type(FastFFIType type, bool is_return) {
9696
switch (type) {
9797
case FastFFIType::kVoid:
9898
return CTypeInfo::Type::kVoid;
99-
case FastFFIType::kBool:
100-
return CTypeInfo::Type::kBool;
10199
case FastFFIType::kUint8:
102100
return CTypeInfo::Type::kUint32;
103101
case FastFFIType::kInt8:

src/ffi/fast.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct FFIFunction;
1717

1818
enum class FastFFIType : uint8_t {
1919
kVoid,
20-
kBool,
2120
kInt8,
2221
kUint8,
2322
kInt16,

src/ffi/platforms/arm64.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ uint32_t UxthW(unsigned reg) {
136136
// to the ABI width expected by the native target before the final call.
137137
bool EmitNarrow(uint32_t** cursor, FastFFIType type, unsigned reg) {
138138
switch (type) {
139-
case FastFFIType::kBool:
140139
case FastFFIType::kUint8:
141140
*(*cursor)++ = UxtbW(reg);
142141
return true;

src/ffi/platforms/loong64.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ bool IsFloatType(FastFFIType type) {
1919

2020
bool IsNarrowType(FastFFIType type) {
2121
switch (type) {
22-
case FastFFIType::kBool:
2322
case FastFFIType::kInt8:
2423
case FastFFIType::kUint8:
2524
case FastFFIType::kInt16:

src/ffi/platforms/ppc64.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ bool IsFloatType(FastFFIType type) {
2323

2424
bool IsNarrowType(FastFFIType type) {
2525
switch (type) {
26-
case FastFFIType::kBool:
2726
case FastFFIType::kInt8:
2827
case FastFFIType::kUint8:
2928
case FastFFIType::kInt16:

src/ffi/platforms/riscv64.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ bool IsFloatType(FastFFIType type) {
1919

2020
bool IsNarrowType(FastFFIType type) {
2121
switch (type) {
22-
case FastFFIType::kBool:
2322
case FastFFIType::kInt8:
2423
case FastFFIType::kUint8:
2524
case FastFFIType::kInt16:

src/ffi/platforms/s390x.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ bool IsFloatType(FastFFIType type) {
1919

2020
bool IsNarrowType(FastFFIType type) {
2121
switch (type) {
22-
case FastFFIType::kBool:
2322
case FastFFIType::kInt8:
2423
case FastFFIType::kUint8:
2524
case FastFFIType::kInt16:

src/ffi/platforms/x64.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ void EmitNarrowInstruction(uint8_t** cursor, uint8_t opcode, unsigned reg) {
232232
// consumes the declared low 8/16 bits.
233233
bool EmitNarrowReturn(uint8_t** cursor, FastFFIType type, unsigned reg) {
234234
switch (type) {
235-
case FastFFIType::kBool:
236235
case FastFFIType::kUint8:
237236
EmitNarrowInstruction(cursor, 0xb6, reg);
238237
return true;
@@ -252,7 +251,6 @@ bool EmitNarrowReturn(uint8_t** cursor, FastFFIType type, unsigned reg) {
252251

253252
bool NeedsNarrow(FastFFIType type) {
254253
switch (type) {
255-
case FastFFIType::kBool:
256254
case FastFFIType::kUint8:
257255
case FastFFIType::kInt8:
258256
case FastFFIType::kUint16:
@@ -655,7 +653,6 @@ void EmitNarrowInstruction(uint8_t** cursor, uint8_t opcode, unsigned reg) {
655653

656654
bool EmitNarrowReturn(uint8_t** cursor, FastFFIType type, unsigned reg) {
657655
switch (type) {
658-
case FastFFIType::kBool:
659656
case FastFFIType::kUint8:
660657
EmitNarrowInstruction(cursor, 0xb6, reg);
661658
return true;
@@ -675,7 +672,6 @@ bool EmitNarrowReturn(uint8_t** cursor, FastFFIType type, unsigned reg) {
675672

676673
bool NeedsNarrow(FastFFIType type) {
677674
switch (type) {
678-
case FastFFIType::kBool:
679675
case FastFFIType::kUint8:
680676
case FastFFIType::kInt8:
681677
case FastFFIType::kUint16:

test/ffi/test-ffi-calls.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-ffi --expose-gc
1+
// Flags: --experimental-ffi --expose-gc --allow-natives-syntax
22
'use strict';
33
const common = require('../common');
44
common.skipIfFFIMissing();
@@ -62,8 +62,16 @@ test('ffi bool signatures use uint8 values', () => {
6262
arguments: ['bool', 'bool'],
6363
return: 'bool',
6464
});
65-
assert.strictEqual(boolAdder(1, 0), 1);
66-
assert.throws(() => boolAdder(true, false), /Argument 0 must be a uint8/);
65+
function callBoolAdder(a, b) {
66+
return boolAdder(a, b);
67+
}
68+
69+
eval('%PrepareFunctionForOptimization(callBoolAdder)');
70+
assert.strictEqual(callBoolAdder(1, 0), 1);
71+
eval('%OptimizeFunctionOnNextCall(callBoolAdder)');
72+
assert.strictEqual(callBoolAdder(1, 0), 1);
73+
assert.throws(
74+
() => callBoolAdder(true, false), /Argument 0 must be a uint8/);
6775
} finally {
6876
lib.close();
6977
}

0 commit comments

Comments
 (0)