ladybird/Libraries/LibWeb/HTML/Scripting/ModuleScript.h
Andrew Kaster 92e4c20ad5 LibJS: Generate FFI header using cbindgen instead of hand-rolling
Replace the BytecodeFactory header with cbindgen.

This will help ensure that types and enums and constants are kept in
sync between the C++ and Rust code. It's also a step in exporting more
Rust enums directly rather than relying on magic constants for
switch statements.

The FFI functions are now all placed in the JS::FFI namespace, which
is the cause for all the churn in the scripting parts of LibJS and
LibWeb.
2026-03-17 20:49:50 -05:00

76 lines
2.2 KiB
C++

/*
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/SourceTextModule.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/Scripting/Script.h>
namespace JS::FFI {
struct ParsedProgram;
}
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webappapis.html#module-script
class ModuleScript : public Script {
GC_CELL(ModuleScript, Script);
public:
virtual ~ModuleScript() override;
protected:
ModuleScript(URL::URL base_url, ByteString filename, JS::Realm&);
private:
virtual bool is_module_script() const final { return true; }
};
class WEB_API JavaScriptModuleScript final : public ModuleScript {
GC_CELL(JavaScriptModuleScript, ModuleScript);
GC_DECLARE_ALLOCATOR(JavaScriptModuleScript);
public:
virtual ~JavaScriptModuleScript() override;
static WebIDL::ExceptionOr<GC::Ptr<JavaScriptModuleScript>> create(ByteString const& filename, StringView source, JS::Realm&, URL::URL base_url);
static WebIDL::ExceptionOr<GC::Ptr<JavaScriptModuleScript>> create_from_pre_parsed(ByteString const& filename, NonnullRefPtr<JS::SourceCode const> source_code, JS::Realm&, URL::URL base_url, JS::FFI::ParsedProgram* parsed);
enum class PreventErrorReporting {
Yes,
No
};
JS::Promise* run(PreventErrorReporting = PreventErrorReporting::No);
JS::SourceTextModule const* record() const { return m_record.ptr(); }
JS::SourceTextModule* record() { return m_record.ptr(); }
protected:
JavaScriptModuleScript(URL::URL base_url, ByteString filename, JS::Realm&);
private:
virtual bool is_javascript_module_script() const final { return true; }
virtual void visit_edges(JS::Cell::Visitor&) override;
GC::Ptr<JS::SourceTextModule> m_record;
size_t m_fetch_internal_request_count { 0 };
size_t m_completed_fetch_internal_request_count { 0 };
Function<void(JavaScriptModuleScript const*)> m_completed_fetch_internal_callback;
};
}
template<>
inline bool JS::Script::HostDefined::fast_is<Web::HTML::ModuleScript>() const { return is_module_script(); }
template<>
inline bool JS::Script::HostDefined::fast_is<Web::HTML::JavaScriptModuleScript>() const { return is_javascript_module_script(); }