@@ -569,7 +569,7 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
569
569
}
570
570
571
571
// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
572
- void Copy (const FunctionCallbackInfo<Value> &args) {
572
+ void SlowCopy (const FunctionCallbackInfo<Value> &args) {
573
573
Environment* env = Environment::GetCurrent (args);
574
574
575
575
THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
@@ -606,6 +606,37 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
606
606
args.GetReturnValue ().Set (to_copy);
607
607
}
608
608
609
+ uint32_t FastCopy (Local<Value> receiver,
610
+ const v8::FastApiArrayBufferView& source,
611
+ v8::FastApiArrayBufferView& target,
612
+ uint32_t target_start,
613
+ uint32_t source_start,
614
+ uint32_t source_end,
615
+ v8::FastApiCallbackOptions& options) {
616
+ // Copy 0 bytes; we're done
617
+ if (target_start >= target.byte_length || source_start >= source_end)
618
+ return 0 ;
619
+
620
+ if (source_start > source.byte_length ) {
621
+ options.fallback = true ;
622
+ return 0 ;
623
+ }
624
+
625
+ if (source_end - source_start > target.byte_length - target_start)
626
+ source_end = source_start + target.byte_length - target_start;
627
+
628
+ uint32_t to_copy = std::min<size_t >(
629
+ std::min<size_t >(source_end - source_start, target.byte_length - target_start),
630
+ source.byte_length - source_start);
631
+
632
+ memmove (reinterpret_cast <char *>(target.data ) + target_start,
633
+ reinterpret_cast <char *>(source.data ) + source_start, to_copy);
634
+
635
+ return to_copy;
636
+ }
637
+
638
+ static v8::CFunction fast_copy (
639
+ v8::CFunction::Make (FastCopy));
609
640
610
641
void Fill (const FunctionCallbackInfo<Value>& args) {
611
642
Environment* env = Environment::GetCurrent (args);
@@ -1275,7 +1306,7 @@ void Initialize(Local<Object> target,
1275
1306
" byteLengthUtf8" ,
1276
1307
SlowByteLengthUtf8,
1277
1308
&fast_byte_length_utf8);
1278
- SetMethod (context, target, " copy" , Copy );
1309
+ SetFastMethod (context, target, " copy" , SlowCopy, &fast_copy );
1279
1310
SetMethodNoSideEffect (context, target, " compare" , Compare);
1280
1311
SetMethodNoSideEffect (context, target, " compareOffset" , CompareOffset);
1281
1312
SetMethod (context, target, " fill" , Fill);
@@ -1335,6 +1366,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1335
1366
registry->Register (fast_byte_length_utf8.GetTypeInfo ());
1336
1367
registry->Register (FastByteLengthUtf8);
1337
1368
registry->Register (Copy);
1369
+ registry->Register (FastCopy);
1370
+ registry->Register (fast_copy.GetTypeInfo ());
1338
1371
registry->Register (Compare);
1339
1372
registry->Register (CompareOffset);
1340
1373
registry->Register (Fill);
0 commit comments