From cb2e7d1c5f702cd80171ace1b201f382aa69f9d5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 16 Mar 2020 19:18:46 +0100 Subject: [PATCH] LibJS+js: Add a debug option (js -g) to GC after every allocation This is very useful for discovering collector bugs. --- Libraries/LibJS/Heap/Heap.cpp | 3 +++ Libraries/LibJS/Heap/Heap.h | 5 +++++ Userland/js.cpp | 3 +++ 3 files changed, 11 insertions(+) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index 31707a11508..f8ee6794c7e 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -49,6 +49,9 @@ Heap::~Heap() Cell* Heap::allocate_cell(size_t size) { + if (should_collect_on_every_allocation()) + collect_garbage(); + for (auto& block : m_blocks) { if (size > block->cell_size()) continue; diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 691c362f357..53e85fe8a51 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -55,6 +55,9 @@ public: Interpreter& interpreter() { return m_interpreter; } + bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; } + void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; } + private: Cell* allocate_cell(size_t); @@ -65,6 +68,8 @@ private: Cell* cell_from_possible_pointer(FlatPtr); + bool m_should_collect_on_every_allocation { false }; + Interpreter& m_interpreter; Vector> m_blocks; }; diff --git a/Userland/js.cpp b/Userland/js.cpp index 9725bcbf90f..9cc0f6ab322 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -41,10 +41,12 @@ int main(int argc, char** argv) { bool dump_ast = false; + bool gc_on_every_allocation = false; const char* script_path = nullptr; Core::ArgsParser args_parser; args_parser.add_option(dump_ast, "Dump the AST", "ast-dump", 'A'); + args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g'); args_parser.add_positional_argument(script_path, "Path to script file", "script"); args_parser.parse(argc, argv); @@ -56,6 +58,7 @@ int main(int argc, char** argv) auto file_contents = file->read_all(); JS::Interpreter interpreter; + interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation); auto program = JS::Parser(JS::Lexer(file_contents)).parse_program();