boa_engine/object/builtins/
jsgenerator.rs1use crate::{
3 Context, JsNativeError, JsResult, JsValue, builtins::generator::Generator, object::JsObject,
4 value::TryFromJs,
5};
6
7use boa_gc::{Finalize, Trace};
8use std::ops::Deref;
9
10#[derive(Debug, Clone, Trace, Finalize)]
12pub struct JsGenerator {
13 inner: JsObject,
14}
15
16impl JsGenerator {
17 #[inline]
19 pub fn from_object(object: JsObject) -> JsResult<Self> {
20 if object.is::<Generator>() {
21 Ok(Self { inner: object })
22 } else {
23 Err(JsNativeError::typ()
24 .with_message("object is not a Generator")
25 .into())
26 }
27 }
28
29 pub fn next<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
33 where
34 T: Into<JsValue>,
35 {
36 Generator::next(&self.inner.clone().into(), &[value.into()], context)
37 }
38
39 pub fn r#return<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
43 where
44 T: Into<JsValue>,
45 {
46 Generator::r#return(&self.inner.clone().into(), &[value.into()], context)
47 }
48
49 pub fn throw<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
54 where
55 T: Into<JsValue>,
56 {
57 Generator::throw(&self.inner.clone().into(), &[value.into()], context)
58 }
59}
60
61impl From<JsGenerator> for JsObject {
62 #[inline]
63 fn from(o: JsGenerator) -> Self {
64 o.inner.clone()
65 }
66}
67
68impl From<JsGenerator> for JsValue {
69 #[inline]
70 fn from(o: JsGenerator) -> Self {
71 o.inner.clone().into()
72 }
73}
74
75impl Deref for JsGenerator {
76 type Target = JsObject;
77
78 #[inline]
79 fn deref(&self) -> &Self::Target {
80 &self.inner
81 }
82}
83
84impl TryFromJs for JsGenerator {
85 fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
86 if let Some(o) = value.as_object() {
87 Self::from_object(o.clone())
88 } else {
89 Err(JsNativeError::typ()
90 .with_message("value is not a Generator object")
91 .into())
92 }
93 }
94}