@@ -1425,6 +1425,59 @@ 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
+ if (offset > ts_obj_length) {
1444
+ return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS (
1445
+ env, " \" offset\" is outside of buffer bounds" );
1446
+ }
1447
+
1448
+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[3 ], ts_obj_length - offset,
1449
+ &max_length));
1450
+
1451
+ max_length = std::min (ts_obj_length - offset, max_length);
1452
+
1453
+ if (max_length == 0 )
1454
+ return args.GetReturnValue ().Set (0 );
1455
+
1456
+ uint32_t written = StringBytes::Write (
1457
+ env->isolate (), ts_obj_data + offset, max_length, str, encoding);
1458
+ args.GetReturnValue ().Set (written);
1459
+ }
1460
+
1461
+ uint32_t FastWriteString (Local<Value> receiver,
1462
+ const v8::FastApiTypedArray<uint8_t >& dst,
1463
+ const v8::FastOneByteString& src,
1464
+ uint32_t offset,
1465
+ uint32_t max_length) {
1466
+ uint8_t * dst_data;
1467
+ CHECK (dst.getStorageIfAligned (&dst_data));
1468
+
1469
+ if (offset > dst.length ()) {
1470
+ // TODO: Throw "\"offset\" is outside of buffer bound
1471
+ }
1472
+
1473
+ memcpy (dst_data, src.data , max_length);
1474
+
1475
+ return max_length;
1476
+ }
1477
+
1478
+ static v8::CFunction fast_write_string (
1479
+ v8::CFunction::Make (FastWriteString));
1480
+
1428
1481
void Initialize (Local<Object> target,
1429
1482
Local<Value> unused,
1430
1483
Local<Context> context,
@@ -1494,6 +1547,22 @@ void Initialize(Local<Object> target,
1494
1547
SetMethod (context, target, " ucs2Write" , StringWrite<UCS2>);
1495
1548
SetMethod (context, target, " utf8Write" , StringWrite<UTF8>);
1496
1549
1550
+ SetFastMethod (context,
1551
+ target,
1552
+ " asciiWriteStatic" ,
1553
+ SlowWriteString<ASCII>,
1554
+ &fast_write_string);
1555
+ SetFastMethod (context,
1556
+ target,
1557
+ " latin1WriteStatic" ,
1558
+ SlowWriteString<LATIN1>,
1559
+ &fast_write_string);
1560
+ SetFastMethod (context,
1561
+ target,
1562
+ " utf8WriteStatic" ,
1563
+ SlowWriteString<UTF8>,
1564
+ &fast_write_string);
1565
+
1497
1566
SetMethod (context, target, " getZeroFillToggle" , GetZeroFillToggle);
1498
1567
}
1499
1568
@@ -1535,6 +1604,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1535
1604
registry->Register (StringSlice<UCS2>);
1536
1605
registry->Register (StringSlice<UTF8>);
1537
1606
1607
+ registry->Register (SlowWriteString<ASCII>);
1608
+ registry->Register (fast_write_string.GetTypeInfo ());
1609
+ registry->Register (FastWriteString);
1538
1610
registry->Register (StringWrite<ASCII>);
1539
1611
registry->Register (StringWrite<BASE64>);
1540
1612
registry->Register (StringWrite<BASE64URL>);
0 commit comments