GeneratorFunctionConstructor.cpp 1.9 KB

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