Skip to content
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

src: convert all endian checks to constexpr #52974

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
} else if (enc == UCS2) {
str_length = str_obj->Length() * sizeof(uint16_t);
node::TwoByteValue str(env->isolate(), args[1]);
if (IsBigEndian())
if constexpr (IsBigEndian())
SwapBytes16(reinterpret_cast<char*>(&str[0]), str_length);

memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));
Expand Down Expand Up @@ -942,7 +942,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(-1);
}

if (IsBigEndian()) {
if constexpr (IsBigEndian()) {
StringBytes::InlineDecoder decoder;
if (decoder.Decode(env, needle, enc).IsNothing()) return;
const uint16_t* decoded_string =
Expand Down
10 changes: 6 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2285,10 +2285,12 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
auto ext = string->GetExternalOneByteStringResource();
buf = const_cast<char*>(ext->data());
len = ext->length();
} else if (enc == UCS2 && IsLittleEndian() && string->IsExternalTwoByte()) {
auto ext = string->GetExternalStringResource();
buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data()));
len = ext->length() * sizeof(*ext->data());
} else if (enc == UCS2 && string->IsExternalTwoByte()) {
if constexpr (IsLittleEndian()) {
auto ext = string->GetExternalStringResource();
buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data()));
len = ext->length() * sizeof(*ext->data());
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) {

static_assert(sizeof(T) == 1 || sizeof(T) == 2,
"Currently only one- or two-byte buffers are supported");
if (sizeof(T) > 1 && IsBigEndian()) {
if constexpr (sizeof(T) > 1 && IsBigEndian()) {
SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf);
SwapBytes16(retbuf_data, retbuf_length);
}
Expand All @@ -128,7 +128,7 @@ void CopySourceBuffer(MaybeStackBuffer<UChar>* dest,
dest->AllocateSufficientStorage(length_in_chars);
char* dst = reinterpret_cast<char*>(**dest);
memcpy(dst, data, length);
if (IsBigEndian()) {
if constexpr (IsBigEndian()) {
SwapBytes16(dst, length);
}
}
Expand Down Expand Up @@ -527,7 +527,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {

char* value = reinterpret_cast<char*>(output) + beginning;

if (IsBigEndian()) {
if constexpr (IsBigEndian()) {
SwapBytes16(value, length);
}

Expand Down
7 changes: 3 additions & 4 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ size_t StringBytes::Write(Isolate* isolate,
// the Buffer, so we need to reorder on BE platforms. See
// https://nodejs.org/api/buffer.html regarding Node's "ucs2"
// encoding specification
if (IsBigEndian())
SwapBytes16(buf, nbytes);
if constexpr (IsBigEndian()) SwapBytes16(buf, nbytes);

break;
}
Expand Down Expand Up @@ -756,7 +755,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,

case UCS2: {
size_t str_len = buflen / 2;
if (IsBigEndian()) {
if constexpr (IsBigEndian()) {
uint16_t* dst = node::UncheckedMalloc<uint16_t>(str_len);
if (str_len != 0 && dst == nullptr) {
*error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
Expand Down Expand Up @@ -803,7 +802,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
// Buffer, so we need to reorder on BE platforms. See
// https://nodejs.org/api/buffer.html regarding Node's "ucs2"
// encoding specification
if (IsBigEndian()) {
if constexpr (IsBigEndian()) {
uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen);
if (dst == nullptr) {
*error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate);
Expand Down
21 changes: 7 additions & 14 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <cstring>

#include <array>
#include <bit>
#include <limits>
#include <memory>
#include <set>
Expand Down Expand Up @@ -778,24 +779,16 @@ inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
.Check(); \
} while (0)

enum class Endianness { LITTLE, BIG };

inline Endianness GetEndianness() {
// Constant-folded by the compiler.
const union {
uint8_t u8[2];
uint16_t u16;
} u = {{1, 0}};
return u.u16 == 1 ? Endianness::LITTLE : Endianness::BIG;
constexpr inline bool IsLittleEndian() {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
return std::endian::native == std::endian::little;
}

inline bool IsLittleEndian() {
return GetEndianness() == Endianness::LITTLE;
constexpr inline bool IsBigEndian() {
return std::endian::native == std::endian::big;
}

inline bool IsBigEndian() {
return GetEndianness() == Endianness::BIG;
}
static_assert(IsLittleEndian() || IsBigEndian(),
"Node.js does not support mixed-endian systems");

// Round up a to the next highest multiple of b.
template <typename T>
Expand Down
Loading