AsyncFunctionConstructor.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. GC_DEFINE_ALLOCATOR(AsyncFunctionConstructor);
  13. AsyncFunctionConstructor::AsyncFunctionConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.AsyncFunction.as_string(), realm.intrinsics().function_constructor())
  15. {
  16. }
  17. void AsyncFunctionConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. Base::initialize(realm);
  21. // 27.7.2.2 AsyncFunction.prototype, https://tc39.es/ecma262/#sec-async-function-constructor-prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().async_function_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  24. }
  25. // 27.7.1.1 AsyncFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments
  26. ThrowCompletionOr<Value> AsyncFunctionConstructor::call()
  27. {
  28. return TRY(construct(*this));
  29. }
  30. // 27.7.1.1 AsyncFunction ( ...parameterArgs, bodyArg ), https://tc39.es/ecma262/#sec-async-function-constructor-arguments
  31. ThrowCompletionOr<GC::Ref<Object>> AsyncFunctionConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& vm = this->vm();
  34. // 1. Let C be the active function object.
  35. auto* constructor = vm.active_function_object();
  36. // 2. If bodyArg is not present, set bodyArg to the empty String.
  37. // NOTE: This does that, as well as the string extraction done inside of CreateDynamicFunction
  38. auto extracted = TRY(extract_parameter_arguments_and_body(vm, vm.running_execution_context().arguments));
  39. // 3. Return ? CreateDynamicFunction(C, NewTarget, async, parameterArgs, bodyArg).
  40. return TRY(FunctionConstructor::create_dynamic_function(vm, *constructor, &new_target, FunctionKind::Async, extracted.parameters, extracted.body));
  41. }
  42. }