AsyncGeneratorFunctionConstructor.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. GC_DEFINE_ALLOCATOR(AsyncGeneratorFunctionConstructor);
  13. AsyncGeneratorFunctionConstructor::AsyncGeneratorFunctionConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.AsyncGeneratorFunction.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void AsyncGeneratorFunctionConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. Base::initialize(realm);
  21. // 27.4.2.1 AsyncGeneratorFunction.length, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-length
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. // 27.4.2.2 AsyncGeneratorFunction.prototype, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().async_generator_function_prototype(), 0);
  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 ( ...parameterArgs, bodyArg ), https://tc39.es/ecma262/#sec-asyncgeneratorfunction
  32. ThrowCompletionOr<GC::Ref<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. If bodyArg is not present, set bodyArg to the empty String.
  38. // NOTE: This does that, as well as the string extraction done inside of CreateDynamicFunction
  39. auto extracted = TRY(extract_parameter_arguments_and_body(vm, vm.running_execution_context().arguments));
  40. // 3. Return ? CreateDynamicFunction(C, NewTarget, async-generator, parameterArgs, bodyArg).
  41. return TRY(FunctionConstructor::create_dynamic_function(vm, *constructor, &new_target, FunctionKind::AsyncGenerator, extracted.parameters, extracted.body));
  42. }
  43. }