Skip to main content

boa_engine/builtins/async_generator_function/
mod.rs

1//! Boa's implementation of ECMAScript's `AsyncGeneratorFunction` object.
2//!
3//! More information:
4//!  - [ECMAScript reference][spec]
5//!
6//! [spec]: https://tc39.es/ecma262/#sec-asyncgeneratorfunction-objects
7
8use crate::{
9    Context, JsResult, JsString,
10    builtins::{BuiltInObject, function::BuiltInFunctionObject},
11    context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
12    object::{JsObject, PROTOTYPE},
13    property::Attribute,
14    realm::Realm,
15    string::StaticJsStrings,
16    symbol::JsSymbol,
17    value::JsValue,
18};
19
20use super::{BuiltInBuilder, BuiltInConstructor, IntrinsicObject};
21
22/// The internal representation of an `AsyncGeneratorFunction` object.
23#[derive(Debug, Clone, Copy)]
24pub struct AsyncGeneratorFunction;
25
26impl IntrinsicObject for AsyncGeneratorFunction {
27    fn init(realm: &Realm) {
28        BuiltInBuilder::from_standard_constructor::<Self>(realm)
29            .inherits(Some(
30                realm.intrinsics().constructors().function().prototype(),
31            ))
32            .constructor_attributes(Attribute::CONFIGURABLE)
33            .property(
34                PROTOTYPE,
35                realm.intrinsics().objects().async_generator(),
36                Attribute::CONFIGURABLE,
37            )
38            .property(
39                JsSymbol::to_string_tag(),
40                Self::NAME,
41                Attribute::CONFIGURABLE,
42            )
43            .build();
44    }
45
46    fn get(intrinsics: &Intrinsics) -> JsObject {
47        Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
48    }
49}
50
51impl BuiltInObject for AsyncGeneratorFunction {
52    const NAME: JsString = StaticJsStrings::ASYNC_GENERATOR_FUNCTION;
53}
54
55impl BuiltInConstructor for AsyncGeneratorFunction {
56    const CONSTRUCTOR_ARGUMENTS: usize = 1;
57    const PROTOTYPE_STORAGE_SLOTS: usize = 2;
58    const CONSTRUCTOR_STORAGE_SLOTS: usize = 0;
59
60    const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
61        StandardConstructors::async_generator_function;
62
63    /// `AsyncGeneratorFunction ( p1, p2, … , pn, body )`
64    ///
65    /// More information:
66    ///  - [ECMAScript reference][spec]
67    ///
68    /// [spec]: https://tc39.es/ecma262/#sec-asyncgeneratorfunction
69    fn constructor(
70        new_target: &JsValue,
71        args: &[JsValue],
72        context: &mut Context,
73    ) -> JsResult<JsValue> {
74        let active_function = context.active_function_object().unwrap_or_else(|| {
75            context
76                .intrinsics()
77                .constructors()
78                .generator_function()
79                .constructor()
80        });
81        BuiltInFunctionObject::create_dynamic_function(
82            active_function,
83            new_target,
84            args,
85            true,
86            true,
87            context,
88        )
89        .map(Into::into)
90    }
91}