Skip to content

Commit 50a8527

Browse files
targosRafaelGSS
authored andcommitted
src: replace uses of FastApiTypedArray
The API was removed from V8. PR-URL: #58070 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 9c1ebb7 commit 50a8527

File tree

3 files changed

+115
-114
lines changed

3 files changed

+115
-114
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: 87 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;
@@ -511,9 +513,9 @@ MaybeLocal<Object> New(Environment* env,
511513
free(data);
512514
};
513515
std::unique_ptr<BackingStore> bs =
514-
v8::ArrayBuffer::NewBackingStore(data, length, free_callback, nullptr);
516+
ArrayBuffer::NewBackingStore(data, length, free_callback, nullptr);
515517

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

518520
Local<Object> obj;
519521
if (Buffer::New(env, ab, 0, length).ToLocal(&obj))
@@ -549,45 +551,47 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
549551
}
550552
}
551553

554+
void CopyImpl(Local<Value> source_obj,
555+
Local<Value> target_obj,
556+
const uint32_t target_start,
557+
const uint32_t source_start,
558+
const uint32_t to_copy) {
559+
ArrayBufferViewContents<char> source(source_obj);
560+
SPREAD_BUFFER_ARG(target_obj, target);
561+
562+
memmove(target_data + target_start, source.data() + source_start, to_copy);
563+
}
564+
552565
// Assume caller has properly validated args.
553566
void SlowCopy(const FunctionCallbackInfo<Value>& args) {
554-
Environment* env = Environment::GetCurrent(args);
567+
Local<Value> source_obj = args[0];
568+
Local<Value> target_obj = args[1];
569+
const uint32_t target_start = args[2].As<Uint32>()->Value();
570+
const uint32_t source_start = args[3].As<Uint32>()->Value();
571+
const uint32_t to_copy = args[4].As<Uint32>()->Value();
555572

556-
ArrayBufferViewContents<char> source(args[0]);
557-
SPREAD_BUFFER_ARG(args[1].As<Object>(), target);
558-
559-
uint32_t target_start;
560-
uint32_t source_start;
561-
uint32_t to_copy;
562-
if (!args[2]->Uint32Value(env->context()).To(&target_start) ||
563-
!args[3]->Uint32Value(env->context()).To(&source_start) ||
564-
!args[4]->Uint32Value(env->context()).To(&to_copy)) {
565-
return;
566-
}
573+
CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);
567574

568-
memmove(target_data + target_start, source.data() + source_start, to_copy);
569575
args.GetReturnValue().Set(to_copy);
570576
}
571577

572578
// Assume caller has properly validated args.
573579
uint32_t FastCopy(Local<Value> receiver,
574-
const v8::FastApiTypedArray<uint8_t>& source,
575-
const v8::FastApiTypedArray<uint8_t>& target,
580+
Local<Value> source_obj,
581+
Local<Value> target_obj,
576582
uint32_t target_start,
577583
uint32_t source_start,
578-
uint32_t to_copy) {
579-
uint8_t* source_data;
580-
CHECK(source.getStorageIfAligned(&source_data));
584+
uint32_t to_copy,
585+
// NOLINTNEXTLINE(runtime/references)
586+
FastApiCallbackOptions& options) {
587+
HandleScope scope(options.isolate);
581588

582-
uint8_t* target_data;
583-
CHECK(target.getStorageIfAligned(&target_data));
584-
585-
memmove(target_data + target_start, source_data + source_start, to_copy);
589+
CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);
586590

587591
return to_copy;
588592
}
589593

590-
static v8::CFunction fast_copy(v8::CFunction::Make(FastCopy));
594+
static CFunction fast_copy(CFunction::Make(FastCopy));
591595

592596
void Fill(const FunctionCallbackInfo<Value>& args) {
593597
Environment* env = Environment::GetCurrent(args);
@@ -731,7 +735,7 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
731735
uint32_t FastByteLengthUtf8(
732736
Local<Value> receiver,
733737
Local<Value> sourceValue,
734-
v8::FastApiCallbackOptions& options) { // NOLINT(runtime/references)
738+
FastApiCallbackOptions& options) { // NOLINT(runtime/references)
735739
TRACK_V8_FAST_API_CALL("Buffer::FastByteLengthUtf8");
736740
auto isolate = options.isolate;
737741
HandleScope handleScope(isolate);
@@ -783,8 +787,7 @@ uint32_t FastByteLengthUtf8(
783787
return answer;
784788
}
785789

786-
static v8::CFunction fast_byte_length_utf8(
787-
v8::CFunction::Make(FastByteLengthUtf8));
790+
static CFunction fast_byte_length_utf8(CFunction::Make(FastByteLengthUtf8));
788791

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

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

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

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

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

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

882-
static v8::CFunction fast_compare(v8::CFunction::Make(FastCompare));
885+
static CFunction fast_compare(CFunction::Make(FastCompare));
883886

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

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

11391144
THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]);
1140-
ArrayBufferViewContents<uint8_t> buffer(args[0]);
11411145

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

1146-
args.GetReturnValue().Set(IndexOfNumber(
1147-
buffer.data(), buffer.length(), needle, offset_i64, is_forward));
1151+
args.GetReturnValue().Set(
1152+
IndexOfNumberImpl(buffer_obj, needle, offset_i64, is_forward));
11481153
}
11491154

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

1161-
static v8::CFunction fast_index_of_number(
1162-
v8::CFunction::Make(FastIndexOfNumber));
1166+
static CFunction fast_index_of_number(CFunction::Make(FastIndexOfNumber));
11631167

