Skip to main content

boa_engine/object/builtins/
jsgenerator.rs

1//! A Rust API wrapper for Boa's `Generator` Builtin ECMAScript Object
2use 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/// `JsGenerator` provides a wrapper for Boa's implementation of the ECMAScript `Generator` builtin object
11#[derive(Debug, Clone, Trace, Finalize)]
12pub struct JsGenerator {
13    inner: JsObject,
14}
15
16impl JsGenerator {
17    /// Creates a `JsGenerator` from a generator `JsObject`
18    #[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    /// Calls `Generator.prototype.next()`
30    ///
31    /// This method returns an object with the properties `done` and `value`
32    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    /// Calls `Generator.prototype.return()`
40    ///
41    /// This method returns the given value and finishes the generator
42    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    /// Calls `Generator.prototype.throw()`
50    ///
51    /// This method resumes the execution of a generator by throwing an error and returning an
52    /// an object with the properties `done` and `value`
53    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}