Skip to content

Commit b7fbc9b

Browse files
win,src: revert simdutf to icu for win arm64
Many tests started failing on ARM64 Windows after migrating from icu to simdutf. This change reverts those changes for the problematic platform. Refs: nodejs#46471 Refs: nodejs#46472 Refs: nodejs#46548 Refs: simdutf/simdutf#216
1 parent 7a5b9d0 commit b7fbc9b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/inspector/main_thread_interface.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#include "main_thread_interface.h"
22

33
#include "env-inl.h"
4+
#if !defined(_WIN32) || !defined(_M_ARM64)
45
#include "simdutf.h"
6+
#endif
57
#include "v8-inspector.h"
8+
#if defined(_WIN32) && defined(_M_ARM64)
9+
#include "util-inl.h"
10+
11+
#include <unicode/unistr.h>
12+
#endif
613

714
#include <functional>
815
#include <memory>
@@ -286,12 +293,19 @@ Deletable* MainThreadInterface::GetObjectIfExists(int id) {
286293
}
287294

288295
std::unique_ptr<StringBuffer> Utf8ToStringView(const std::string_view message) {
296+
#if defined(_WIN32) && defined(_M_ARM64)
297+
icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8(
298+
icu::StringPiece(message.data(), message.length()));
299+
StringView view(reinterpret_cast<const uint16_t*>(utf16.getBuffer()),
300+
utf16.length());
301+
#else
289302
size_t expected_u16_length =
290303
simdutf::utf16_length_from_utf8(message.data(), message.length());
291304
MaybeStackBuffer<char16_t> buffer(expected_u16_length);
292305
size_t utf16_length = simdutf::convert_utf8_to_utf16(
293306
message.data(), message.length(), buffer.out());
294307
StringView view(reinterpret_cast<uint16_t*>(buffer.out()), utf16_length);
308+
#endif
295309
return StringBuffer::create(view);
296310
}
297311

src/inspector/node_string.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include "node_string.h"
22
#include "node/inspector/protocol/Protocol.h"
33
#include "node_util.h"
4+
#if !defined(_WIN32) || !defined(_M_ARM64)
45
#include "simdutf.h"
6+
#endif
7+
8+
#if defined(_WIN32) && defined(_M_ARM64)
9+
#include <unicode/unistr.h>
10+
#endif
511

