Skip to content

Commit

Permalink
Add FormData.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Apr 2, 2024
1 parent fe38410 commit eb20961
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion runtime/src/globals/form_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ion::{
conversions::{FromValue, ToValue},
function::{Opt, Wrap},
symbol::WellKnownSymbolCode,
ClassDefinition, Context, JSIterator, Object, Result, TracedHeap,
ClassDefinition, Context, Error, ErrorKind, Function, JSIterator, Object, Result, ResultExc, TracedHeap, Value,
};
use mozjs::jsapi::{JSObject, ToStringSlow};

Expand Down Expand Up @@ -187,6 +187,22 @@ impl FormData {
Ok(())
}

#[ion(name = "forEach")]
pub fn for_each(&self, cx: &Context, callback: Function, Opt(this_arg): Opt<Object>) -> ResultExc<()> {
let this_arg = this_arg.unwrap_or_else(|| Object::null(cx));
for pair in &self.kv_pairs {
let this = Value::object(cx, &cx.root(self.reflector.get()).into());
callback
.call(cx, &this_arg, &[pair.value.as_value(cx), pair.key.as_value(cx), this])
.map_err(|e| {
e.map(|e| e.exception).unwrap_or_else(|| {
ion::Exception::Error(Error::new("Unknown failure in callback", ErrorKind::Normal))
})
})?;
}
Ok(())
}

#[ion(name = WellKnownSymbolCode::Iterator, alias = ["entries"])]
pub fn iterator<'cx: 'o, 'o>(&self, cx: &'cx Context) -> ion::Iterator {
self.make_iterator(cx, FormDataIteratorMode::Both)
Expand Down

0 comments on commit eb20961

Please sign in to comment.