4242#include " nbytes.h"
4343
4444#define THROW_AND_RETURN_UNLESS_BUFFER (env, obj ) \
45- THROW_AND_RETURN_IF_NOT_BUFFER (env, obj, " argument" ) \
45+ THROW_AND_RETURN_IF_NOT_BUFFER (env, obj, " argument" )
46+
47+ #define THROW_AND_RETURN_VAL_UNLESS_BUFFER (isolate, val, prefix, retval ) \
48+ do { \
49+ if (!Buffer::HasInstance (val)) { \
50+ node::THROW_ERR_INVALID_ARG_TYPE (isolate, prefix " must be a buffer" ); \
51+ return retval; \
52+ } \
53+ } while (0 )
4654
4755#define THROW_AND_RETURN_IF_OOB (r ) \
4856 do { \
@@ -60,7 +68,6 @@ using v8::ArrayBufferView;
6068using v8::BackingStore;
6169using v8::Context;
6270using v8::EscapableHandleScope;
63- using v8::FastApiTypedArray;
6471using v8::FunctionCallbackInfo;
6572using v8::Global;
6673using v8::HandleScope;
@@ -582,19 +589,17 @@ void SlowCopy(const FunctionCallbackInfo<Value>& args) {
582589
583590// Assume caller has properly validated args.
584591uint32_t FastCopy (Local<Value> receiver,
585- const v8::FastApiTypedArray< uint8_t >& source ,
586- const v8::FastApiTypedArray< uint8_t >& target ,
592+ Local<Value> source_obj ,
593+ Local<Value> target_obj ,
587594 uint32_t target_start,
588595 uint32_t source_start,
589- uint32_t to_copy) {
590- uint8_t * source_data;
591- CHECK (source.getStorageIfAligned (&source_data));
592-
593- uint8_t * target_data;
594- CHECK (target.getStorageIfAligned (&target_data));
595-
596- memmove (target_data + target_start, source_data + source_start, to_copy);
596+ uint32_t to_copy,
597+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
598+ v8::FastApiCallbackOptions& options) {
599+ ArrayBufferViewContents<char > source (source_obj);
600+ SPREAD_BUFFER_ARG (target_obj, target);
597601
602+ memmove (target_data + target_start, source.data () + source_start, to_copy);
598603 return to_copy;
599604}
600605
@@ -857,24 +862,6 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
857862 args.GetReturnValue ().Set (val);
858863}
859864
860- int32_t FastCompare (v8::Local<v8::Value>,
861- const FastApiTypedArray<uint8_t >& a,
862- const FastApiTypedArray<uint8_t >& b) {
863- uint8_t * data_a;
864- uint8_t * data_b;
865- CHECK (a.getStorageIfAligned (&data_a));
866- CHECK (b.getStorageIfAligned (&data_b));
867-
868- size_t cmp_length = std::min (a.length (), b.length ());
869-
870- return normalizeCompareVal (
871- cmp_length > 0 ? memcmp (data_a, data_b, cmp_length) : 0 ,
872- a.length (),
873- b.length ());
874- }
875-
876- static v8::CFunction fast_compare (v8::CFunction::Make(FastCompare));
877-
878865// Computes the offset for starting an indexOf or lastIndexOf search.
879866// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
880867// or -1 to signal that there is no possible match.
@@ -1125,7 +1112,7 @@ int32_t IndexOfNumber(const uint8_t* buffer_data,
11251112 return ptr != nullptr ? static_cast <int32_t >(ptr_uint8 - buffer_data) : -1 ;
11261113}
11271114
1128- void SlowIndexOfNumber (const FunctionCallbackInfo<Value>& args) {
1115+ void IndexOfNumber (const FunctionCallbackInfo<Value>& args) {
11291116 CHECK (args[1 ]->IsUint32 ());
11301117 CHECK (args[2 ]->IsNumber ());
11311118 CHECK (args[3 ]->IsBoolean ());
@@ -1141,20 +1128,6 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11411128 buffer.data (), buffer.length (), needle, offset_i64, is_forward));
11421129}
11431130
1144- int32_t FastIndexOfNumber (v8::Local<v8::Value>,
1145- const FastApiTypedArray<uint8_t >& buffer,
1146- uint32_t needle,
1147- int64_t offset_i64,
1148- bool is_forward) {
1149- uint8_t * buffer_data;
1150- CHECK (buffer.getStorageIfAligned (&buffer_data));
1151- return IndexOfNumber (
1152- buffer_data, buffer.length (), needle, offset_i64, is_forward);
1153- }
1154-
1155- static v8::CFunction fast_index_of_number (
1156- v8::CFunction::Make (FastIndexOfNumber));
1157-
11581131void Swap16 (const FunctionCallbackInfo<Value>& args) {
11591132 Environment* env = Environment::GetCurrent (args);
11601133 THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
@@ -1502,21 +1475,25 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
15021475
15031476template <encoding encoding>
15041477uint32_t FastWriteString (Local<Value> receiver,
1505- const v8::FastApiTypedArray< uint8_t >& dst,
1478+ Local<Value> dst,
15061479 const v8::FastOneByteString& src,
15071480 uint32_t offset,
1508- uint32_t max_length) {
1509- uint8_t * dst_data;
1510- CHECK (dst.getStorageIfAligned (&dst_data));
1511- CHECK (offset <= dst.length ());
1512- CHECK (dst.length () - offset <= std::numeric_limits<uint32_t >::max ());
1481+ uint32_t max_length,
1482+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
1483+ v8::FastApiCallbackOptions& options) {
1484+ THROW_AND_RETURN_VAL_UNLESS_BUFFER (options.isolate , dst, " dst" , 0 );
1485+ SPREAD_BUFFER_ARG (dst, dst_buffer);
1486+ CHECK (dst_buffer_length <=
1487+ static_cast <size_t >(std::numeric_limits<uint32_t >::max ()));
1488+ uint32_t dst_size = static_cast <uint32_t >(dst_buffer_length);
1489+ CHECK (offset <= dst_size);
15131490 TRACK_V8_FAST_API_CALL (" buffer.writeString" );
15141491
15151492 return WriteOneByteString<encoding>(
15161493 src.data ,
15171494 src.length ,
1518- reinterpret_cast <char *>(dst_data + offset),
1519- std::min<uint32_t >(dst. length () - offset, max_length));
1495+ reinterpret_cast <char *>(dst_buffer_data + offset),
1496+ std::min<uint32_t >(dst_size - offset, max_length));
15201497}
15211498
15221499static v8::CFunction fast_write_string_ascii (
@@ -1543,16 +1520,12 @@ void Initialize(Local<Object> target,
15431520 " byteLengthUtf8" ,
15441521 SlowByteLengthUtf8,
15451522 &fast_byte_length_utf8);
1546- SetFastMethod (context, target, " copy" , SlowCopy, &fast_copy );
1547- SetFastMethodNoSideEffect (context, target, " compare" , Compare, &fast_compare );
1523+ SetMethod (context, target, " copy" , SlowCopy);
1524+ SetMethod (context, target, " compare" , Compare);
15481525 SetMethodNoSideEffect (context, target, " compareOffset" , CompareOffset);
15491526 SetMethod (context, target, " fill" , Fill);
15501527 SetMethodNoSideEffect (context, target, " indexOfBuffer" , IndexOfBuffer);
1551- SetFastMethodNoSideEffect (context,
1552- target,
1553- " indexOfNumber" ,
1554- SlowIndexOfNumber,
1555- &fast_index_of_number);
1528+ SetMethodNoSideEffect (context, target, " indexOfNumber" , IndexOfNumber);
15561529 SetMethodNoSideEffect (context, target, " indexOfString" , IndexOfString);
15571530
15581531 SetMethod (context, target, " detachArrayBuffer" , DetachArrayBuffer);
@@ -1622,14 +1595,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
16221595 registry->Register (fast_copy.GetTypeInfo ());
16231596 registry->Register (FastCopy);
16241597 registry->Register (Compare);
1625- registry->Register (FastCompare);
1626- registry->Register (fast_compare.GetTypeInfo ());
16271598 registry->Register (CompareOffset);
16281599 registry->Register (Fill);
16291600 registry->Register (IndexOfBuffer);
1630- registry->Register (SlowIndexOfNumber);
1631- registry->Register (FastIndexOfNumber);
1632- registry->Register (fast_index_of_number.GetTypeInfo ());
1601+ registry->Register (IndexOfNumber);
16331602 registry->Register (IndexOfString);
16341603
16351604 registry->Register (Swap16);
0 commit comments