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