Skip to content

Commit 54410b2

Browse files
committed
fixup! buffer: add fast api for isAscii & isUtf7
1 parent 62dea03 commit 54410b2

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/node_buffer.cc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "string_bytes.h"
3434

3535
#include "util-inl.h"
36+
#include "util.h"
3637
#include "v8-fast-api-calls.h"
3738
#include "v8.h"
3839

@@ -1191,23 +1192,33 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
11911192
}
11921193

11931194
bool FastIsUtf8(v8::Local<v8::Value>,
1194-
const v8::FastApiTypedArray<uint8_t>& buffer) {
1195-
uint8_t* buffer_data;
1196-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1195+
Local<Value> buffer,
1196+
FastApiCallbackOptions& options) {
11971197
TRACK_V8_FAST_API_CALL("buffer.isUtf8");
1198-
return simdutf::validate_utf8(reinterpret_cast<const char*>(buffer_data),
1199-
buffer.length());
1198+
ArrayBufferViewContents<uint8_t> view(buffer);
1199+
if (view.WasDetached()) {
1200+
node::THROW_ERR_INVALID_STATE(options.isolate,
1201+
"Cannot validate on a detached buffer");
1202+
return false;
1203+
}
1204+
return simdutf::validate_utf8(reinterpret_cast<const char*>(view.data()),
1205+
view.length());
12001206
}
12011207

12021208
static v8::CFunction fast_is_utf8(v8::CFunction::Make(FastIsUtf8));
12031209

12041210
bool FastIsAscii(v8::Local<v8::Value>,
1205-
const v8::FastApiTypedArray<uint8_t>& buffer) {
1206-
uint8_t* buffer_data;
1207-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1211+
Local<Value> buffer,
1212+
FastApiCallbackOptions& options) {
12081213
TRACK_V8_FAST_API_CALL("buffer.isAscii");
1209-
return simdutf::validate_ascii(reinterpret_cast<const char*>(buffer_data),
1210-
buffer.length());
1214+
ArrayBufferViewContents<uint8_t> view(buffer);
1215+
if (view.WasDetached()) {
1216+
node::THROW_ERR_INVALID_STATE(options.isolate,
1217+
"Cannot validate on a detached buffer");
1218+
return false;
1219+
}
1220+
return simdutf::validate_ascii(reinterpret_cast<const char*>(view.data()),
1221+
view.length());
12111222
}
12121223

12131224
static v8::CFunction fast_is_ascii(v8::CFunction::Make(FastIsAscii));

src/node_external_reference.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> unused,
4646
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> unused,
4747
v8::Local<v8::Object> receiver,
4848
bool);
49-
using CFunctionFastIsUtf8 = bool (*)(
50-
v8::Local<v8::Value>, const v8::FastApiTypedArray<uint8_t>& buffer);
5149
using CFunctionCallbackWithString =
5250
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
5351
using CFunctionCallbackWithStrings =
@@ -113,7 +111,6 @@ class ExternalReferenceRegistry {
113111
V(CFunctionCallbackValueReturnDoubleUnusedReceiver) \
114112
V(CFunctionCallbackWithInt64) \
115113
V(CFunctionCallbackWithBool) \
116-
V(CFunctionFastIsUtf8) \
117114
V(CFunctionCallbackWithString) \
118115
V(CFunctionCallbackWithStrings) \
119116
V(CFunctionCallbackWithTwoUint8Arrays) \

test/parallel/test-buffer-isutf8-isascii-fast-api.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ assert.strictEqual(buffer.isAscii(nonAsciiBuffer), false);
2424

2525
// Test detached buffers
2626
const detachedBuffer = new ArrayBuffer(10);
27-
try {
28-
detachedBuffer.detach();
29-
} catch (_e) {
30-
console.log('Skipping detached buffer tests - detach not supported');
31-
}
27+
// Let's detach the buffer if it's supported
28+
detachedBuffer.detach?.();
3229

3330
if (detachedBuffer.detached) {
3431
const typedArray = new Uint8Array(detachedBuffer);

0 commit comments

Comments
 (0)