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 iterable sequences with element metadata

ldm05c56c0acfd48
diff --git a/moli-webidl-derive/src/attrs.rs b/moli-webidl-derive/src/attrs.rs
index dd094ffd2..70d432408 100644
--- a/moli-webidl-derive/src/attrs.rs
+++ b/moli-webidl-derive/src/attrs.rs
@@ -57,6 +57,7 @@ pub(crate) struct FieldAttrs {
pub(crate) default: Option<Expr>,
pub(crate) converter: Option<crate::converter::ConverterKind>,
pub(crate) dictionary: bool,
+ pub(crate) sequence: bool,
pub(crate) interface: Option<Path>,
pub(crate) brand_check: Option<Path>,
pub(crate) missing_message: Option<LitStr>,
@@ -136,6 +137,13 @@ pub(crate) fn parse_field_attrs(field: &Field) -> Result<FieldAttrs, Error> {
parsed.dictionary = true;
return Ok(());
}
+ if meta.path.is_ident("sequence") {
+ if parsed.sequence {
+ return Err(meta.error("duplicate sequence attribute"));
+ }
+ parsed.sequence = true;
+ return Ok(());
+ }
if meta.path.is_ident("brand_check") {
if parsed.brand_check.is_some() {
return Err(meta.error("duplicate brand_check attribute"));
@@ -169,6 +177,12 @@ pub(crate) fn parse_field_attrs(field: &Field) -> Result<FieldAttrs, Error> {
}
}
if parsed.dictionary {
+ if parsed.sequence {
+ return Err(Error::new(
+ field.span(),
+ "dictionary cannot be combined with sequence",
+ ));
+ }
if parsed.interface.is_some() || parsed.converter.is_some() || parsed.with.is_some() {
return Err(Error::new(
field.span(),
@@ -188,6 +202,12 @@ pub(crate) fn parse_field_attrs(field: &Field) -> Result<FieldAttrs, Error> {
));
}
}
+ if parsed.sequence && (parsed.variadic || parsed.with.is_some()) {
+ return Err(Error::new(
+ field.span(),
+ "sequence cannot be combined with variadic or with",
+ ));
+ }
Ok(parsed)
}
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rs
index 69d16068a..d053a1d55 100644
--- a/moli-webidl-derive/src/converter.rs
+++ b/moli-webidl-derive/src/converter.rs
@@ -209,7 +209,57 @@ impl ConverterKind {
}
}
-pub(crate) fn converter_kind(field: &Field, attrs: &FieldAttrs) -> Result<ConverterKind, Error> {
+/// A field conversion, optionally collecting an iterable of converted items.
+#[derive(Clone, Copy)]
+pub(crate) struct FieldConverter {
+ kind: ConverterKind,
+ sequence: bool,
+}
+
+impl FieldConverter {
+ pub(crate) fn wrapper_type(self, ty: &Type) -> proc_macro2::TokenStream {
+ if self.sequence {
+ let item_type = vec_inner_type(ty).expect("validated sequence Vec");
+ let item_converter = self.kind.wrapper_type(item_type);
+ quote!(::moli_webidl::Sequence<#item_converter>)
+ } else {
+ self.kind.wrapper_type(ty)
+ }
+ }
+
+ pub(crate) fn wrap_default(self, expr: Expr, ty: &Type) -> proc_macro2::TokenStream {
+ if self.sequence {
+ let item = self.kind.wrap_default(syn::parse_quote!(item));
+ quote!({
+ let default_items: #ty = #expr;
+ ::moli_webidl::Sequence(default_items.into_iter().map(|item| #item).collect())
+ })
+ } else {
+ self.kind.wrap_default(expr)
+ }
+ }
+
+ pub(crate) fn unwrap_value(self, value: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
+ if self.sequence {
+ if self.kind == ConverterKind::Raw {
+ return quote!(#value.0);
+ }
+ let item = self.kind.unwrap_value(quote!(item));
+ quote!(#value.0.into_iter().map(|item| #item).collect::<::std::vec::Vec<_>>())
+ } else {
+ self.kind.unwrap_value(value)
+ }
+ }
+
+ pub(crate) fn options_expr(
+ self,
+ attrs: &FieldAttrs,
+ ) -> Result<proc_macro2::TokenStream, Error> {
+ self.kind.options_expr(attrs)
+ }
+}
+
+pub(crate) fn converter_kind(field: &Field, attrs: &FieldAttrs) -> Result<FieldConverter, Error> {
converter_kind_for_field_type(field, attrs, inner_type_for_field(field))
}
@@ -217,6 +267,28 @@ pub(crate) fn converter_kind_for_field_type(
field: &Field,
attrs: &FieldAttrs,
ty: &Type,
+) -> Result<FieldConverter, Error> {
+ let ty = if attrs.sequence {
+ vec_inner_type(ty).ok_or_else(|| {
+ Error::new(
+ ty.span(),
+ "sequence fields must use Vec<T> (or Option<Vec<T>>)",
+ )
+ })?
+ } else {
+ ty
+ };
+ let kind = element_converter_kind(field, attrs, ty)?;
+ Ok(FieldConverter {
+ kind,
+ sequence: attrs.sequence,
+ })
+}
+
+fn element_converter_kind(
+ field: &Field,
+ attrs: &FieldAttrs,
+ ty: &Type,
) -> Result<ConverterKind, Error> {
if attrs.dictionary {
return Ok(ConverterKind::Dictionary);
@@ -225,7 +297,7 @@ pub(crate) fn converter_kind_for_field_type(
if !is_v8_object_type(ty) {
return Err(Error::new(
ty.span(),
- "interface fields must use v8::Local<'_, v8::Object> (or Option/variadic Vec of it)",
+ "interface fields must use v8::Local<'_, v8::Object> (or Option/sequence/variadic Vec of it)",
));
}
return Ok(ConverterKind::Interface);
diff --git a/moli-webidl-derive/src/expand.rs b/moli-webidl-derive/src/expand.rs
index 0d03f5c90..1ad1a1f0b 100644
--- a/moli-webidl-derive/src/expand.rs
+++ b/moli-webidl-derive/src/expand.rs
@@ -421,7 +421,7 @@ fn expand_args_field(
"optional WebIdlArgs fields require #[webidl(default = ...)]",
)
})?;
- let wrapped_default = converter.wrap_default(default);
+ let wrapped_default = converter.wrap_default(default, field_type);
let unwrap_default = converter.unwrap_value(wrapped_default);
quote! {
let #ident = {
@@ -601,7 +601,7 @@ fn expand_dictionary_field(
"WebIdlDictionary fields currently require #[webidl(required)] or #[webidl(default = ...)]",
)
})?;
- let wrapped_default = converter.wrap_default(default);
+ let wrapped_default = converter.wrap_default(default, inner_type_for_field(field));
Ok(quote! {
let #ident = {
let default_value = #wrapped_default;
@@ -666,6 +666,51 @@ mod tests {
}
}
+ #[test]
+ fn sequence_attributes_require_a_vec_and_one_conversion_path() {
+ for attrs in [
+ quote::quote!(sequence, variadic),
+ quote::quote!(sequence, with = parse),
+ quote::quote!(sequence, dictionary),
+ quote::quote!(sequence, sequence),
+ ] {
+ let input: syn::DeriveInput = parse_quote! {
+ struct Fields {
+ #[webidl(#attrs)]
+ values: Option<Vec<String>>,
+ }
+ };
+ assert!(expand_webidl_args(input.clone()).is_err());
+ assert!(expand_webidl_dictionary(input).is_err());
+ }
+ for ty in [quote::quote!(String), quote::quote!(Sequence<DomString>)] {
+ let input = parse_quote! {
+ struct Fields {
+ #[webidl(sequence)]
+ values: Option<#ty>,
+ }
+ };
+ let error = expand_webidl_args(input).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .starts_with("sequence fields must use Vec")
+ );
+ }
+ let input: syn::DeriveInput = parse_quote! {
+ struct Fields<'s> {
+ #[webidl(sequence, interface = Base)]
+ values: Option<Vec<v8::Local<'s, v8::Value>>>,
+ }
+ };
+ assert!(
+ expand_webidl_dictionary(input)
+ .unwrap_err()
+ .to_string()
+ .starts_with("interface fields must use")
+ );
+ }
+
#[test]
fn interface_attributes_reject_conflicting_or_incomplete_metadata() {
let cases = [
diff --git a/moli-webidl-derive/src/lib.rs b/moli-webidl-derive/src/lib.rs
index 15ac96af3..d2541aef5 100644
--- a/moli-webidl-derive/src/lib.rs
+++ b/moli-webidl-derive/src/lib.rs
@@ -18,6 +18,8 @@ use syn::{DeriveInput, parse_macro_input};
/// position; `required` additionally enables the initial arity check.
/// `#[webidl(dictionary)]` delegates to a dictionary derive. Missing/nullish
/// 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.
#[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)) {
@@ -38,6 +40,7 @@ pub fn derive_webidl_args(input: TokenStream) -> TokenStream {
/// using the same native identity conversion as positional arguments.
/// Nested members support `dictionary`: null parses an empty dictionary while
/// `Option<T>` skips missing/undefined and `nullable` also skips null.
+/// Iterable members support `sequence` with `Vec<T>` or `Option<Vec<T>>`.
#[proc_macro_derive(WebIdlDictionary, attributes(webidl))]
pub fn derive_webidl_dictionary(input: TokenStream) -> TokenStream {
match expand::expand_webidl_dictionary(parse_macro_input!(input as DeriveInput)) {
diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rs
index e4888ee18..698e59cdc 100644
--- a/moli-webidl/src/lib.rs
+++ b/moli-webidl/src/lib.rs
@@ -47,6 +47,13 @@
//! Explicit `default = expr` replaces missing/undefined values only, following
//! the same evaluation order as other field defaults. Null still parses an
//! empty dictionary. Empty dictionaries have no inherited JavaScript properties.
+//!
+//! Iterable fields use `#[webidl(sequence)]` with `Vec<T>` or `Option<Vec<T>>`.
+//! Item conversion is inferred from `T`, or selected by `converter = "..."`.
+//! `sequence, interface = Type` validates every item using the same interface
+//! metadata and optional `brand_check` as scalar interface fields. Conversion
+//! reuses the runtime iterator and converts each item before reading the next.
+//! `sequence` converts one iterable argument; `variadic` converts argument tails.
extern crate self as moli_webidl;
diff --git a/moli-webidl/tests/sequences.rs b/moli-webidl/tests/sequences.rs
new file mode 100644
index 000000000..6353d58da
--- /dev/null
+++ b/moli-webidl/tests/sequences.rs
@@ -0,0 +1,390 @@
+use moli_webidl::{WebIdlArgs, WebIdlDictionary};
+
+mod interfaces {
+ moli_webapi_declare::declare_web_api_interfaces! {
+ pub Base = "SequenceBase";
+ pub Derived: Base;
+ }
+}
+
+#[derive(WebIdlArgs, WebIdlDictionary)]
+#[webidl(prefix = "Test.sequence")]
+struct InterfaceSequence<'scope> {
+ #[webidl(default = "")]
+ before: String,
+ #[webidl(required, sequence, interface = interfaces::Base)]
+ objects: Vec<v8::Local<'scope, v8::Object>>,
+ #[webidl(default = "")]
+ after: String,
+}
+
+#[derive(WebIdlArgs, WebIdlDictionary)]
+#[webidl(prefix = "Test.optionalSequence")]
+struct OptionalSequence<'scope> {
+ #[webidl(sequence, interface = interfaces::Base)]
+ objects: Option<Vec<v8::Local<'scope, v8::Object>>>,
+ #[webidl(sequence, interface = interfaces::Base, nullable)]
+ nullable: Option<Vec<v8::Local<'scope, v8::Object>>>,
+ #[webidl(sequence, interface = interfaces::Base, brand_check = is_current_global)]
+ special: Option<Vec<v8::Local<'scope, v8::Object>>>,
+}
+
+#[derive(WebIdlArgs, WebIdlDictionary)]
+#[webidl(prefix = "Test.itemConverters")]
+struct ItemConverters {
+ #[webidl(sequence, converter = "usv_string", default = Vec::new())]
+ strings: Vec<String>,
+ #[webidl(sequence, treat_null_as_empty_string)]
+ null_strings: Option<Vec<String>>,
+ #[webidl(sequence, converter = "raw")]
+ utf16: Option<Vec<moli_webidl::DomString16>>,
+ #[webidl(sequence, converter = "enforce_range_unsigned_long")]
+ numbers: Option<Vec<u32>>,
+}
+
+fn is_current_global<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ object: v8::Local<'s, v8::Object>,
+) -> bool {
+ object.strict_equals(scope.get_current_context().global(scope).into())
+}
+
+fn objects_array<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ objects: Vec<v8::Local<'s, v8::Object>>,
+) -> v8::Local<'s, v8::Array> {
+ let values: Vec<_> = objects.into_iter().map(Into::into).collect();
+ v8::Array::new_with_elements(scope, &values)
+}
+
+fn sequence_array<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ parsed: InterfaceSequence<'s>,
+) -> v8::Local<'s, v8::Array> {
+ let before = v8::String::new(scope, &parsed.before).unwrap();
+ let objects = objects_array(scope, parsed.objects);
+ let after = v8::String::new(scope, &parsed.after).unwrap();
+ v8::Array::new_with_elements(scope, &[before.into(), objects.into(), after.into()])
+}
+
+fn optional_array<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ parsed: OptionalSequence<'s>,
+) -> v8::Local<'s, v8::Array> {
+ let values: Vec<_> = [parsed.objects, parsed.nullable, parsed.special]
+ .into_iter()
+ .map(|objects| {
+ objects
+ .map(|objects| objects_array(scope, objects).into())
+ .unwrap_or_else(|| v8::null(scope).into())
+ })
+ .collect();
+ v8::Array::new_with_elements(scope, &values)
+}
+
+fn items_array<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ parsed: ItemConverters,
+) -> v8::Local<'s, v8::Array> {
+ let strings: Vec<_> = parsed
+ .strings
+ .into_iter()
+ .map(|string| v8::String::new(scope, &string).unwrap().into())
+ .collect();
+ let strings = v8::Array::new_with_elements(scope, &strings);
+ let null_strings: Vec<_> = parsed
+ .null_strings
+ .unwrap_or_default()
+ .into_iter()
+ .map(|string| v8::String::new(scope, &string).unwrap().into())
+ .collect();
+ let null_strings = v8::Array::new_with_elements(scope, &null_strings);
+ let utf16: Vec<_> = parsed
+ .utf16
+ .unwrap_or_default()
+ .into_iter()
+ .map(|string| {
+ v8::String::new_from_two_byte(scope, &string.0, v8::NewStringType::Normal)
+ .unwrap()
+ .into()
+ })
+ .collect();
+ let utf16 = v8::Array::new_with_elements(scope, &utf16);
+ let numbers: Vec<_> = parsed
+ .numbers
+ .unwrap_or_default()
+ .into_iter()
+ .map(|number| v8::Integer::new_from_unsigned(scope, number).into())
+ .collect();
+ let numbers = v8::Array::new_with_elements(scope, &numbers);
+ v8::Array::new_with_elements(
+ scope,
+ &[
+ strings.into(),
+ null_strings.into(),
+ utf16.into(),
+ numbers.into(),
+ ],
+ )
+}
+
+fn sequence_args<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(parsed) = moli_webidl::parse_args::<InterfaceSequence>(scope, &args) {
+ rv.set(sequence_array(scope, parsed).into());
+ }
+}
+
+fn sequence_dictionary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ match moli_webidl::parse_dictionary_object::<InterfaceSequence>(
+ scope,
+ args.get(0).try_into().unwrap(),
+ ) {
+ Ok(parsed) => rv.set(sequence_array(scope, parsed).into()),
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+fn optional_args<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(parsed) = moli_webidl::parse_args::<OptionalSequence>(scope, &args) {
+ rv.set(optional_array(scope, parsed).into());
+ }
+}
+
+fn optional_dictionary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ match moli_webidl::parse_dictionary_object::<OptionalSequence>(
+ scope,
+ args.get(0).try_into().unwrap(),
+ ) {
+ Ok(parsed) => rv.set(optional_array(scope, parsed).into()),
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+fn item_args<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(parsed) = moli_webidl::parse_args::<ItemConverters>(scope, &args) {
+ rv.set(items_array(scope, parsed).into());
+ }
+}
+
+fn item_dictionary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ match moli_webidl::parse_dictionary_object::<ItemConverters>(
+ scope,
+ args.get(0).try_into().unwrap(),
+ ) {
+ Ok(parsed) => rv.set(items_array(scope, parsed).into()),
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+fn run_checks(source: &str) {
+ moli_v8_test_util::ensure_v8();
+ let mut isolate = v8::Isolate::new(Default::default());
+ let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let callee = v8::Context::new(scope, Default::default());
+ let exports = {
+ let scope = &mut v8::ContextScope::new(scope, callee);
+ let foreign = v8::Object::new(scope);
+ interfaces::Derived::DESCRIPTOR
+ .initialize(scope, foreign)
+ .unwrap();
+ let handler = v8::Object::new(scope);
+ let native = v8::Proxy::new(scope, foreign, handler).unwrap();
+ moli_webapi_declare::register_web_api_proxy(scope, native).unwrap();
+ let key = v8::String::new(scope, "TypeError").unwrap();
+ let type_error = callee.global(scope).get(scope, key.into()).unwrap();
+ [
+ (
+ "sequenceArgs",
+ v8::Function::new(scope, sequence_args).unwrap().into(),
+ ),
+ (
+ "sequence",
+ v8::Function::new(scope, sequence_dictionary)
+ .unwrap()
+ .into(),
+ ),
+ (
+ "optionalArgs",
+ v8::Function::new(scope, optional_args).unwrap().into(),
+ ),
+ (
+ "optional",
+ v8::Function::new(scope, optional_dictionary)
+ .unwrap()
+ .into(),
+ ),
+ (
+ "itemsArgs",
+ v8::Function::new(scope, item_args).unwrap().into(),
+ ),
+ (
+ "items",
+ v8::Function::new(scope, item_dictionary).unwrap().into(),
+ ),
+ ("CalleeTypeError", type_error),
+ ("foreign", foreign.into()),
+ ("native", native.into()),
+ ("special", callee.global(scope).into()),
+ ]
+ };
+ let caller = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, caller);
+ let real = v8::Object::new(scope);
+ interfaces::Derived::DESCRIPTOR
+ .initialize(scope, real)
+ .unwrap();
+ for (name, value) in exports.into_iter().chain([("real", real.into())]) {
+ let key = v8::String::new(scope, name).unwrap();
+ assert_eq!(
+ caller.global(scope).set(scope, key.into(), value),
+ Some(true)
+ );
+ }
+ let scope = std::pin::pin!(v8::TryCatch::new(scope));
+ let scope = &mut scope.init();
+ let source = v8::String::new(scope, source).unwrap();
+ let result = v8::Script::compile(scope, source, None).unwrap().run(scope);
+ if result.is_none() {
+ panic!(
+ "sequence assertions failed: {}",
+ scope.exception().unwrap().to_rust_string_lossy(scope)
+ );
+ }
+ assert!(result.unwrap().is_true());
+}
+
+#[test]
+fn interface_sequences_accept_iterables_and_retain_native_proxy_and_cross_realm_identity() {
+ run_checks(
+ r#"
+ const assert = (ok, message) => { if (!ok) throw new Error(message); };
+ Object.setPrototypeOf(real, null);
+ const output = sequenceArgs('before', new Set([real, foreign, native]), 'after');
+ assert(output[1][0] === real && output[1][1] === foreign && output[1][2] === native, 'sequence identity');
+ assert(sequence({objects: [native]})[1][0] === native, 'dictionary sequence');
+ const fail = value => {
+ let error;
+ try { sequenceArgs('', value); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError && !(error instanceof TypeError), 'callee TypeError');
+ assert(error.message.includes('Argument 2'), 'argument context');
+ };
+ for (const value of [null, undefined, 1, 'text', {0: real, length: 1}]) fail(value);
+ let traps = 0;
+ const fake = Object.create(foreign);
+ const proxy = new Proxy(real, {get() {traps++; throw 'trap';}});
+ const {proxy: revoked, revoke} = Proxy.revocable(real, {});
+ revoke();
+ for (const value of [fake, proxy, revoked, {}, null]) fail([value]);
+ assert(traps === 0, 'brand validation must not invoke author traps');
+ assert(optional({})[0] === null && optional({objects: undefined})[0] === null, 'optional absence');
+ assert(optionalArgs(undefined, null)[1] === null, 'nullable sequence');
+ assert(optional({nullable: null})[1] === null, 'nullable member');
+ assert(optional({objects: []})[0].length === 0, 'present empty sequence');
+ assert(optionalArgs(undefined, undefined, [special])[2][0] === special, 'custom item predicate');
+ let error;
+ try { optionalArgs(null); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError, 'optional non-nullable rejects null');
+ try { optionalArgs(undefined, undefined, [real]); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError, 'custom predicate overrides default brand');
+ true;
+ "#,
+ );
+}
+
+#[test]
+fn sequence_item_options_defaults_and_wrapper_outputs_use_runtime_conversion() {
+ run_checks(
+ r#"
+ const assert = (ok, message) => { if (!ok) throw new Error(message); };
+ const parsed = itemsArgs(['\uD800'], [null, 'text'], ['\uD800'], [3.9]);
+ assert(parsed[0][0] === '\uFFFD', 'USVString item converter');
+ assert(parsed[1].join('|') === '|text', 'item string options');
+ assert(parsed[2][0].charCodeAt(0) === 0xD800, 'raw DomString16 wrapper');
+ assert(parsed[3][0] === 3, 'explicit item numeric converter');
+ assert(itemsArgs()[0].length === 0 && items({})[0].length === 0, 'default empty Vec');
+ let error;
+ try { items({numbers: [-1]}); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError && error.message.includes('numbers'), 'item error context');
+ true;
+ "#,
+ );
+}
+
+#[test]
+fn sequence_iteration_stays_interleaved_with_conversion_and_stops_at_first_error() {
+ run_checks(
+ r#"
+ const assert = (ok, message) => { if (!ok) throw new Error(message); };
+ const order = [];
+ const iterable = {
+ get [Symbol.iterator]() {
+ order.push('iterator');
+ return function() {
+ order.push('call'); let index = 0;
+ return {
+ next() {order.push('next'); return index++ < 2
+ ? {value: {toString() {order.push('string'); return 'value';}}, done: false}
+ : {done: true}; }
+ };
+ };
+ }
+ };
+ items({get strings() {order.push('strings'); return iterable;}, get nullStrings() {order.push('after');}});
+ assert(order.join() === 'strings,iterator,call,next,string,next,string,next,after', 'interleaving: ' + order);
+ let beforeCalls = 0;
+ const poison = {toString() {beforeCalls++; throw 'before';}};
+ let error;
+ try { sequenceArgs(poison); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError && beforeCalls === 0, 'required arity preflight');
+ let nextCalls = 0, closes = 0, afterCalls = 0;
+ const invalid = {
+ [Symbol.iterator]() {
+ return {next() {nextCalls++; return {value: {}, done: false};},
+ return() {closes++; return {done: true};}};
+ }
+ };
+ try { sequence({objects: invalid, get after() {afterCalls++; return '';}}); } catch (caught) {error = caught;}
+ assert(error instanceof CalleeTypeError && nextCalls === 1 && closes === 0 && afterCalls === 0,
+ 'brand failure must stop without iterator close or later getters');
+ const sentinel = {};
+ for (const objects of [
+ {get [Symbol.iterator]() {throw sentinel;}},
+ {[Symbol.iterator]() {throw sentinel;}},
+ {[Symbol.iterator]() {return {next() {throw sentinel;}};}},
+ {[Symbol.iterator]() {return {next() {return {get done() {throw sentinel;}};}};}},
+ {[Symbol.iterator]() {return {next() {return {done: false, get value() {throw sentinel;}};}};}},
+ ]) {
+ try { sequence({objects, get after() {afterCalls++; return '';}}); } catch (caught) {error = caught;}
+ assert(error === sentinel, 'preserve original iterator exception');
+ }
+ try { itemsArgs([{toString() {throw sentinel;}}]); } catch (caught) {error = caught;}
+ assert(error === sentinel && afterCalls === 0, 'item exception and stop position');
+ true;
+ "#,
+ );
+}