diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index a89f86029..d3d31ba70 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -103,6 +103,7 @@ require("./tests/testPostFrameCallback"); require("./tests/testPerformance"); require("./tests/console/logTests.js"); require('./tests/testURLImpl.js'); +require('./tests/testURLPattern.js'); require('./tests/testURLSearchParamsImpl.js'); require('./tests/testQueueMicrotask'); require('./tests/testErrorEvents'); diff --git a/test-app/app/src/main/assets/app/tests/testURLImpl.js b/test-app/app/src/main/assets/app/tests/testURLImpl.js index 8bb1d4ff9..680cc876d 100644 --- a/test-app/app/src/main/assets/app/tests/testURLImpl.js +++ b/test-app/app/src/main/assets/app/tests/testURLImpl.js @@ -58,5 +58,57 @@ describe("URL", function () { expect(url.searchParams.get("q")).toBe("hello"); expect(url.pathname).toBe("/some/path"); }); + + it("throws a TypeError without arguments", function () { + expect(() => new URL()).toThrowError(TypeError); + }); + + 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); + }); }); \ No newline at end of file diff --git a/test-app/app/src/main/assets/app/tests/testURLPattern.js b/test-app/app/src/main/assets/app/tests/testURLPattern.js index 0c2d1c1f3..6a3d2de19 100644 --- a/test-app/app/src/main/assets/app/tests/testURLPattern.js +++ b/test-app/app/src/main/assets/app/tests/testURLPattern.js @@ -34,6 +34,17 @@ describe("URLPattern", function () { }); + 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("parses with undefined base", function () { const pattern = new URLPattern("https://google.com", undefined); expect(pattern.protocol).toBe("https"); @@ -46,4 +57,22 @@ describe("URLPattern", function () { expect(pattern.hostname).toBe("google.com"); }); + 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); + expect(pattern.test("https://example.com/movies/123")).toBe(false); + }); + + it("exec() extracts named capture groups", function () { + const pattern = new URLPattern("https://example.com/books/:id"); + const result = pattern.exec("https://example.com/books/123"); + expect(result).not.toBeNull(); + expect(result.pathname.input).toBe("/books/123"); + expect(result.pathname.groups.id).toBe("123"); + }); + + it("exec() returns null when the URL does not match", function () { + const pattern = new URLPattern("https://example.com/books/:id"); + expect(pattern.exec("https://example.com/movies/123")).toBeNull(); + }); }); diff --git a/test-app/runtime/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index 8f64c421d..e39e054a1 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -124,6 +124,9 @@ if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD OR NOT CMAKE_BUILD_TYPE ST # (Debug). AGP builds the release variant as RelWithDebInfo, whose default -O2 would # otherwise win. string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -O3") + # ada at -Os instead of -O3: about 50 KB less per ABI for URL parses that + # are 7-19% (tens of nanoseconds) slower. + set_source_files_properties(src/main/cpp/ada/ada.cpp PROPERTIES COMPILE_OPTIONS "-Os") else () set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -g") endif () diff --git a/test-app/runtime/src/main/cpp/URLImpl.cpp b/test-app/runtime/src/main/cpp/URLImpl.cpp index 6820cd25d..cc44c7d11 100644 --- a/test-app/runtime/src/main/cpp/URLImpl.cpp +++ b/test-app/runtime/src/main/cpp/URLImpl.cpp @@ -4,10 +4,12 @@ #include "URLImpl.h" +#include + using namespace tns; using namespace ada; -URLImpl::URLImpl(url_aggregator url) : url_(url) {} +URLImpl::URLImpl(url_aggregator url) : url_(std::move(url)) {} URLImpl *URLImpl::GetPointer(v8::Local object) { if (object->InternalFieldCount() < 1) { @@ -90,85 +92,48 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo &args) { auto value = args[0]; auto isolate = args.GetIsolate(); auto context = isolate->GetCurrentContext(); - if (count >= 1 && !value->IsString()) { + if (count < 1 || !value->IsString()) { isolate->ThrowException( v8::Exception::TypeError(v8::String::Empty(isolate))); return; } - url_aggregator url; - auto url_string = ArgConverter::ConvertToString(args[0].As()); + std::optional base_string; if (count > 1) { if (args[1]->IsString()) { - auto base_string = ArgConverter::ConvertToString(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(v8::String::Empty(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(v8::String::Empty(isolate))); - return; - } + base_string = ArgConverter::ConvertToString(args[1].As()); } else if (args[1]->IsObject()) { - auto base_string = ArgConverter::ConvertToString( - 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(v8::String::Empty(isolate))); + v8::Local base; + if (!args[1]->ToString(context).ToLocal(&base)) { return; } + base_string = ArgConverter::ConvertToString(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(v8::String::Empty(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(v8::String::Empty(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(v8::String::Empty(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(v8::String::Empty(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); @@ -495,17 +460,26 @@ 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. + auto isolate = info.GetIsolate(); + auto context = isolate->GetCurrentContext(); + v8::Local input; + if (!info[0]->ToString(context).ToLocal(&input)) { + return; + } + auto url_string = ArgConverter::ConvertToString(input); bool value; - auto count = info.Length(); - - - if (count > 1) { - auto url_string = ArgConverter::ConvertToString(info[0].As()); - auto base_string = ArgConverter::ConvertToString(info[1].As()); - std::string_view base_string_view(base_string.data(), base_string.length()); - value = can_parse(url_string, &base_string_view); + if (info.Length() > 1 && !info[1]->IsUndefined()) { + v8::Local base_input; + if (!info[1]->ToString(context).ToLocal(&base_input)) { + return; + } + auto base = ada::parse( + ArgConverter::ConvertToString(base_input), nullptr); + value = base && ada::parse(url_string, &base.value()); } else { - value = can_parse(ArgConverter::ConvertToString(info[0].As()).c_str(), nullptr); + value = ada::parse(url_string, nullptr).has_value(); } info.GetReturnValue().Set(value); diff --git a/test-app/runtime/src/main/cpp/URLPatternImpl.cpp b/test-app/runtime/src/main/cpp/URLPatternImpl.cpp index 70a483aba..a6eef3609 100644 --- a/test-app/runtime/src/main/cpp/URLPatternImpl.cpp +++ b/test-app/runtime/src/main/cpp/URLPatternImpl.cpp @@ -69,7 +69,9 @@ v8_regex_provider::regex_search(std::string_view input, auto array = matches.As(); auto len = array->Length(); ret.reserve(len); - for (int i = 0; i < len; i++) { + // Element 0 is the whole match. ada pairs exec_result[i] with + // group_name_list[i], so it expects only the capture groups. + for (uint32_t i = 1; i < len; i++) { v8::Local item; if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&item)) { return std::nullopt; @@ -146,7 +148,7 @@ v8::Local URLPatternImpl::GetCtor(v8::Isolate *isolate) { v8::FunctionTemplate::New(isolate, &Exec)); tmpl->SetLazyDataProperty( - ArgConverter::ConvertToV8String(isolate, "hasRegexpGroups"), + ArgConverter::ConvertToV8String(isolate, "hasRegExpGroups"), GetHasRegexpGroups); tmpl->SetLazyDataProperty(