Interpreter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. #include <LibJS/Forward.h>
  31. #include <LibJS/Heap.h>
  32. #include <LibJS/Value.h>
  33. namespace JS {
  34. enum class ScopeType {
  35. Function,
  36. Block,
  37. };
  38. struct Variable {
  39. Value value;
  40. DeclarationType declaration_type;
  41. };
  42. struct ScopeFrame {
  43. ScopeType type;
  44. const ScopeNode& scope_node;
  45. HashMap<String, Variable> variables;
  46. };
  47. struct Argument {
  48. String name;
  49. Value value;
  50. };
  51. class Interpreter {
  52. public:
  53. Interpreter();
  54. ~Interpreter();
  55. Value run(const ScopeNode&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
  56. Object& global_object() { return *m_global_object; }
  57. const Object& global_object() const { return *m_global_object; }
  58. Heap& heap() { return m_heap; }
  59. void do_return();
  60. Value get_variable(const String& name);
  61. void set_variable(String name, Value);
  62. void declare_variable(String name, DeclarationType);
  63. void collect_roots(Badge<Heap>, HashTable<Cell*>&);
  64. void enter_scope(const ScopeNode&, Vector<Argument>, ScopeType);
  65. void exit_scope(const ScopeNode&);
  66. void push_this_value(Value value) { m_this_stack.append(move(value)); }
  67. void pop_this_value() { m_this_stack.take_last(); }
  68. Value this_value() const
  69. {
  70. if (m_this_stack.is_empty())
  71. return m_global_object;
  72. return m_this_stack.last();
  73. }
  74. private:
  75. Heap m_heap;
  76. Vector<ScopeFrame> m_scope_stack;
  77. Vector<Value> m_this_stack;
  78. Object* m_global_object { nullptr };
  79. };
  80. }