AsyncGeneratorFunctionConstructor.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.h>
  8. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  9. #include <LibJS/Runtime/FunctionConstructor.h>
  10. #include <LibJS/Runtime/FunctionObject.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. namespace JS {
  13. AsyncGeneratorFunctionConstructor::AsyncGeneratorFunctionConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.AsyncGeneratorFunction.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void AsyncGeneratorFunctionConstructor::initialize(GlobalObject& global_object)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(global_object);
  21. // 27.4.2.2 AsyncGeneratorFunction.prototype, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-prototype
  22. define_direct_property(vm.names.prototype, global_object.async_generator_function_prototype(), 0);
  23. // 27.4.2.1 AsyncGeneratorFunction.length, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-length
  24. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  25. // 27.4.2.2 AsyncGeneratorFunction.prototype, https://tc39.es/ecma262/#sec-asyncgeneratorfunction-prototype
  26. define_direct_property(vm.names.prototype, global_object.async_generator_function_prototype(), 0);
  27. }
  28. // 27.4.1.1 AsyncGeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-asyncgeneratorfunction
  29. ThrowCompletionOr<Value> AsyncGeneratorFunctionConstructor::call()
  30. {
  31. return TRY(construct(*this));
  32. }
  33. // 27.4.1.1 AsyncGeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-asyncgeneratorfunction
  34. ThrowCompletionOr<Object*> AsyncGeneratorFunctionConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto function = TRY(FunctionConstructor::create_dynamic_function_node(global_object(), new_target, FunctionKind::AsyncGenerator));
  38. OwnPtr<Interpreter> local_interpreter;
  39. Interpreter* interpreter = vm.interpreter_if_exists();
  40. if (!interpreter) {
  41. local_interpreter = Interpreter::create_with_existing_realm(*realm());
  42. interpreter = local_interpreter.ptr();
  43. }
  44. VM::InterpreterExecutionScope scope(*interpreter);
  45. auto result = function->execute(*interpreter, global_object());
  46. if (auto* exception = vm.exception())
  47. return throw_completion(exception->value());
  48. VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object()));
  49. return &result.as_object();
  50. }
  51. }