VM.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <LibJS/Interpreter.h>
  27. #include <LibJS/Runtime/Symbol.h>
  28. #include <LibJS/Runtime/VM.h>
  29. namespace JS {
  30. NonnullRefPtr<VM> VM::create()
  31. {
  32. return adopt(*new VM);
  33. }
  34. VM::VM()
  35. : m_heap(*this)
  36. {
  37. #define __JS_ENUMERATE(SymbolName, snake_name) \
  38. m_well_known_symbol_##snake_name = js_symbol(*this, "Symbol." #SymbolName, false);
  39. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  40. #undef __JS_ENUMERATE
  41. }
  42. VM::~VM()
  43. {
  44. }
  45. Interpreter& VM::interpreter()
  46. {
  47. ASSERT(!m_interpreters.is_empty());
  48. return *m_interpreters.last();
  49. }
  50. Interpreter* VM::interpreter_if_exists()
  51. {
  52. if (m_interpreters.is_empty())
  53. return nullptr;
  54. return m_interpreters.last();
  55. }
  56. void VM::push_interpreter(Interpreter& interpreter)
  57. {
  58. m_interpreters.append(&interpreter);
  59. }
  60. void VM::pop_interpreter(Interpreter& interpreter)
  61. {
  62. ASSERT(!m_interpreters.is_empty());
  63. auto* popped_interpreter = m_interpreters.take_last();
  64. ASSERT(popped_interpreter == &interpreter);
  65. }
  66. VM::InterpreterExecutionScope::InterpreterExecutionScope(Interpreter& interpreter)
  67. : m_interpreter(interpreter)
  68. {
  69. m_interpreter.vm().push_interpreter(m_interpreter);
  70. }
  71. VM::InterpreterExecutionScope::~InterpreterExecutionScope()
  72. {
  73. m_interpreter.vm().pop_interpreter(m_interpreter);
  74. }
  75. void VM::gather_roots(HashTable<Cell*>& roots)
  76. {
  77. if (m_exception)
  78. roots.set(m_exception);
  79. for (auto* interpreter : m_interpreters)
  80. interpreter->gather_roots(roots);
  81. #define __JS_ENUMERATE(SymbolName, snake_name) \
  82. roots.set(well_known_symbol_##snake_name());
  83. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  84. #undef __JS_ENUMERATE
  85. for (auto& symbol : m_global_symbol_map)
  86. roots.set(symbol.value);
  87. }
  88. Symbol* VM::get_global_symbol(const String& description)
  89. {
  90. auto result = m_global_symbol_map.get(description);
  91. if (result.has_value())
  92. return result.value();
  93. auto new_global_symbol = js_symbol(*this, description, true);
  94. m_global_symbol_map.set(description, new_global_symbol);
  95. return new_global_symbol;
  96. }
  97. }