From 1448f4384dadd59bf30291c4148f4a04ef1dbe43 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 12 Mar 2020 20:11:35 +0100 Subject: [PATCH] LibJS: Move GlobalObject to its own Object subclass This is mostly for tidiness at the moment. --- Libraries/LibJS/GlobalObject.cpp | 30 ++++++++++++++++++++++++++++++ Libraries/LibJS/GlobalObject.h | 16 ++++++++++++++++ Libraries/LibJS/Interpreter.cpp | 14 ++------------ Libraries/LibJS/Makefile | 1 + 4 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 Libraries/LibJS/GlobalObject.cpp create mode 100644 Libraries/LibJS/GlobalObject.h diff --git a/Libraries/LibJS/GlobalObject.cpp b/Libraries/LibJS/GlobalObject.cpp new file mode 100644 index 0000000000..4f18c81190 --- /dev/null +++ b/Libraries/LibJS/GlobalObject.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace JS { + +GlobalObject::GlobalObject(Heap& heap) +{ + put("print", heap.allocate([](Interpreter&, Vector arguments) -> Value { + for (auto& argument : arguments) + printf("%s ", argument.value.to_string().characters()); + return js_undefined(); + })); + put("gc", heap.allocate([](Interpreter& interpreter, Vector) -> Value { + dbg() << "Forced garbage collection requested!"; + interpreter.heap().collect_garbage(); + return js_undefined(); + })); +} + +GlobalObject::~GlobalObject() +{ +} + +} diff --git a/Libraries/LibJS/GlobalObject.h b/Libraries/LibJS/GlobalObject.h new file mode 100644 index 0000000000..4bbb21fdde --- /dev/null +++ b/Libraries/LibJS/GlobalObject.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace JS { + +class GlobalObject final : public Object { +public: + explicit GlobalObject(Heap&); + virtual ~GlobalObject() override; + +private: + virtual const char* class_name() const override { return "GlobalObject"; } +}; + +} diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index a1983025d8..e25e31d61f 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -26,28 +26,18 @@ #include #include +#include #include #include #include #include -#include namespace JS { Interpreter::Interpreter() : m_heap(*this) { - m_global_object = heap().allocate(); - m_global_object->put("print", heap().allocate([](Interpreter&, Vector arguments) -> Value { - for (auto& argument : arguments) - printf("%s ", argument.value.to_string().characters()); - return js_undefined(); - })); - m_global_object->put("gc", heap().allocate([](Interpreter& interpreter, Vector) -> Value { - dbg() << "Forced garbage collection requested!"; - interpreter.heap().collect_garbage(); - return js_undefined(); - })); + m_global_object = heap().allocate(heap()); } Interpreter::~Interpreter() diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index 64d4b9bed2..b553889594 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -2,6 +2,7 @@ OBJS = \ AST.o \ Cell.o \ Function.o \ + GlobalObject.o \ Heap.o \ HeapBlock.o \ Interpreter.o \