Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 68 additions & 9 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1360,37 +1360,93 @@ void FastSwap64(Local<Value> receiver,

static CFunction fast_swap64(CFunction::Make(FastSwap64));

struct ValidationResult {
bool is_valid;
bool was_detached;
};

static ValidationResult ValidateUtf8(Local<Value> value) {
ArrayBufferViewContents<char> abv(value);
bool was_detached = abv.WasDetached();
return {!was_detached && simdutf::validate_utf8(abv.data(), abv.length()),
was_detached};
}

static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());
ArrayBufferViewContents<char> abv(args[0]);

if (abv.WasDetached()) {
const ValidationResult result = ValidateUtf8(args[0]);
if (result.was_detached) {
return node::THROW_ERR_INVALID_STATE(
env, "Cannot validate on a detached buffer");
}

args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length()));
args.GetReturnValue().Set(result.is_valid);
}

static bool FastIsUtf8(Local<Value> receiver,
Local<Value> value,
// NOLINTNEXTLINE(runtime/references)
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("buffer.isUtf8");
HandleScope scope(options.isolate);

const ValidationResult result = ValidateUtf8(value);
if (result.was_detached) {
node::THROW_ERR_INVALID_STATE(options.isolate,
"Cannot validate on a detached buffer");
return false;
}
return result.is_valid;
}

static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8));

static ValidationResult ValidateAscii(Local<Value> value) {
ArrayBufferViewContents<char> abv(value);
bool was_detached = abv.WasDetached();
return {
!was_detached &&
!simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error,
was_detached};
}

static void IsAscii(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());
ArrayBufferViewContents<char> abv(args[0]);

if (abv.WasDetached()) {
const ValidationResult result = ValidateAscii(args[0]);
if (result.was_detached) {
return node::THROW_ERR_INVALID_STATE(
env, "Cannot validate on a detached buffer");
}

args.GetReturnValue().Set(
!simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error);
args.GetReturnValue().Set(result.is_valid);
}

static bool FastIsAscii(Local<Value> receiver,
Local<Value> value,
// NOLINTNEXTLINE(runtime/references)
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("buffer.isAscii");
HandleScope scope(options.isolate);

const ValidationResult result = ValidateAscii(value);
if (result.was_detached) {
node::THROW_ERR_INVALID_STATE(options.isolate,
"Cannot validate on a detached buffer");
return false;
}
return result.is_valid;
}

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

void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);

Expand Down Expand Up @@ -1762,8 +1818,9 @@ void Initialize(Local<Object> target,
SetFastMethod(context, target, "swap32", Swap32, &fast_swap32);
SetFastMethod(context, target, "swap64", Swap64, &fast_swap64);

SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8);
SetMethodNoSideEffect(context, target, "isAscii", IsAscii);
SetFastMethodNoSideEffect(context, target, "isUtf8", IsUtf8, &fast_is_utf8);
SetFastMethodNoSideEffect(
context, target, "isAscii", IsAscii, &fast_is_ascii);

target
->Set(context,
Expand Down Expand Up @@ -1836,7 +1893,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(fast_swap64);

registry->Register(IsUtf8);
registry->Register(fast_is_utf8);
registry->Register(IsAscii);
registry->Register(fast_is_ascii);

registry->Register(StringSlice<ASCII>);
registry->Register(StringSlice<BASE64>);
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-buffer-isutf8-isascii-fast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Flags: --expose-internals --no-warnings --allow-natives-syntax
'use strict';

const common = require('../common');
const assert = require('assert');
const { Buffer, isAscii, isUtf8 } = require('buffer');

const ascii = Buffer.from('hello');
const utf8 = Buffer.from('hello \xc4\x9f');

function testFastIsAscii() {
assert.strictEqual(isAscii(ascii), true);
}

function testFastIsUtf8() {
assert.strictEqual(isUtf8(utf8), true);
}

eval('%PrepareFunctionForOptimization(isAscii)');
testFastIsAscii();
eval('%OptimizeFunctionOnNextCall(isAscii)');
testFastIsAscii();

eval('%PrepareFunctionForOptimization(isUtf8)');
testFastIsUtf8();
eval('%OptimizeFunctionOnNextCall(isUtf8)');
testFastIsUtf8();

if (common.isDebug) {
const { internalBinding } = require('internal/test/binding');
const { getV8FastApiCallCount } = internalBinding('debug');
assert.strictEqual(getV8FastApiCallCount('buffer.isAscii'), 1);
assert.strictEqual(getV8FastApiCallCount('buffer.isUtf8'), 1);
}
Loading