Skip to content

Commit cc77d64

Browse files
committed
src: replace uses of FastApiTypedArray
The API was removed from V8.
1 parent 0bc594f commit cc77d64

File tree

3 files changed

+117
-111
lines changed

3 files changed

+117
-111
lines changed

src/crypto/crypto_timing.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace node {
1212

13+
using v8::CFunction;
1314
using v8::FastApiCallbackOptions;
14-
using v8::FastApiTypedArray;
1515
using v8::FunctionCallbackInfo;
1616
using v8::HandleScope;
1717
using v8::Local;
@@ -51,25 +51,24 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
5151
}
5252

5353
bool FastTimingSafeEqual(Local<Value> receiver,
54-
const FastApiTypedArray<uint8_t>& a,
55-
const FastApiTypedArray<uint8_t>& b,
54+
Local<Value> a_obj,
55+
Local<Value> b_obj,
5656
// NOLINTNEXTLINE(runtime/references)
5757
FastApiCallbackOptions& options) {
58-
uint8_t* data_a;
59-
uint8_t* data_b;
60-
if (a.length() != b.length() || !a.getStorageIfAligned(&data_a) ||
61-
!b.getStorageIfAligned(&data_b)) {
58+
HandleScope scope(options.isolate);
59+
ArrayBufferViewContents<uint8_t> a(a_obj);
60+
ArrayBufferViewContents<uint8_t> b(b_obj);
61+
if (a.length() != b.length()) {
6262
TRACK_V8_FAST_API_CALL("crypto.timingSafeEqual.error");
63-
HandleScope scope(options.isolate);
6463
THROW_ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(options.isolate);
6564
return false;
6665
}
6766

6867
TRACK_V8_FAST_API_CALL("crypto.timingSafeEqual.ok");
69-
return CRYPTO_memcmp(data_a, data_b, a.length()) == 0;
68+
return CRYPTO_memcmp(a.data(), b.data(), a.length()) == 0;
7069
}
7170

72-
static v8::CFunction fast_equal(v8::CFunction::Make(FastTimingSafeEqual));
71+
static CFunction fast_equal(CFunction::Make(FastTimingSafeEqual));
7372

7473
void Initialize(Environment* env, Local<Object> target) {
7574
SetFastMethodNoSideEffect(

src/node_buffer.cc

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ using v8::ArrayBuffer;
5959
using v8::ArrayBufferView;
6060
using v8::BackingStore;
6161
using v8::BackingStoreInitializationMode;
62+
using v8::CFunction;
6263
using v8::Context;
6364
using v8::EscapableHandleScope;
64-
using v8::FastApiTypedArray;
65+
using v8::FastApiCallbackOptions;
66+
using v8::FastOneByteString;
6567
using v8::FunctionCallbackInfo;
6668
using v8::Global;
6769
using v8::HandleScope;
@@ -513,9 +515,9 @@ MaybeLocal<Object> New(Environment* env,
513515
free(data);
514516
};
515517
std::unique_ptr<BackingStore> bs =
516-
v8::ArrayBuffer::NewBackingStore(data, length, free_callback, nullptr);
518+
ArrayBuffer::NewBackingStore(data, length, free_callback, nullptr);
517519

518-
Local<ArrayBuffer> ab = v8::ArrayBuffer::New(env->isolate(), std::move(bs));
520+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
519521

520522
Local<Object> obj;
521523
if (Buffer::New(env, ab, 0, length).ToLocal(&obj))
@@ -560,45 +562,47 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
560562
args.GetReturnValue().Set(ret);
561563
}
562564

565+
void CopyImpl(Local<Value> source_obj,
566+
Local<Value> target_obj,
567+
const uint32_t target_start,
568+
const uint32_t source_start,
569+
const uint32_t to_copy) {
570+
ArrayBufferViewContents<char> source(source_obj);
571+
SPREAD_BUFFER_ARG(target_obj, target);
572+
573+
memmove(target_data + target_start, source.data() + source_start, to_copy);
574+
}
575+
563576
// Assume caller has properly validated args.
564577
void SlowCopy(const FunctionCallbackInfo<Value>& args) {
565-
Environment* env = Environment::GetCurrent(args);
578+
Local<Value> source_obj = args[0];
579+
Local<Value> target_obj = args[1];
580+
const uint32_t target_start = args[2].As<Uint32>()->Value();
581+
const uint32_t source_start = args[3].As<Uint32>()->Value();
582+
const uint32_t to_copy = args[4].As<Uint32>()->Value();
566583

567-
ArrayBufferViewContents<char> source(args[0]);
568-
SPREAD_BUFFER_ARG(args[1].As<Object>(), target);
569-
570-
uint32_t target_start;
571-
uint32_t source_start;
572-
uint32_t to_copy;
573-
if (!args[2]->Uint32Value(env->context()).To(&target_start) ||
574-
!args[3]->Uint32Value(env->context()).To(&source_start) ||
575-
!args[4]->Uint32Value(env->context()).To(&to_copy)) {
576-
return;
577-
}
584+
CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);
578585

579-
memmove(target_data + target_start, source.data() + source_start, to_copy);
580586
args.GetReturnValue().Set(to_copy);
581587
}
582588

583589
// Assume caller has properly validated args.
584590
uint32_t FastCopy(Local<Value> receiver,
585-
const v8::FastApiTypedArray<uint8_t>& source,
586-
const v8::FastApiTypedArray<uint8_t>& target,
591+
Local<Value> source_obj,
592+
Local<Value> target_obj,
587593
uint32_t target_start,
588594
uint32_t source_start,
589-
uint32_t to_copy) {
590-
uint8_t* source_data;
591-
CHECK(source.getStorageIfAligned(&source_data));
595+
uint32_t to_copy,
596+
// NOLINTNEXTLINE(runtime/references)
597+
FastApiCallbackOptions& options) {
598+
HandleScope scope(options.isolate);
592599

593-
uint8_t* target_data;
594-
CHECK(target.getStorageIfAligned(&target_data));
595-
596-
memmove(target_data + target_start, source_data + source_start, to_copy);
600+
CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);
597601

598602
return to_copy;
599603
}
600604

