Prechádzať zdrojové kódy

LibJS+js: Add a debug option (js -g) to GC after every allocation

This is very useful for discovering collector bugs.
Andreas Kling 5 rokov pred
rodič
commit
cb2e7d1c5f

+ 3 - 0
Libraries/LibJS/Heap/Heap.cpp

@@ -49,6 +49,9 @@ Heap::~Heap()
 
 
 Cell* Heap::allocate_cell(size_t size)
 Cell* Heap::allocate_cell(size_t size)
 {
 {
+    if (should_collect_on_every_allocation())
+        collect_garbage();
+
     for (auto& block : m_blocks) {
     for (auto& block : m_blocks) {
         if (size > block->cell_size())
         if (size > block->cell_size())
             continue;
             continue;

+ 5 - 0
Libraries/LibJS/Heap/Heap.h

@@ -55,6 +55,9 @@ public:
 
 
     Interpreter& interpreter() { return m_interpreter; }
     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:
 private:
     Cell* allocate_cell(size_t);
     Cell* allocate_cell(size_t);
 
 
@@ -65,6 +68,8 @@ private:
 
 
     Cell* cell_from_possible_pointer(FlatPtr);
     Cell* cell_from_possible_pointer(FlatPtr);
 
 
+    bool m_should_collect_on_every_allocation { false };
+
     Interpreter& m_interpreter;
     Interpreter& m_interpreter;
     Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
     Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
 };
 };

+ 3 - 0
Userland/js.cpp

@@ -41,10 +41,12 @@
 int main(int argc, char** argv)
 int main(int argc, char** argv)
 {
 {
     bool dump_ast = false;
     bool dump_ast = false;
+    bool gc_on_every_allocation = false;
     const char* script_path = nullptr;
     const char* script_path = nullptr;
 
 
     Core::ArgsParser args_parser;
     Core::ArgsParser args_parser;
     args_parser.add_option(dump_ast, "Dump the AST", "ast-dump", 'A');
     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.add_positional_argument(script_path, "Path to script file", "script");
     args_parser.parse(argc, argv);
     args_parser.parse(argc, argv);
 
 
@@ -56,6 +58,7 @@ int main(int argc, char** argv)
     auto file_contents = file->read_all();
     auto file_contents = file->read_all();
 
 
     JS::Interpreter interpreter;
     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();
     auto program = JS::Parser(JS::Lexer(file_contents)).parse_program();