-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
src: move all 1-byte encodings to native #61118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||
| #include "encoding_binding.h" | ||||||||||||
| #include "ada.h" | ||||||||||||
| #include "encoding_singlebyte.h" | ||||||||||||
| #include "env-inl.h" | ||||||||||||
| #include "node_errors.h" | ||||||||||||
| #include "node_external_reference.h" | ||||||||||||
|
|
@@ -389,6 +390,70 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) { | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void BindingData::DecodeSingleByte(const FunctionCallbackInfo<Value>& args) { | ||||||||||||
| Environment* env = Environment::GetCurrent(args); | ||||||||||||
|
|
||||||||||||
| CHECK_GE(args.Length(), 2); | ||||||||||||
| Isolate* isolate = env->isolate(); | ||||||||||||
|
|
||||||||||||
| if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() || | ||||||||||||
| args[0]->IsArrayBufferView())) { | ||||||||||||
| return node::THROW_ERR_INVALID_ARG_TYPE( | ||||||||||||
| isolate, | ||||||||||||
| "The \"input\" argument must be an instance of SharedArrayBuffer, " | ||||||||||||
| "ArrayBuffer or ArrayBufferView."); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| static constexpr int kXUserDefined = 28; // Last one, see encoding.js | ||||||||||||
|
|
||||||||||||
| CHECK(args[1]->IsInt32()); | ||||||||||||
| const int encoding = args[1].As<v8::Int32>()->Value(); | ||||||||||||
| CHECK(encoding >= 0 && encoding <= kXUserDefined); | ||||||||||||
|
|
||||||||||||
| ArrayBufferViewContents<uint8_t> buffer(args[0]); | ||||||||||||
| const uint8_t* data = buffer.data(); | ||||||||||||
| size_t length = buffer.length(); | ||||||||||||
|
|
||||||||||||
| if (length == 0) return args.GetReturnValue().SetEmptyString(); | ||||||||||||
|
|
||||||||||||
| const char* dataChar = reinterpret_cast<const char*>(data); | ||||||||||||
| if (!simdutf::validate_ascii_with_errors(dataChar, length).error) { | ||||||||||||
| Local<Value> ret; | ||||||||||||
| if (StringBytes::Encode(isolate, dataChar, length, LATIN1).ToLocal(&ret)) { | ||||||||||||
| args.GetReturnValue().Set(ret); | ||||||||||||
| } | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| uint16_t* dst = node::UncheckedMalloc<uint16_t>(length); | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we can allocate a huge Maybe it would be better to guard this with sth like:
Suggested change
Let me know if I'm missing something |
||||||||||||
| if (dst == nullptr) return node::THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate); | ||||||||||||
|
|
||||||||||||
| if (encoding == kXUserDefined) { | ||||||||||||
| // x-user-defined | ||||||||||||
| for (size_t i = 0; i < length; i++) { | ||||||||||||
| dst[i] = data[i] >= 0x80 ? data[i] + 0xf700 : data[i]; | ||||||||||||
| } | ||||||||||||
| } else { | ||||||||||||
| bool has_fatal = args[2]->IsTrue(); | ||||||||||||
|
|
||||||||||||
| const uint16_t* table = tSingleByteEncodings[encoding]; | ||||||||||||
| for (size_t i = 0; i < length; i++) dst[i] = table[data[i]]; | ||||||||||||
|
|
||||||||||||
| const char16_t* dst16 = reinterpret_cast<char16_t*>(dst); | ||||||||||||
| if (has_fatal && fSingleByteEncodings[encoding] && | ||||||||||||
| simdutf::find(dst16, dst16 + length, 0xfffd) != dst16 + length) { | ||||||||||||
| free(dst); | ||||||||||||
| return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA( | ||||||||||||
| isolate, "The encoded data was not valid for this encoding"); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Local<Value> ret; | ||||||||||||
| if (StringBytes::Raw(isolate, dst, length).ToLocal(&ret)) { | ||||||||||||
| args.GetReturnValue().Set(ret); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void BindingData::ToASCII(const FunctionCallbackInfo<Value>& args) { | ||||||||||||
| Environment* env = Environment::GetCurrent(args); | ||||||||||||
| CHECK_GE(args.Length(), 1); | ||||||||||||
|
|
@@ -421,6 +486,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, | |||||||||||
| SetMethod(isolate, target, "encodeInto", EncodeInto); | ||||||||||||
| SetMethodNoSideEffect(isolate, target, "encodeUtf8String", EncodeUtf8String); | ||||||||||||
| SetMethodNoSideEffect(isolate, target, "decodeUTF8", DecodeUTF8); | ||||||||||||
| SetMethodNoSideEffect(isolate, target, "decodeSingleByte", DecodeSingleByte); | ||||||||||||
| SetMethodNoSideEffect(isolate, target, "toASCII", ToASCII); | ||||||||||||
| SetMethodNoSideEffect(isolate, target, "toUnicode", ToUnicode); | ||||||||||||
| } | ||||||||||||
|
|
@@ -438,6 +504,7 @@ void BindingData::RegisterTimerExternalReferences( | |||||||||||
| registry->Register(EncodeInto); | ||||||||||||
| registry->Register(EncodeUtf8String); | ||||||||||||
| registry->Register(DecodeUTF8); | ||||||||||||
| registry->Register(DecodeSingleByte); | ||||||||||||
| registry->Register(ToASCII); | ||||||||||||
| registry->Register(ToUnicode); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.