Skip to content

Commit 0de89fa

Browse files
committed
use struct
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 0217108 commit 0de89fa

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

src/node_buffer.cc

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,16 @@ void FastSwap64(Local<Value> receiver,
13601360

13611361
static CFunction fast_swap64(CFunction::Make(FastSwap64));
13621362

1363-
static bool ValidateUtf8(Local<Value> value, bool* was_detached) {
1363+
struct ValidationResult {
1364+
bool is_valid;
1365+
bool was_detached;
1366+
};
1367+
1368+
static ValidationResult ValidateUtf8(Local<Value> value) {
13641369
ArrayBufferViewContents<char> abv(value);
1365-
*was_detached = abv.WasDetached();
1366-
return !*was_detached && simdutf::validate_utf8(abv.data(), abv.length());
1370+
bool was_detached = abv.WasDetached();
1371+
return { !was_detached && simdutf::validate_utf8(abv.data(), abv.length()),
1372+
was_detached };
13671373
}
13681374

13691375
static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
@@ -1372,14 +1378,13 @@ static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
13721378
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
13731379
args[0]->IsSharedArrayBuffer());
13741380

1375-
bool was_detached;
1376-
const bool result = ValidateUtf8(args[0], &was_detached);
1377-
if (was_detached) {
1381+
const ValidationResult result = ValidateUtf8(args[0]);
1382+
if (result.was_detached) {
13781383
return node::THROW_ERR_INVALID_STATE(
13791384
env, "Cannot validate on a detached buffer");
13801385
}
13811386

1382-
args.GetReturnValue().Set(result);
1387+
args.GetReturnValue().Set(result.is_valid);
13831388
}
13841389

13851390
static bool FastIsUtf8(Local<Value> receiver,
@@ -1389,23 +1394,24 @@ static bool FastIsUtf8(Local<Value> receiver,
13891394
TRACK_V8_FAST_API_CALL("buffer.isUtf8");
13901395
HandleScope scope(options.isolate);
13911396

1392-
bool was_detached;
1393-
const bool result = ValidateUtf8(value, &was_detached);
1394-
if (was_detached) {
1397+
const ValidationResult result = ValidateUtf8(value);
1398+
if (result.was_detached) {
13951399
node::THROW_ERR_INVALID_STATE(options.isolate,
13961400
"Cannot validate on a detached buffer");
13971401
return false;
13981402
}
1399-
return result;
1403+
return result.is_valid;
14001404
}
14011405

14021406
static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8));
14031407

1404-
static bool ValidateAscii(Local<Value> value, bool* was_detached) {
1408+
static ValidationResult ValidateAscii(Local<Value> value) {
14051409
ArrayBufferViewContents<char> abv(value);
1406-
*was_detached = abv.WasDetached();
1407-
return !*was_detached &&
1408-
!simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error;
1410+
bool was_detached = abv.WasDetached();
1411+
return { !was_detached &&
1412+
!simdutf::validate_ascii_with_errors(abv.data(), abv.length())
1413+
.error,
1414+
was_detached };
14091415
}
14101416

14111417
static void IsAscii(const FunctionCallbackInfo<Value>& args) {
@@ -1414,14 +1420,13 @@ static void IsAscii(const FunctionCallbackInfo<Value>& args) {
14141420
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
14151421
args[0]->IsSharedArrayBuffer());
14161422

1417-
bool was_detached;
1418-
const bool result = ValidateAscii(args[0], &was_detached);
1419-
if (was_detached) {
1423+
const ValidationResult result = ValidateAscii(args[0]);
1424+
if (result.was_detached) {
14201425
return node::THROW_ERR_INVALID_STATE(
14211426
env, "Cannot validate on a detached buffer");
14221427
}
14231428

1424-
args.GetReturnValue().Set(result);
1429+
args.GetReturnValue().Set(result.is_valid);
14251430
}
14261431

14271432
static bool FastIsAscii(Local<Value> receiver,
@@ -1431,14 +1436,13 @@ static bool FastIsAscii(Local<Value> receiver,
14311436
TRACK_V8_FAST_API_CALL("buffer.isAscii");
14321437
HandleScope scope(options.isolate);
14331438

1434-
bool was_detached;
1435-
const bool result = ValidateAscii(value, &was_detached);
1436-
if (was_detached) {
1439+
const ValidationResult result = ValidateAscii(value);
1440+
if (result.was_detached) {
14371441
node::THROW_ERR_INVALID_STATE(options.isolate,
14381442
"Cannot validate on a detached buffer");
14391443
return false;
14401444
}
1441-
return result;
1445+
return result.is_valid;
14421446
}
14431447

14441448
static CFunction fast_is_ascii(CFunction::Make(FastIsAscii));

0 commit comments

Comments
 (0)