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 { \
@@ -61,7 +69,6 @@ using v8::BackingStore;
6169using v8::BackingStoreInitializationMode;
6270using v8::Context;
6371using v8::EscapableHandleScope;
64- using v8::FastApiTypedArray;
6572using v8::FunctionCallbackInfo;
6673using v8::Global;
6774using v8::HandleScope;
@@ -578,19 +585,17 @@ void SlowCopy(const FunctionCallbackInfo<Value>& args) {
578585
579586// Assume caller has properly validated args.
580587uint32_t FastCopy (Local<Value> receiver,
581- const v8::FastApiTypedArray< uint8_t >& source ,
582- const v8::FastApiTypedArray< uint8_t >& target ,
588+ Local<Value> source_obj ,
589+ Local<Value> target_obj ,
583590 uint32_t target_start,
584591 uint32_t source_start,
585- uint32_t to_copy) {
586- uint8_t * source_data;
587- CHECK (source.getStorageIfAligned (&source_data));
588-
589- uint8_t * target_data;
590- CHECK (target.getStorageIfAligned (&target_data));
591-
592- memmove (target_data + target_start, source_data + source_start, to_copy);
592+ uint32_t to_copy,
593+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
594+ v8::FastApiCallbackOptions& options) {
595+ ArrayBufferViewContents<char > source (source_obj);
596+ SPREAD_BUFFER_ARG (target_obj, target);
593597
598+ memmove (target_data + target_start, source.data () + source_start, to_copy);
594599 return to_copy;
595600}
596601
@@ -853,24 +858,6 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
853858 args.GetReturnValue ().Set (val);
854859}
855860
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 ());
865-
866- return normalizeCompareVal (
867- cmp_length > 0 ? memcmp (data_a, data_b, cmp_length) : 0 ,
868- a.length (),
869- b.length ());
870- }
871-
872- static v8::CFunction fast_compare (v8::CFunction::Make(FastCompare));
873-
874861// Computes the offset for starting an indexOf or lastIndexOf search.
875862// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
876863// or -1 to signal that there is no possible match.
@@ -1121,7 +1108,7 @@ int32_t IndexOfNumber(const uint8_t* buffer_data,
11211108 return ptr != nullptr ? static_cast <int32_t >(ptr_uint8 - buffer_data) : -1 ;
11221109}
11231110
1124- void SlowIndexOfNumber (const FunctionCallbackInfo<Value>& args) {
1111+ void IndexOfNumber (const FunctionCallbackInfo<Value>& args) {
11251112 CHECK (args[1 ]->IsUint32 ());
11261113 CHECK (args[2 ]->IsNumber ());
11271114 CHECK (args[3 ]->IsBoolean ());
@@ -1137,20 +1124,6 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
11371124 buffer.data (), buffer.length (), needle, offset_i64, is_forward));
11381125}
11391126
1140- int32_t FastIndexOfNumber (v8::Local<v8::Value>,
1141- const FastApiTypedArray<uint8_t >& buffer,
1142- uint32_t needle,
1143- 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);
1149- }
1150-
1151- static v8::CFunction fast_index_of_number (
1152- v8::CFunction::Make (FastIndexOfNumber));
1153-
11541127void Swap16 (const FunctionCallbackInfo<Value>& args) {
11551128 Environment* env = Environment::GetCurrent (args);
11561129 THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
@@ -1498,21 +1471,25 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
14981471
14991472template <encoding encoding>
15001473uint32_t FastWriteString (Local<Value> receiver,
1501- const v8::FastApiTypedArray< uint8_t >& dst,
1474+ Local<Value> dst,
15021475 const v8::FastOneByteString& src,
15031476 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 ());
1477+ uint32_t max_length,
1478+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
1479+ v8::FastApiCallbackOptions& options) {
1480+ THROW_AND_RETURN_VAL_UNLESS_BUFFER (options.isolate , dst, " dst" , 0 );
1481+ SPREAD_BUFFER_ARG (dst, dst_buffer);
1482+ CHECK (dst_buffer_length <=
1483+ static_cast <size_t >(std::numeric_limits<uint32_t >::max ()));
1484+ uint32_t dst_size = static_cast <uint32_t >(dst_buffer_length);
1485+ CHECK (offset <= dst_size);
15091486 TRACK_V8_FAST_API_CALL (" buffer.writeString" );
15101487
15111488 return WriteOneByteString<encoding>(
15121489 src.data ,
15131490 src.length ,
1514- reinterpret_cast <char *>(dst_data + offset),
1515- std::min<uint32_t >(dst. length () - offset, max_length));
1491+ reinterpret_cast <char *>(dst_buffer_data + offset),
1492+ std::min<uint32_t >(dst_size - offset, max_length));
15161493}
15171494
15181495static v8::CFunction fast_write_string_ascii (
@@ -1539,16 +1516,12 @@ void Initialize(Local<Object> target,
15391516 " byteLengthUtf8" ,
15401517 SlowByteLengthUtf8,
15411518 &fast_byte_length_utf8);
1542- SetFastMethod (context, target, " copy" , SlowCopy, &fast_copy );
1543- SetFastMethodNoSideEffect (context, target, " compare" , Compare, &fast_compare );
1519+ SetMethod (context, target, " copy" , SlowCopy);
1520+ SetMethod (context, target, " compare" , Compare);
15441521 SetMethodNoSideEffect (context, target, " compareOffset" , CompareOffset);
15451522 SetMethod (context, target, " fill" , Fill);
15461523 SetMethodNoSideEffect (context, target, " indexOfBuffer" , IndexOfBuffer);
1547- SetFastMethodNoSideEffect (context,
1548- target,
1549- " indexOfNumber" ,
1550- SlowIndexOfNumber,
1551- &fast_index_of_number);
1524+ SetMethodNoSideEffect (context, target, " indexOfNumber" , IndexOfNumber);
15521525 SetMethodNoSideEffect (context, target, " indexOfString" , IndexOfString);
15531526
15541527 SetMethod (context, target, " detachArrayBuffer" , DetachArrayBuffer);
@@ -1618,14 +1591,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
16181591 registry->Register (fast_copy.GetTypeInfo ());
16191592 registry->Register (FastCopy);
16201593 registry->Register (Compare);
1621- registry->Register (FastCompare);
1622- registry->Register (fast_compare.GetTypeInfo ());
16231594 registry->Register (CompareOffset);
16241595 registry->Register (Fill);
16251596 registry->Register (IndexOfBuffer);
1626- registry->Register (SlowIndexOfNumber);
1627- registry->Register (FastIndexOfNumber);
1628- registry->Register (fast_index_of_number.GetTypeInfo ());
1597+ registry->Register (IndexOfNumber);
16291598 registry->Register (IndexOfString);
16301599
16311600 registry->Register (Swap16);
0 commit comments