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