VM.cpp 3.5 KB

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