@@ -1302,31 +1302,17 @@ void FastSwap64(Local<Value> receiver,
13021302
13031303static CFunction fast_swap64 (CFunction::Make(FastSwap64));
13041304
1305- struct ValidationResult {
1306- bool is_valid;
1307- bool was_detached;
1308- };
1309-
1310- static ValidationResult ValidateUtf8 (Local<Value> value) {
1305+ static bool ValidateUtf8 (Local<Value> value) {
13111306 ArrayBufferViewContents<char > abv (value);
1312- bool was_detached = abv.WasDetached ();
1313- return {!was_detached && simdutf::validate_utf8 (abv.data (), abv.length ()),
1314- was_detached};
1307+ return abv.length () == 0 || simdutf::validate_utf8 (abv.data (), abv.length ());
13151308}
13161309
13171310static void IsUtf8 (const FunctionCallbackInfo<Value>& args) {
1318- Environment* env = Environment::GetCurrent (args);
13191311 CHECK_EQ (args.Length (), 1 );
13201312 CHECK (args[0 ]->IsTypedArray () || args[0 ]->IsArrayBuffer () ||
13211313 args[0 ]->IsSharedArrayBuffer ());
13221314
1323- const ValidationResult result = ValidateUtf8 (args[0 ]);
1324- if (result.was_detached ) {
1325- return node::THROW_ERR_INVALID_STATE (
1326- env, " Cannot validate on a detached buffer" );
1327- }
1328-
1329- args.GetReturnValue ().Set (result.is_valid );
1315+ args.GetReturnValue ().Set (ValidateUtf8 (args[0 ]));
13301316}
13311317
13321318static bool FastIsUtf8 (Local<Value> receiver,
@@ -1335,40 +1321,23 @@ static bool FastIsUtf8(Local<Value> receiver,
13351321 FastApiCallbackOptions& options) {
13361322 TRACK_V8_FAST_API_CALL (" buffer.isUtf8" );
13371323 HandleScope scope (options.isolate );
1338-
1339- const ValidationResult result = ValidateUtf8 (value);
1340- if (result.was_detached ) {
1341- node::THROW_ERR_INVALID_STATE (options.isolate ,
1342- " Cannot validate on a detached buffer" );
1343- return false ;
1344- }
1345- return result.is_valid ;
1324+ return ValidateUtf8 (value);
13461325}
13471326
13481327static CFunction fast_is_utf8 (CFunction::Make(FastIsUtf8));
13491328
1350- static ValidationResult ValidateAscii (Local<Value> value) {
1329+ static bool ValidateAscii (Local<Value> value) {
13511330 ArrayBufferViewContents<char > abv (value);
1352- bool was_detached = abv.WasDetached ();
1353- return {
1354- !was_detached &&
1355- !simdutf::validate_ascii_with_errors (abv.data (), abv.length ()).error ,
1356- was_detached};
1331+ return abv.length () == 0 ||
1332+ !simdutf::validate_ascii_with_errors (abv.data (), abv.length ()).error ;
13571333}
13581334
13591335static void IsAscii (const FunctionCallbackInfo<Value>& args) {
1360- Environment* env = Environment::GetCurrent (args);
13611336 CHECK_EQ (args.Length (), 1 );
13621337 CHECK (args[0 ]->IsTypedArray () || args[0 ]->IsArrayBuffer () ||
13631338 args[0 ]->IsSharedArrayBuffer ());
13641339
1365- const ValidationResult result = ValidateAscii (args[0 ]);
1366- if (result.was_detached ) {
1367- return node::THROW_ERR_INVALID_STATE (
1368- env, " Cannot validate on a detached buffer" );
1369- }
1370-
1371- args.GetReturnValue ().Set (result.is_valid );
1340+ args.GetReturnValue ().Set (ValidateAscii (args[0 ]));
13721341}
13731342
13741343static bool FastIsAscii (Local<Value> receiver,
@@ -1377,14 +1346,7 @@ static bool FastIsAscii(Local<Value> receiver,
13771346 FastApiCallbackOptions& options) {
13781347 TRACK_V8_FAST_API_CALL (" buffer.isAscii" );
13791348 HandleScope scope (options.isolate );
1380-
1381- const ValidationResult result = ValidateAscii (value);
1382- if (result.was_detached ) {
1383- node::THROW_ERR_INVALID_STATE (options.isolate ,
1384- " Cannot validate on a detached buffer" );
1385- return false ;
1386- }
1387- return result.is_valid ;
1349+ return ValidateAscii (value);
13881350}
13891351
13901352static CFunction fast_is_ascii (CFunction::Make(FastIsAscii));
0 commit comments