AsyncGeneratorFunctionConstructor.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = TRY(function->execute(*interpreter, global_object())).release_value();
  46. VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object()));
  47. return &result.as_object();
  48. }
  49. }