Interpreter.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Generator.h"
  8. #include <LibJS/Bytecode/Label.h>
  9. #include <LibJS/Bytecode/Register.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Heap/Handle.h>
  13. #include <LibJS/Runtime/Exception.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode {
  16. using RegisterWindow = Vector<Value>;
  17. class Interpreter {
  18. public:
  19. explicit Interpreter(GlobalObject&);
  20. ~Interpreter();
  21. // FIXME: Remove this thing once we don't need it anymore!
  22. static Interpreter* current();
  23. GlobalObject& global_object() { return m_global_object; }
  24. VM& vm() { return m_vm; }
  25. Value run(Bytecode::Executable const&);
  26. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  27. Value& reg(Register const& r) { return registers()[r.index()]; }
  28. void jump(Label const& label)
  29. {
  30. m_pending_jump = &label.block();
  31. }
  32. void do_return(Value return_value) { m_return_value = return_value; }
  33. void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
  34. void leave_unwind_context();
  35. void continue_pending_unwind(Label const& resume_label);
  36. Executable const& current_executable() { return *m_current_executable; }
  37. private:
  38. RegisterWindow& registers() { return m_register_windows.last(); }
  39. VM& m_vm;
  40. GlobalObject& m_global_object;
  41. NonnullOwnPtrVector<RegisterWindow> m_register_windows;
  42. Optional<BasicBlock const*> m_pending_jump;
  43. Value m_return_value;
  44. Executable const* m_current_executable { nullptr };
  45. Vector<UnwindInfo> m_unwind_contexts;
  46. Handle<Exception> m_saved_exception;
  47. };
  48. }