Skip to content

Truly independent web browser

Meta: Support ReturnsJSValue attributes in Rust bindings

Shannon Booth7b0780d430d2
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index ad2106a397d..45a2132d5cc 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -794,7 +794,11 @@ def implementation_method_signatures(
signatures.append(f"fn {attribute.setter_method}(&self, cx: &mut CallContext, value: Value)")
continue
if not attribute.attribute.inherit:
- result_type = rust_type(attribute.attribute.type, context, what, is_result=True)
+ result_type = (
+ "Value"
+ if attribute.returns_js_value
+ else rust_type(attribute.attribute.type, context, what, is_result=True)
+ )
signatures.append(
f"fn {attribute.getter_method}(&self, cx: &mut CallContext) -> WebIdlResult<{result_type}>"
)
@@ -1273,7 +1277,11 @@ def write_rust_interface(
if attribute.event_type is not None:
write_rust_event_handler_attribute(out, attribute)
continue
- conversion = rust_to_js_value(attribute.attribute.type, "result", "&cx", context, f"{interface.name}.{name}")
+ conversion = (
+ "Ok(result)"
+ if attribute.returns_js_value
+ else rust_to_js_value(attribute.attribute.type, "result", "&cx", context, f"{interface.name}.{name}")
+ )
call = f"Implementation::{attribute.getter_method}(&implementation, &mut cx)"
if attribute.attribute.inherit:
call = inherited_attribute_getter_call(context, interface, attribute)
@@ -1514,7 +1522,11 @@ def namespace_function_signatures(context: GenerationContext, interface: Interfa
signatures = []
for attribute in members.attributes:
what = f"{interface.name}.{attribute.attribute.name}"
- result_type = rust_type(attribute.attribute.type, context, what, is_result=True)
+ result_type = (
+ "Value"
+ if attribute.returns_js_value
+ else rust_type(attribute.attribute.type, context, what, is_result=True)
+ )
signatures.append(f"fn {attribute.getter_method}(cx: &mut CallContext) -> WebIdlResult<{result_type}>")
for operation_set in members.operation_sets:
for overload in operation_set.overloads:
@@ -1566,7 +1578,11 @@ def write_rust_namespace(out: TextIO, context: GenerationContext, interface: Int
)
for attribute in members.attributes:
what = f"{interface.name}.{attribute.attribute.name}"
- conversion = rust_to_js_value(attribute.attribute.type, "result", "&cx", context, what)
+ conversion = (
+ "Ok(result)"
+ if attribute.returns_js_value
+ else rust_to_js_value(attribute.attribute.type, "result", "&cx", context, what)
+ )
out.write(
f"""
fn {attribute.getter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.py
index c7e7abf04dd..ea061388ea2 100644
--- a/Meta/Generators/libweb_bindings/rust_members.py
+++ b/Meta/Generators/libweb_bindings/rust_members.py
@@ -45,6 +45,7 @@ SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES = {
"NeedsCallerRealm",
"NewObject",
"PutForwards",
+ "ReturnsJSValue",
"SameObject",
"SecureContext",
}
@@ -97,6 +98,10 @@ class RustAttribute:
the object: [CachedAccessor]."""
return "CachedAccessor" in self.attribute.extended_attributes
+ @property
+ def returns_js_value(self) -> bool:
+ return "ReturnsJSValue" in self.attribute.extended_attributes
+
@property
def function_stem(self) -> str:
"""What the generated native functions are named after. [AttributeCallbackName] tells apart the attributes that