612
namespace node {
713
namespace inspector {
@@ -15,6 +21,13 @@ void builderAppendQuotedString(StringBuilder& builder,
1521
const std::string_view string) {
1622
builder.put('"');
1723
if (!string.empty()) {
24+
#if defined(_WIN32) && defined(_M_ARM64)
25+
icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8(
26+
icu::StringPiece(string.data(), string.length()));
27+
escapeWideStringForJSON(
28+
reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length(),
29+
&builder);
30+
#else
1831
size_t expected_utf16_length =
1932
simdutf::utf16_length_from_utf8(string.data(), string.length());
2033
MaybeStackBuffer<char16_t> buffer(expected_utf16_length);
@@ -24,13 +37,21 @@ void builderAppendQuotedString(StringBuilder& builder,
2437
escapeWideStringForJSON(reinterpret_cast<const uint16_t*>(buffer.out()),
2538
utf16_length,
2639
&builder);
40+
#endif
2741
}
2842
builder.put('"');
2943
}
3044

3145
std::unique_ptr<Value> parseJSON(const std::string_view string) {
3246
if (string.empty())
3347
return nullptr;
48+
#if defined(_WIN32) && defined(_M_ARM64)
49+
icu::UnicodeString utf16 =
50+
icu::UnicodeString::fromUTF8(icu::StringPiece(string.data(),
51+
string.length()));
52+
return parseJSONCharacters(
53+
reinterpret_cast<const uint16_t*>(utf16.getBuffer()), utf16.length());
54+
#else
3455
size_t expected_utf16_length =
3556
simdutf::utf16_length_from_utf8(string.data(), string.length());
3657
MaybeStackBuffer<char16_t> buffer(expected_utf16_length);
@@ -39,6 +60,7 @@ std::unique_ptr<Value> parseJSON(const std::string_view string) {
3960
CHECK_EQ(expected_utf16_length, utf16_length);
4061
return parseJSONCharacters(reinterpret_cast<const uint16_t*>(buffer.out()),
4162
utf16_length);
63+
#endif
4264
}
4365

4466
std::unique_ptr<Value> parseJSON(v8_inspector::StringView string) {
@@ -56,6 +78,26 @@ String StringViewToUtf8(v8_inspector::StringView view) {
5678
return std::string(reinterpret_cast<const char*>(view.characters8()),
5779
view.length());
5880
}
81+
#if defined(_WIN32) && defined(_M_ARM64)
82+
const uint16_t* source = view.characters16();
83+
const UChar* unicodeSource = reinterpret_cast<const UChar*>(source);
84+
static_assert(sizeof(*source) == sizeof(*unicodeSource),
85+
"sizeof(*source) == sizeof(*unicodeSource)");
86+
87+
size_t result_length = view.length() * sizeof(*source);
88+
std::string result(result_length, '\0');
89+
icu::UnicodeString utf16(unicodeSource, view.length());
90+
// ICU components for std::string compatibility are not enabled in build...
91+
bool done = false;
92+
while (!done) {
93+
icu::CheckedArrayByteSink sink(&result[0], result_length);
94+
utf16.toUTF8(sink);
95+
result_length = sink.NumberOfBytesAppended();
96+
result.resize(result_length);
97+
done = !sink.Overflowed();
98+
}
99+
return result;
100+
#else
59101
const char16_t* source =
60102
reinterpret_cast<const char16_t*>(view.characters16());
61103
size_t expected_utf8_length =
@@ -65,6 +107,7 @@ String StringViewToUtf8(v8_inspector::StringView view) {
65107
simdutf::convert_utf16_to_utf8(source, view.length(), buffer.out());
66108
CHECK_EQ(expected_utf8_length, utf8_length);
67109
return String(buffer.out(), utf8_length);
110+
#endif
68111
}
69112

70113
String fromDouble(double d) {
@@ -107,6 +150,11 @@ String fromUTF8(const uint8_t* data, size_t length) {
107150
}
108151

109152
String fromUTF16(const uint16_t* data, size_t length) {
153+
#if defined(_WIN32) && defined(_M_ARM64)
154+
icu::UnicodeString utf16(reinterpret_cast<const char16_t*>(data), length);
155+
std::string result;
156+
return utf16.toUTF8String(result);
157+
#else
110158
auto casted_data = reinterpret_cast<const char16_t*>(data);
111159
size_t expected_utf8_length =
112160
simdutf::utf8_length_from_utf16(casted_data, length);
@@ -115,15 +163,22 @@ String fromUTF16(const uint16_t* data, size_t length) {
115163
simdutf::convert_utf16_to_utf8(casted_data, length, buffer.out());
116164
CHECK_EQ(expected_utf8_length, utf8_length);
117165
return String(buffer.out(), utf8_length);
166+
#endif
118167
}
119168

120169
const uint8_t* CharactersUTF8(const std::string_view s) {
121170
return reinterpret_cast<const uint8_t*>(s.data());
122171
}
123172

124173
size_t CharacterCount(const std::string_view s) {
174+
#if defined(_WIN32) && defined(_M_ARM64)
175+
icu::UnicodeString utf16 =
176+
icu::UnicodeString::fromUTF8(icu::StringPiece(s.data(), s.length()));
177+
return utf16.countChar32();
178+
#else
125179
// TODO(@anonrig): Test to make sure CharacterCount returns correctly.
126180
return simdutf::utf32_length_from_utf8(s.data(), s.length());
181+
#endif
127182
}
128183

129184
} // namespace StringUtil

0 commit comments

Comments
 (0)