Configuration.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  8. namespace Wasm {
  9. class Configuration {
  10. public:
  11. explicit Configuration(Store& store)
  12. : m_store(store)
  13. {
  14. }
  15. Optional<Label> nth_label(size_t);
  16. void set_frame(Frame&& frame)
  17. {
  18. m_current_frame_index = m_stack.size();
  19. Label label(frame.arity(), frame.expression().instructions().size());
  20. m_stack.push(move(frame));
  21. m_stack.push(label);
  22. }
  23. auto& frame() const { return m_stack.entries()[m_current_frame_index].get<Frame>(); }
  24. auto& frame() { return m_stack.entries()[m_current_frame_index].get<Frame>(); }
  25. auto& ip() const { return m_ip; }
  26. auto& ip() { return m_ip; }
  27. auto& depth() const { return m_depth; }
  28. auto& depth() { return m_depth; }
  29. auto& stack() const { return m_stack; }
  30. auto& stack() { return m_stack; }
  31. auto& store() const { return m_store; }
  32. auto& store() { return m_store; }
  33. struct CallFrameHandle {
  34. explicit CallFrameHandle(Configuration& configuration)
  35. : frame_index(configuration.m_current_frame_index)
  36. , stack_size(configuration.m_stack.size())
  37. , ip(configuration.ip())
  38. , configuration(configuration)
  39. {
  40. configuration.depth()++;
  41. }
  42. ~CallFrameHandle()
  43. {
  44. configuration.unwind({}, *this);
  45. }
  46. size_t frame_index { 0 };
  47. size_t stack_size { 0 };
  48. InstructionPointer ip { 0 };
  49. Configuration& configuration;
  50. };
  51. void unwind(Badge<CallFrameHandle>, const CallFrameHandle&);
  52. Result call(Interpreter&, FunctionAddress, Vector<Value> arguments);
  53. Result execute(Interpreter&);
  54. void dump_stack();
  55. private:
  56. Store& m_store;
  57. size_t m_current_frame_index { 0 };
  58. Stack m_stack;
  59. size_t m_depth { 0 };
  60. InstructionPointer m_ip;
  61. };
  62. }