Skip to content

Commit d36127d

Browse files
XadillaXtargos
authored andcommittedOct 4, 2021
src: move ToUSVString() to node_util.cc
Since `toUSVString()` was exposed in `util` as a public API, not only for internal `url` any more. PR-URL: #40204 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
1 parent dd39422 commit d36127d

File tree

4 files changed

+59
-63
lines changed

4 files changed

+59
-63
lines changed
 

‎lib/internal/util.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ const {
2828
SymbolFor,
2929
} = primordials;
3030

31-
const {
32-
toUSVString: _toUSVString,
33-
} = internalBinding('url');
34-
3531
const {
3632
hideStackFrames,
3733
codes: {
@@ -47,7 +43,8 @@ const {
4743
setHiddenValue,
4844
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
4945
decorated_private_symbol: kDecoratedPrivateSymbolIndex,
50-
sleep: _sleep
46+
sleep: _sleep,
47+
toUSVString: _toUSVString,
5148
} = internalBinding('util');
5249
const { isNativeError } = internalBinding('types');
5350

‎src/node_url.cc

-58
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ namespace {
5252
// https://url.spec.whatwg.org/#eof-code-point
5353
constexpr char kEOL = -1;
5454

55-
// Used in ToUSVString().
56-
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;
57-
5855
// https://url.spec.whatwg.org/#concept-host
5956
class URLHost {
6057
public:
@@ -160,14 +157,6 @@ enum url_error_cb_args {
160157
#undef XX
161158
};
162159

163-
#define CHAR_TEST(bits, name, expr) \
164-
template <typename T> \
165-
bool name(const T ch) { \
166-
static_assert(sizeof(ch) >= (bits) / 8, \
167-
"Character must be wider than " #bits " bits"); \
168-
return (expr); \
169-
}
170-
171160
#define TWO_CHAR_STRING_TEST(bits, name, expr) \
172161
template <typename T> \
173162
bool name(const T ch1, const T ch2) { \
@@ -225,19 +214,8 @@ TWO_CHAR_STRING_TEST(8, IsWindowsDriveLetter,
225214
TWO_CHAR_STRING_TEST(8, IsNormalizedWindowsDriveLetter,
226215
(IsASCIIAlpha(ch1) && ch2 == ':'))
227216

228-
// If a UTF-16 character is a low/trailing surrogate.
229-
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
230-
231-
// If a UTF-16 character is a surrogate.
232-
CHAR_TEST(16, IsUnicodeSurrogate, (ch & 0xF800) == 0xD800)
233-
234-
// If a UTF-16 surrogate is a low/trailing one.
235-
CHAR_TEST(16, IsUnicodeSurrogateTrail, (ch & 0x400) != 0)
236-
237-
#undef CHAR_TEST
238217
#undef TWO_CHAR_STRING_TEST
239218

240-
241219
bool BitAt(const uint8_t a[], const uint8_t i) {
242220
return !!(a[i >> 3] & (1 << (i & 7)));
243221
}
@@ -1736,40 +1714,6 @@ void EncodeAuthSet(const FunctionCallbackInfo<Value>& args) {
17361714
String::NewFromUtf8(env->isolate(), output.c_str()).ToLocalChecked());
17371715
}
17381716

1739-
void ToUSVString(const FunctionCallbackInfo<Value>& args) {
1740-
Environment* env = Environment::GetCurrent(args);
1741-
CHECK_GE(args.Length(), 2);
1742-
CHECK(args[0]->IsString());
1743-
CHECK(args[1]->IsNumber());
1744-
1745-
TwoByteValue value(env->isolate(), args[0]);
1746-
1747-
int64_t start = args[1]->IntegerValue(env->context()).FromJust();
1748-
CHECK_GE(start, 0);
1749-
1750-
for (size_t i = start; i < value.length(); i++) {
1751-
char16_t c = value[i];
1752-
if (!IsUnicodeSurrogate(c)) {
1753-
continue;
1754-
} else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
1755-
value[i] = kUnicodeReplacementCharacter;
1756-
} else {
1757-
char16_t d = value[i + 1];
1758-
if (IsUnicodeTrail(d)) {
1759-
i++;
1760-
} else {
1761-
value[i] = kUnicodeReplacementCharacter;
1762-
}
1763-
}
1764-
}
1765-
1766-
args.GetReturnValue().Set(
1767-
String::NewFromTwoByte(env->isolate(),
1768-
*value,
1769-
NewStringType::kNormal,
1770-
value.length()).ToLocalChecked());
1771-
}
1772-
17731717
void DomainToASCII(const FunctionCallbackInfo<Value>& args) {
17741718
Environment* env = Environment::GetCurrent(args);
17751719
CHECK_GE(args.Length(), 1);
@@ -1820,7 +1764,6 @@ void Initialize(Local<Object> target,
18201764
Environment* env = Environment::GetCurrent(context);
18211765
env->SetMethod(target, "parse", Parse);
18221766
env->SetMethodNoSideEffect(target, "encodeAuth", EncodeAuthSet);
1823-
env->SetMethodNoSideEffect(target, "toUSVString", ToUSVString);
18241767
env->SetMethodNoSideEffect(target, "domainToASCII", DomainToASCII);
18251768
env->SetMethodNoSideEffect(target, "domainToUnicode", DomainToUnicode);
18261769
env->SetMethod(target, "setURLConstructor", SetURLConstructor);
@@ -1838,7 +1781,6 @@ void Initialize(Local<Object> target,
18381781
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
18391782
registry->Register(Parse);
18401783
registry->Register(EncodeAuthSet);
1841-
registry->Register(ToUSVString);
18421784
registry->Register(DomainToASCII);
18431785
registry->Register(DomainToUnicode);
18441786
registry->Register(SetURLConstructor);

‎src/node_util.cc

+49
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ using v8::String;
3535
using v8::Uint32;
3636
using v8::Value;
3737

38+
// Used in ToUSVString().
39+
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;
40+
41+
// If a UTF-16 character is a low/trailing surrogate.
42+
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
43+
44+
// If a UTF-16 character is a surrogate.
45+
CHAR_TEST(16, IsUnicodeSurrogate, (ch & 0xF800) == 0xD800)
46+
47+
// If a UTF-16 surrogate is a low/trailing one.
48+
CHAR_TEST(16, IsUnicodeSurrogateTrail, (ch & 0x400) != 0)
49+
3850
static void GetOwnNonIndexProperties(
3951
const FunctionCallbackInfo<Value>& args) {
4052
Environment* env = Environment::GetCurrent(args);
@@ -282,6 +294,40 @@ static void IsConstructor(const FunctionCallbackInfo<Value>& args) {
282294
args.GetReturnValue().Set(args[0].As<v8::Function>()->IsConstructor());
283295
}
284296

297+
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
298+
Environment* env = Environment::GetCurrent(args);
299+
CHECK_GE(args.Length(), 2);
300+
CHECK(args[0]->IsString());
301+
CHECK(args[1]->IsNumber());
302+
303+
TwoByteValue value(env->isolate(), args[0]);
304+
305+
int64_t start = args[1]->IntegerValue(env->context()).FromJust();
306+
CHECK_GE(start, 0);
307+
308+
for (size_t i = start; i < value.length(); i++) {
309+
char16_t c = value[i];
310+
if (!IsUnicodeSurrogate(c)) {
311+
continue;
312+
} else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
313+
value[i] = kUnicodeReplacementCharacter;
314+
} else {
315+
char16_t d = value[i + 1];
316+
if (IsUnicodeTrail(d)) {
317+
i++;
318+
} else {
319+
value[i] = kUnicodeReplacementCharacter;
320+
}
321+
}
322+
}
323+
324+
args.GetReturnValue().Set(
325+
String::NewFromTwoByte(env->isolate(),
326+
*value,
327+
v8::NewStringType::kNormal,
328+
value.length()).ToLocalChecked());
329+
}
330+
285331
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
286332
registry->Register(GetHiddenValue);
287333
registry->Register(SetHiddenValue);
@@ -299,6 +345,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
299345
registry->Register(WeakReference::DecRef);
300346
registry->Register(GuessHandleType);
301347
registry->Register(IsConstructor);
348+
registry->Register(ToUSVString);
302349
}
303350

304351
void Initialize(Local<Object> target,
@@ -370,6 +417,8 @@ void Initialize(Local<Object> target,
370417
env->SetConstructorFunction(target, "WeakReference", weak_ref);
371418

372419
env->SetMethod(target, "guessHandleType", GuessHandleType);
420+
421+
env->SetMethodNoSideEffect(target, "toUSVString", ToUSVString);
373422
}
374423

375424
} // namespace util

‎src/util-inl.h

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
(((x) & 0x00000000000000FFull) << 56)
6565
#endif
6666

67+
#define CHAR_TEST(bits, name, expr) \
68+
template <typename T> \
69+
bool name(const T ch) { \
70+
static_assert(sizeof(ch) >= (bits) / 8, \
71+
"Character must be wider than " #bits " bits"); \
72+
return (expr); \
73+
}
74+
6775
namespace node {
6876

6977
template <typename T>

0 commit comments

Comments
 (0)
Please sign in to comment.