2
2
#include " ada.h"
3
3
#include " env-inl.h"
4
4
#include " node_buffer.h"
5
+ #include " node_debug.h"
5
6
#include " node_errors.h"
6
7
#include " node_external_reference.h"
7
8
#include " simdutf.h"
8
9
#include " string_bytes.h"
10
+ #include " v8-fast-api-calls.h"
9
11
#include " v8.h"
10
12
11
13
#include < cstdint>
@@ -16,7 +18,9 @@ namespace encoding_binding {
16
18
using v8::ArrayBuffer;
17
19
using v8::BackingStore;
18
20
using v8::BackingStoreInitializationMode;
21
+ using v8::CFunction;
19
22
using v8::Context;
23
+ using v8::FastApiCallbackOptions;
20
24
using v8::FunctionCallbackInfo;
21
25
using v8::HandleScope;
22
26
using v8::Isolate;
@@ -113,6 +117,42 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
113
117
binding_data->encode_into_results_buffer_ [1 ] = written;
114
118
}
115
119
120
+ void BindingData::FastEncodeInto (
121
+ Local<Value> receiver,
122
+ Local<Value> source,
123
+ Local<Value> dest,
124
+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
125
+ FastApiCallbackOptions& options) {
126
+ TRACK_V8_FAST_API_CALL (" encoding_binding.encodeInto" );
127
+ CHECK (source->IsString ());
128
+ CHECK (dest->IsUint8Array ());
129
+
130
+ HandleScope scope (options.isolate );
131
+ auto context = options.isolate ->GetCurrentContext ();
132
+ Realm* realm = Realm::GetCurrent (context);
133
+ BindingData* binding_data = realm->GetBindingData <BindingData>();
134
+
135
+ auto source_ = source.As <String>();
136
+ auto dest_ = dest.As <Uint8Array>();
137
+ Local<ArrayBuffer> buf = dest_->Buffer ();
138
+ char * write_result = static_cast <char *>(buf->Data ()) + dest_->ByteOffset ();
139
+ size_t dest_length = dest_->ByteLength ();
140
+
141
+ int nchars;
142
+ int written = source_->WriteUtf8 (
143
+ options.isolate ,
144
+ write_result,
145
+ dest_length,
146
+ &nchars,
147
+ String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
148
+
149
+ binding_data->encode_into_results_buffer_ [0 ] = nchars;
150
+ binding_data->encode_into_results_buffer_ [1 ] = written;
151
+ }
152
+
153
+ static CFunction fast_encode_into_ =
154
+ CFunction::Make (BindingData::FastEncodeInto);
155
+
116
156
// Encode a single string to a UTF-8 Uint8Array (not Buffer).
117
157
// Used in TextEncoder.prototype.encode.
118
158
void BindingData::EncodeUtf8String (const FunctionCallbackInfo<Value>& args) {
@@ -218,7 +258,7 @@ void BindingData::ToUnicode(const FunctionCallbackInfo<Value>& args) {
218
258
void BindingData::CreatePerIsolateProperties (IsolateData* isolate_data,
219
259
Local<ObjectTemplate> target) {
220
260
Isolate* isolate = isolate_data->isolate ();
221
- SetMethod (isolate, target, " encodeInto" , EncodeInto);
261
+ SetFastMethod (isolate, target, " encodeInto" , EncodeInto, &fast_encode_into_ );
222
262
SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
223
263
SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
224
264
SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
@@ -237,6 +277,8 @@ void BindingData::CreatePerContextProperties(Local<Object> target,
237
277
void BindingData::RegisterTimerExternalReferences (
238
278
ExternalReferenceRegistry* registry) {
239
279
registry->Register (EncodeInto);
280
+ registry->Register (FastEncodeInto);
281
+ registry->Register (fast_encode_into_.GetTypeInfo ());
240
282
registry->Register (EncodeUtf8String);
241
283
registry->Register (DecodeUTF8);
242
284
registry->Register (ToASCII);
0 commit comments