2022-11-23 14:18:38 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
|
2023-10-28 23:45:38 +02:00
|
|
|
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
|
2022-11-23 14:18:38 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2025-09-22 16:46:22 -07:00
|
|
|
#include <AK/StdLibExtras.h>
|
2025-08-06 11:12:58 -04:00
|
|
|
#include <AK/Utf16FlyString.h>
|
2022-11-23 14:18:38 +01:00
|
|
|
#include <AK/Vector.h>
|
2023-10-28 23:45:38 +02:00
|
|
|
#include <LibJS/Module.h>
|
2022-11-23 14:18:38 +01:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2025-04-28 15:03:56 -04:00
|
|
|
// https://tc39.es/ecma262/#importattribute-record
|
2023-12-02 16:20:01 +01:00
|
|
|
struct ImportAttribute {
|
2025-08-06 11:12:58 -04:00
|
|
|
Utf16String key;
|
|
|
|
|
Utf16String value;
|
2024-12-01 13:03:11 +13:00
|
|
|
|
|
|
|
|
bool operator==(ImportAttribute const&) const = default;
|
2023-12-02 16:20:01 +01:00
|
|
|
};
|
2022-11-23 14:18:38 +01:00
|
|
|
|
2025-09-22 16:46:22 -07:00
|
|
|
// https://tc39.es/ecma262/#loadedmodulerequest-record
|
|
|
|
|
struct LoadedModuleRequest {
|
|
|
|
|
Utf16String specifier; // [[Specifier]]
|
|
|
|
|
Vector<ImportAttribute> attributes; // [[Attributes]]
|
|
|
|
|
GC::Ref<Module> module; // [[Module]]
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-28 15:03:56 -04:00
|
|
|
// https://tc39.es/ecma262/#modulerequest-record
|
2025-07-19 10:41:08 -07:00
|
|
|
struct ModuleRequest {
|
2022-11-23 14:18:38 +01:00
|
|
|
ModuleRequest() = default;
|
|
|
|
|
|
2025-08-06 11:12:58 -04:00
|
|
|
explicit ModuleRequest(Utf16FlyString specifier)
|
2022-11-23 14:18:38 +01:00
|
|
|
: module_specifier(move(specifier))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 11:12:58 -04:00
|
|
|
ModuleRequest(Utf16FlyString specifier, Vector<ImportAttribute> attributes);
|
2022-11-23 14:18:38 +01:00
|
|
|
|
2025-08-06 11:12:58 -04:00
|
|
|
void add_attribute(Utf16String key, Utf16String value)
|
2022-11-23 14:18:38 +01:00
|
|
|
{
|
2023-12-02 16:20:01 +01:00
|
|
|
attributes.empend(move(key), move(value));
|
2022-11-23 14:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-06 11:12:58 -04:00
|
|
|
Utf16FlyString module_specifier; // [[Specifier]]
|
2025-03-18 18:08:02 -05:00
|
|
|
Vector<ImportAttribute> attributes; // [[Attributes]]
|
2024-12-01 13:03:11 +13:00
|
|
|
|
|
|
|
|
bool operator==(ModuleRequest const&) const = default;
|
2022-11-23 14:18:38 +01:00
|
|
|
};
|
|
|
|
|
|
2025-09-22 16:46:22 -07:00
|
|
|
inline auto const& specifier_of(ModuleRequest const& r) { return r.module_specifier; }
|
|
|
|
|
inline auto const& specifier_of(LoadedModuleRequest const& r) { return r.specifier; }
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
concept ModuleRequestLike = IsOneOf<RemoveCVReference<T>, ModuleRequest, LoadedModuleRequest>;
|
|
|
|
|
|
|
|
|
|
// 16.2.1.3.1 ModuleRequestsEqual ( left, right ), https://tc39.es/ecma262/#sec-modulerequestsequal
|
|
|
|
|
template<ModuleRequestLike L, ModuleRequestLike R>
|
|
|
|
|
bool module_requests_equal(L const& left, R const& right)
|
|
|
|
|
{
|
|
|
|
|
// 1. If left.[[Specifier]] is not right.[[Specifier]], return false.
|
|
|
|
|
if (specifier_of(left) != specifier_of(right))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 2. Let leftAttrs be left.[[Attributes]].
|
|
|
|
|
// 3. Let rightAttrs be right.[[Attributes]].
|
|
|
|
|
auto const& left_attrs = left.attributes;
|
|
|
|
|
auto const& right_attrs = right.attributes;
|
|
|
|
|
|
|
|
|
|
// 4. Let leftAttrsCount be the number of elements in leftAttrs.
|
|
|
|
|
// 5. Let rightAttrsCount be the number of elements in rightAttrs.
|
|
|
|
|
// 6. If leftAttrsCount ≠ rightAttrsCount, return false.
|
|
|
|
|
if (left_attrs.size() != right_attrs.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 7. For each ImportAttribute Record l of leftAttrs
|
|
|
|
|
// a. If rightAttrs does not contain an ImportAttribute Record r such that l.[[Key]] is r.[[Key]] and l.[[Value]] is r.[[Value]], return false.
|
|
|
|
|
// 8. Return true.
|
|
|
|
|
return AK::all_of(left_attrs, [&right_attrs](auto const& l) {
|
|
|
|
|
return AK::any_of(right_attrs, [&l](auto const& r) {
|
|
|
|
|
return l.key == r.key && l.value == r.value;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:18:38 +01:00
|
|
|
}
|