GeneratorFunctionConstructor.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. ThrowCompletionOr<void> GeneratorFunctionConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. MUST_OR_THROW_OOM(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.intrinsics().generator_function_prototype(), 0);
  23. return {};
  24. }
  25. // 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
  26. ThrowCompletionOr<Value> GeneratorFunctionConstructor::call()
  27. {
  28. return TRY(construct(*this));
  29. }
  30. // 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
  31. ThrowCompletionOr<NonnullGCPtr<Object>> GeneratorFunctionConstructor::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. 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(vm, *constructor, &new_target, FunctionKind::Generator, args));
  40. }
  41. }