Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

perf(webidl): share object function construction

ldm01b164fc27af8
diff --git a/moli-webapi-declare-derive/src/expand.rs b/moli-webapi-declare-derive/src/expand.rs
index 0b68aaeec..cb4e7bb46 100644
--- a/moli-webapi-declare-derive/src/expand.rs
+++ b/moli-webapi-declare-derive/src/expand.rs
@@ -1457,26 +1457,22 @@ fn expand_function_builder(
)
})?;
});
- let attach_data = data.map(|_| quote!(.data(__webapi_callback_data)));
- if receiver_access_check {
- return quote! {{
- #convert_data
- let template = ::moli_webapi_declare::v8::FunctionTemplate::builder(#callback)
- .length(#length)
- #attach_data
- .constructor_behavior(::moli_webapi_declare::v8::ConstructorBehavior::Throw)
- .build(scope);
- template.set_accept_any_receiver(false);
- template.get_function(scope)
- }};
- }
+ let data = if data.is_some() {
+ quote!(::std::option::Option::Some(__webapi_callback_data))
+ } else {
+ quote!(::std::option::Option::None)
+ };
quote! {{
#convert_data
- ::moli_webapi_declare::v8::Function::builder(#callback)
- .length(#length)
- #attach_data
- .constructor_behavior(::moli_webapi_declare::v8::ConstructorBehavior::Throw)
- .build(scope)
+ ::moli_webapi_declare::__private::build_object_function(
+ scope,
+ ::moli_webapi_declare::v8::MapFnTo::<
+ ::moli_webapi_declare::v8::FunctionCallback,
+ >::map_fn_to(#callback),
+ #length,
+ #data,
+ #receiver_access_check,
+ )
}}
}
diff --git a/moli-webapi-declare/src/__private.rs b/moli-webapi-declare/src/__private.rs
index 0cebc519b..7380985ca 100644
--- a/moli-webapi-declare/src/__private.rs
+++ b/moli-webapi-declare/src/__private.rs
@@ -87,3 +87,36 @@ pub fn install_function_template_accessor<'s>(
let setter = setter.map(|setter| build_function_template_member(scope, setter));
target.set_accessor_property(property_key, Some(getter), setter, attributes);
}
+
+/// Creates a declared object method or accessor without repeating V8 builder
+/// code at every installation site. Callback data remains the original value.
+#[doc(hidden)]
+#[inline(never)]
+pub fn build_object_function<'s>(
+ scope: &v8::PinScope<'s, '_>,
+ callback: v8::FunctionCallback,
+ length: i32,
+ data: Option<v8::Local<'s, v8::Value>>,
+ receiver_access_check: bool,
+) -> Option<v8::Local<'s, v8::Function>> {
+ if receiver_access_check {
+ return build_function_template_member(
+ scope,
+ FunctionTemplateMember {
+ callback,
+ length,
+ data,
+ class_name: None,
+ receiver_access_check,
+ },
+ )
+ .get_function(scope);
+ }
+ let builder = v8::Function::builder_raw(callback)
+ .length(length)
+ .constructor_behavior(v8::ConstructorBehavior::Throw);
+ match data {
+ Some(data) => builder.data(data).build(scope),
+ None => builder.build(scope),
+ }
+}