Skip to content

Truly independent web browser

Meta: Support namespace attributes and read-write maplike set in IDL

Andreas Kling7d65726e3863
diff --git a/Meta/Generators/libweb_bindings/interface_declaration.py b/Meta/Generators/libweb_bindings/interface_declaration.py
index 0a13ff663c3..db719240f21 100644
--- a/Meta/Generators/libweb_bindings/interface_declaration.py
+++ b/Meta/Generators/libweb_bindings/interface_declaration.py
@@ -162,6 +162,8 @@ private:
out.write(" JS_DECLARE_NATIVE_FUNCTION(get);\n")
out.write(" JS_DECLARE_NATIVE_FUNCTION(has);\n")
if not interface.maplike.readonly:
+ if "set" not in operation_callbacks:
+ out.write(" JS_DECLARE_NATIVE_FUNCTION(set);\n")
out.write(" JS_DECLARE_NATIVE_FUNCTION(delete_);\n")
out.write(" JS_DECLARE_NATIVE_FUNCTION(clear);\n")
if interface.setlike is not None:
diff --git a/Meta/Generators/libweb_bindings/iterables.py b/Meta/Generators/libweb_bindings/iterables.py
index 061553ff689..efbe658cb90 100644
--- a/Meta/Generators/libweb_bindings/iterables.py
+++ b/Meta/Generators/libweb_bindings/iterables.py
@@ -121,6 +121,9 @@ def define_the_maplike_declaration(
"""
)
if not interface.maplike.readonly:
+ operation_callbacks = operation_callback_names(interface)
+ if "set" not in operation_callbacks:
+ out.write(" object.define_native_function(realm, vm.names.set, set, 2, default_attributes);\n")
out.write(
""" object.define_native_function(realm, vm.names.delete_, delete_, 1, default_attributes);
object.define_native_function(realm, vm.names.clear, clear, 0, default_attributes);
@@ -616,7 +619,7 @@ JS_DEFINE_NATIVE_FUNCTION({interface.prototype_class}::set)
{type_check_idl_value(interface.maplike.value_type, "value", includes, context, interface.name)}
// 9. Set map[key] to value.
- TRY(WebIDL::throw_dom_exception_if_needed(vm, this_object_realm, [&] {{ return set(this_object_realm, *this_impl, key.to_string(), value); }}));
+ TRY(WebIDL::throw_dom_exception_if_needed(vm, this_object_realm, [&] {{ return Web::Bindings::set(this_object_realm, *this_impl, key, value); }}));
// 10. Return O.
return vm.this_value();
diff --git a/Meta/Generators/libweb_bindings/namespaces.py b/Meta/Generators/libweb_bindings/namespaces.py
index 2528f69b3da..b0f09c987e9 100644
--- a/Meta/Generators/libweb_bindings/namespaces.py
+++ b/Meta/Generators/libweb_bindings/namespaces.py
@@ -6,15 +6,18 @@
from typing import TextIO
from Generators.libweb_bindings import overload_resolution
+from Generators.libweb_bindings.attributes import attribute_getter_callback_name
from Generators.libweb_bindings.attributes import define_the_regular_attributes
from Generators.libweb_bindings.constants import define_the_constants
from Generators.libweb_bindings.context import GenerationContext
from Generators.libweb_bindings.cpp_types import fully_qualified_name_for_interface
from Generators.libweb_bindings.cpp_types import idl_identifier_cpp_name
+from Generators.libweb_bindings.cpp_types import idl_implementation_cpp_name
from Generators.libweb_bindings.cpp_types import implementation_header_for_interface
from Generators.libweb_bindings.includes import GeneratedIncludes
from Generators.libweb_bindings.operations import define_the_regular_operations
from Generators.libweb_bindings.operations import write_regular_operations
+from Generators.libweb_bindings.to_js_value import to_javascript_value
from Utils.webidl_parser import Interface
UNSUPPORTED_NAMESPACE_EXTENDED_ATTRIBUTES = ("WithGCVisitor", "WithFinalizer")
@@ -40,6 +43,8 @@ def write_namespace_declaration(
private:
"""
)
+ for attribute in namespace_attributes(interface):
+ out.write(f" JS_DECLARE_NATIVE_FUNCTION({attribute_getter_callback_name(attribute)});\n")
for operations in overload_resolution.operation_overload_sets(interface).values():
operation = operations[0]
out.write(f" JS_DECLARE_NATIVE_FUNCTION({idl_identifier_cpp_name(operation)});\n")
@@ -55,6 +60,42 @@ private:
)
+def namespace_attributes(interface: Interface) -> list:
+ attributes = [
+ attribute for attribute in interface.regular_attributes if "FIXME" not in attribute.extended_attributes
+ ]
+ for attribute in attributes:
+ # NB: Namespaces can only have read only regular attributes, see https://webidl.spec.whatwg.org/#idl-namespaces
+ if not attribute.readonly:
+ raise RuntimeError(f"Namespace attribute {interface.name}.{attribute.name} must be read only")
+ return attributes
+
+
+# https://webidl.spec.whatwg.org/#dfn-attribute-getter
+def write_namespace_attribute_getters(
+ out: TextIO, context: GenerationContext, includes: GeneratedIncludes, interface: Interface
+) -> None:
+ callee = fully_qualified_name_for_interface(interface).partition("::")[0]
+ for attribute in namespace_attributes(interface):
+ includes.add("LibWeb/WebIDL/ExceptionOrUtils.h")
+ first_argument = "realm" if "NeedsCallerRealm" in attribute.extended_attributes else "vm"
+ out.write(f"""JS_DEFINE_NATIVE_FUNCTION({interface.namespace_class}::{attribute_getter_callback_name(attribute)})
+{{
+ [[maybe_unused]] auto& realm = *vm.current_realm();
+
+ // 1. Let idlObject be null.
+ // NB: Step 2 only applies when target is an interface.
+
+ // 3. Let R be the result of running the getter steps of attribute with idlObject as this.
+ auto R = TRY(WebIDL::throw_dom_exception_if_needed(vm, realm, [&] {{ return {callee}::{idl_implementation_cpp_name(attribute)}({first_argument}); }}));
+
+ // 4. Return the result of converting R to an ECMAScript value of the type attribute is declared as.
+ return {to_javascript_value(attribute.type, "R", includes, context)};
+}}
+
+""")
+
+
# https://webidl.spec.whatwg.org/#namespace-object
def write_namespace_implementation(
out: TextIO, context: GenerationContext, includes: GeneratedIncludes, interface: Interface
@@ -103,4 +144,5 @@ def write_namespace_implementation(
"""
)
+ write_namespace_attribute_getters(out, context, includes, interface)
write_regular_operations(out, context, includes, interface)
diff --git a/Meta/Generators/libweb_bindings/to_js_value.py b/Meta/Generators/libweb_bindings/to_js_value.py
index fc4be6c165d..3bd30d99951 100644
--- a/Meta/Generators/libweb_bindings/to_js_value.py
+++ b/Meta/Generators/libweb_bindings/to_js_value.py
@@ -449,11 +449,18 @@ def sequence_to_javascript_value(
) -> str:
includes.add("LibJS/Runtime/Array.h")
element_type = sequence_type.parameters[0]
- length_name = "sequence_length"
- array_name = "sequence_array"
- index_name = "sequence_index"
- element_name = "sequence_element"
- js_element_name = "js_sequence_element"
+
+ # A sequence nested in the element of another sequence must not shadow the names of the outer conversion, as the
+ # value it converts refers to them.
+ nesting_depth = 0
+ while f"sequence_element{f'_{nesting_depth}' if nesting_depth else ''}" in value:
+ nesting_depth += 1
+ suffix = f"_{nesting_depth}" if nesting_depth else ""
+ length_name = f"sequence_length{suffix}"
+ array_name = f"sequence_array{suffix}"
+ index_name = f"sequence_index{suffix}"
+ element_name = f"sequence_element{suffix}"
+ js_element_name = f"js_sequence_element{suffix}"
converted_element = to_javascript_value(element_type, element_name, includes, context, realm, wrapper_world)
freeze_array = ""