Skip to content

armgit / moli

Public Imported from GitHub
0

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

feat(webidl): expose declared prototype attribute metadata

ldm029bb4dc91d7d
diff --git a/moli-webapi-declare-derive/src/expand.rs b/moli-webapi-declare-derive/src/expand.rs
index acb0ef2a4..e1b8060e9 100644
--- a/moli-webapi-declare-derive/src/expand.rs
+++ b/moli-webapi-declare-derive/src/expand.rs
@@ -137,6 +137,18 @@ pub(crate) fn expand_webapi_function_template(
}
})
});
+ let prototype_attribute_names = fields.iter().filter_map(|DeclaredField { attrs, .. }| {
+ if attrs.symbol.is_none()
+ && matches!(
+ attrs.kind,
+ FieldKind::AccessorProperty | FieldKind::NativeDataProperty
+ )
+ {
+ webapi_field_name_literal(attrs.name.as_ref())
+ } else {
+ None
+ }
+ });
let template_fields = expand_function_template_fields(&fields, &template_name)?;
let FunctionTemplateFieldExpansions {
template_methods,
@@ -183,6 +195,7 @@ pub(crate) fn expand_webapi_function_template(
impl #impl_generics ::moli_webapi_declare::WebApiFunctionTemplateDeclaration for #struct_name #ty_generics #where_clause {
const NAME: &'static str = #template_name;
+ const PROTOTYPE_ATTRIBUTE_NAMES: &'static [&'static str] = &[#(#prototype_attribute_names),*];
fn build<'s>(
scope: &mut ::moli_webapi_declare::v8::PinScope<'s, '_, ()>,
diff --git a/moli-webapi-declare/src/declaration.rs b/moli-webapi-declare/src/declaration.rs
index c27da4465..d52213a31 100644
--- a/moli-webapi-declare/src/declaration.rs
+++ b/moli-webapi-declare/src/declaration.rs
@@ -12,6 +12,14 @@ use crate::{__private, BindError, WebApiObject, v8};
pub trait WebApiFunctionTemplateDeclaration {
const NAME: &'static str;
+ /// Statically named attributes installed on this declaration's prototype.
+ ///
+ /// Derived implementations include accessor and native data properties,
+ /// excluding symbol keys and names computed at template installation time.
+ /// Native wrappers can use this metadata without consulting author-visible
+ /// prototype objects. Inherited declarations are listed separately.
+ const PROTOTYPE_ATTRIBUTE_NAMES: &'static [&'static str] = &[];
+
fn build<'s>(scope: &mut v8::PinScope<'s, '_, ()>) -> v8::Local<'s, v8::FunctionTemplate>;
fn initialize_template<'s>(
diff --git a/moli-webapi-declare/tests/declare.rs b/moli-webapi-declare/tests/declare.rs
index 5d28343fa..c77a5cbaf 100644
--- a/moli-webapi-declare/tests/declare.rs
+++ b/moli-webapi-declare/tests/declare.rs
@@ -432,6 +432,60 @@ struct SampleFunctionTemplateDeclaration {
native_holder: (),
}
+const DYNAMIC_ATTRIBUTE_NAME: &str = "runtimeAttribute";
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "AttributeMetadata", enumerable)]
+struct AttributeMetadataTemplateDeclaration {
+ #[webapi(accessor_property, getter = sample_receiver_getter)]
+ implicit_name: (),
+ #[webapi(accessor_property = "explicit-name", getter = sample_receiver_getter)]
+ explicit: (),
+ #[webapi(native_data_property, getter = sample_native_holder_getter)]
+ native_holder: (),
+ #[webapi(accessor_property = DYNAMIC_ATTRIBUTE_NAME, getter = sample_receiver_getter)]
+ dynamic: (),
+ #[webapi(accessor_property, symbol = "toStringTag", getter = sample_receiver_getter)]
+ tag: (),
+ #[webapi(method, callback = sample_now_callback)]
+ method: (),
+ #[webapi(constant = "READY", value = 4u32)]
+ ready: (),
+}
+
+#[test]
+fn prototype_attribute_metadata_tracks_static_attribute_keys() {
+ ensure_v8();
+ let mut isolate = v8::Isolate::new(v8::CreateParams::default());
+ let scope = pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let context = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let global = context.global(scope);
+ install_template::<AttributeMetadataTemplateDeclaration>(scope, global);
+ let attributes = AttributeMetadataTemplateDeclaration::PROTOTYPE_ATTRIBUTE_NAMES;
+ assert_eq!(
+ attributes,
+ ["implicitName", "explicit-name", "nativeHolder"]
+ );
+ for attribute in attributes {
+ assert!(
+ run_script(
+ scope,
+ &format!("Object.hasOwn(AttributeMetadata.prototype, {attribute:?})")
+ )
+ .boolean_value(scope)
+ );
+ }
+ assert!(
+ run_script(
+ scope,
+ "['runtimeAttribute', 'method', 'READY'].every(key => Object.hasOwn(AttributeMetadata.prototype, key)) && Object.hasOwn(AttributeMetadata.prototype, Symbol.toStringTag)"
+ )
+ .boolean_value(scope)
+ );
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(name = "IntrinsicArrayTemplate", enumerable)]
struct IntrinsicArrayTemplateDeclaration {