diff --git a/NativeScript/ada/ada.cpp b/NativeScript/ada/ada.cpp index 31f7a35c..82db8140 100644 --- a/NativeScript/ada/ada.cpp +++ b/NativeScript/ada/ada.cpp @@ -1,6 +1,7 @@ /* auto-generated on 2026-11-19 15:19:30 -0500. Do not edit! */ /* begin file src/ada.cpp */ #include "ada.h" +#include "system_idna.h" /* begin file src/checkers.cpp */ #include @@ -7119,6 +7120,9 @@ bool valid_name_code_point(char32_t code_point, bool first) { return false; } +#if NS_ADA_SYSTEM_IDNA + return tns::SystemIsNameCodePoint(code_point, first); +#else if (!ensure_tables() || id_start == nullptr || id_continue == nullptr) { return false; } @@ -7136,6 +7140,7 @@ bool valid_name_code_point(char32_t code_point, bool first) { [](const auto& range) { return range[1]; }); // project to range-high return iter != ranges.end() && code_point >= (*iter)[0]; +#endif } } // namespace ada::idna /* end file src/identifier.cpp */ @@ -7818,7 +7823,11 @@ bool to_ascii(std::optional& out, const std::string_view plain, input = percent_decoded_buffer; } // input is a non-empty UTF-8 string, must be percent decoded +#if NS_ADA_SYSTEM_IDNA + std::string idna_ascii = tns::SystemIdnaToAscii(input); +#else std::string idna_ascii = ada::idna::to_ascii(input); +#endif if (idna_ascii.empty() || contains_forbidden_domain_code_point( idna_ascii.data(), idna_ascii.size())) { return false; diff --git a/NativeScript/ada/system_idna.cpp b/NativeScript/ada/system_idna.cpp new file mode 100644 index 00000000..221890de --- /dev/null +++ b/NativeScript/ada/system_idna.cpp @@ -0,0 +1,85 @@ +#include "system_idna.h" + +#if NS_ADA_SYSTEM_IDNA +#include +#include + +#include "ada.h" + +// Autolinks libicucore so that turning the flag on is the only switch. +__asm__(".linker_option \"-licucore\""); + +namespace tns { + +static const UIDNA* Uts46() { + static const UIDNA* idna = [] { + UErrorCode err = U_ZERO_ERROR; + // Explicit nontransitional bits: UIDNA_DEFAULT changed meaning in ICU 76. + return uidna_openUTS46(UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ | + UIDNA_NONTRANSITIONAL_TO_ASCII | + UIDNA_NONTRANSITIONAL_TO_UNICODE, + &err); + }(); + return idna; +} + +std::string SystemIdnaToAscii(std::string_view input) { + // WHATWG "domain to ASCII" with beStrict=false lowercases an all-ASCII + // domain whatever ToASCII would say; ICU rejects e.g. invalid "xn--" labels. + bool ascii = true; + for (unsigned char c : input) { + ascii &= c < 0x80; + } + if (ascii) { + std::string lower(input); + for (char& c : lower) { + if (c >= 'A' && c <= 'Z') { + c += 'a' - 'A'; + } + } + return lower; + } + // ada bounds the non-ASCII inputs it converts; keep the same limit. + if (input.size() > ada::idna::max_domain_input_bytes) { + return {}; + } + + // CheckHyphens and VerifyDnsLength are off for URLs, and ICU has no options + // for them, so their errors are not failures. + constexpr uint32_t kIgnoredErrors = + UIDNA_ERROR_EMPTY_LABEL | UIDNA_ERROR_LABEL_TOO_LONG | + UIDNA_ERROR_DOMAIN_NAME_TOO_LONG | UIDNA_ERROR_LEADING_HYPHEN | + UIDNA_ERROR_TRAILING_HYPHEN | UIDNA_ERROR_HYPHEN_3_4; + + const UIDNA* idna = Uts46(); + if (idna == nullptr) { + return {}; + } + std::string out(input.size() * 2 + 64, '\0'); + for (int attempt = 0; attempt < 2; attempt++) { + UIDNAInfo info = UIDNA_INFO_INITIALIZER; + UErrorCode err = U_ZERO_ERROR; + int32_t length = uidna_nameToASCII_UTF8( + idna, input.data(), static_cast(input.size()), out.data(), + static_cast(out.size()), &info, &err); + if (err == U_BUFFER_OVERFLOW_ERROR) { + out.resize(length); + continue; + } + if (U_FAILURE(err) || (info.errors & ~kIgnoredErrors) != 0 || + length == 0) { + return {}; + } + out.resize(length); + return out; + } + return {}; +} + +bool SystemIsNameCodePoint(char32_t code_point, bool first) { + return u_hasBinaryProperty(static_cast(code_point), + first ? UCHAR_ID_START : UCHAR_ID_CONTINUE); +} + +} // namespace tns +#endif diff --git a/NativeScript/ada/system_idna.h b/NativeScript/ada/system_idna.h new file mode 100644 index 00000000..efddc6f4 --- /dev/null +++ b/NativeScript/ada/system_idna.h @@ -0,0 +1,36 @@ +#ifndef NS_ADA_SYSTEM_IDNA_H +#define NS_ADA_SYSTEM_IDNA_H + +// 1: ada uses the system ICU (libicucore) for UTS #46 host conversion and for +// URLPattern's identifier code point check, so its own compressed Unicode +// tables dead-strip (~80 KB). Results follow the OS's Unicode data: iOS +// 13-17.3 ship ICU 64-72 (Unicode 12.1-15.0), which rejects newer characters +// and maps U+1E9E to "ss" where current UTS #46 keeps "ß". +// +// ada.cpp is vendored: after upgrading it, re-add the include of this header +// below `#include "ada.h"` and the NS_ADA_SYSTEM_IDNA branches in +// `ada::unicode::to_ascii` and `ada::idna::valid_name_code_point`. Both must +// be switched, or the tables stay linked. +#ifndef NS_ADA_SYSTEM_IDNA +#define NS_ADA_SYSTEM_IDNA 0 +#endif + +#if NS_ADA_SYSTEM_IDNA +#include +#include + +namespace tns { + +// Same contract as ada::idna::to_ascii: an empty string means failure. +__attribute__((visibility("hidden"))) std::string SystemIdnaToAscii( + std::string_view input); + +// The table lookup of ada::idna::valid_name_code_point: ID_Start when +// `first`, otherwise ID_Continue. +__attribute__((visibility("hidden"))) bool SystemIsNameCodePoint( + char32_t code_point, bool first); + +} // namespace tns +#endif + +#endif // NS_ADA_SYSTEM_IDNA_H diff --git a/NativeScript/runtime/Console.cpp b/NativeScript/runtime/Console.cpp index ef8a3b58..13021c2b 100644 --- a/NativeScript/runtime/Console.cpp +++ b/NativeScript/runtime/Console.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -69,13 +68,72 @@ bool isErrorMessage(const std::string& line) { return line.find("Error") != std::string::npos; } +namespace { + +// The character classes of ECMAScript-grammar std::regex on char: \s, the +// line terminators `.` stops at, and \d. +bool IsSpace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); } +bool IsLineEnd(char c) { return c == '\n' || c == '\r'; } +bool IsDigit(char c) { return c >= '0' && c <= '9'; } + +// Whether "::" starts at `i`. +bool LineColumnAt(const std::string& s, size_t i) { + for (int part = 0; part < 2; part++) { + if (i >= s.size() || s[i] != ':') return false; + size_t digits = ++i; + while (i < s.size() && IsDigit(s[i])) i++; + if (i == digits) return false; + } + return true; +} + +// From a ")" at `close`, the ':' that starts the "::" ending +// right before it, or npos. +size_t LineColumnBefore(const std::string& s, size_t close) { + size_t i = close; + for (int part = 0; part < 2; part++) { + size_t end = i; + while (i > 0 && IsDigit(s[i - 1])) i--; + if (i == end || i == 0 || s[i - 1] != ':') return std::string::npos; + i--; + } + return i; +} + +} // namespace + +// Whether the text contains a frame in either V8 style, as the regexes +// \s+at\s+.*\(.+?:\d+:\d+\) " at foo (/path/to/file.ts:123:45)" +// \s+at\s+[^\s\(\)]+:\d+:\d+ " at /path/to/file.ts:123:45" +// would find it. bool isStackFrame(const std::string& line) { - // Recognize both styles: - // " at foo (/path/to/file.ts:123:45)" -> with parentheses - // " at /path/to/file.ts:123:45" -> bare location - static const std::regex withParens(R"(\s+at\s+.*\(.+?:\d+:\d+\))"); - static const std::regex bare(R"(\s+at\s+[^\s\(\)]+:\d+:\d+)"); - return std::regex_search(line, withParens) || std::regex_search(line, bare); + for (size_t at = line.find("at", 1); at != std::string::npos; + at = line.find("at", at + 1)) { + if (!IsSpace(line[at - 1])) continue; + size_t begin = at + 2; + if (begin >= line.size() || !IsSpace(line[begin])) continue; + while (begin < line.size() && IsSpace(line[begin])) begin++; + + size_t name_end = begin; + while (name_end < line.size() && !IsSpace(line[name_end]) && + line[name_end] != '(' && line[name_end] != ')') { + name_end++; + } + for (size_t colon = begin + 1; colon < name_end; colon++) { + if (LineColumnAt(line, colon)) return true; + } + + size_t first_paren = std::string::npos; + for (size_t i = begin; i < line.size() && !IsLineEnd(line[i]); i++) { + if (line[i] == '(' && first_paren == std::string::npos) { + first_paren = i; + } else if (line[i] == ')' && first_paren != std::string::npos) { + size_t colon = LineColumnBefore(line, i); + if (colon != std::string::npos && colon >= first_paren + 2) return true; + } + } + } + return false; } void Console::LogCallback(const FunctionCallbackInfo& args) { diff --git a/NativeScript/runtime/URLImpl.cpp b/NativeScript/runtime/URLImpl.cpp index 230108b2..781139de 100644 --- a/NativeScript/runtime/URLImpl.cpp +++ b/NativeScript/runtime/URLImpl.cpp @@ -4,6 +4,8 @@ #include "URLImpl.h" +#include + #include "Caches.h" #include "Helpers.h" #include "ModuleBinding.hpp" @@ -22,7 +24,7 @@ struct URLConstructorState { } // namespace -URLImpl::URLImpl(url_aggregator url) : url_(url) {} +URLImpl::URLImpl(url_aggregator url) : url_(std::move(url)) {} void URLImpl::Init(v8::Isolate* isolate, v8::Local globalTemplate) { @@ -113,84 +115,41 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo& args) { return; } - url_aggregator url; - auto url_string = tns::ToString(isolate, args[0]); + std::optional base_string; if (count > 1) { if (args[1]->IsString()) { - auto base_string = tns::ToString(isolate, args[1].As()); - std::string_view base_string_view(base_string.data(), - base_string.length()); - - if (!can_parse(url_string, &base_string_view)) { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); - return; - } - auto base_url = - ada::parse(base_string_view, nullptr); - - auto result = - ada::parse(url_string, &base_url.value()); - - if (result) { - url = result.value(); - } else { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); - return; - } + base_string = tns::ToString(isolate, args[1].As()); } else if (args[1]->IsObject()) { - auto base_string = - tns::ToString(isolate, args[1]->ToString(context).ToLocalChecked()); - std::string_view base_string_view(base_string.data(), - base_string.length()); - if (!can_parse(std::string_view(url_string.data(), url_string.length()), - &base_string_view)) { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); + v8::Local base; + if (!args[1]->ToString(context).ToLocal(&base)) { return; } + base_string = tns::ToString(isolate, base); + } + // Any other 2nd argument is treated as undefined. + } - auto base_url = - ada::parse(base_string_view, nullptr); - - auto result = - ada::parse(url_string, &base_url.value()); - - if (result) { - url = result.value(); - } else { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); - return; - } - } else { - // treat 2nd arg as undefined otherwise. - auto result = ada::parse(url_string, nullptr); - if (result) { - url = result.value(); - } else { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); - return; - } + auto parse = [&]() { + if (!base_string) { + return ada::parse(url_string, nullptr); } - } else { - auto result = ada::parse(url_string, nullptr); - if (result) { - url = result.value(); - } else { - isolate->ThrowException( - v8::Exception::TypeError(ToV8String(isolate, ""))); - return; + auto base = ada::parse(*base_string, nullptr); + if (!base) { + return base; } + return ada::parse(url_string, &base.value()); + }; + auto result = parse(); + if (!result) { + isolate->ThrowException(v8::Exception::TypeError(ToV8String(isolate, ""))); + return; } auto ret = args.This(); - auto urlImpl = new URLImpl(url); + auto urlImpl = new URLImpl(std::move(result.value())); ret->SetAlignedPointerInInternalField(0, urlImpl, v8::kEmbedderDataTypeTagDefault); @@ -502,17 +461,17 @@ void URLImpl::ToString(const v8::FunctionCallbackInfo& info) { } void URLImpl::CanParse(const v8::FunctionCallbackInfo& info) { + // ada::parse rather than ada::can_parse: the constructor already links it, + // and can_parse pulls in a second instantiation of ada's parser. bool value; - auto count = info.Length(); - auto isolate = info.GetIsolate(); - if (count > 1) { - auto url_string = tns::ToString(isolate, info[0]); - auto base_string = tns::ToString(isolate, info[1]); - std::string_view base_string_view(base_string.data(), base_string.length()); - value = can_parse(url_string, &base_string_view); + auto url_string = tns::ToString(isolate, info[0]); + if (info.Length() > 1 && !info[1]->IsUndefined()) { + auto base = + ada::parse(tns::ToString(isolate, info[1]), nullptr); + value = base && ada::parse(url_string, &base.value()); } else { - value = can_parse(tns::ToString(isolate, info[0]).c_str(), nullptr); + value = ada::parse(url_string, nullptr).has_value(); } info.GetReturnValue().Set(value); diff --git a/TestRunner/app/tests/URL.js b/TestRunner/app/tests/URL.js index 06cfef8f..ea49c63e 100644 --- a/TestRunner/app/tests/URL.js +++ b/TestRunner/app/tests/URL.js @@ -65,4 +65,52 @@ describe("URL", function () { expect(JSON.stringify(url)).toBe(JSON.stringify(url.href)); expect(JSON.stringify({ u: url })).toBe(JSON.stringify({ u: url.href })); }); + + it("resolves a relative URL against a string base", function () { + const url = new URL("../c?d=1#e", "https://a.test/x/y/z"); + expect(url.href).toBe("https://a.test/x/c?d=1#e"); + }); + + it("resolves a relative URL against a URL object base", function () { + const url = new URL("page", new URL("https://a.test/dir/")); + expect(url.href).toBe("https://a.test/dir/page"); + }); + + it("throws a TypeError for an invalid base", function () { + expect(() => new URL("page", "not a url")).toThrowError(TypeError); + }); + + it("throws a TypeError when the input does not parse against the base", function () { + expect(() => new URL("http://[", "https://a.test/")).toThrowError(TypeError); + }); + + it("propagates an exception thrown by the base's toString", function () { + const base = { + toString() { + throw new Error("base toString"); + }, + }; + expect(() => new URL("page", base)).toThrowError("base toString"); + }); + + it("canParse agrees with the constructor", function () { + expect(URL.canParse("https://a.test/")).toBe(true); + expect(URL.canParse("not a url")).toBe(false); + expect(URL.canParse("page", "https://a.test/dir/")).toBe(true); + expect(URL.canParse("page", "not a url")).toBe(false); + expect(URL.canParse("http://[", "https://a.test/")).toBe(false); + }); + + it("canParse stringifies its arguments and ignores an undefined base", function () { + expect(URL.canParse(new URL("https://a.test/x"))).toBe(true); + expect(URL.canParse("page", new URL("https://a.test/dir/"))).toBe(true); + expect(URL.canParse("https://a.test/", undefined)).toBe(true); + }); + + it("converts internationalized hostnames to ASCII", function () { + expect(new URL("https://B\u00FCcher.example/").hostname).toBe("xn--bcher-kva.example"); + expect(new URL("https://\u4F8B\u3048.\u30C6\u30B9\u30C8/").hostname).toBe("xn--r8jz45g.xn--zckzah"); + expect(new URL("https://XN--BCHER-KVA.example/").hostname).toBe("xn--bcher-kva.example"); + expect(() => new URL("https://a\u200Db.example/")).toThrowError(TypeError); + }); }); diff --git a/TestRunner/app/tests/URLPattern.js b/TestRunner/app/tests/URLPattern.js index 889e33cb..66d194d4 100644 --- a/TestRunner/app/tests/URLPattern.js +++ b/TestRunner/app/tests/URLPattern.js @@ -46,6 +46,17 @@ describe("URLPattern", function () { expect(pattern.hostname).toBe("google.com"); }); + it("accepts non-ASCII group names", function () { + const pattern = new URLPattern({ pathname: "/:caf\u00E9/:\u540D\u524D" }); + const result = pattern.exec({ pathname: "/a/b" }); + expect(result.pathname.groups["caf\u00E9"]).toBe("a"); + expect(result.pathname.groups["\u540D\u524D"]).toBe("b"); + }); + + it("rejects a group name that does not start with an identifier start", function () { + expect(() => new URLPattern({ pathname: "/:\u0663x" })).toThrowError(TypeError); + }); + it("test() matches a URL against a pattern with a capture group", function () { const pattern = new URLPattern("https://example.com/books/:id"); expect(pattern.test("https://example.com/books/123")).toBe(true); diff --git a/v8ios.xcodeproj/project.pbxproj b/v8ios.xcodeproj/project.pbxproj index 78783e78..e86c798c 100644 --- a/v8ios.xcodeproj/project.pbxproj +++ b/v8ios.xcodeproj/project.pbxproj @@ -339,7 +339,9 @@ F1F30E762B58FC74006A62C0 /* URLSearchParamsImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = F1F30E722B58FC74006A62C0 /* URLSearchParamsImpl.h */; }; F1F30E772B58FC74006A62C0 /* URLImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = F1F30E732B58FC74006A62C0 /* URLImpl.h */; }; F1F30E8B2B58FE28006A62C0 /* ada.h in Headers */ = {isa = PBXBuildFile; fileRef = F1F30E872B58FE28006A62C0 /* ada.h */; }; - F1F30E8C2B58FE28006A62C0 /* ada.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F1F30E882B58FE28006A62C0 /* ada.cpp */; settings = {COMPILER_FLAGS = "-Wno-unused-function"; }; }; + F1F30E8C2B58FE28006A62C0 /* ada.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F1F30E882B58FE28006A62C0 /* ada.cpp */; settings = {COMPILER_FLAGS = "-Wno-unused-function -fvisibility=hidden"; }; }; + AD1D0A000000000000000003 /* system_idna.h in Headers */ = {isa = PBXBuildFile; fileRef = AD1D0A000000000000000001 /* system_idna.h */; }; + AD1D0A000000000000000004 /* system_idna.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD1D0A000000000000000002 /* system_idna.cpp */; }; F6191AAE29C0FCE8003F588F /* JsV8InspectorClient.h in Headers */ = {isa = PBXBuildFile; fileRef = F6191AA629C0FCE7003F588F /* JsV8InspectorClient.h */; }; F6191AB029C0FCE8003F588F /* JsV8InspectorClient.mm in Sources */ = {isa = PBXBuildFile; fileRef = F6191AA829C0FCE7003F588F /* JsV8InspectorClient.mm */; }; A1B2C3D40000000000000003 /* WorkerInspectorClient.h in Headers */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000001 /* WorkerInspectorClient.h */; }; @@ -893,6 +895,8 @@ F1F30E732B58FC74006A62C0 /* URLImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = URLImpl.h; sourceTree = ""; }; F1F30E872B58FE28006A62C0 /* ada.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ada.h; sourceTree = ""; }; F1F30E882B58FE28006A62C0 /* ada.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ada.cpp; sourceTree = ""; }; + AD1D0A000000000000000001 /* system_idna.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = system_idna.h; sourceTree = ""; }; + AD1D0A000000000000000002 /* system_idna.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = system_idna.cpp; sourceTree = ""; }; F6191AA629C0FCE7003F588F /* JsV8InspectorClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JsV8InspectorClient.h; sourceTree = ""; }; F6191AA829C0FCE7003F588F /* JsV8InspectorClient.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JsV8InspectorClient.mm; sourceTree = ""; }; A1B2C3D40000000000000001 /* WorkerInspectorClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerInspectorClient.h; sourceTree = ""; }; @@ -1670,6 +1674,8 @@ children = ( F1F30E872B58FE28006A62C0 /* ada.h */, F1F30E882B58FE28006A62C0 /* ada.cpp */, + AD1D0A000000000000000001 /* system_idna.h */, + AD1D0A000000000000000002 /* system_idna.cpp */, ); path = ada; sourceTree = ""; @@ -1727,6 +1733,7 @@ C2DDEBA4229EAC8300345BFE /* Runtime.h in Headers */, F1CB51842D5C37100042555E /* URLPatternImpl.h in Headers */, F1F30E8B2B58FE28006A62C0 /* ada.h in Headers */, + AD1D0A000000000000000003 /* system_idna.h in Headers */, C2DDEB8D229EAC8300345BFE /* Common.h in Headers */, C247C17122F82842001D2CA2 /* v8config.h in Headers */, 6573B9CD291FE29F00B0ED7C /* V8Runtime.h in Headers */, @@ -2438,6 +2445,7 @@ 4E10A9100000000000000203 /* NodeApiEmbed.mm in Sources */, 4E10A9100000000000000204 /* NapiRuntime.mm in Sources */, F1F30E8C2B58FE28006A62C0 /* ada.cpp in Sources */, + AD1D0A000000000000000004 /* system_idna.cpp in Sources */, C20AB5E626E1015300E2B41D /* OneByteStringResource.cpp in Sources */, 6573B9CE291FE29F00B0ED7C /* JSIV8ValueConverter.cpp in Sources */, C2DDEB92229EAC8300345BFE /* WeakRef.cpp in Sources */, @@ -3168,6 +3176,9 @@ OTHER_LDFLAGS = ( "-ld_classic", "-Wl,-dead_strip", + "-Wl,-unexported_symbol,__ZN3ada*", + "-Wl,-unexported_symbol,__ZNK3ada*", + "-Wl,-unexported_symbol,__ZGVN3ada*", "-Wl,-search_paths_first", "-fno-builtin-stpcpy", "-fno-rtti", @@ -3261,6 +3272,9 @@ OTHER_LDFLAGS = ( "-ld_classic", "-Wl,-dead_strip", + "-Wl,-unexported_symbol,__ZN3ada*", + "-Wl,-unexported_symbol,__ZNK3ada*", + "-Wl,-unexported_symbol,__ZGVN3ada*", "-Wl,-search_paths_first", "-fno-builtin-stpcpy", "-fno-rtti",