@@ -1442,6 +1442,52 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
14421442 memcpy (dest, src, bytes_to_copy);
14431443}
14441444
1445+ template <encoding encoding>
1446+ void SlowWriteString (const FunctionCallbackInfo<Value>& args) {
1447+ Environment* env = Environment::GetCurrent (args);
1448+
1449+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
1450+ SPREAD_BUFFER_ARG (args[0 ], ts_obj);
1451+
1452+ THROW_AND_RETURN_IF_NOT_STRING (env, args[1 ], " argument" );
1453+
1454+ Local<String> str = args[1 ]->ToString (env->context ()).ToLocalChecked ();
1455+
1456+ size_t offset = 0 ;
1457+ size_t max_length = 0 ;
1458+
1459+ THROW_AND_RETURN_IF_OOB (ParseArrayIndex (env, args[2 ], 0 , &offset));
1460+ THROW_AND_RETURN_IF_OOB (
1461+ ParseArrayIndex (env, args[3 ], ts_obj_length - offset, &max_length));
1462+
1463+ max_length = std::min (ts_obj_length - offset, max_length);
1464+
1465+ if (max_length == 0 ) return args.GetReturnValue ().Set (0 );
1466+
1467+ uint32_t written = StringBytes::Write (
1468+ env->isolate (), ts_obj_data + offset, max_length, str, encoding);
1469+ args.GetReturnValue ().Set (written);
1470+ }
1471+
1472+ uint32_t FastWriteString (Local<Value> receiver,
1473+ const v8::FastApiTypedArray<uint8_t >& dst,
1474+ const v8::FastOneByteString& src,
1475+ uint32_t offset,
1476+ uint32_t max_length) {
1477+ uint8_t * dst_data;
1478+ CHECK (dst.getStorageIfAligned (&dst_data));
1479+ CHECK (offset <= dst.length ());
1480+ CHECK (dst.length () - offset <= std::numeric_limits<uint32_t >::max ());
1481+
1482+ max_length = std::min<uint32_t >(dst.length () - offset, max_length);
1483+
1484+ memcpy (dst_data, src.data , max_length);
1485+
1486+ return max_length;
1487+ }
1488+
1489+ static v8::CFunction fast_write_string (v8::CFunction::Make(FastWriteString));
1490+
14451491void Initialize (Local<Object> target,
14461492 Local<Value> unused,
14471493 Local<Context> context,
@@ -1502,13 +1548,26 @@ void Initialize(Local<Object> target,
15021548 SetMethodNoSideEffect (context, target, " ucs2Slice" , StringSlice<UCS2>);
15031549 SetMethodNoSideEffect (context, target, " utf8Slice" , StringSlice<UTF8>);
15041550
1505- SetMethod (context, target, " asciiWrite" , StringWrite<ASCII>);
15061551 SetMethod (context, target, " base64Write" , StringWrite<BASE64>);
15071552 SetMethod (context, target, " base64urlWrite" , StringWrite<BASE64URL>);
1508- SetMethod (context, target, " latin1Write" , StringWrite<LATIN1>);
15091553 SetMethod (context, target, " hexWrite" , StringWrite<HEX>);
15101554 SetMethod (context, target, " ucs2Write" , StringWrite<UCS2>);
1511- SetMethod (context, target, " utf8Write" , StringWrite<UTF8>);
1555+
1556+ SetFastMethod (context,
1557+ target,
1558+ " asciiWriteStatic" ,
1559+ SlowWriteString<ASCII>,
1560+ &fast_write_string);
1561+ SetFastMethod (context,
1562+ target,
1563+ " latin1WriteStatic" ,
1564+ SlowWriteString<LATIN1>,
1565+ &fast_write_string);
1566+ SetFastMethod (context,
1567+ target,
1568+ " utf8WriteStatic" ,
1569+ SlowWriteString<UTF8>,
1570+ &fast_write_string);
15121571
15131572 SetMethod (context, target, " getZeroFillToggle" , GetZeroFillToggle);
15141573}
@@ -1550,6 +1609,11 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
15501609 registry->Register (StringSlice<UCS2>);
15511610 registry->Register (StringSlice<UTF8>);
15521611
1612+ registry->Register (SlowWriteString<ASCII>);
1613+ registry->Register (SlowWriteString<LATIN1>);
1614+ registry->Register (SlowWriteString<UTF8>);
1615+ registry->Register (fast_write_string.GetTypeInfo ());
1616+ registry->Register (FastWriteString);
15531617 registry->Register (StringWrite<ASCII>);
15541618 registry->Register (StringWrite<BASE64>);
15551619 registry->Register (StringWrite<BASE64URL>);
0 commit comments