quickjs_runtime/quickjs_utils/
iterators.rs1use crate::jsutils::JsError;
4use crate::quickjs_utils::{functions, objects, primitives};
5use crate::quickjsvalueadapter::QuickJsValueAdapter;
6use libquickjs_sys as q;
7
8pub unsafe fn iterate<C: Fn(QuickJsValueAdapter) -> Result<R, JsError>, R>(
12 ctx: *mut q::JSContext,
13 iterator_ref: &QuickJsValueAdapter,
14 consumer_producer: C,
15) -> Result<Vec<R>, JsError> {
16 let mut res = vec![];
17
18 loop {
19 let next_obj = functions::invoke_member_function(ctx, iterator_ref, "next", &[])?;
20 if primitives::to_bool(&objects::get_property(ctx, &next_obj, "done")?)? {
21 break;
22 } else {
23 let next_item = objects::get_property(ctx, &next_obj, "value")?;
24 res.push(consumer_producer(next_item)?);
25 }
26 }
27
28 Ok(res)
29}