Skip to main content

boa_engine/builtins/async_function/
mod.rs

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