/src/serenity/Userland/Libraries/LibJS/Runtime/AsyncFunctionConstructor.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Runtime/AsyncFunctionConstructor.h> |
8 | | #include <LibJS/Runtime/ECMAScriptFunctionObject.h> |
9 | | #include <LibJS/Runtime/FunctionConstructor.h> |
10 | | #include <LibJS/Runtime/FunctionObject.h> |
11 | | #include <LibJS/Runtime/GlobalObject.h> |
12 | | |
13 | | namespace JS { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(AsyncFunctionConstructor); |
16 | | |
17 | | AsyncFunctionConstructor::AsyncFunctionConstructor(Realm& realm) |
18 | 52 | : NativeFunction(realm.vm().names.AsyncFunction.as_string(), realm.intrinsics().function_constructor()) |
19 | 52 | { |
20 | 52 | } |
21 | | |
22 | | void AsyncFunctionConstructor::initialize(Realm& realm) |
23 | 52 | { |
24 | 52 | auto& vm = this->vm(); |
25 | 52 | Base::initialize(realm); |
26 | | |
27 | | // 27.7.2.2 AsyncFunction.prototype, https://tc39.es/ecma262/#sec-async-function-constructor-prototype |
28 | 52 | define_direct_property(vm.names.prototype, realm.intrinsics().async_function_prototype(), 0); |
29 | | |
30 | 52 | define_direct_property(vm.names.length, Value(1), Attribute::Configurable); |
31 | 52 | } |
32 | | |
33 | | // 27.7.1.1 AsyncFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments |
34 | | ThrowCompletionOr<Value> AsyncFunctionConstructor::call() |
35 | 0 | { |
36 | 0 | return TRY(construct(*this)); |
37 | 0 | } |
38 | | |
39 | | // 27.7.1.1 AsyncFunction ( ...parameterArgs, bodyArg ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments |
40 | | ThrowCompletionOr<NonnullGCPtr<Object>> AsyncFunctionConstructor::construct(FunctionObject& new_target) |
41 | 0 | { |
42 | 0 | auto& vm = this->vm(); |
43 | | |
44 | | // 1. Let C be the active function object. |
45 | 0 | auto* constructor = vm.active_function_object(); |
46 | | |
47 | | // 2. If bodyArg is not present, set bodyArg to the empty String. |
48 | | // NOTE: This does that, as well as the string extraction done inside of CreateDynamicFunction |
49 | 0 | auto extracted = TRY(extract_parameter_arguments_and_body(vm, vm.running_execution_context().arguments)); |
50 | | |
51 | | // 3. Return ? CreateDynamicFunction(C, NewTarget, async, parameterArgs, bodyArg). |
52 | 0 | return TRY(FunctionConstructor::create_dynamic_function(vm, *constructor, &new_target, FunctionKind::Async, extracted.parameters, extracted.body)); |
53 | 0 | } |
54 | | |
55 | | } |