Interpreter.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/Block.h>
  7. #include <LibJS/Bytecode/Instruction.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS::Bytecode {
  11. Interpreter::Interpreter(GlobalObject& global_object)
  12. : m_vm(global_object.vm())
  13. , m_global_object(global_object)
  14. {
  15. }
  16. Interpreter::~Interpreter()
  17. {
  18. }
  19. void Interpreter::run(Bytecode::Block const& block)
  20. {
  21. dbgln("Bytecode::Interpreter will run block {:p}", &block);
  22. CallFrame global_call_frame;
  23. global_call_frame.this_value = &global_object();
  24. static FlyString global_execution_context_name = "(*BC* global execution context)";
  25. global_call_frame.function_name = global_execution_context_name;
  26. global_call_frame.scope = &global_object();
  27. VERIFY(!vm().exception());
  28. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  29. // global_call_frame.is_strict_mode = ???;
  30. vm().push_call_frame(global_call_frame, global_object());
  31. VERIFY(!vm().exception());
  32. m_registers.resize(block.register_count());
  33. size_t pc = 0;
  34. while (pc < block.instructions().size()) {
  35. auto& instruction = block.instructions()[pc];
  36. instruction.execute(*this);
  37. if (m_pending_jump.has_value()) {
  38. pc = m_pending_jump.release_value();
  39. continue;
  40. }
  41. ++pc;
  42. }
  43. dbgln("Bytecode::Interpreter did run block {:p}", &block);
  44. for (size_t i = 0; i < m_registers.size(); ++i) {
  45. String value_string;
  46. if (m_registers[i].is_empty())
  47. value_string = "(empty)";
  48. else
  49. value_string = m_registers[i].to_string_without_side_effects();
  50. dbgln("[{:3}] {}", i, value_string);
  51. }
  52. }
  53. }