use crate::{
builtins::map::MapIterator,
error::JsNativeError,
object::{JsObject, JsObjectType},
value::TryFromJs,
Context, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;
#[derive(Debug, Clone, Finalize, Trace)]
pub struct JsMapIterator {
inner: JsObject,
}
impl JsMapIterator {
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
if object.is::<MapIterator>() {
Ok(Self { inner: object })
} else {
Err(JsNativeError::typ()
.with_message("object is not a MapIterator")
.into())
}
}
pub fn next(&self, context: &mut Context) -> JsResult<JsValue> {
MapIterator::next(&self.inner.clone().into(), &[], context)
}
}
impl From<JsMapIterator> for JsObject {
#[inline]
fn from(o: JsMapIterator) -> Self {
o.inner.clone()
}
}
impl From<JsMapIterator> for JsValue {
#[inline]
fn from(o: JsMapIterator) -> Self {
o.inner.clone().into()
}
}
impl Deref for JsMapIterator {
type Target = JsObject;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl JsObjectType for JsMapIterator {}
impl TryFromJs for JsMapIterator {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not a MapIterator object")
.into()),
}
}
}