42
42
#include " nbytes.h"
43
43
44
44
#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 )
46
54
47
55
#define THROW_AND_RETURN_IF_OOB (r ) \
48
56
do { \
@@ -60,7 +68,6 @@ using v8::ArrayBufferView;
60
68
using v8::BackingStore;
61
69
using v8::Context;
62
70
using v8::EscapableHandleScope;
63
- using v8::FastApiTypedArray;
64
71
using v8::FunctionCallbackInfo;
65
72
using v8::Global;
66
73
using v8::HandleScope;
@@ -582,19 +589,17 @@ void SlowCopy(const FunctionCallbackInfo<Value>& args) {
582
589
583
590
// Assume caller has properly validated args.
584
591
uint32_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 ,
587
594
uint32_t target_start,
588
595
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);
597
601
602
+ memmove (target_data + target_start, source.data () + source_start, to_copy);
598
603
return to_copy;
599
604
}
600
605
@@ -857,24 +862,6 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
857
862
args.GetReturnValue ().Set (val);
858
863
}
859
864
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
-
878
865
// Computes the offset for starting an indexOf or lastIndexOf search.
879
866
// Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
880
867
// or -1 to signal that there is no possible match.
@@ -1125,7 +1112,7 @@ int32_t IndexOfNumber(const uint8_t* buffer_data,
1125
1112
return ptr != nullptr ? static_cast <int32_t >(ptr_uint8 - buffer_data) : -1 ;
1126
1113
}
1127
1114
1128
- void SlowIndexOfNumber (const FunctionCallbackInfo<Value>& args) {
1115
+ void IndexOfNumber (const FunctionCallbackInfo<Value>& args) {
1129
1116
CHECK (args[1 ]->IsUint32 ());
1130
1117
CHECK (args[2 ]->IsNumber ());
1131
1118
CHECK (args[3 ]->IsBoolean ());
@@ -1141,20 +1128,6 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
1141
1128
buffer.data (), buffer.length (), needle, offset_i64, is_forward));
1142
1129
}
1143
1130
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
-
1158
1131
void Swap16 (const FunctionCallbackInfo<Value>& args) {
1159
1132
Environment* env = Environment::GetCurrent (args);
1160
1133
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
@@ -1502,21 +1475,25 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
1502
1475
1503
1476
template <encoding encoding>
1504
1477
uint32_t FastWriteString (Local<Value> receiver,
1505
- const v8::FastApiTypedArray< uint8_t >& dst,
1478
+ Local<Value> dst,
1506
1479
const v8::FastOneByteString& src,
1507
1480
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);
1513
1490
TRACK_V8_FAST_API_CALL (" buffer.writeString" );
1514
1491
1515
1492
return WriteOneByteString<encoding>(
1516
1493
src.data ,
1517
1494
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));
1520
1497
}
1521
1498
1522
1499
static v8::CFunction fast_write_string_ascii (
@@ -1543,16 +1520,12 @@ void Initialize(Local<Object> target,
1543
1520
" byteLengthUtf8" ,
1544
1521
SlowByteLengthUtf8,
1545
1522
&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);
1548
1525
SetMethodNoSideEffect (context, target, " compareOffset" , CompareOffset);
1549
1526
SetMethod (context, target, " fill" , Fill);
1550
1527
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);
1556
1529
SetMethodNoSideEffect (context, target, " indexOfString" , IndexOfString);
1557
1530
1558
1531
SetMethod (context, target, " detachArrayBuffer" , DetachArrayBuffer);
@@ -1622,14 +1595,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1622
1595
registry->Register (fast_copy.GetTypeInfo ());
1623
1596
registry->Register (FastCopy);
1624
1597
registry->Register (Compare);
1625
- registry->Register (FastCompare);
1626
- registry->Register (fast_compare.GetTypeInfo ());
1627
1598
registry->Register (CompareOffset);
1628
1599
registry->Register (Fill);
1629
1600
registry->Register (IndexOfBuffer);
1630
- registry->Register (SlowIndexOfNumber);
1631
- registry->Register (FastIndexOfNumber);
1632
- registry->Register (fast_index_of_number.GetTypeInfo ());
1601
+ registry->Register (IndexOfNumber);
1633
1602
registry->Register (IndexOfString);
1634
1603
1635
1604
registry->Register (Swap16);
0 commit comments