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): derive sequences of dictionary values

ldm0e0301f124e31
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rs
index 2f926bbb2..3498440c0 100644
--- a/moli-webidl-derive/src/converter.rs
+++ b/moli-webidl-derive/src/converter.rs
@@ -66,6 +66,7 @@ impl ConverterKind {
"unrestricted_double" => Ok(Self::UnrestrictedDouble),
"boolean" => Ok(Self::Boolean),
"enum" => Ok(Self::Enum),
+ "dictionary" => Ok(Self::Dictionary),
"raw" => Ok(Self::Raw),
_ => Err(Error::new(value.span(), "unsupported converter kind")),
}
diff --git a/moli-webidl-derive/src/lib.rs b/moli-webidl-derive/src/lib.rs
index e9f7e0ccb..8f8237356 100644
--- a/moli-webidl-derive/src/lib.rs
+++ b/moli-webidl-derive/src/lib.rs
@@ -20,6 +20,7 @@ use syn::{DeriveInput, parse_macro_input};
/// values parse an empty dictionary; optional fields skip missing/undefined.
/// `#[webidl(sequence)]` converts one iterable to `Vec<T>`, using inferred or
/// explicit item conversion, including `interface` and `brand_check` metadata.
+/// `sequence, converter = "dictionary"` converts each item as a dictionary.
#[proc_macro_derive(WebIdlArgs, attributes(webidl))]
pub fn derive_webidl_args(input: TokenStream) -> TokenStream {
match expand::expand_webidl_args(parse_macro_input!(input as DeriveInput)) {
diff --git a/moli-webidl/tests/dictionary_sequence.rs b/moli-webidl/tests/dictionary_sequence.rs
new file mode 100644
index 000000000..0a4c273c9
--- /dev/null
+++ b/moli-webidl/tests/dictionary_sequence.rs
@@ -0,0 +1,54 @@
+use moli_webidl::{Context, WebIdlDictionary};
+
+#[derive(WebIdlDictionary)]
+#[webidl(prefix = "Plane")]
+struct Plane {
+ #[webidl(required)]
+ offset: u32,
+}
+#[derive(WebIdlDictionary)]
+#[webidl(prefix = "Planes")]
+struct Planes {
+ #[webidl(sequence, converter = "dictionary")]
+ layout: Option<Vec<Plane>>,
+}
+#[test]
+fn sequence_dictionary_conversion_observes_iteration_and_required_members() {
+ moli_v8_test_util::ensure_v8();
+ let mut isolate = v8::Isolate::new(Default::default());
+ v8::scope!(let scope, &mut isolate);
+ let context = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, context);
+ for (source, expected) in [
+ ("({layout:[{offset:3},{offset:7}]})", Some(vec![3, 7])),
+ ("({layout:[]})", Some(vec![])),
+ ("({})", None),
+ ] {
+ let source = v8::String::new(scope, source).unwrap();
+ let value = v8::Script::compile(scope, source, None)
+ .unwrap()
+ .run(scope)
+ .unwrap();
+ let planes =
+ moli_webidl::parse_dictionary::<Planes>(scope, value, Context::argument("Test", 1))
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ planes
+ .layout
+ .map(|list| list.into_iter().map(|p| p.offset).collect::<Vec<_>>()),
+ expected
+ );
+ }
+ for source in ["({layout:[null]})", "({layout:[{}]})", "({layout:{}})"] {
+ let source = v8::String::new(scope, source).unwrap();
+ let value = v8::Script::compile(scope, source, None)
+ .unwrap()
+ .run(scope)
+ .unwrap();
+ assert!(
+ moli_webidl::parse_dictionary::<Planes>(scope, value, Context::argument("Test", 1))
+ .is_err()
+ );
+ }
+}