GeneratorFunctionConstructor.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Optional.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Lexer.h>
  10. #include <LibJS/Parser.h>
  11. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  12. #include <LibJS/Runtime/FunctionConstructor.h>
  13. #include <LibJS/Runtime/GeneratorFunctionConstructor.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. namespace JS {
  16. GeneratorFunctionConstructor::GeneratorFunctionConstructor(GlobalObject& global_object)
  17. : NativeFunction(*static_cast<Object*>(global_object.function_constructor()))
  18. {
  19. }
  20. void GeneratorFunctionConstructor::initialize(GlobalObject& global_object)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(global_object);
  24. // 27.3.2.1 GeneratorFunction.length, https://tc39.es/ecma262/#sec-generatorfunction.length
  25. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  26. // 27.3.2.2 GeneratorFunction.prototype, https://tc39.es/ecma262/#sec-generatorfunction.length
  27. define_direct_property(vm.names.prototype, global_object.generator_function_prototype(), 0);
  28. }
  29. GeneratorFunctionConstructor::~GeneratorFunctionConstructor()
  30. {
  31. }
  32. // 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
  33. ThrowCompletionOr<Value> GeneratorFunctionConstructor::call()
  34. {
  35. return TRY(construct(*this));
  36. }
  37. // 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
  38. ThrowCompletionOr<Object*> GeneratorFunctionConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto function = FunctionConstructor::create_dynamic_function_node(global_object(), new_target, FunctionKind::Generator);
  41. if (auto* exception = vm().exception())
  42. return throw_completion(exception->value());
  43. auto* bytecode_interpreter = Bytecode::Interpreter::current();
  44. VERIFY(bytecode_interpreter);
  45. auto executable = Bytecode::Generator::generate(function->body(), true);
  46. auto& passes = JS::Bytecode::Interpreter::optimization_pipeline();
  47. passes.perform(executable);
  48. if constexpr (JS_BYTECODE_DEBUG) {
  49. dbgln("Optimisation passes took {}us", passes.elapsed());
  50. dbgln("Compiled Bytecode::Block for function '{}':", function->name());
  51. for (auto& block : executable.basic_blocks)
  52. block.dump(executable);
  53. }
  54. return ECMAScriptFunctionObject::create(global_object(), function->name(), function->body(), function->parameters(), function->function_length(), vm().lexical_environment(), nullptr, FunctionKind::Generator, function->is_strict_mode(), function->might_need_arguments_object());
  55. }
  56. }