Skip to content

Commit 7e84063

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

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/node_buffer.cc

+20-10
Original file line numberDiff line numberDiff line change
@@ -1191,23 +1191,33 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
11911191
}
11921192

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

12021207
static v8::CFunction fast_is_utf8(v8::CFunction::Make(FastIsUtf8));
12031208

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

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

src/node_external_reference.h

-3
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

+2-5
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)