LibWeb: Generate Window{Constructor,Prototype} from IDL

The Window object is massive, so let's do the conversion to IDL step
by step. First up: getting rid of the manual constructor and prototype
definitions, which can be generated from an empty `interface Window`.
This commit is contained in:
Linus Groh 2023-03-05 15:42:58 +00:00
parent cdc77407bf
commit de83f5422d
Notes: sideshowbarker 2024-07-16 23:34:44 +09:00
8 changed files with 7 additions and 151 deletions

View file

@ -89,12 +89,6 @@ class @legacy_constructor_class@;)~~~");
add_interface(gen, interface.prototype_class, interface.constructor_class, lookup_legacy_constructor(interface));
}
// FIXME: Special case window. We should convert Window to use IDL.
{
auto gen = generator.fork();
add_interface(gen, "WindowPrototype"sv, "WindowConstructor"sv, {});
}
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
{
auto gen = generator.fork();
@ -142,11 +136,6 @@ static ErrorOr<void> generate_intrinsic_definitions(StringView output_path, Vect
}
}
// FIXME: Special case window. We should convert Window to use IDL.
generator.append(R"~~~(
#include <LibWeb/Bindings/WindowConstructor.h>
#include <LibWeb/Bindings/WindowPrototype.h>)~~~");
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
generator.append(R"~~~(
#include <LibWeb/WebAssembly/WebAssemblyMemoryConstructor.h>
@ -204,12 +193,6 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
add_interface(gen, interface.name, interface.prototype_class, interface.constructor_class, lookup_legacy_constructor(interface));
}
// FIXME: Special case window. We should convert Window to use IDL
{
auto gen = generator.fork();
add_interface(gen, "Window"sv, "WindowPrototype"sv, "WindowConstructor"sv, {});
}
// FIXME: Special case WebAssembly. We should convert WASM to use IDL.
{
auto gen = generator.fork();
@ -285,13 +268,6 @@ static ErrorOr<void> generate_exposed_interface_implementation(StringView class_
}
}
// FIXME: Special case window. We should convert Window to use IDL
if (class_name == "Window"sv) {
generator.append(R"~~~(#include <LibWeb/Bindings/WindowConstructor.h>
#include <LibWeb/Bindings/WindowPrototype.h>
)~~~");
}
generator.append(R"~~~(
namespace Web::Bindings {
@ -319,12 +295,6 @@ void add_@global_object_snake_name@_exposed_interfaces(JS::Object& global)
add_interface(gen, interface.name, interface.prototype_class, lookup_legacy_constructor(interface));
}
// FIXME: Special case window. We should convert Window to use IDL
if (class_name == "Window"sv) {
auto gen = generator.fork();
add_interface(gen, "Window"sv, "WindowPrototype"sv, {});
}
generator.append(R"~~~(
}

View file

@ -1,41 +0,0 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/WindowConstructor.h>
#include <LibWeb/Bindings/WindowPrototype.h>
namespace Web::Bindings {
WindowConstructor::WindowConstructor(JS::Realm& realm)
: NativeFunction(*realm.intrinsics().function_prototype())
{
}
WindowConstructor::~WindowConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WindowConstructor::call()
{
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "Window");
}
JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WindowConstructor::construct(FunctionObject&)
{
return vm().throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, "Window");
}
JS::ThrowCompletionOr<void> WindowConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
define_direct_property(vm.names.prototype, &ensure_web_prototype<Bindings::WindowPrototype>(realm, "Window"), 0);
define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
return {};
}
}

View file

@ -1,28 +0,0 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace Web::Bindings {
class WindowConstructor : public JS::NativeFunction {
JS_OBJECT(WindowConstructor, JS::NativeFunction);
public:
explicit WindowConstructor(JS::Realm&);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual ~WindowConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }
};
}

View file

@ -1,25 +0,0 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/WindowPrototype.h>
namespace Web::Bindings {
WindowPrototype::WindowPrototype(JS::Realm& realm)
: JS::Object(realm, nullptr)
{
}
JS::ThrowCompletionOr<void> WindowPrototype::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::EventTargetPrototype>(realm, "EventTarget"));
return {};
}
}

View file

@ -1,25 +0,0 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
class WindowPrototype final : public JS::Object {
JS_OBJECT(WindowPrototype, JS::Object);
public:
explicit WindowPrototype(JS::Realm& realm);
private:
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
};
}

View file

@ -15,8 +15,6 @@ set(SOURCES
Bindings/MainThreadVM.cpp
Bindings/OptionConstructor.cpp
Bindings/PlatformObject.cpp
Bindings/WindowConstructor.cpp
Bindings/WindowPrototype.cpp
Crypto/Crypto.cpp
Crypto/SubtleCrypto.cpp
CSS/Angle.cpp

View file

@ -0,0 +1,6 @@
#import <DOM/EventTarget.idl>
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
[Global=Window, Exposed=Window, LegacyUnenumerableNamedProperties]
interface Window : EventTarget {
};

View file

@ -163,6 +163,7 @@ libweb_js_bindings(HTML/PromiseRejectionEvent)
libweb_js_bindings(HTML/Storage)
libweb_js_bindings(HTML/SubmitEvent)
libweb_js_bindings(HTML/TextMetrics)
libweb_js_bindings(HTML/Window)
libweb_js_bindings(HTML/Worker)
libweb_js_bindings(HTML/WorkerGlobalScope)
libweb_js_bindings(HTML/WorkerLocation)