601-
static v8::CFunction fast_copy(v8::CFunction::Make(FastCopy));
605+
static CFunction fast_copy(CFunction::Make(FastCopy));
602606

603607
void Fill(const FunctionCallbackInfo<Value>& args) {
604608
Environment* env = Environment::GetCurrent(args);
@@ -740,7 +744,7 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
740744
}
741745

742746
uint32_t FastByteLengthUtf8(Local<Value> receiver,
743-
const v8::FastOneByteString& source) {
747+
const FastOneByteString& source) {
744748
// For short inputs, the function call overhead to simdutf is maybe
745749
// not worth it, reserve simdutf for long strings.
746750
if (source.length > 128) {
@@ -782,8 +786,7 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
782786
return answer;
783787
}
784788

785-
static v8::CFunction fast_byte_length_utf8(
786-
v8::CFunction::Make(FastByteLengthUtf8));
789+
static CFunction fast_byte_length_utf8(CFunction::Make(FastByteLengthUtf8));
787790

788791
// Normalize val to be an integer in the range of [1, -1] since
789792
// implementations of memcmp() can vary by platform.
@@ -846,39 +849,39 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
846849
args.GetReturnValue().Set(val);
847850
}
848851

852+
int32_t CompareImpl(Local<Value> a_obj, Local<Value> b_obj) {
853+
ArrayBufferViewContents<char> a(a_obj);
854+
ArrayBufferViewContents<char> b(b_obj);
855+
856+
size_t cmp_length = std::min(a.length(), b.length());
857+
858+
return normalizeCompareVal(
859+
cmp_length > 0 ? memcmp(a.data(), b.data(), cmp_length) : 0,
860+
a.length(),
861+
b.length());
862+
}
863+
849864
void Compare(const FunctionCallbackInfo<Value> &args) {
850865
Environment* env = Environment::GetCurrent(args);
851-
852866
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
853867
THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]);
854-
ArrayBufferViewContents<char> a(args[0]);
855-
ArrayBufferViewContents<char> b(args[1]);
856868

857-
size_t cmp_length = std::min(a.length(), b.length());
869+
int val = CompareImpl(args[0], args[1]);
858870

859-
int val = normalizeCompareVal(cmp_length > 0 ?
860-
memcmp(a.data(), b.data(), cmp_length) : 0,
861-
a.length(), b.length());
862871
args.GetReturnValue().Set(val);
863872
}
864873

