ExecutionContext.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Bytecode/Executable.h>
  9. #include <LibJS/Heap/Heap.h>
  10. #include <LibJS/Runtime/ExecutionContext.h>
  11. #include <LibJS/Runtime/FunctionObject.h>
  12. namespace JS {
  13. class ExecutionContextAllocator {
  14. public:
  15. NonnullOwnPtr<ExecutionContext> allocate(Heap& heap)
  16. {
  17. if (m_execution_contexts.is_empty())
  18. return adopt_own(*new ExecutionContext(heap));
  19. void* slot = m_execution_contexts.take_last();
  20. return adopt_own(*new (slot) ExecutionContext(heap));
  21. }
  22. void deallocate(void* ptr)
  23. {
  24. m_execution_contexts.append(ptr);
  25. }
  26. private:
  27. Vector<void*> m_execution_contexts;
  28. };
  29. static NeverDestroyed<ExecutionContextAllocator> s_execution_context_allocator;
  30. NonnullOwnPtr<ExecutionContext> ExecutionContext::create(Heap& heap)
  31. {
  32. return s_execution_context_allocator->allocate(heap);
  33. }
  34. void ExecutionContext::operator delete(void* ptr)
  35. {
  36. s_execution_context_allocator->deallocate(ptr);
  37. }
  38. ExecutionContext::ExecutionContext(Heap& heap)
  39. : m_heap(heap)
  40. {
  41. }
  42. ExecutionContext::~ExecutionContext()
  43. {
  44. }
  45. NonnullOwnPtr<ExecutionContext> ExecutionContext::copy() const
  46. {
  47. auto copy = create(m_heap);
  48. copy->function = function;
  49. copy->realm = realm;
  50. copy->script_or_module = script_or_module;
  51. copy->lexical_environment = lexical_environment;
  52. copy->variable_environment = variable_environment;
  53. copy->private_environment = private_environment;
  54. copy->program_counter = program_counter;
  55. copy->function_name = function_name;
  56. copy->this_value = this_value;
  57. copy->is_strict_mode = is_strict_mode;
  58. copy->executable = executable;
  59. copy->arguments = arguments;
  60. copy->passed_argument_count = passed_argument_count;
  61. copy->registers_and_constants_and_locals = registers_and_constants_and_locals;
  62. copy->unwind_contexts = unwind_contexts;
  63. copy->saved_lexical_environments = saved_lexical_environments;
  64. copy->previously_scheduled_jumps = previously_scheduled_jumps;
  65. return copy;
  66. }
  67. void ExecutionContext::visit_edges(Cell::Visitor& visitor)
  68. {
  69. visitor.visit(function);
  70. visitor.visit(realm);
  71. visitor.visit(variable_environment);
  72. visitor.visit(lexical_environment);
  73. visitor.visit(private_environment);
  74. visitor.visit(context_owner);
  75. visitor.visit(this_value);
  76. visitor.visit(executable);
  77. visitor.visit(function_name);
  78. visitor.visit(arguments);
  79. visitor.visit(registers_and_constants_and_locals);
  80. for (auto& context : unwind_contexts) {
  81. visitor.visit(context.lexical_environment);
  82. }
  83. visitor.visit(saved_lexical_environments);
  84. script_or_module.visit(
  85. [](Empty) {},
  86. [&](auto& script_or_module) {
  87. visitor.visit(script_or_module);
  88. });
  89. }
  90. }