LibJS+LibWeb: Use realm.create<T> instead of heap.allocate<T>

The main motivation behind this is to remove JS specifics of the Realm
from the implementation of the Heap.

As a side effect of this change, this is a bit nicer to read than the
previous approach, and in my opinion, also makes it a little more clear
that this method is specific to a JavaScript Realm.
This commit is contained in:
Shannon Booth 2024-11-14 05:50:17 +13:00 committed by Tim Flynn
commit 9b79a686eb
Notes: github-actions[bot] 2024-11-13 21:52:48 +00:00
326 changed files with 697 additions and 714 deletions

View file

@ -710,7 +710,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
if (!@js_name@@js_suffix@.is_object() || !(is<JS::TypedArrayBase>(@js_name@@js_suffix@.as_object()) || is<JS::ArrayBuffer>(@js_name@@js_suffix@.as_object()) || is<JS::DataView>(@js_name@@js_suffix@.as_object())))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
@cpp_name@ = JS::make_handle(vm.heap().allocate<WebIDL::BufferSource>(realm, @js_name@@js_suffix@.as_object()));
@cpp_name@ = JS::make_handle(realm.create<WebIDL::BufferSource>(@js_name@@js_suffix@.as_object()));
)~~~");
if (optional) {
@ -724,7 +724,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
if (!@js_name@@js_suffix@.is_object() || !(is<JS::TypedArrayBase>(@js_name@@js_suffix@.as_object()) || is<JS::DataView>(@js_name@@js_suffix@.as_object())))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@");
auto @cpp_name@ = JS::make_handle(vm.heap().allocate<WebIDL::ArrayBufferView>(realm, @js_name@@js_suffix@.as_object()));
auto @cpp_name@ = JS::make_handle(realm.create<WebIDL::ArrayBufferView>(@js_name@@js_suffix@.as_object()));
)~~~");
if (optional) {
scoped_generator.append(R"~~~(
@ -1227,7 +1227,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
if (any_of(types, [](auto const& type) { return type->name() == "BufferSource"; }) && !includes_object) {
union_generator.append(R"~~~(
if (is<JS::ArrayBuffer>(@js_name@@js_suffix@_object) || is<JS::DataView>(@js_name@@js_suffix@_object) || is<JS::TypedArrayBase>(@js_name@@js_suffix@_object)) {
JS::NonnullGCPtr<WebIDL::BufferSource> source_object = vm.heap().allocate<WebIDL::BufferSource>(realm, @js_name@@js_suffix@_object);
JS::NonnullGCPtr<WebIDL::BufferSource> source_object = realm.create<WebIDL::BufferSource>(@js_name@@js_suffix@_object);
return JS::make_handle(source_object);
}
)~~~");
@ -2496,7 +2496,7 @@ static void generate_html_constructor(SourceGenerator& generator, IDL::Construct
// 3. Set element's namespace to the HTML namespace.
// 4. Set element's namespace prefix to null.
// 5. Set element's local name to definition's local name.
auto element = realm.heap().allocate<@fully_qualified_name@>(realm, window.associated_document(), DOM::QualifiedName { definition->local_name(), {}, Namespace::HTML });
auto element = realm.create<@fully_qualified_name@>(window.associated_document(), DOM::QualifiedName { definition->local_name(), {}, Namespace::HTML });
// https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface
// Important steps from "internally create a new object implementing the interface"

View file

@ -103,7 +103,7 @@ namespace Web::Bindings {
template<>
void Intrinsics::create_web_namespace<@namespace_class@>(JS::Realm& realm)
{
auto namespace_object = heap().allocate<@namespace_class@>(realm, realm);
auto namespace_object = realm.create<@namespace_class@>(realm);
m_namespaces.set("@interface_name@"_fly_string, namespace_object);
[[maybe_unused]] static constexpr u8 attr = JS::Attribute::Writable | JS::Attribute::Configurable;)~~~");
@ -139,16 +139,16 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
if (!named_properties_class.is_empty()) {
gen.set("named_properties_class", named_properties_class);
gen.append(R"~~~(
auto named_properties_object = heap().allocate<@named_properties_class@>(realm, realm);
auto named_properties_object = realm.create<@named_properties_class@>(realm);
m_prototypes.set("@named_properties_class@"_fly_string, named_properties_object);
)~~~");
}
gen.append(R"~~~(
auto prototype = heap().allocate<@prototype_class@>(realm, realm);
auto prototype = realm.create<@prototype_class@>(realm);
m_prototypes.set("@interface_name@"_fly_string, prototype);
auto constructor = heap().allocate<@constructor_class@>(realm, realm);
auto constructor = realm.create<@constructor_class@>(realm);
m_constructors.set("@interface_name@"_fly_string, constructor);
prototype->define_direct_property(vm.names.constructor, constructor.ptr(), JS::Attribute::Writable | JS::Attribute::Configurable);
@ -159,7 +159,7 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
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);
auto legacy_constructor = realm.create<@legacy_constructor_class@>(realm);
m_constructors.set("@legacy_interface_name@"_fly_string, legacy_constructor);
legacy_constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@legacy_interface_name@"_string), JS::Attribute::Configurable);)~~~");