diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 5145d6b103..0f366c2df0 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -68,16 +68,16 @@ struct Argument { class Interpreter { public: - Interpreter(); - ~Interpreter(); - - template - void initialize_global_object(Args&&... args) + template + static NonnullOwnPtr create(Args&&... args) { - ASSERT(!m_global_object); - m_global_object = heap().allocate(forward(args)...); + auto interpreter = adopt_own(*new Interpreter); + interpreter->m_global_object = interpreter->heap().allocate(forward(args)...); + return interpreter; } + ~Interpreter(); + Value run(const Statement&, Vector = {}, ScopeType = ScopeType::Block); Object& global_object() { return *m_global_object; } @@ -136,6 +136,8 @@ public: } private: + Interpreter(); + Heap m_heap; Vector m_scope_stack; diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 9dee2a6e02..430d51590e 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -360,10 +360,8 @@ Color Document::visited_link_color() const JS::Interpreter& Document::interpreter() { - if (!m_interpreter) { - m_interpreter = make(); - m_interpreter->initialize_global_object(*m_window); - } + if (!m_interpreter) + m_interpreter = JS::Interpreter::create(*m_window); return *m_interpreter; } diff --git a/Userland/js.cpp b/Userland/js.cpp index 4970a870d2..48df830cc7 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -198,16 +198,15 @@ int main(int argc, char** argv) args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); - JS::Interpreter interpreter; - interpreter.initialize_global_object(); - interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation); + auto interpreter = JS::Interpreter::create(); + interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); - interpreter.global_object().put("global", &interpreter.global_object()); + interpreter->global_object().put("global", &interpreter->global_object()); if (script_path == nullptr) { editor = make(); editor->initialize(); - repl(interpreter); + repl(*interpreter); } else { auto file = Core::File::construct(script_path); if (!file->open(Core::IODevice::ReadOnly)) { @@ -232,7 +231,7 @@ int main(int argc, char** argv) if (dump_ast) program->dump(0); - auto result = interpreter.run(*program); + auto result = interpreter->run(*program); if (print_last_result) printf("%s\n", result.to_string().characters());