LibWeb: Generate dedicated methods to create Web constructors/prototypes
Currently, for each exposed interface, we generate one massive function to create every Web constructor and prototype. In an effort to lazily create these instead, this first step is to extract the creation of each of these into its own method. First, this generates a forwarding header for all IDL types. This is to allow callers to remain unchanged without forcing them to include the (very heavy) generated IDL headers. This header is included by LibWeb's forwarding header. Next, this defines a base template method on Web::Bindings::Intrinsics to create a prototype/constructor pair. Specializations of this template are now generated in a new .cpp file, IntrinsicDefinitions.cpp. The base Intrinsics class is updated to use this new method, and will continue to cache the result. Last, some WebAssembly classes are updated to use this new mechanism. They were using some ad hoc cache keys that are now in line with the generated specializations. That one massive function is still used to invoke these specializations, so they are not lazy as of this commit.
This commit is contained in:
parent
59104262a4
commit
7bd8fd000f
Notes:
sideshowbarker
2024-07-17 07:43:44 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/7bd8fd000f Pull-request: https://github.com/SerenityOS/serenity/pull/16945 Reviewed-by: https://github.com/alimpfard
14 changed files with 313 additions and 107 deletions
|
@ -147,14 +147,18 @@ function (generate_js_bindings target)
|
|||
endfunction()
|
||||
|
||||
function(generate_exposed_interface_files)
|
||||
set(exposed_interface_sources DedicatedWorkerExposedInterfaces.cpp DedicatedWorkerExposedInterfaces.h
|
||||
SharedWorkerExposedInterfaces.cpp SharedWorkerExposedInterfaces.h
|
||||
WindowExposedInterfaces.cpp WindowExposedInterfaces.h)
|
||||
set(exposed_interface_sources
|
||||
Forward.h IntrinsicDefinitions.cpp
|
||||
DedicatedWorkerExposedInterfaces.cpp DedicatedWorkerExposedInterfaces.h
|
||||
SharedWorkerExposedInterfaces.cpp SharedWorkerExposedInterfaces.h
|
||||
WindowExposedInterfaces.cpp WindowExposedInterfaces.h)
|
||||
list(TRANSFORM exposed_interface_sources PREPEND "Bindings/")
|
||||
add_custom_command(
|
||||
OUTPUT ${exposed_interface_sources}
|
||||
COMMAND "${CMAKE_COMMAND}" -E make_directory "tmp"
|
||||
COMMAND $<TARGET_FILE:Lagom::GenerateWindowOrWorkerInterfaces> -o "${CMAKE_CURRENT_BINARY_DIR}/tmp" -b "${LIBWEB_INPUT_FOLDER}" ${LIBWEB_ALL_IDL_FILES}
|
||||
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/Forward.h "Bindings/Forward.h"
|
||||
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/IntrinsicDefinitions.cpp "Bindings/IntrinsicDefinitions.cpp"
|
||||
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/DedicatedWorkerExposedInterfaces.h "Bindings/DedicatedWorkerExposedInterfaces.h"
|
||||
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/DedicatedWorkerExposedInterfaces.cpp "Bindings/DedicatedWorkerExposedInterfaces.cpp"
|
||||
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/SharedWorkerExposedInterfaces.h "Bindings/SharedWorkerExposedInterfaces.h"
|
||||
|
|
|
@ -14,9 +14,241 @@
|
|||
#include <LibIDL/Types.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
static ErrorOr<void> add_to_interface_sets(IDL::Interface&, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed);
|
||||
static ErrorOr<void> add_to_interface_sets(IDL::Interface&, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed);
|
||||
static DeprecatedString s_error_string;
|
||||
|
||||
struct LegacyConstructor {
|
||||
DeprecatedString name;
|
||||
DeprecatedString constructor_class;
|
||||
};
|
||||
|
||||
static void consume_whitespace(GenericLexer& lexer)
|
||||
{
|
||||
bool consumed = true;
|
||||
while (consumed) {
|
||||
consumed = lexer.consume_while(is_ascii_space).length() > 0;
|
||||
|
||||
if (lexer.consume_specific("//")) {
|
||||
lexer.consume_until('\n');
|
||||
lexer.ignore();
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Optional<LegacyConstructor> const& lookup_legacy_constructor(IDL::Interface& interface)
|
||||
{
|
||||
static HashMap<StringView, Optional<LegacyConstructor>> s_legacy_constructors;
|
||||
if (auto cache = s_legacy_constructors.get(interface.name); cache.has_value())
|
||||
return cache.value();
|
||||
|
||||
auto attribute = interface.extended_attributes.get("LegacyFactoryFunction"sv);
|
||||
if (!attribute.has_value()) {
|
||||
s_legacy_constructors.set(interface.name, {});
|
||||
return s_legacy_constructors.get(interface.name).value();
|
||||
}
|
||||
|
||||
GenericLexer function_lexer(attribute.value());
|
||||
consume_whitespace(function_lexer);
|
||||
|
||||
auto name = function_lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '('; });
|
||||
auto constructor_class = DeprecatedString::formatted("{}Constructor", name);
|
||||
|
||||
s_legacy_constructors.set(interface.name, LegacyConstructor { name, move(constructor_class) });
|
||||
return s_legacy_constructors.get(interface.name).value();
|
||||
}
|
||||
|
||||
static ErrorOr<void> generate_forwarding_header(StringView output_path, Vector<IDL::Interface&>& exposed_interfaces)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator(builder);
|
||||
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
namespace Web::Bindings {
|
||||
)~~~");
|
||||
|
||||
auto add_interface = [](SourceGenerator& gen, StringView prototype_class, StringView constructor_class, Optional<LegacyConstructor> const& legacy_constructor) {
|
||||
gen.set("prototype_class", prototype_class);
|
||||
gen.set("constructor_class", constructor_class);
|
||||
|
||||
gen.append(R"~~~(
|
||||
class @prototype_class@;
|
||||
class @constructor_class@;)~~~");
|
||||
|
||||
if (legacy_constructor.has_value()) {
|
||||
gen.set("legacy_constructor_class", legacy_constructor->constructor_class);
|
||||
gen.append(R"~~~(
|
||||
class @legacy_constructor_class@;)~~~");
|
||||
}
|
||||
};
|
||||
|
||||
for (auto& interface : exposed_interfaces) {
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, interface.prototype_class, interface.constructor_class, lookup_legacy_constructor(interface));
|
||||
}
|
||||
|
||||
// FIXME: Special case window. We should convert Window and Location to use IDL.
|
||||
{
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, "WindowPrototype"sv, "WindowConstructor"sv, {});
|
||||
add_interface(gen, "LocationPrototype"sv, "LocationConstructor"sv, {});
|
||||
}
|
||||
|
||||
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
|
||||
{
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, "WebAssemblyMemoryPrototype"sv, "WebAssemblyMemoryConstructor"sv, {});
|
||||
add_interface(gen, "WebAssemblyInstancePrototype"sv, "WebAssemblyInstanceConstructor"sv, {});
|
||||
add_interface(gen, "WebAssemblyModulePrototype"sv, "WebAssemblyModuleConstructor"sv, {});
|
||||
add_interface(gen, "WebAssemblyTablePrototype"sv, "WebAssemblyTableConstructor"sv, {});
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
auto generated_forward_path = LexicalPath(output_path).append("Forward.h"sv).string();
|
||||
auto generated_forward_file = TRY(Core::Stream::File::open(generated_forward_path, Core::Stream::OpenMode::Write));
|
||||
TRY(generated_forward_file->write(generator.as_string_view().bytes()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
static ErrorOr<void> generate_intrinsic_definitions(StringView output_path, Vector<IDL::Interface&>& exposed_interfaces)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator(builder);
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <LibJS/Heap/DeferGC.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>)~~~");
|
||||
|
||||
for (auto& interface : exposed_interfaces) {
|
||||
auto gen = generator.fork();
|
||||
gen.set("prototype_class", interface.prototype_class);
|
||||
gen.set("constructor_class", interface.constructor_class);
|
||||
|
||||
gen.append(R"~~~(
|
||||
#include <LibWeb/Bindings/@constructor_class@.h>
|
||||
#include <LibWeb/Bindings/@prototype_class@.h>)~~~");
|
||||
|
||||
if (auto const& legacy_constructor = lookup_legacy_constructor(interface); legacy_constructor.has_value()) {
|
||||
gen.set("legacy_constructor_class", legacy_constructor->constructor_class);
|
||||
gen.append(R"~~~(
|
||||
#include <LibWeb/Bindings/@legacy_constructor_class@.h>)~~~");
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Special case window. We should convert Window and Location to use IDL.
|
||||
generator.append(R"~~~(
|
||||
#include <LibWeb/Bindings/WindowConstructor.h>
|
||||
#include <LibWeb/Bindings/WindowPrototype.h>
|
||||
#include <LibWeb/Bindings/LocationConstructor.h>
|
||||
#include <LibWeb/Bindings/LocationPrototype.h>)~~~");
|
||||
|
||||
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
|
||||
generator.append(R"~~~(
|
||||
#include <LibWeb/WebAssembly/WebAssemblyMemoryConstructor.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyModuleConstructor.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyModulePrototype.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyTableConstructor.h>
|
||||
#include <LibWeb/WebAssembly/WebAssemblyTablePrototype.h>)~~~");
|
||||
|
||||
generator.append(R"~~~(
|
||||
|
||||
namespace Web::Bindings {
|
||||
)~~~");
|
||||
|
||||
auto add_interface = [&](SourceGenerator& gen, StringView name, StringView prototype_class, StringView constructor_class, Optional<LegacyConstructor> const& legacy_constructor, bool attach_to_global) {
|
||||
gen.set("interface_name", name);
|
||||
gen.set("prototype_class", prototype_class);
|
||||
gen.set("constructor_class", constructor_class);
|
||||
|
||||
gen.append(R"~~~(
|
||||
template<>
|
||||
void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Realm& realm)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
auto prototype = heap().allocate<@prototype_class@>(realm, realm);
|
||||
m_prototypes.set("@interface_name@"sv, prototype);
|
||||
|
||||
auto constructor = heap().allocate<@constructor_class@>(realm, realm);
|
||||
m_constructors.set("@interface_name@"sv, constructor);
|
||||
)~~~");
|
||||
|
||||
if (attach_to_global) {
|
||||
gen.append(R"~~~(
|
||||
auto& global = realm.global_object();
|
||||
global.define_direct_property("@interface_name@", constructor.ptr(), JS::Attribute::Writable | JS::Attribute::Configurable);)~~~");
|
||||
}
|
||||
|
||||
gen.append(R"~~~(
|
||||
prototype->define_direct_property(vm.names.constructor, constructor.ptr(), JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@interface_name@"sv), JS::Attribute::Configurable);
|
||||
)~~~");
|
||||
|
||||
if (legacy_constructor.has_value()) {
|
||||
gen.set("legacy_interface_name", legacy_constructor->name);
|
||||
gen.set("legacy_constructor_class", legacy_constructor->constructor_class);
|
||||
gen.append(R"~~~(
|
||||
auto legacy_constructor = heap().allocate<@legacy_constructor_class@>(realm, realm);
|
||||
m_constructors.set("@legacy_interface_name@"sv, legacy_constructor);
|
||||
)~~~");
|
||||
|
||||
if (attach_to_global) {
|
||||
gen.append(R"~~~(
|
||||
global.define_direct_property("@legacy_interface_name@", legacy_constructor.ptr(), JS::Attribute::Writable | JS::Attribute::Configurable);)~~~");
|
||||
}
|
||||
|
||||
gen.append(R"~~~(
|
||||
legacy_constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@legacy_interface_name@"sv), JS::Attribute::Configurable);)~~~");
|
||||
}
|
||||
|
||||
gen.append(R"~~~(
|
||||
}
|
||||
)~~~");
|
||||
};
|
||||
|
||||
for (auto& interface : exposed_interfaces) {
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, interface.name, interface.prototype_class, interface.constructor_class, lookup_legacy_constructor(interface), true);
|
||||
}
|
||||
|
||||
// FIXME: Special case window. We should convert Window and Location to use IDL
|
||||
{
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, "Window"sv, "WindowPrototype"sv, "WindowConstructor"sv, {}, true);
|
||||
add_interface(gen, "Location"sv, "LocationPrototype"sv, "LocationConstructor"sv, {}, true);
|
||||
}
|
||||
|
||||
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
|
||||
{
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, "WebAssembly.Memory"sv, "WebAssemblyMemoryPrototype"sv, "WebAssemblyMemoryConstructor"sv, {}, false);
|
||||
add_interface(gen, "WebAssembly.Instance"sv, "WebAssemblyInstancePrototype"sv, "WebAssemblyInstanceConstructor"sv, {}, false);
|
||||
add_interface(gen, "WebAssembly.Module"sv, "WebAssemblyModulePrototype"sv, "WebAssemblyModuleConstructor"sv, {}, false);
|
||||
add_interface(gen, "WebAssembly.Table"sv, "WebAssemblyTablePrototype"sv, "WebAssemblyTableConstructor"sv, {}, false);
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
)~~~");
|
||||
|
||||
auto generated_intrinsics_path = LexicalPath(output_path).append("IntrinsicDefinitions.cpp"sv).string();
|
||||
auto generated_intrinsics_file = TRY(Core::Stream::File::open(generated_intrinsics_path, Core::Stream::OpenMode::Write));
|
||||
TRY(generated_intrinsics_file->write(generator.as_string_view().bytes()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
static ErrorOr<void> generate_exposed_interface_header(StringView class_name, StringView output_path)
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
@ -63,10 +295,14 @@ static ErrorOr<void> generate_exposed_interface_implementation(StringView class_
|
|||
gen.set("constructor_class", interface.constructor_class);
|
||||
|
||||
gen.append(R"~~~(#include <LibWeb/Bindings/@constructor_class@.h>
|
||||
#include <LibWeb/Bindings/@prototype_class@.h>
|
||||
)~~~");
|
||||
if (interface.parent_name != "[Synthetic Interface]"sv)
|
||||
gen.append(R"~~~(#include <LibWeb/Bindings/@prototype_class@.h>
|
||||
|
||||
if (auto const& legacy_constructor = lookup_legacy_constructor(interface); legacy_constructor.has_value()) {
|
||||
gen.set("legacy_constructor_class", legacy_constructor->constructor_class);
|
||||
gen.append(R"~~~(#include <LibWeb/Bindings/@legacy_constructor_class@.h>
|
||||
)~~~");
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Special case window. We should convert Window and Location to use IDL
|
||||
|
@ -92,36 +328,31 @@ void add_@global_object_snake_name@_exposed_interfaces(JS::Object& global, JS::R
|
|||
JS::DeferGC defer_gc(vm.heap());
|
||||
)~~~");
|
||||
|
||||
auto add_interface = [](SourceGenerator& gen, StringView name, StringView prototype_class, StringView constructor_class) {
|
||||
auto add_interface = [](SourceGenerator& gen, StringView name, StringView prototype_class) {
|
||||
gen.set("interface_name", name);
|
||||
gen.set("prototype_class", prototype_class);
|
||||
gen.set("constructor_class", constructor_class);
|
||||
|
||||
gen.append(R"~~~( {
|
||||
auto& prototype = Bindings::ensure_web_prototype<Bindings::@prototype_class@>(realm, "@interface_name@");
|
||||
auto& constructor = Bindings::ensure_web_constructor<Bindings::@constructor_class@>(realm, "@interface_name@");
|
||||
global.define_direct_property("@interface_name@", &constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
prototype.define_direct_property(vm.names.constructor, &constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
constructor.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@interface_name@"), JS::Attribute::Configurable);
|
||||
}
|
||||
)~~~"); };
|
||||
gen.append(R"~~~(
|
||||
(void)ensure_web_prototype<@prototype_class@>(realm, "@interface_name@"sv);)~~~"); };
|
||||
|
||||
for (auto& interface : exposed_interfaces) {
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, interface.name, interface.prototype_class, interface.constructor_class);
|
||||
add_interface(gen, interface.name, interface.prototype_class);
|
||||
}
|
||||
|
||||
// FIXME: Special case window. We should convert Window and Location to use IDL
|
||||
if (class_name == "Window"sv) {
|
||||
auto gen = generator.fork();
|
||||
add_interface(gen, "Window"sv, "WindowPrototype"sv, "WindowConstructor"sv);
|
||||
add_interface(gen, "Location"sv, "LocationPrototype"sv, "LocationConstructor"sv);
|
||||
add_interface(gen, "Window"sv, "WindowPrototype"sv);
|
||||
add_interface(gen, "Location"sv, "LocationPrototype"sv);
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
auto generated_implementation_path = LexicalPath(output_path).append(DeprecatedString::formatted("{}ExposedInterfaces.cpp", class_name)).string();
|
||||
auto generated_implementation_file = TRY(Core::Stream::File::open(generated_implementation_path, Core::Stream::OpenMode::Write));
|
||||
TRY(generated_implementation_file->write(generator.as_string_view().bytes()));
|
||||
|
@ -162,6 +393,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
VERIFY(paths.size() == file_contents.size());
|
||||
|
||||
Vector<IDL::Parser> parsers;
|
||||
Vector<IDL::Interface&> intrinsics;
|
||||
Vector<IDL::Interface&> window_exposed;
|
||||
Vector<IDL::Interface&> dedicated_worker_exposed;
|
||||
Vector<IDL::Interface&> shared_worker_exposed;
|
||||
|
@ -169,10 +401,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
for (size_t i = 0; i < paths.size(); ++i) {
|
||||
IDL::Parser parser(paths[i], file_contents[i], lexical_base.string());
|
||||
TRY(add_to_interface_sets(parser.parse(), window_exposed, dedicated_worker_exposed, shared_worker_exposed));
|
||||
TRY(add_to_interface_sets(parser.parse(), intrinsics, window_exposed, dedicated_worker_exposed, shared_worker_exposed));
|
||||
parsers.append(move(parser));
|
||||
}
|
||||
|
||||
TRY(generate_forwarding_header(output_path, intrinsics));
|
||||
TRY(generate_intrinsic_definitions(output_path, intrinsics));
|
||||
|
||||
TRY(generate_exposed_interface_header("Window"sv, output_path));
|
||||
TRY(generate_exposed_interface_header("DedicatedWorker"sv, output_path));
|
||||
TRY(generate_exposed_interface_header("SharedWorker"sv, output_path));
|
||||
|
@ -186,20 +421,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void consume_whitespace(GenericLexer& lexer)
|
||||
{
|
||||
bool consumed = true;
|
||||
while (consumed) {
|
||||
consumed = lexer.consume_while(is_ascii_space).length() > 0;
|
||||
|
||||
if (lexer.consume_specific("//")) {
|
||||
lexer.consume_until('\n');
|
||||
lexer.ignore();
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ExposedTo {
|
||||
Nobody = 0x0,
|
||||
DedicatedWorker = 0x1,
|
||||
|
@ -264,30 +485,15 @@ static ErrorOr<ExposedTo> parse_exposure_set(IDL::Interface& interface)
|
|||
return Error::from_string_view(s_error_string);
|
||||
}
|
||||
|
||||
static IDL::Interface& add_synthetic_interface(IDL::Interface& reference_interface)
|
||||
{
|
||||
static Vector<NonnullOwnPtr<IDL::Interface>> s_synthetic_interfaces;
|
||||
|
||||
GenericLexer function_lexer(reference_interface.extended_attributes.get("LegacyFactoryFunction").value());
|
||||
consume_whitespace(function_lexer);
|
||||
auto name = function_lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '('; });
|
||||
|
||||
auto new_interface = make<IDL::Interface>();
|
||||
new_interface->name = name;
|
||||
new_interface->constructor_class = DeprecatedString::formatted("{}Constructor", new_interface->name);
|
||||
new_interface->prototype_class = reference_interface.prototype_class;
|
||||
new_interface->parent_name = "[Synthetic Interface]";
|
||||
|
||||
s_synthetic_interfaces.append(move(new_interface));
|
||||
return *s_synthetic_interfaces.last();
|
||||
}
|
||||
|
||||
ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed)
|
||||
ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed)
|
||||
{
|
||||
// TODO: Add service worker exposed and audio worklet exposed
|
||||
auto whom = TRY(parse_exposure_set(interface));
|
||||
VERIFY(whom != ExposedTo::Nobody);
|
||||
|
||||
if ((whom & ExposedTo::Window) || (whom & ExposedTo::DedicatedWorker) || (whom & ExposedTo::SharedWorker))
|
||||
intrinsics.append(interface);
|
||||
|
||||
if (whom & ExposedTo::Window)
|
||||
window_exposed.append(interface);
|
||||
|
||||
|
@ -297,17 +503,5 @@ ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Inter
|
|||
if (whom & ExposedTo::SharedWorker)
|
||||
shared_worker_exposed.append(interface);
|
||||
|
||||
if (interface.extended_attributes.contains("LegacyFactoryFunction")) {
|
||||
auto& synthetic_interface = add_synthetic_interface(interface);
|
||||
if (whom & ExposedTo::Window)
|
||||
window_exposed.append(synthetic_interface);
|
||||
|
||||
if (whom & ExposedTo::DedicatedWorker)
|
||||
dedicated_worker_exposed.append(synthetic_interface);
|
||||
|
||||
if (whom & ExposedTo::SharedWorker)
|
||||
shared_worker_exposed.append(synthetic_interface);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -27,36 +27,34 @@ public:
|
|||
|
||||
JS::Object& cached_web_prototype(DeprecatedString const& class_name);
|
||||
|
||||
template<typename T>
|
||||
template<typename PrototypeType>
|
||||
JS::Object& ensure_web_prototype(DeprecatedString const& class_name)
|
||||
{
|
||||
auto it = m_prototypes.find(class_name);
|
||||
if (it != m_prototypes.end())
|
||||
if (auto it = m_prototypes.find(class_name); it != m_prototypes.end())
|
||||
return *it->value;
|
||||
auto& realm = *m_realm;
|
||||
auto prototype = heap().allocate<T>(realm, realm);
|
||||
m_prototypes.set(class_name, prototype);
|
||||
return prototype;
|
||||
|
||||
create_web_prototype_and_constructor<PrototypeType>(*m_realm);
|
||||
return *m_prototypes.find(class_name)->value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename PrototypeType>
|
||||
JS::NativeFunction& ensure_web_constructor(DeprecatedString const& class_name)
|
||||
{
|
||||
auto it = m_constructors.find(class_name);
|
||||
if (it != m_constructors.end())
|
||||
if (auto it = m_constructors.find(class_name); it != m_constructors.end())
|
||||
return *it->value;
|
||||
auto& realm = *m_realm;
|
||||
auto constructor = heap().allocate<T>(realm, realm);
|
||||
m_constructors.set(class_name, constructor);
|
||||
return constructor;
|
||||
|
||||
create_web_prototype_and_constructor<PrototypeType>(*m_realm);
|
||||
return *m_constructors.find(class_name)->value;
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void visit_edges(JS::Cell::Visitor&) override;
|
||||
|
||||
HashMap<DeprecatedString, JS::Object*> m_prototypes;
|
||||
HashMap<DeprecatedString, JS::NativeFunction*> m_constructors;
|
||||
template<typename PrototypeType>
|
||||
void create_web_prototype_and_constructor(JS::Realm& realm);
|
||||
|
||||
HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_prototypes;
|
||||
HashMap<DeprecatedString, JS::GCPtr<JS::NativeFunction>> m_constructors;
|
||||
JS::NonnullGCPtr<JS::Realm> m_realm;
|
||||
};
|
||||
|
||||
|
|
|
@ -10,6 +10,17 @@
|
|||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Fetch/HeadersIterator.h>
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
template<>
|
||||
void Intrinsics::create_web_prototype_and_constructor<HeadersIteratorPrototype>(JS::Realm& realm)
|
||||
{
|
||||
auto prototype = heap().allocate<HeadersIteratorPrototype>(realm, realm);
|
||||
m_prototypes.set("HeadersIterator"sv, prototype);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Web::Fetch {
|
||||
|
||||
JS::NonnullGCPtr<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/Bindings/Forward.h>
|
||||
|
||||
namespace Web {
|
||||
class XMLDocumentBuilder;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,17 @@
|
|||
#include <LibWeb/Bindings/URLSearchParamsIteratorPrototype.h>
|
||||
#include <LibWeb/URL/URLSearchParamsIterator.h>
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
template<>
|
||||
void Intrinsics::create_web_prototype_and_constructor<URLSearchParamsIteratorPrototype>(JS::Realm& realm)
|
||||
{
|
||||
auto prototype = heap().allocate<URLSearchParamsIteratorPrototype>(realm, realm);
|
||||
m_prototypes.set("URLSearchParamsIterator"sv, prototype);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Web::URL {
|
||||
|
||||
JS::NonnullGCPtr<URLSearchParamsIterator> URLSearchParamsIterator::create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
|
||||
|
|
|
@ -44,7 +44,7 @@ void WebAssemblyInstanceConstructor::initialize(JS::Realm& realm)
|
|||
auto& vm = this->vm();
|
||||
|
||||
NativeFunction::initialize(realm);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssemblyInstancePrototype"), 0);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"), 0);
|
||||
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
namespace Web::Bindings {
|
||||
|
||||
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssemblyInstancePrototype"))
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"))
|
||||
, m_index(index)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ void WebAssemblyMemoryConstructor::initialize(JS::Realm& realm)
|
|||
auto& vm = this->vm();
|
||||
|
||||
NativeFunction::initialize(realm);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyMemoryPrototype>(realm, "WebAssemblyMemoryPrototype"), 0);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyMemoryPrototype>(realm, "WebAssembly.Memory"), 0);
|
||||
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ void WebAssemblyModuleConstructor::initialize(JS::Realm& realm)
|
|||
auto& vm = this->vm();
|
||||
|
||||
NativeFunction::initialize(realm);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssemblyModulePrototype"), 0);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssembly.Module"), 0);
|
||||
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
namespace Web::Bindings {
|
||||
|
||||
WebAssemblyModuleObject::WebAssemblyModuleObject(JS::Realm& realm, size_t index)
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssemblyModulePrototype"))
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssembly.Module"))
|
||||
, m_index(index)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -40,30 +40,16 @@ void WebAssemblyObject::initialize(JS::Realm& realm)
|
|||
define_native_function(realm, "compile", compile, 1, attr);
|
||||
define_native_function(realm, "instantiate", instantiate, 1, attr);
|
||||
|
||||
auto& vm = this->vm();
|
||||
|
||||
auto& memory_constructor = Bindings::ensure_web_constructor<WebAssemblyMemoryConstructor>(realm, "WebAssembly.Memory");
|
||||
memory_constructor.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "WebAssembly.Memory"), JS::Attribute::Configurable);
|
||||
auto& memory_prototype = Bindings::ensure_web_prototype<WebAssemblyMemoryPrototype>(realm, "WebAssemblyMemoryPrototype");
|
||||
memory_prototype.define_direct_property(vm.names.constructor, &memory_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
auto& memory_constructor = Bindings::ensure_web_constructor<WebAssemblyMemoryPrototype>(realm, "WebAssembly.Memory"sv);
|
||||
define_direct_property("Memory", &memory_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
|
||||
auto& instance_constructor = Bindings::ensure_web_constructor<WebAssemblyInstanceConstructor>(realm, "WebAssembly.Instance");
|
||||
instance_constructor.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "WebAssembly.Instance"), JS::Attribute::Configurable);
|
||||
auto& instance_prototype = Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssemblyInstancePrototype");
|
||||
instance_prototype.define_direct_property(vm.names.constructor, &instance_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
auto& instance_constructor = Bindings::ensure_web_constructor<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"sv);
|
||||
define_direct_property("Instance", &instance_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
|
||||
auto& module_constructor = Bindings::ensure_web_constructor<WebAssemblyModuleConstructor>(realm, "WebAssembly.Module");
|
||||
module_constructor.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "WebAssembly.Module"), JS::Attribute::Configurable);
|
||||
auto& module_prototype = Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssemblyModulePrototype");
|
||||
module_prototype.define_direct_property(vm.names.constructor, &module_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
auto& module_constructor = Bindings::ensure_web_constructor<WebAssemblyModulePrototype>(realm, "WebAssembly.Module"sv);
|
||||
define_direct_property("Module", &module_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
|
||||
auto& table_constructor = Bindings::ensure_web_constructor<WebAssemblyTableConstructor>(realm, "WebAssembly.Table");
|
||||
table_constructor.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "WebAssembly.Table"), JS::Attribute::Configurable);
|
||||
auto& table_prototype = Bindings::ensure_web_prototype<WebAssemblyTablePrototype>(realm, "WebAssemblyTablePrototype");
|
||||
table_prototype.define_direct_property(vm.names.constructor, &table_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
auto& table_constructor = Bindings::ensure_web_constructor<WebAssemblyTablePrototype>(realm, "WebAssembly.Table"sv);
|
||||
define_direct_property("Table", &table_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
@ -483,7 +469,7 @@ JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress add
|
|||
}
|
||||
|
||||
WebAssemblyMemoryObject::WebAssemblyMemoryObject(JS::Realm& realm, Wasm::MemoryAddress address)
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyMemoryPrototype>(realm, "WebAssemblyMemoryPrototype"))
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyMemoryPrototype>(realm, "WebAssembly.Memory"))
|
||||
, m_address(address)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ void WebAssemblyTableConstructor::initialize(JS::Realm& realm)
|
|||
auto& vm = this->vm();
|
||||
|
||||
NativeFunction::initialize(realm);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyTablePrototype>(realm, "WebAssemblyTablePrototype"), 0);
|
||||
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyTablePrototype>(realm, "WebAssembly.Table"), 0);
|
||||
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
namespace Web::Bindings {
|
||||
|
||||
WebAssemblyTableObject::WebAssemblyTableObject(JS::Realm& realm, Wasm::TableAddress address)
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyTablePrototype>(realm, "WebAssemblyTablePrototype"))
|
||||
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyTablePrototype>(realm, "WebAssembly.Table"))
|
||||
, m_address(address)
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue