GeneratorFunctionConstructor.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.global_object().function_constructor()))
  13. {
  14. }
  15. void GeneratorFunctionConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::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.global_object().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<Object*> GeneratorFunctionConstructor::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, generator, args).
  39. return TRY(FunctionConstructor::create_dynamic_function(global_object, *constructor, &new_target, FunctionKind::Generator, args));
  40. }
  41. }