@@ -59,9 +59,11 @@ using v8::ArrayBuffer;
59
59
using v8::ArrayBufferView;
60
60
using v8::BackingStore;
61
61
using v8::BackingStoreInitializationMode;
62
+ using v8::CFunction;
62
63
using v8::Context;
63
64
using v8::EscapableHandleScope;
64
- using v8::FastApiTypedArray;
65
+ using v8::FastApiCallbackOptions;
66
+ using v8::FastOneByteString;
65
67
using v8::FunctionCallbackInfo;
66
68
using v8::Global;
67
69
using v8::HandleScope;
@@ -511,9 +513,9 @@ MaybeLocal<Object> New(Environment* env,
511
513
free (data);
512
514
};
513
515
std::unique_ptr<BackingStore> bs =
514
- v8:: ArrayBuffer::NewBackingStore (data, length, free_callback, nullptr );
516
+ ArrayBuffer::NewBackingStore (data, length, free_callback, nullptr );
515
517
516
- Local<ArrayBuffer> ab = v8:: ArrayBuffer::New (env->isolate (), std::move (bs));
518
+ Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
517
519
518
520
Local<Object> obj;
519
521
if (Buffer::New (env, ab, 0 , length).ToLocal (&obj))
@@ -549,45 +551,47 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
549
551
}
550
552
}
551
553
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
+
552
565
// Assume caller has properly validated args.
553
566
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 ();
555
572
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);
567
574
568
- memmove (target_data + target_start, source.data () + source_start, to_copy);
569
575
args.GetReturnValue ().Set (to_copy);
570
576
}
571
577
572
578
// Assume caller has properly validated args.
573
579
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 ,
576
582
uint32_t target_start,
577
583
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 );
581
588
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);
586
590
587
591
return to_copy;
588
592
}
589
593
590
- static v8:: CFunction fast_copy (v8:: CFunction::Make(FastCopy));
594
+ static CFunction fast_copy (CFunction::Make(FastCopy));
591
595
592
596
void Fill (const FunctionCallbackInfo<Value>& args) {
593
597
Environment* env = Environment::GetCurrent (args);
@@ -731,7 +735,7 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
731
735
uint32_t FastByteLengthUtf8 (
732
736
Local<Value> receiver,
733
737
Local<Value> sourceValue,
734
- v8:: FastApiCallbackOptions& options) { // NOLINT(runtime/references)
738
+ FastApiCallbackOptions& options) { // NOLINT(runtime/references)
735
739
TRACK_V8_FAST_API_CALL (" Buffer::FastByteLengthUtf8" );
736
740
auto isolate = options.isolate ;
737
741
HandleScope handleScope (isolate);
@@ -783,8 +787,7 @@ uint32_t FastByteLengthUtf8(
783
787
return answer;
784
788
}
785
789
786
- static v8::CFunction fast_byte_length_utf8 (
787
- v8::CFunction::Make (FastByteLengthUtf8));
790
+ static CFunction fast_byte_length_utf8 (CFunction::Make(FastByteLengthUtf8));
788
791
789
792
// Normalize val to be an integer in the range of [1, -1] since
790
793
// implementations of memcmp() can vary by platform.
@@ -847,39 +850,39 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
847
850
args.GetReturnValue ().Set (val);
848
851
}
849
852
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
+
850
865
void Compare (const FunctionCallbackInfo<Value> &args) {
851
866
Environment* env = Environment::GetCurrent (args);
852
-
853
867
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
854
868
THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
855
- ArrayBufferViewContents<char > a (args[0 ]);
856
- ArrayBufferViewContents<char > b (args[1 ]);
857
869
858
- size_t cmp_length = std::min (a. length (), b. length () );
870
+ int val = CompareImpl (args[ 0 ], args[ 1 ] );
859
871
860
- int val = normalizeCompareVal (cmp_length > 0 ?
861
- memcmp (a.data (), b.data (), cmp_length) : 0 ,
862
- a.length (), b.length ());
863
872
args.GetReturnValue ().Set (val);
864
873
}
865
874
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 );
875
881
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);
880
883
}
881
884
882
- static v8:: CFunction fast_compare (v8:: CFunction::Make(FastCompare));
885
+ static CFunction fast_compare (CFunction::Make(FastCompare));
883
886
884
887
// Computes the offset for starting an indexOf or lastIndexOf search.
885
888
// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
@@ -1109,11 +1112,13 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
1109
1112
result == haystack_length ? -1 : static_cast <int >(result));
1110
1113
}
1111
1114
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 ();
1117
1122
int64_t opt_offset = IndexOfOffset (buffer_length, offset_i64, 1 , is_forward);
1118
1123
if (opt_offset <= -1 || buffer_length == 0 ) {
1119
1124
return -1 ;
@@ -1137,29 +1142,28 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
1137
1142
CHECK (args[3 ]->IsBoolean ());
1138
1143
1139
1144
THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
1140
- ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
1141
1145
1146
+ Local<Value> buffer_obj = args[0 ];
1142
1147
uint32_t needle = args[1 ].As <Uint32>()->Value ();
1143
1148
int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
1144
1149
bool is_forward = args[3 ]->IsTrue ();
1145
1150
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));
1148
1153
}
1149
1154
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 ,
1152
1157
uint32_t needle,
1153
1158
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);
1159
1164
}
1160
1165
1161
- static v8::CFunction fast_index_of_number (
1162
- v8::CFunction::Make (FastIndexOfNumber));
1166
+ static CFunction fast_index_of_number (CFunction::Make(FastIndexOfNumber));
1163
1167
1164
1168
void Swap16 (const FunctionCallbackInfo<Value>& args) {
1165
1169
Environment* env = Environment::GetCurrent (args);
@@ -1503,29 +1507,31 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
1503
1507
1504
1508
template <encoding encoding>
1505
1509
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,
1508
1512
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 ());
1514
1520
TRACK_V8_FAST_API_CALL (" buffer.writeString" );
1515
1521
1516
1522
return WriteOneByteString<encoding>(
1517
1523
src.data ,
1518
1524
src.length ,
1519
1525
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));
1521
1527
}
1522
1528
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>));
1529
1535
1530
1536
void Initialize (Local<Object> target,
1531
1537
Local<Value> unused,
0 commit comments