11641168
void Swap16(const FunctionCallbackInfo<Value>& args) {
11651169
Environment* env = Environment::GetCurrent(args);
@@ -1503,29 +1507,31 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
15031507

15041508
template <encoding encoding>
15051509
uint32_t FastWriteString(Local<Value> receiver,
1506-
const v8::FastApiTypedArray<uint8_t>& dst,
1507-
const v8::FastOneByteString& src,
1510+
Local<Value> dst_obj,
1511+
const FastOneByteString& src,
15081512
uint32_t offset,
1509-
uint32_t max_length) {
1510-
uint8_t* dst_data;
1511-
CHECK(dst.getStorageIfAligned(&dst_data));
1512-
CHECK(offset <= dst.length());
1513-
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());
1513+
uint32_t max_length,
1514+
// NOLINTNEXTLINE(runtime/references)
1515+
FastApiCallbackOptions& options) {
1516+
HandleScope handle_scope(options.isolate);
1517+
SPREAD_BUFFER_ARG(dst_obj, dst);
1518+
CHECK(offset <= dst_length);
1519+
CHECK(dst_length - offset <= std::numeric_limits<uint32_t>::max());
15141520
TRACK_V8_FAST_API_CALL("buffer.writeString");
15151521

15161522
return WriteOneByteString<encoding>(
15171523
src.data,
15181524
src.length,
15191525
reinterpret_cast<char*>(dst_data + offset),
1520-
std::min<uint32_t>(dst.length() - offset, max_length));
1526+
std::min<uint32_t>(dst_length - offset, max_length));
15211527
}
15221528

1523-
static const v8::CFunction fast_write_string_ascii(
1524-
v8::CFunction::Make(FastWriteString<ASCII>));
1525-
static const v8::CFunction fast_write_string_latin1(
1526-
v8::CFunction::Make(FastWriteString<LATIN1>));
1527-
static const v8::CFunction fast_write_string_utf8(
1528-
v8::CFunction::Make(FastWriteString<UTF8>));
1529+
static const CFunction fast_write_string_ascii(
1530+
CFunction::Make(FastWriteString<ASCII>));
1531+
static const CFunction fast_write_string_latin1(
1532+
CFunction::Make(FastWriteString<LATIN1>));
1533+
static const CFunction fast_write_string_utf8(
1534+
CFunction::Make(FastWriteString<UTF8>));
15291535

15301536
void Initialize(Local<Object> target,
15311537
Local<Value> unused,

src/node_external_reference.h

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,16 @@ using CFunctionCallbackWithStrings =
5454
const v8::FastOneByteString& base);
5555
using CFunctionCallbackWithTwoUint8Arrays =
5656
int32_t (*)(v8::Local<v8::Value>,
57-
const v8::FastApiTypedArray<uint8_t>&,
58-
const v8::FastApiTypedArray<uint8_t>&);
59-
using CFunctionCallbackWithTwoUint8ArraysFallback =
60-
bool (*)(v8::Local<v8::Value>,
61-
const v8::FastApiTypedArray<uint8_t>&,
62-
const v8::FastApiTypedArray<uint8_t>&,
63-
v8::FastApiCallbackOptions&);
57+
v8::Local<v8::Value>,
58+
v8::Local<v8::Value>,
59+
v8::FastApiCallbackOptions&);
6460
using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
6561
int32_t (*)(v8::Local<v8::Value>,
66-
const v8::FastApiTypedArray<uint8_t>&,
62+
v8::Local<v8::Value>,
6763
uint32_t,
6864
int64_t,
69-
bool);
65+
bool,
66+
v8::FastApiCallbackOptions&);
7067
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
7168
const uint32_t input);
7269
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
@@ -80,20 +77,20 @@ using CFunctionWithBool = void (*)(v8::Local<v8::Value>,
8077
v8::Local<v8::Value>,
8178
bool);
8279

83-
using CFunctionWriteString =
84-
uint32_t (*)(v8::Local<v8::Value> receiver,
85-
const v8::FastApiTypedArray<uint8_t>& dst,
86-
const v8::FastOneByteString& src,
87-
uint32_t offset,
88-
uint32_t max_length);
80+
using CFunctionWriteString = uint32_t (*)(v8::Local<v8::Value>,
81+
v8::Local<v8::Value>,
82+
const v8::FastOneByteString&,
83+
uint32_t,
84+
uint32_t,
85+
v8::FastApiCallbackOptions&);
8986

90-
using CFunctionBufferCopy =
91-
uint32_t (*)(v8::Local<v8::Value> receiver,
92-
const v8::FastApiTypedArray<uint8_t>& source,
93-
const v8::FastApiTypedArray<uint8_t>& target,
94-
uint32_t target_start,
95-
uint32_t source_start,
96-
uint32_t to_copy);
87+
using CFunctionBufferCopy = uint32_t (*)(v8::Local<v8::Value>,
88+
v8::Local<v8::Value>,
89+
v8::Local<v8::Value>,
90+
uint32_t,
91+
uint32_t,
92+
uint32_t,
93+
v8::FastApiCallbackOptions&);
9794

9895
// This class manages the external references from the V8 heap
9996
// to the C++ addresses in Node.js.
@@ -117,7 +114,6 @@ class ExternalReferenceRegistry {
117114
V(CFunctionCallbackWithString) \
118115
V(CFunctionCallbackWithStrings) \
119116
V(CFunctionCallbackWithTwoUint8Arrays) \
120-
V(CFunctionCallbackWithTwoUint8ArraysFallback) \
121117
V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \
122118
V(CFunctionWithUint32) \
123119
V(CFunctionWithDoubleReturnDouble) \

0 commit comments

Comments
 (0)