AsyncGeneratorFunctionConstructor.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.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. AsyncGeneratorFunctionConstructor::AsyncGeneratorFunctionConstructor(Realm& realm)
  13. : NativeFunction(realm.vm().names.AsyncGeneratorFunction.as_string(), realm.intrinsics().function_prototype())
  14. {
  15. }
  16. ThrowCompletionOr<void> AsyncGeneratorFunctionConstructor::initialize(Realm& realm)
  17. {
  18. auto& vm = this->vm();
  19. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  20. // 27.4.2.1 AsyncGeneratorFunction.length, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-length
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. // 27.4.2.2 AsyncGeneratorFunction.prototype, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().async_generator_function_prototype(), 0);
  24. return {};
  25. }
  26. // 27.4.1.1 AsyncGeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-asyncgeneratorfunction
  27. ThrowCompletionOr<Value> AsyncGeneratorFunctionConstructor::call()
  28. {
  29. return TRY(construct(*this));
  30. }
  31. // 27.4.1.1 AsyncGeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-asyncgeneratorfunction
  32. ThrowCompletionOr<NonnullGCPtr<Object>> AsyncGeneratorFunctionConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. // 1. Let C be the active function object.
  36. auto* constructor = vm.active_function_object();
  37. // 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
  38. auto& args = vm.running_execution_context().arguments;
  39. // 3. Return ? CreateDynamicFunction(C, NewTarget, asyncGenerator, args).
  40. return *TRY(FunctionConstructor::create_dynamic_function(vm, *constructor, &new_target, FunctionKind::AsyncGenerator, args));
  41. }
  42. }