@@ -1425,6 +1425,59 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
14251425 memcpy (dest, src, bytes_to_copy);
14261426}
14271427
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+
14281481void Initialize (Local<Object> target,
14291482 Local<Value> unused,
14301483 Local<Context> context,
@@ -1494,6 +1547,22 @@ void Initialize(Local<Object> target,
14941547 SetMethod (context, target, " ucs2Write" , StringWrite<UCS2>);
14951548 SetMethod (context, target, " utf8Write" , StringWrite<UTF8>);
14961549
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+
14971566 SetMethod (context, target, " getZeroFillToggle" , GetZeroFillToggle);
14981567}
14991568
@@ -1535,6 +1604,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
15351604 registry->Register (StringSlice<UCS2>);
15361605 registry->Register (StringSlice<UTF8>);
15371606
1607+ registry->Register (SlowWriteString<ASCII>);
1608+ registry->Register (fast_write_string.GetTypeInfo ());
1609+ registry->Register (FastWriteString);
15381610 registry->Register (StringWrite<ASCII>);
15391611 registry->Register (StringWrite<BASE64>);
15401612 registry->Register (StringWrite<BASE64URL>);
0 commit comments