AsyncFunctionConstructor.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AsyncFunctionConstructor.h>
  7. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  8. #include <LibJS/Runtime/FunctionConstructor.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS {
  12. AsyncFunctionConstructor::AsyncFunctionConstructor(Realm& realm)
  13. : NativeFunction(vm().names.AsyncFunction.as_string(), *realm.global_object().function_constructor())
  14. {
  15. }
  16. void AsyncFunctionConstructor::initialize(Realm& realm)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(realm);
  20. // 27.7.2.2 AsyncFunction.prototype, https://tc39.es/ecma262/#sec-async-function-constructor-prototype
  21. define_direct_property(vm.names.prototype, realm.global_object().async_function_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. }
  24. // 27.7.1.1 AsyncFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments
  25. ThrowCompletionOr<Value> AsyncFunctionConstructor::call()
  26. {
  27. return TRY(construct(*this));
  28. }
  29. // 27.7.1.1 AsyncFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments
  30. ThrowCompletionOr<Object*> AsyncFunctionConstructor::construct(FunctionObject& new_target)
  31. {
  32. auto& vm = this->vm();
  33. auto& global_object = this->global_object();
  34. // 1. Let C be the active function object.
  35. auto* constructor = vm.active_function_object();
  36. // 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
  37. auto& args = vm.running_execution_context().arguments;
  38. // 3. Return CreateDynamicFunction(C, NewTarget, async, args).
  39. return TRY(FunctionConstructor::create_dynamic_function(global_object, *constructor, &new_target, FunctionKind::Async, args));
  40. }
  41. }