Interpreter.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibJS/Bytecode/Block.h>
  8. #include <LibJS/Bytecode/Instruction.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS::Bytecode {
  12. static Interpreter* s_current;
  13. Interpreter* Interpreter::current()
  14. {
  15. return s_current;
  16. }
  17. Interpreter::Interpreter(GlobalObject& global_object)
  18. : m_vm(global_object.vm())
  19. , m_global_object(global_object)
  20. {
  21. VERIFY(!s_current);
  22. s_current = this;
  23. }
  24. Interpreter::~Interpreter()
  25. {
  26. VERIFY(s_current == this);
  27. s_current = nullptr;
  28. }
  29. Value Interpreter::run(Bytecode::Block const& block)
  30. {
  31. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run block {:p}", &block);
  32. CallFrame global_call_frame;
  33. if (vm().call_stack().is_empty()) {
  34. global_call_frame.this_value = &global_object();
  35. static FlyString global_execution_context_name = "(*BC* global execution context)";
  36. global_call_frame.function_name = global_execution_context_name;
  37. global_call_frame.scope = &global_object();
  38. VERIFY(!vm().exception());
  39. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  40. // global_call_frame.is_strict_mode = ???;
  41. vm().push_call_frame(global_call_frame, global_object());
  42. VERIFY(!vm().exception());
  43. }
  44. m_register_windows.append(make<RegisterWindow>());
  45. registers().resize(block.register_count());
  46. Bytecode::InstructionStreamIterator pc(block.instruction_stream());
  47. while (!pc.at_end()) {
  48. auto& instruction = *pc;
  49. instruction.execute(*this);
  50. if (m_pending_jump.has_value()) {
  51. pc.jump(m_pending_jump.release_value());
  52. continue;
  53. }
  54. if (!m_return_value.is_empty())
  55. break;
  56. ++pc;
  57. }
  58. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run block {:p}", &block);
  59. if constexpr (JS_BYTECODE_DEBUG) {
  60. for (size_t i = 0; i < registers().size(); ++i) {
  61. String value_string;
  62. if (registers()[i].is_empty())
  63. value_string = "(empty)";
  64. else
  65. value_string = registers()[i].to_string_without_side_effects();
  66. dbgln("[{:3}] {}", i, value_string);
  67. }
  68. }
  69. m_register_windows.take_last();
  70. auto return_value = m_return_value.value_or(js_undefined());
  71. m_return_value = {};
  72. // NOTE: The return value from a called function is put into $0 in the caller context.
  73. if (!m_register_windows.is_empty())
  74. m_register_windows.last()[0] = return_value;
  75. if (vm().call_stack().size() == 1)
  76. vm().pop_call_frame();
  77. return return_value;
  78. }
  79. }