Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick 3 ladybird PRs #25403

Merged
merged 4 commits into from
Nov 16, 2024
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
3 changes: 3 additions & 0 deletions AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ class StringView {

[[nodiscard]] constexpr int compare(StringView other) const
{
if (m_length == 0 && other.m_length == 0)
return 0;

if (m_characters == nullptr)
return other.m_characters ? -1 : 0;

Expand Down
47 changes: 47 additions & 0 deletions AK/Utf8View.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <AK/Format.h>
#include <AK/Function.h>
#include <AK/StringView.h>
#include <AK/Types.h>

Expand Down Expand Up @@ -162,6 +163,52 @@ class Utf8View {
return true;
}

template<typename Callback>
auto for_each_split_view(Function<bool(u32)> splitter, SplitBehavior split_behavior, Callback callback) const
{
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
bool keep_trailing_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);

auto start_offset = 0u;
auto offset = 0u;

auto run_callback = [&]() {
auto length = offset - start_offset;

if (length == 0 && !keep_empty)
return;

auto substring = unicode_substring_view(start_offset, length);

// Reject splitter-only entries if we're not keeping empty results
if (keep_trailing_separator && !keep_empty && length == 1 && splitter(*substring.begin()))
return;

callback(substring);
};

auto iterator = begin();
while (iterator != end()) {
if (splitter(*iterator)) {
if (keep_trailing_separator)
++offset;

run_callback();

if (!keep_trailing_separator)
++offset;

start_offset = offset;
++iterator;
continue;
}

++offset;
++iterator;
}
run_callback();
}

private:
friend class Utf8CodePointIterator;

Expand Down
7 changes: 7 additions & 0 deletions Tests/AK/TestStringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ TEST_CASE(compare_views)
EXPECT_EQ(view1, foo1);
EXPECT_EQ(view1, foo2);
EXPECT_EQ(view1, "foo");

ByteString empty = "";
auto empty_view = view1.substring_view(0, 0);
StringView default_view = {};
EXPECT_EQ(empty.view(), ""sv);
EXPECT_EQ(empty_view, ""sv);
EXPECT_EQ(default_view, ""sv);
}

TEST_CASE(string_view_literal_operator)
Expand Down
23 changes: 23 additions & 0 deletions Tests/AK/TestUtf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,26 @@ TEST_CASE(trim)
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "\u180E");
}
}

static bool is_period(u32 code_point) { return code_point == '.'; }

TEST_CASE(for_each_split_view)
{
Utf8View view { "...Well..hello.friends!..."sv };
auto gather = [&](auto split_behavior) {
Vector<StringView> results;
view.for_each_split_view(is_period, split_behavior, [&](auto part) {
results.append(part.as_string());
});
return results;
};

EXPECT_EQ(gather(SplitBehavior::Nothing),
Vector({ "Well"sv, "hello"sv, "friends!"sv }));
EXPECT_EQ(gather(SplitBehavior::KeepEmpty),
Vector({ ""sv, ""sv, ""sv, "Well"sv, ""sv, "hello"sv, "friends!"sv, ""sv, ""sv, ""sv }));
EXPECT_EQ(gather(SplitBehavior::KeepTrailingSeparator),
Vector({ "Well."sv, "hello."sv, "friends!."sv }));
EXPECT_EQ(gather(SplitBehavior::KeepEmpty | SplitBehavior::KeepTrailingSeparator),
Vector({ "."sv, "."sv, "."sv, "Well."sv, "."sv, "hello."sv, "friends!."sv, "."sv, "."sv, ""sv }));
}
2 changes: 1 addition & 1 deletion Userland/Libraries/LibCore/MimeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static Array const s_registered_mime_type = {
MimeType { .name = "application/x-blender"sv, .common_extensions = { ".blend"sv, ".blended"sv }, .description = "Blender project file"sv, .magic_bytes = Vector<u8> { 'B', 'L', 'E', 'N', 'D', 'E', 'R' } },
MimeType { .name = "application/x-bzip2"sv, .common_extensions = { ".bz2"sv }, .description = "BZIP2 compressed data"sv, .magic_bytes = Vector<u8> { 'B', 'Z', 'h' } },
MimeType { .name = "application/x-sheets+json"sv, .common_extensions = { ".sheets"sv }, .description = "Serenity Spreadsheet document"sv },
MimeType { .name = "application/xhtml+xml"sv, .common_extensions = { ".xhtml"sv }, .description = "XHTML document"sv },
MimeType { .name = "application/xhtml+xml"sv, .common_extensions = { ".xhtml"sv, ".xht"sv }, .description = "XHTML document"sv },
MimeType { .name = "application/zip"sv, .common_extensions = { ".zip"sv }, .description = "ZIP archive"sv, .magic_bytes = Vector<u8> { 0x50, 0x4B } },

MimeType { .name = "audio/flac"sv, .common_extensions = { ".flac"sv }, .description = "FLAC audio"sv, .magic_bytes = Vector<u8> { 'f', 'L', 'a', 'C' } },
Expand Down
10 changes: 6 additions & 4 deletions Userland/Libraries/LibJS/SourceTextModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,10 @@ ThrowCompletionOr<void> SourceTextModule::execute_module(VM& vm, GCPtr<PromiseCa
// the top-level module code.
// FIXME: Improve this situation, so we can match the spec better.

// AD-HOC: We push/pop the moduleContext around the function construction to ensure that the async execution context
// captures the module execution context.
vm.push_execution_context(*module_context);

FunctionParsingInsights parsing_insights;
parsing_insights.uses_this_from_environment = true;
parsing_insights.uses_this = true;
Expand All @@ -768,12 +772,10 @@ ThrowCompletionOr<void> SourceTextModule::execute_module(VM& vm, GCPtr<PromiseCa
{}, 0, {}, environment(), nullptr, FunctionKind::Async, true, parsing_insights);
module_wrapper_function->set_is_module_wrapper(true);

// AD-HOC: We push/pop the moduleContext around the call to ensure that the async execution context
// captures the module execution context.
vm.push_execution_context(*module_context);
auto result = call(vm, Value { module_wrapper_function }, js_undefined(), ReadonlySpan<Value> {});
vm.pop_execution_context();

auto result = call(vm, Value { module_wrapper_function }, js_undefined(), ReadonlySpan<Value> {});

// AD-HOC: This is basically analogous to what AsyncBlockStart would do.
if (result.is_throw_completion()) {
MUST(call(vm, *capability->reject(), js_undefined(), result.throw_completion().value().value()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ describe("normal behavior", () => {

expect(value).not.toHaveProperty("default", null);
expect(value).not.toHaveProperty("bar", null);
expect(value).not.toHaveProperty("baz", null);
expect(value).not.toHaveProperty("qux", null);
passed = true;
})
.catch(value => {
error = value;
});
runQueuedPromiseJobs();
expect(error).toBeNull();
expect(passed).toBeTrue();
});

test("value from async module from top-level awaited function", () => {
const shadowRealm = new ShadowRealm();
const promise = shadowRealm.importValue("./async-module.mjs", "qux");
expect(promise).toBeInstanceOf(Promise);
let error = null;
let passed = false;
promise
.then(value => {
expect(value).toBe("'qux' export");
expect(typeof value).toBe("string");

expect(value).not.toHaveProperty("default", null);
expect(value).not.toHaveProperty("foo", null);
expect(value).not.toHaveProperty("bar", null);
expect(value).not.toHaveProperty("baz", null);
passed = true;
})
.catch(value => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export default "Default export";
await Promise.resolve(2);

export const bar = "'bar' export";

async function baz() {
return "'qux' export";
}

export const qux = await baz();
Loading