865-
int32_t FastCompare(v8::Local<v8::Value>,
866-
const FastApiTypedArray<uint8_t>& a,
867-
const FastApiTypedArray<uint8_t>& b) {
868-
uint8_t* data_a;
869-
uint8_t* data_b;
870-
CHECK(a.getStorageIfAligned(&data_a));
871-
CHECK(b.getStorageIfAligned(&data_b));
872-
873-
size_t cmp_length = std::min(a.length(), b.length());
874+
int32_t FastCompare(Local<Value>,
875+
Local<Value> a_obj,
876+
Local<Value> b_obj,
877+
// NOLINTNEXTLINE(runtime/references)
878+
FastApiCallbackOptions& options) {
879+
HandleScope scope(options.isolate);
874880

875-
return normalizeCompareVal(
876-
cmp_length > 0 ? memcmp(data_a, data_b, cmp_length) : 0,
877-
a.length(),
878-
b.length());
881+
return CompareImpl(a_obj, b_obj);
879882
}
880883

881-
static v8::CFunction fast_compare(v8::CFunction::Make(FastCompare));
884+
static CFunction fast_compare(CFunction::Make(FastCompare));
882885

883886
// Computes the offset for starting an indexOf or lastIndexOf search.
884887
// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
@@ -1108,11 +1111,13 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
11081111
result == haystack_length ? -1 : static_cast<int>(result));
11091112
}
11101113

1111-
int32_t IndexOfNumber(const uint8_t* buffer_data,
1112-
size_t buffer_length,
1113-
uint32_t needle,
1114-
int64_t offset_i64,
1115-
bool is_forward) {
1114+
int32_t IndexOfNumberImpl(Local<Value> buffer_obj,
1115+
const uint32_t needle,
1116+
const int64_t offset_i64,
1117+
const bool is_forward) {
1118+
ArrayBufferViewContents<uint8_t> buffer(buffer_obj);
1119+
const uint8_t* buffer_data = buffer.data();
1120+
const size_t buffer_length = buffer.length();
11161121
int64_t opt_offset = IndexOfOffset(buffer_length, offset_i64, 1, is_forward);
11171122
if (opt_offset <= -1 || buffer_length == 0) {
11181123
return -1;
@@ -1136,29 +1141,28 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11361141
CHECK(args[3]->IsBoolean());
11371142

11381143
THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]);
1139-
ArrayBufferViewContents<uint8_t> buffer(args[0]);
11401144

1145+
Local<Value> buffer_obj = args[0];
11411146
uint32_t needle = args[1].As<Uint32>()->Value();
11421147
int64_t offset_i64 = args[2].As<Integer>()->Value();
11431148
bool is_forward = args[3]->IsTrue();
11441149

1145-
args.GetReturnValue().Set(IndexOfNumber(
1146-
buffer.data(), buffer.length(), needle, offset_i64, is_forward));
1150+
args.GetReturnValue().Set(
1151+
IndexOfNumberImpl(buffer_obj, needle, offset_i64, is_forward));
11471152
}
11481153

1149-
int32_t FastIndexOfNumber(v8::Local<v8::Value>,
1150-
const FastApiTypedArray<uint8_t>& buffer,
1154+
int32_t FastIndexOfNumber(Local<Value>,
1155+
Local<Value> buffer_obj,
11511156
uint32_t needle,
11521157
int64_t offset_i64,
1153-
bool is_forward) {
1154-
uint8_t* buffer_data;
1155-
CHECK(buffer.getStorageIfAligned(&buffer_data));
1156-
return IndexOfNumber(
1157-
buffer_data, buffer.length(), needle, offset_i64, is_forward);
1158+
bool is_forward,
1159+
// NOLINTNEXTLINE(runtime/references)
1160+
FastApiCallbackOptions& options) {
1161+
HandleScope scope(options.isolate);
1162+
return IndexOfNumberImpl(buffer_obj, needle, offset_i64, is_forward);
11581163
}
11591164

1160-
static v8::CFunction fast_index_of_number(
1161-
v8::CFunction::Make(FastIndexOfNumber));
1165+
static CFunction fast_index_of_number(CFunction::Make(FastIndexOfNumber));
11621166

11631167
void Swap16(const FunctionCallbackInfo<Value>& args) {
11641168
Environment* env = Environment::GetCurrent(args);
@@ -1508,29 +1512,30 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
15081512

15091513
template <encoding encoding>
15101514
uint32_t FastWriteString(Local<Value> receiver,
1511-
const v8::FastApiTypedArray<uint8_t>& dst,
1512-
const v8::FastOneByteString& src,
1515+
Local<Value> dst_obj,
1516+
const FastOneByteString& src,
15131517
uint32_t offset,
1514-
uint32_t max_length) {
1515-
uint8_t* dst_data;
1516-
CHECK(dst.getStorageIfAligned(&dst_data));
1517-
CHECK(offset <= dst.length());
1518-
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());
1518+
uint32_t max_length,
1519+
// NOLINTNEXTLINE(runtime/references)
1520+
FastApiCallbackOptions& options) {
1521+
HandleScope handle_scope(options.isolate);
1522+
SPREAD_BUFFER_ARG(dst_obj, dst);
1523+
CHECK(offset <= dst_length);
1524+
CHECK(dst_length - offset <= std::numeric_limits<uint32_t>::max());
15191525
TRACK_V8_FAST_API_CALL("buffer.writeString");
15201526

15211527
return WriteOneByteString<encoding>(
15221528
src.data,
15231529
src.length,
15241530
reinterpret_cast<char*>(dst_data + offset),
1525-
std::min<uint32_t>(dst.length() - offset, max_length));
1531+
std::min<uint32_t>(dst_length - offset, max_length));
15261532
}
15271533

