Skip to content

Commit d7ffbed

Browse files
committed
fixup! buffer: add fast api for isAscii & isUtf8
1 parent 3e56274 commit d7ffbed

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/node_buffer.cc

+22-10
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

@@ -61,6 +62,7 @@ using v8::BackingStore;
6162
using v8::BackingStoreInitializationMode;
6263
using v8::Context;
6364
using v8::EscapableHandleScope;
65+
using v8::FastApiCallbackOptions;
6466
using v8::FastApiTypedArray;
6567
using v8::FunctionCallbackInfo;
6668
using v8::Global;
@@ -1176,23 +1178,33 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
11761178
}
11771179

11781180
bool FastIsUtf8(v8::Local<v8::Value>,
1179-
const v8::FastApiTypedArray<uint8_t>& buffer) {
1180-
uint8_t* buffer_data;
1181-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1181+
Local<Value> buffer,
1182+
FastApiCallbackOptions& options) {
11821183
TRACK_V8_FAST_API_CALL("buffer.isUtf8");
1183-
return simdutf::validate_utf8(reinterpret_cast<const char*>(buffer_data),
1184-
buffer.length());
1184+
ArrayBufferViewContents<uint8_t> view(buffer);
1185+
if (view.WasDetached()) {
1186+
node::THROW_ERR_INVALID_STATE(options.isolate,
1187+
"Cannot validate on a detached buffer");
1188+
return false;
1189+
}
1190+
return simdutf::validate_utf8(reinterpret_cast<const char*>(view.data()),
1191+
view.length());
11851192
}
11861193

11871194
static v8::CFunction fast_is_utf8(v8::CFunction::Make(FastIsUtf8));
11881195

11891196
bool FastIsAscii(v8::Local<v8::Value>,
1190-
const v8::FastApiTypedArray<uint8_t>& buffer) {
1191-
uint8_t* buffer_data;
1192-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1197+
Local<Value> buffer,
1198+
FastApiCallbackOptions& options) {
11931199
TRACK_V8_FAST_API_CALL("buffer.isAscii");
1194-
return simdutf::validate_ascii(reinterpret_cast<const char*>(buffer_data),
1195-
buffer.length());
1200+
ArrayBufferViewContents<uint8_t> view(buffer);
1201+
if (view.WasDetached()) {
1202+
node::THROW_ERR_INVALID_STATE(options.isolate,
1203+
"Cannot validate on a detached buffer");
1204+
return false;
1205+
}
1206+
return simdutf::validate_ascii(reinterpret_cast<const char*>(view.data()),
1207+
view.length());
11961208
}
11971209

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

src/node_external_reference.h

-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> unused,
4242
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> unused,
4343
v8::Local<v8::Object> receiver,
4444
bool);
45-
using CFunctionFastIsUtf8 = bool (*)(
46-
v8::Local<v8::Value>, const v8::FastApiTypedArray<uint8_t>& buffer);
4745
using CFunctionCallbackWithString =
4846
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
4947
using CFunctionCallbackWithStrings =
@@ -111,7 +109,6 @@ class ExternalReferenceRegistry {
111109
V(CFunctionCallbackValueReturnDoubleUnusedReceiver) \
112110
V(CFunctionCallbackWithInt64) \
113111
V(CFunctionCallbackWithBool) \
114-
V(CFunctionFastIsUtf8) \
115112
V(CFunctionCallbackWithString) \
116113
V(CFunctionCallbackWithStrings) \
117114
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)