use crate::jsutils::JsError;
use crate::quickjs_utils::{functions, objects, primitives};
use crate::quickjsvalueadapter::QuickJsValueAdapter;
use libquickjs_sys as q;
pub unsafe fn iterate<C: Fn(QuickJsValueAdapter) -> Result<R, JsError>, R>(
ctx: *mut q::JSContext,
iterator_ref: &QuickJsValueAdapter,
consumer_producer: C,
) -> Result<Vec<R>, JsError> {
let mut res = vec![];
loop {
let next_obj = functions::invoke_member_function(ctx, iterator_ref, "next", &[])?;
if primitives::to_bool(&objects::get_property(ctx, &next_obj, "done")?)? {
break;
} else {
let next_item = objects::get_property(ctx, &next_obj, "value")?;
res.push(consumer_producer(next_item)?);
}
}
Ok(res)
}