FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/encoding_binding.cc at buffer-with-resizable-arraybuffer · jasnell/node · GitHub
jasnell
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
6
Star
11
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
encoding_binding.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
259 lines (216 loc) · 8.23 KB
Breadcrumbs
node
/
src
/
encoding_binding.cc
Copy path
File metadata and controls
259 lines (216 loc) · 8.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
include
"
encoding_binding.h
"
#
include
"
ada.h
"
#
include
"
env-inl.h
"
#
include
"
node_errors.h
"
#
include
"
node_external_reference.h
"
#
include
"
simdutf.h
"
#
include
"
string_bytes.h
"
#
include
"
v8.h
"
#
include
<
cstdint
>
namespace
node
{
namespace
encoding_binding
{
using
v8::ArrayBuffer;
using
v8::BackingStore;
using
v8::Context;
using
v8::FunctionCallbackInfo;
using
v8::Isolate;
using
v8::Local;
using
v8::MaybeLocal;
using
v8::Object;
using
v8::ObjectTemplate;
using
v8::String;
using
v8::Uint8Array;
using
v8::Value;
void
BindingData::MemoryInfo
(MemoryTracker* tracker)
const
{
tracker->
TrackField
(
"
encode_into_results_buffer
"
,
encode_into_results_buffer_);
}
BindingData::BindingData
(Realm* realm,
v8::Local<v8::Object> object,
InternalFieldInfo* info)
: SnapshotableObject(realm, object, type_int),
encode_into_results_buffer_
(
realm->
isolate
(),
kEncodeIntoResultsLength,
MAYBE_FIELD_PTR(info, encode_into_results_buffer)) {
if
(info ==
nullptr
) {
object
->
Set
(realm->
context
(),
FIXED_ONE_BYTE_STRING
(realm->
isolate
(),
"
encodeIntoResults
"
),
encode_into_results_buffer_.
GetJSArray
())
.
Check
();
}
else
{
encode_into_results_buffer_.
Deserialize
(realm->
context
());
}
encode_into_results_buffer_.
MakeWeak
();
}
bool
BindingData::PrepareForSerialization
(Local<Context> context,
v8::SnapshotCreator* creator) {
DCHECK_NULL
(internal_field_info_);
internal_field_info_ = InternalFieldInfoBase::New<InternalFieldInfo>(
type
());
internal_field_info_->
encode_into_results_buffer
=
encode_into_results_buffer_.
Serialize
(context, creator);
//
Return true because we need to maintain the reference to the binding from
//
JS land.
return
true
;
}
InternalFieldInfoBase*
BindingData::Serialize
(
int
index) {
DCHECK_IS_SNAPSHOT_SLOT
(index);
InternalFieldInfo* info = internal_field_info_;
internal_field_info_ =
nullptr
;
return
info;
}
void
BindingData::Deserialize
(Local<Context> context,
Local<Object> holder,
int
index,
InternalFieldInfoBase* info) {
DCHECK_IS_SNAPSHOT_SLOT
(index);
v8::HandleScope
scope
(context->
GetIsolate
());
Realm* realm =
Realm::GetCurrent
(context);
//
Recreate the buffer in the constructor.
InternalFieldInfo* casted_info =
static_cast
<InternalFieldInfo*>(info);
BindingData* binding =
realm->
AddBindingData
<BindingData>(holder, casted_info);
CHECK_NOT_NULL
(binding);
}
void
BindingData::EncodeInto
(
const
FunctionCallbackInfo<Value>& args) {
CHECK_GE
(args.
Length
(),
2
);
CHECK
(args[
0
]->
IsString
());
CHECK
(args[
1
]->
IsUint8Array
());
Realm* realm =
Realm::GetCurrent
(args);
Isolate* isolate = realm->
isolate
();
BindingData* binding_data = realm->
GetBindingData
<BindingData>();
Local<String> source = args[
0
].
As
<String>();
Local<Uint8Array> dest = args[
1
].
As
<Uint8Array>();
Local<ArrayBuffer> buf = dest->
Buffer
();
char
* write_result =
static_cast
<
char
*>(buf->
Data
()) + dest->
ByteOffset
();
size_t
dest_length = dest->
ByteLength
();
int
nchars;
int
written = source->
WriteUtf8
(
isolate,
write_result,
dest_length,
&nchars,
String::
NO_NULL_TERMINATION
| String::
REPLACE_INVALID_UTF8
);
binding_data->
encode_into_results_buffer_
[
0
] = nchars;
binding_data->
encode_into_results_buffer_
[
1
] = written;
}
//
Encode a single string to a UTF-8 Uint8Array (not Buffer).
//
Used in TextEncoder.prototype.encode.
void
BindingData::EncodeUtf8String
(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
Isolate* isolate = env->
isolate
();
CHECK_GE
(args.
Length
(),
1
);
CHECK
(args[
0
]->
IsString
());
Local<String> str = args[
0
].
As
<String>();
size_t
length = str->
Utf8Length
(isolate);
Local<ArrayBuffer> ab;
{
NoArrayBufferZeroFillScope
no_zero_fill_scope
(env->
isolate_data
());
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore
(isolate, length);
CHECK
(bs);
str->
WriteUtf8
(isolate,
static_cast
<
char
*>(bs->
Data
()),
-
1
,
//
We are certain that `data` is sufficiently large
nullptr
,
String::
NO_NULL_TERMINATION
| String::
REPLACE_INVALID_UTF8
);
ab =
ArrayBuffer::New
(isolate,
std::move
(bs));
}
auto
array =
Uint8Array::New
(ab,
0
, length);
args.
GetReturnValue
().
Set
(array);
}
//
Convert the input into an encoded string
void
BindingData::DecodeUTF8
(
const
FunctionCallbackInfo<Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
//
list, flags
CHECK_GE
(args.
Length
(),
1
);
if
(!(args[
0
]->
IsArrayBuffer
() || args[
0
]->
IsSharedArrayBuffer
() ||
args[
0
]->
IsArrayBufferView
())) {
return
node::THROW_ERR_INVALID_ARG_TYPE
(
env->
isolate
(),
"
The
\"
list
\"
argument must be an instance of SharedArrayBuffer,
"
"
ArrayBuffer or ArrayBufferView.
"
);
}
ArrayBufferViewContents<
char
>
buffer
(args[
0
]);
bool
ignore_bom = args[
1
]->
IsTrue
();
bool
has_fatal = args[
2
]->
IsTrue
();
const
char
* data = buffer.
data
();
size_t
length = buffer.
length
();
if
(has_fatal) {
auto
result =
simdutf::validate_utf8_with_errors
(data, length);
if
(result.
error
) {
return
node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA
(
env->
isolate
(),
"
The encoded data was not valid for encoding utf-8
"
);
}
}
if
(!ignore_bom && length >=
3
) {
if
(
memcmp
(data,
"
\xEF\xBB\xBF
"
,
3
) ==
0
) {
data +=
3
;
length -=
3
;
}
}
if
(length ==
0
)
return
args.
GetReturnValue
().
SetEmptyString
();
Local<Value> error;
MaybeLocal<Value> maybe_ret =
StringBytes::Encode
(env->
isolate
(), data, length,
UTF8
, &error);
Local<Value> ret;
if
(!maybe_ret.
ToLocal
(&ret)) {
CHECK
(!error.
IsEmpty
());
env->
isolate
()->
ThrowException
(error);
return
;
}
args.
GetReturnValue
().
Set
(ret);
}
void
BindingData::ToASCII
(
const
v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
CHECK_GE
(args.
Length
(),
1
);
CHECK
(args[
0
]->
IsString
());
Utf8Value
input
(env->
isolate
(), args[
0
]);
auto
out =
ada::idna::to_ascii
(input.
ToStringView
());
args.
GetReturnValue
().
Set
(
String::NewFromUtf8
(env->
isolate
(), out.
c_str
()).
ToLocalChecked
());
}
void
BindingData::ToUnicode
(
const
v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env =
Environment::GetCurrent
(args);
CHECK_GE
(args.
Length
(),
1
);
CHECK
(args[
0
]->
IsString
());
Utf8Value
input
(env->
isolate
(), args[
0
]);
auto
out =
ada::idna::to_unicode
(input.
ToStringView
());
args.
GetReturnValue
().
Set
(
String::NewFromUtf8
(env->
isolate
(), out.
c_str
()).
ToLocalChecked
());
}
void
BindingData::CreatePerIsolateProperties
(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->
isolate
();
SetMethod
(isolate, target,
"
encodeInto
"
, EncodeInto);
SetMethodNoSideEffect
(isolate, target,
"
encodeUtf8String
"
, EncodeUtf8String);
SetMethodNoSideEffect
(isolate, target,
"
decodeUTF8
"
, DecodeUTF8);
SetMethodNoSideEffect
(isolate, target,
"
toASCII
"
, ToASCII);
SetMethodNoSideEffect
(isolate, target,
"
toUnicode
"
, ToUnicode);
}
void
BindingData::CreatePerContextProperties
(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void
* priv) {
Realm* realm =
Realm::GetCurrent
(context);
realm->
AddBindingData
<BindingData>(target);
}
void
BindingData::RegisterTimerExternalReferences
(
ExternalReferenceRegistry* registry) {
registry->
Register
(EncodeInto);
registry->
Register
(EncodeUtf8String);
registry->
Register
(DecodeUTF8);
registry->
Register
(ToASCII);
registry->
Register
(ToUnicode);
}
}
//
namespace encoding_binding
}
//
namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL
(
encoding_binding,
node::encoding_binding::BindingData::CreatePerContextProperties)
NODE_BINDING_PER_ISOLATE_INIT(
encoding_binding,
node::encoding_binding::BindingData::CreatePerIsolateProperties)
NODE_BINDING_EXTERNAL_REFERENCE(
encoding_binding,
node::encoding_binding::BindingData::RegisterTimerExternalReferences)
Back
|
FazBrowse Home
|
New Git URL