forked from boa-dev/boa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsasyncgenerator.rs
More file actions
127 lines (116 loc) · 4 KB
/
jsasyncgenerator.rs
File metadata and controls
127 lines (116 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! A Rust API wrapper for Boa's `AsyncGenerator` Builtin ECMAScript Object
use super::JsPromise;
use crate::{
Context, JsNativeError, JsResult, JsValue,
builtins::{async_generator::AsyncGenerator, promise::PromiseCapability},
js_error,
object::JsObject,
value::TryFromJs,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;
/// `JsAsyncGenerator` provides a wrapper for Boa's implementation of the ECMAScript `AsyncGenerator` builtin object.
#[derive(Debug, Clone, Trace, Finalize)]
#[boa_gc(unsafe_no_drop)]
pub struct JsAsyncGenerator {
inner: JsObject<AsyncGenerator>,
}
impl JsAsyncGenerator {
/// Creates a `JsAsyncGenerator` from an async generator `JsObject`.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
object
.downcast::<AsyncGenerator>()
.map(|inner| Self { inner })
.map_err(|_| js_error!(TypeError: "object is not an AsyncGenerator object"))
}
/// Calls `AsyncGenerator.prototype.next()`.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/next
pub fn next<T>(&self, value: T, context: &mut Context) -> JsResult<JsPromise>
where
T: Into<JsValue>,
{
let (typed_promise, functions) = JsPromise::new_pending(context);
let capability = PromiseCapability {
functions,
promise: JsObject::clone(&typed_promise).clone().upcast(),
};
AsyncGenerator::inner_next(&self.inner, capability, value.into(), context)?;
Ok(typed_promise)
}
/// Calls `AsyncGenerator.prototype.return()`.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/return
pub fn r#return<T>(&self, value: T, context: &mut Context) -> JsResult<JsPromise>
where
T: Into<JsValue>,
{
let (typed_promise, functions) = JsPromise::new_pending(context);
let capability = PromiseCapability {
functions,
promise: JsObject::clone(&typed_promise).upcast(),
};
AsyncGenerator::inner_return(&self.inner, capability, value.into(), context)?;
Ok(typed_promise)
}
/// Calls `AsyncGenerator.prototype.throw()`.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/throw
pub fn throw<T>(&self, value: T, context: &mut Context) -> JsResult<JsPromise>
where
T: Into<JsValue>,
{
let (typed_promise, functions) = JsPromise::new_pending(context);
let capability = PromiseCapability {
functions,
promise: JsObject::clone(&typed_promise).clone().upcast(),
};
AsyncGenerator::inner_throw(&self.inner, capability, value.into(), context)?;
Ok(typed_promise)
}
}
impl From<JsAsyncGenerator> for JsObject {
#[inline]
fn from(o: JsAsyncGenerator) -> Self {
o.inner.upcast()
}
}
impl From<JsAsyncGenerator> for JsValue {
#[inline]
fn from(o: JsAsyncGenerator) -> Self {
o.inner.clone().into()
}
}
impl Deref for JsAsyncGenerator {
type Target = JsObject<AsyncGenerator>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl TryFromJs for JsAsyncGenerator {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
if let Some(o) = value.as_object() {
Self::from_object(o.clone())
} else {
Err(JsNativeError::typ()
.with_message("value is not an AsyncGenerator object")
.into())
}
}
}