@@ -1425,6 +1425,54 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
1425
1425
memcpy (dest, src, bytes_to_copy);
1426
1426
}
1427
1427
1428
+ template <encoding encoding>
1429
+ void SlowWriteString (const FunctionCallbackInfo<Value>& args) {
1430
+ Environment* env = Environment::GetCurrent (args);
1431
+
1432
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
1433
+ SPREAD_BUFFER_ARG (args[0 ], ts_obj);
1434
+
1435
+ THROW_AND_RETURN_IF_NOT_STRING (env, args[1 ], " argument" );
1436
+
1437
+ Local<String> str = args[1 ]->ToString (env->context ()).ToLocalChecked ();
1438
+
1439
+ size_t offset = 0 ;
1440
+ size_t max_length = 0 ;
1441
+
1442
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[2 ], 0 , &offset));
1443
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[3 ], ts_obj_length - offset,
1444
+ &max_length));
1445
+
1446
+ max_length = std::min (ts_obj_length - offset, max_length);
1447
+
1448
+ if (max_length == 0 )
1449
+ return args.GetReturnValue ().Set (0 );
1450
+
1451
+ uint32_t written = StringBytes::Write (
1452
+ env->isolate (), ts_obj_data + offset, max_length, str, encoding);
1453
+ args.GetReturnValue ().Set (written);
1454
+ }
1455
+
1456
+ uint32_t FastWriteString (Local<Value> receiver,
1457
+ const v8::FastApiTypedArray<uint8_t >& dst,
1458
+ const v8::FastOneByteString& src,
1459
+ uint32_t offset,
1460
+ uint32_t max_length) {
1461
+ uint8_t * dst_data;
1462
+ CHECK (dst.getStorageIfAligned (&dst_data));
1463
+ CHECK (offset <= dst.length ());
1464
+ CHECK (dst.length () <= std::numeric_limits<uint32_t >::max ());
1465
+
1466
+ max_length = std::min (static_cast <uint32_t >(dst.length ()) - offset, max_length);
1467
+
1468
+ memcpy (dst_data, src.data , max_length);
1469
+
1470
+ return max_length;
1471
+ }
1472
+
1473
+ static v8::CFunction fast_write_string (
1474
+ v8::CFunction::Make (FastWriteString));
1475
+
1428
1476
void Initialize (Local<Object> target,
1429
1477
Local<Value> unused,
1430
1478
Local<Context> context,
@@ -1486,13 +1534,26 @@ void Initialize(Local<Object> target,
1486
1534
SetMethodNoSideEffect (context, target, " ucs2Slice" , StringSlice<UCS2>);
1487
1535
SetMethodNoSideEffect (context, target, " utf8Slice" , StringSlice<UTF8>);
1488
1536
1489
- SetMethod (context, target, " asciiWrite" , StringWrite<ASCII>);
1490
1537
SetMethod (context, target, " base64Write" , StringWrite<BASE64>);
1491
1538
SetMethod (context, target, " base64urlWrite" , StringWrite<BASE64URL>);
1492
- SetMethod (context, target, " latin1Write" , StringWrite<LATIN1>);
1493
1539
SetMethod (context, target, " hexWrite" , StringWrite<HEX>);
1494
1540
SetMethod (context, target, " ucs2Write" , StringWrite<UCS2>);
1495
- SetMethod (context, target, " utf8Write" , StringWrite<UTF8>);
1541
+
1542
+ SetFastMethod (context,
1543
+ target,
1544
+ " asciiWriteStatic" ,
1545
+ SlowWriteString<ASCII>,
1546
+ &fast_write_string);
1547
+ SetFastMethod (context,
1548
+ target,
1549
+ " latin1WriteStatic" ,
1550
+ SlowWriteString<LATIN1>,
1551
+ &fast_write_string);
1552
+ SetFastMethod (context,
1553
+ target,
1554
+ " utf8WriteStatic" ,
1555
+ SlowWriteString<UTF8>,
1556
+ &fast_write_string);
1496
1557
1497
1558
SetMethod (context, target, " getZeroFillToggle" , GetZeroFillToggle);
1498
1559
}
@@ -1535,6 +1596,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1535
1596
registry->Register (StringSlice<UCS2>);
1536
1597
registry->Register (StringSlice<UTF8>);
1537
1598
1599
+ registry->Register (SlowWriteString<ASCII>);
1600
+ registry->Register (fast_write_string.GetTypeInfo ());
1601
+ registry->Register (FastWriteString);
1538
1602
registry->Register (StringWrite<ASCII>);
1539
1603
registry->Register (StringWrite<BASE64>);
1540
1604
registry->Register (StringWrite<BASE64URL>);
0 commit comments