@@ -1302,31 +1302,17 @@ void FastSwap64(Local<Value> receiver, |
1302 | 1302 |
|
1303 | 1303 | static CFunction fast_swap64(CFunction::Make(FastSwap64)); |
1304 | 1304 |
|
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) { |
1311 | 1306 | 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()); |
1315 | 1308 | } |
1316 | 1309 |
|
1317 | 1310 | static void IsUtf8(const FunctionCallbackInfo<Value>& args) { |
1318 | | - Environment* env = Environment::GetCurrent(args); |
1319 | 1311 | CHECK_EQ(args.Length(), 1); |
1320 | 1312 | CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || |
1321 | 1313 | args[0]->IsSharedArrayBuffer()); |
1322 | 1314 |
|
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])); |
1330 | 1316 | } |
1331 | 1317 |
|
1332 | 1318 | static bool FastIsUtf8(Local<Value> receiver, |
@@ -1335,40 +1321,23 @@ static bool FastIsUtf8(Local<Value> receiver, |
1335 | 1321 | FastApiCallbackOptions& options) { |
1336 | 1322 | TRACK_V8_FAST_API_CALL("buffer.isUtf8"); |
1337 | 1323 | 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); |
1346 | 1325 | } |
1347 | 1326 |
|
1348 | 1327 | static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8)); |
1349 | 1328 |
|
1350 | | -static ValidationResult ValidateAscii(Local<Value> value) { |
| 1329 | +static bool ValidateAscii(Local<Value> value) { |
1351 | 1330 | 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; |
1357 | 1333 | } |
1358 | 1334 |
|
1359 | 1335 | static void IsAscii(const FunctionCallbackInfo<Value>& args) { |
1360 | | - Environment* env = Environment::GetCurrent(args); |
1361 | 1336 | CHECK_EQ(args.Length(), 1); |
1362 | 1337 | CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || |
1363 | 1338 | args[0]->IsSharedArrayBuffer()); |
1364 | 1339 |
|
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])); |
1372 | 1341 | } |
1373 | 1342 |
|
1374 | 1343 | static bool FastIsAscii(Local<Value> receiver, |
@@ -1377,14 +1346,7 @@ static bool FastIsAscii(Local<Value> receiver, |
1377 | 1346 | FastApiCallbackOptions& options) { |
1378 | 1347 | TRACK_V8_FAST_API_CALL("buffer.isAscii"); |
1379 | 1348 | 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); |
1388 | 1350 | } |
1389 | 1351 |
|
1390 | 1352 | static CFunction fast_is_ascii(CFunction::Make(FastIsAscii)); |
|