@@ -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;
@@ -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.
553566void 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.
573579uint32_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
592596void Fill (const FunctionCallbackInfo<Value>& args) {
593597 Environment* env = Environment::GetCurrent (args);
@@ -731,7 +735,7 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
731735uint32_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+
850865void 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
11641168void Swap16 (const FunctionCallbackInfo<Value>& args) {
11651169 Environment* env = Environment::GetCurrent (args);
@@ -1503,29 +1507,31 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
15031507
15041508template <encoding encoding>
15051509uint32_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
15301536void Initialize (Local<Object> target,
15311537 Local<Value> unused,
0 commit comments