Skip to content
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
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
52 changes: 52 additions & 0 deletions test-app/app/src/main/assets/app/tests/testURLImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

29 changes: 29 additions & 0 deletions test-app/app/src/main/assets/app/tests/testURLPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
});
});
3 changes: 3 additions & 0 deletions test-app/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
114 changes: 44 additions & 70 deletions test-app/runtime/src/main/cpp/URLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#include "URLImpl.h"

#include <optional>

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<v8::Object> object) {
if (object->InternalFieldCount() < 1) {
Expand Down Expand Up @@ -90,85 +92,48 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &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<v8::String>());

std::optional<std::string> base_string;
if (count > 1) {
if (args[1]->IsString()) {
auto base_string = ArgConverter::ConvertToString(args[1].As<v8::String>());
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<ada::url_aggregator>(base_string_view, nullptr);

auto result = ada::parse<ada::url_aggregator>(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<v8::String>());
} 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<v8::String> 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<ada::url_aggregator>(base_string_view, nullptr);

auto result = ada::parse<ada::url_aggregator>(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<ada::url_aggregator>(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_aggregator>(url_string, nullptr);
}
} else {
auto result = ada::parse<ada::url_aggregator>(url_string, nullptr);
if (result) {
url = result.value();
} else {
isolate->ThrowException(
v8::Exception::TypeError(v8::String::Empty(isolate)));
return;
auto base = ada::parse<url_aggregator>(*base_string, nullptr);
if (!base) {
return base;
}
return ada::parse<url_aggregator>(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);
Expand Down Expand Up @@ -495,17 +460,26 @@ void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &info) {


void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &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<v8::String> 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<v8::String>());
auto base_string = ArgConverter::ConvertToString(info[1].As<v8::String>());
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<v8::String> base_input;
if (!info[1]->ToString(context).ToLocal(&base_input)) {
return;
}
auto base = ada::parse<url_aggregator>(
ArgConverter::ConvertToString(base_input), nullptr);
value = base && ada::parse<url_aggregator>(url_string, &base.value());
} else {
value = can_parse(ArgConverter::ConvertToString(info[0].As<v8::String>()).c_str(), nullptr);
value = ada::parse<url_aggregator>(url_string, nullptr).has_value();
}

info.GetReturnValue().Set(value);
Expand Down
6 changes: 4 additions & 2 deletions test-app/runtime/src/main/cpp/URLPatternImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ v8_regex_provider::regex_search(std::string_view input,
auto array = matches.As<v8::Array>();
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<v8::Value> item;
if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&item)) {
return std::nullopt;
Expand Down Expand Up @@ -146,7 +148,7 @@ v8::Local<v8::FunctionTemplate> URLPatternImpl::GetCtor(v8::Isolate *isolate) {
v8::FunctionTemplate::New(isolate, &Exec));

tmpl->SetLazyDataProperty(
ArgConverter::ConvertToV8String(isolate, "hasRegexpGroups"),
ArgConverter::ConvertToV8String(isolate, "hasRegExpGroups"),
GetHasRegexpGroups);

tmpl->SetLazyDataProperty(
Expand Down