1528-
static v8::CFunction fast_write_string_ascii(
1529-
v8::CFunction::Make(FastWriteString<ASCII>));
1530-
static v8::CFunction fast_write_string_latin1(
1531-
v8::CFunction::Make(FastWriteString<LATIN1>));
1532-
static v8::CFunction fast_write_string_utf8(
1533-
v8::CFunction::Make(FastWriteString<UTF8>));
1534+
static CFunction fast_write_string_ascii(
1535+
CFunction::Make(FastWriteString<ASCII>));
1536+
static CFunction fast_write_string_latin1(
1537+
CFunction::Make(FastWriteString<LATIN1>));
1538+
static CFunction fast_write_string_utf8(CFunction::Make(FastWriteString<UTF8>));
15341539

15351540
void Initialize(Local<Object> target,
15361541
Local<Value> unused,

src/node_external_reference.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,21 @@ using CFunctionCallbackWithStrings =
4343
const v8::FastOneByteString& base);
4444
using CFunctionCallbackWithTwoUint8Arrays =
4545
int32_t (*)(v8::Local<v8::Value>,
46-
const v8::FastApiTypedArray<uint8_t>&,
47-
const v8::FastApiTypedArray<uint8_t>&);
46+
v8::Local<v8::Value>,
47+
v8::Local<v8::Value>,
48+
v8::FastApiCallbackOptions&);
4849
using CFunctionCallbackWithTwoUint8ArraysFallback =
4950
bool (*)(v8::Local<v8::Value>,
50-
const v8::FastApiTypedArray<uint8_t>&,
51-
const v8::FastApiTypedArray<uint8_t>&,
51+
v8::Local<v8::Value>,
52+
v8::Local<v8::Value>,
5253
v8::FastApiCallbackOptions&);
5354
using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
5455
int32_t (*)(v8::Local<v8::Value>,
55-
const v8::FastApiTypedArray<uint8_t>&,
56+
v8::Local<v8::Value>,
5657
uint32_t,
5758
int64_t,
58-
bool);
59+
bool,
60+
v8::FastApiCallbackOptions&);
5961
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
6062
const uint32_t input);
6163
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
@@ -69,20 +71,20 @@ using CFunctionWithBool = void (*)(v8::Local<v8::Value>,
6971
v8::Local<v8::Value>,
7072
bool);
7173

72-
using CFunctionWriteString =
73-
uint32_t (*)(v8::Local<v8::Value> receiver,
74-
const v8::FastApiTypedArray<uint8_t>& dst,
75-
const v8::FastOneByteString& src,
76-
uint32_t offset,
77-
uint32_t max_length);
78-
79-
using CFunctionBufferCopy =
80-
uint32_t (*)(v8::Local<v8::Value> receiver,
81-
const v8::FastApiTypedArray<uint8_t>& source,
82-
const v8::FastApiTypedArray<uint8_t>& target,
83-
uint32_t target_start,
84-
uint32_t source_start,
85-
uint32_t to_copy);
74+
using CFunctionWriteString = uint32_t (*)(v8::Local<v8::Value>,
75+
v8::Local<v8::Value>,
76+
const v8::FastOneByteString&,
77+
uint32_t,
78+
uint32_t,
79+
v8::FastApiCallbackOptions&);
80+
81+
using CFunctionBufferCopy = uint32_t (*)(v8::Local<v8::Value>,
82+
v8::Local<v8::Value>,
83+
v8::Local<v8::Value>,
84+
uint32_t,
85+
uint32_t,
86+
uint32_t,
87+
v8::FastApiCallbackOptions&);
8688

8789
// This class manages the external references from the V8 heap
8890
// to the C++ addresses in Node.js.

0 commit comments

Comments
 (0)