@@ -59,9 +59,11 @@ using v8::ArrayBuffer;
5959using v8::ArrayBufferView;
6060using v8::BackingStore;
6161using v8::BackingStoreInitializationMode;
62+ using v8::CFunction;
6263using v8::Context;
6364using v8::EscapableHandleScope;
64- using v8::FastApiTypedArray;
65+ using v8::FastApiCallbackOptions;
66+ using v8::FastOneByteString;
6567using v8::FunctionCallbackInfo;
6668using v8::Global;
6769using v8::HandleScope;
@@ -514,9 +516,9 @@ MaybeLocal<Object> New(Environment* env,
514516 free (data);
515517 };
516518 std::unique_ptr<BackingStore> bs =
517- v8:: ArrayBuffer::NewBackingStore (data, length, free_callback, nullptr );
519+ ArrayBuffer::NewBackingStore (data, length, free_callback, nullptr );
518520
519- Local<ArrayBuffer> ab = v8:: ArrayBuffer::New (env->isolate (), std::move (bs));
521+ Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
520522
521523 Local<Object> obj;
522524 if (Buffer::New (env, ab, 0 , length).ToLocal (&obj))
@@ -561,40 +563,47 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
561563 args.GetReturnValue ().Set (ret);
562564}
563565
566+ void CopyImpl (Local<Value> source_obj,
567+ Local<Value> target_obj,
568+ const uint32_t target_start,
569+ const uint32_t source_start,
570+ const uint32_t to_copy) {
571+ ArrayBufferViewContents<char > source (source_obj);
572+ SPREAD_BUFFER_ARG (target_obj, target);
573+
574+ memmove (target_data + target_start, source.data () + source_start, to_copy);
575+ }
576+
564577// Assume caller has properly validated args.
565578void SlowCopy (const FunctionCallbackInfo<Value>& args) {
566- Environment* env = Environment::GetCurrent (args);
567-
568- ArrayBufferViewContents<char > source (args[0 ]);
569- SPREAD_BUFFER_ARG (args[1 ].As <Object>(), target);
579+ Local<Value> source_obj = args[0 ];
580+ Local<Value> target_obj = args[1 ];
581+ const uint32_t target_start = args[2 ].As <Uint32>()->Value ();
582+ const uint32_t source_start = args[3 ].As <Uint32>()->Value ();
583+ const uint32_t to_copy = args[4 ].As <Uint32>()->Value ();
570584
571- const auto target_start = args[2 ]->Uint32Value (env->context ()).ToChecked ();
572- const auto source_start = args[3 ]->Uint32Value (env->context ()).ToChecked ();
573- const auto to_copy = args[4 ]->Uint32Value (env->context ()).ToChecked ();
585+ CopyImpl (source_obj, target_obj, target_start, source_start, to_copy);
574586
575- memmove (target_data + target_start, source.data () + source_start, to_copy);
576587 args.GetReturnValue ().Set (to_copy);
577588}
578589
579590// Assume caller has properly validated args.
580591uint32_t FastCopy (Local<Value> receiver,
581- const v8::FastApiTypedArray< uint8_t >& source ,
582- const v8::FastApiTypedArray< uint8_t >& target ,
592+ Local<Value> source_obj ,
593+ Local<Value> target_obj ,
583594 uint32_t target_start,
584595 uint32_t source_start,
585- uint32_t to_copy) {
586- uint8_t * source_data;
587- CHECK (source.getStorageIfAligned (&source_data));
596+ uint32_t to_copy,
597+ // NOLINTNEXTLINE(runtime/references)
598+ FastApiCallbackOptions& options) {
599+ HandleScope scope (options.isolate );
588600
589- uint8_t * target_data;
590- CHECK (target.getStorageIfAligned (&target_data));
591-
592- memmove (target_data + target_start, source_data + source_start, to_copy);
601+ CopyImpl (source_obj, target_obj, target_start, source_start, to_copy);
593602
594603 return to_copy;
595604}
596605
597- static v8:: CFunction fast_copy (v8:: CFunction::Make(FastCopy));
606+ static CFunction fast_copy (CFunction::Make(FastCopy));
598607
599608void Fill (const FunctionCallbackInfo<Value>& args) {
600609 Environment* env = Environment::GetCurrent (args);
@@ -731,7 +740,7 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
731740}
732741
733742uint32_t FastByteLengthUtf8 (Local<Value> receiver,
734- const v8:: FastOneByteString& source) {
743+ const FastOneByteString& source) {
735744 // For short inputs, the function call overhead to simdutf is maybe
736745 // not worth it, reserve simdutf for long strings.
737746 if (source.length > 128 ) {
@@ -773,8 +782,7 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
773782 return answer;
774783}
775784
776- static v8::CFunction fast_byte_length_utf8 (
777- v8::CFunction::Make (FastByteLengthUtf8));
785+ static CFunction fast_byte_length_utf8 (CFunction::Make(FastByteLengthUtf8));
778786
779787// Normalize val to be an integer in the range of [1, -1] since
780788// implementations of memcmp() can vary by platform.
@@ -837,39 +845,39 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
837845 args.GetReturnValue ().Set (val);
838846}
839847
848+ int32_t CompareImpl (Local<Value> a_obj, Local<Value> b_obj) {
849+ ArrayBufferViewContents<char > a (a_obj);
850+ ArrayBufferViewContents<char > b (b_obj);
851+
852+ size_t cmp_length = std::min (a.length (), b.length ());
853+
854+ return normalizeCompareVal (
855+ cmp_length > 0 ? memcmp (a.data (), b.data (), cmp_length) : 0 ,
856+ a.length (),
857+ b.length ());
858+ }
859+
840860void Compare (const FunctionCallbackInfo<Value> &args) {
841861 Environment* env = Environment::GetCurrent (args);
842-
843862 THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
844863 THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
845- ArrayBufferViewContents<char > a (args[0 ]);
846- ArrayBufferViewContents<char > b (args[1 ]);
847864
848- size_t cmp_length = std::min (a. length (), b. length () );
865+ int val = CompareImpl (args[ 0 ], args[ 1 ] );
849866
850- int val = normalizeCompareVal (cmp_length > 0 ?
851- memcmp (a.data (), b.data (), cmp_length) : 0 ,
852- a.length (), b.length ());
853867 args.GetReturnValue ().Set (val);
854868}
855869
856- int32_t FastCompare (v8::Local<v8::Value>,
857- const FastApiTypedArray<uint8_t >& a,
858- const FastApiTypedArray<uint8_t >& b) {
859- uint8_t * data_a;
860- uint8_t * data_b;
861- CHECK (a.getStorageIfAligned (&data_a));
862- CHECK (b.getStorageIfAligned (&data_b));
863-
864- size_t cmp_length = std::min (a.length (), b.length ());
870+ int32_t FastCompare (Local<Value>,
871+ Local<Value> a_obj,
872+ Local<Value> b_obj,
873+ // NOLINTNEXTLINE(runtime/references)
874+ FastApiCallbackOptions& options) {
875+ HandleScope scope (options.isolate );
865876
866- return normalizeCompareVal (
867- cmp_length > 0 ? memcmp (data_a, data_b, cmp_length) : 0 ,
868- a.length (),
869- b.length ());
877+ return CompareImpl (a_obj, b_obj);
870878}
871879
872- static v8:: CFunction fast_compare (v8:: CFunction::Make(FastCompare));
880+ static CFunction fast_compare (CFunction::Make(FastCompare));
873881
874882// Computes the offset for starting an indexOf or lastIndexOf search.
875883// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
@@ -1099,11 +1107,13 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
10991107 result == haystack_length ? -1 : static_cast <int >(result));
11001108}
11011109
1102- int32_t IndexOfNumber (const uint8_t * buffer_data,
1103- size_t buffer_length,
1104- uint32_t needle,
1105- int64_t offset_i64,
1106- bool is_forward) {
1110+ int32_t IndexOfNumberImpl (Local<Value> buffer_obj,
1111+ const uint32_t needle,
1112+ const int64_t offset_i64,
1113+ const bool is_forward) {
1114+ ArrayBufferViewContents<uint8_t > buffer (buffer_obj);
1115+ const uint8_t * buffer_data = buffer.data ();
1116+ const size_t buffer_length = buffer.length ();
11071117 int64_t opt_offset = IndexOfOffset (buffer_length, offset_i64, 1 , is_forward);
11081118 if (opt_offset <= -1 || buffer_length == 0 ) {
11091119 return -1 ;
@@ -1127,29 +1137,28 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11271137 CHECK (args[3 ]->IsBoolean ());
11281138
11291139 THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
1130- ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
11311140
1141+ Local<Value> buffer_obj = args[0 ];
11321142 uint32_t needle = args[1 ].As <Uint32>()->Value ();
11331143 int64_t offset_i64 = args[2 ].As <Integer>()->Value ();
11341144 bool is_forward = args[3 ]->IsTrue ();
11351145
1136- args.GetReturnValue ().Set (IndexOfNumber (
1137- buffer. data (), buffer. length () , needle, offset_i64, is_forward));
1146+ args.GetReturnValue ().Set (
1147+ IndexOfNumberImpl (buffer_obj , needle, offset_i64, is_forward));
11381148}
11391149
1140- int32_t FastIndexOfNumber (v8:: Local<v8:: Value>,
1141- const FastApiTypedArray< uint8_t >& buffer ,
1150+ int32_t FastIndexOfNumber (Local<Value>,
1151+ Local<Value> buffer_obj ,
11421152 uint32_t needle,
11431153 int64_t offset_i64,
1144- bool is_forward) {
1145- uint8_t * buffer_data;
1146- CHECK (buffer. getStorageIfAligned (&buffer_data));
1147- return IndexOfNumber (
1148- buffer_data, buffer. length () , needle, offset_i64, is_forward);
1154+ bool is_forward,
1155+ // NOLINTNEXTLINE(runtime/references)
1156+ FastApiCallbackOptions& options) {
1157+ HandleScope scope (options. isolate );
1158+ return IndexOfNumberImpl (buffer_obj , needle, offset_i64, is_forward);
11491159}
11501160
1151- static v8::CFunction fast_index_of_number (
1152- v8::CFunction::Make (FastIndexOfNumber));
1161+ static CFunction fast_index_of_number (CFunction::Make(FastIndexOfNumber));
11531162
11541163void Swap16 (const FunctionCallbackInfo<Value>& args) {
11551164 Environment* env = Environment::GetCurrent (args);
@@ -1498,29 +1507,30 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
14981507
14991508template <encoding encoding>
15001509uint32_t FastWriteString (Local<Value> receiver,
1501- const v8::FastApiTypedArray< uint8_t >& dst ,
1502- const v8:: FastOneByteString& src,
1510+ Local<Value> dst_obj ,
1511+ const FastOneByteString& src,
15031512 uint32_t offset,
1504- uint32_t max_length) {
1505- uint8_t * dst_data;
1506- CHECK (dst.getStorageIfAligned (&dst_data));
1507- CHECK (offset <= dst.length ());
1508- 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 ());
15091520 TRACK_V8_FAST_API_CALL (" buffer.writeString" );
15101521
15111522 return WriteOneByteString<encoding>(
15121523 src.data ,
15131524 src.length ,
15141525 reinterpret_cast <char *>(dst_data + offset),
1515- std::min<uint32_t >(dst. length () - offset, max_length));
1526+ std::min<uint32_t >(dst_length - offset, max_length));
15161527}
15171528
1518- static v8::CFunction fast_write_string_ascii (
1519- v8::CFunction::Make (FastWriteString<ASCII>));
1520- static v8::CFunction fast_write_string_latin1 (
1521- v8::CFunction::Make (FastWriteString<LATIN1>));
1522- static v8::CFunction fast_write_string_utf8 (
1523- v8::CFunction::Make (FastWriteString<UTF8>));
1529+ static CFunction fast_write_string_ascii (
1530+ CFunction::Make (FastWriteString<ASCII>));
1531+ static CFunction fast_write_string_latin1 (
1532+ CFunction::Make (FastWriteString<LATIN1>));
1533+ static CFunction fast_write_string_utf8 (CFunction::Make(FastWriteString<UTF8>));
15241534
15251535void Initialize (Local<Object> target,
15261536 Local<Value> unused,
0 commit comments