diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-12 20:11:35 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-12 20:11:35 +0100 |
commit | 1448f4384dadd59bf30291c4148f4a04ef1dbe43 (patch) | |
tree | ef1a58a1e04c1abcf722c6688f51127fe0d5e89f /Libraries | |
parent | d1d136b4e5c935ec659fe49fc5e81ca08a969f9e (diff) | |
download | serenity-1448f4384dadd59bf30291c4148f4a04ef1dbe43.zip |
LibJS: Move GlobalObject to its own Object subclass
This is mostly for tidiness at the moment.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/GlobalObject.cpp | 30 | ||||
-rw-r--r-- | Libraries/LibJS/GlobalObject.h | 16 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibJS/Makefile | 1 |
4 files changed, 49 insertions, 12 deletions
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 <AK/LogStream.h> +#include <AK/String.h> +#include <LibJS/GlobalObject.h> +#include <LibJS/Heap.h> +#include <LibJS/Interpreter.h> +#include <LibJS/NativeFunction.h> +#include <LibJS/Value.h> +#include <stdio.h> + +namespace JS { + +GlobalObject::GlobalObject(Heap& heap) +{ + put("print", heap.allocate<NativeFunction>([](Interpreter&, Vector<Argument> arguments) -> Value { + for (auto& argument : arguments) + printf("%s ", argument.value.to_string().characters()); + return js_undefined(); + })); + put("gc", heap.allocate<NativeFunction>([](Interpreter& interpreter, Vector<Argument>) -> 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 <LibJS/Object.h> + +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 <AK/Badge.h> #include <LibJS/AST.h> +#include <LibJS/GlobalObject.h> #include <LibJS/Interpreter.h> #include <LibJS/NativeFunction.h> #include <LibJS/Object.h> #include <LibJS/Value.h> -#include <stdio.h> namespace JS { Interpreter::Interpreter() : m_heap(*this) { - m_global_object = heap().allocate<Object>(); - m_global_object->put("print", heap().allocate<NativeFunction>([](Interpreter&, Vector<Argument> arguments) -> Value { - for (auto& argument : arguments) - printf("%s ", argument.value.to_string().characters()); - return js_undefined(); - })); - m_global_object->put("gc", heap().allocate<NativeFunction>([](Interpreter& interpreter, Vector<Argument>) -> Value { - dbg() << "Forced garbage collection requested!"; - interpreter.heap().collect_garbage(); - return js_undefined(); - })); + m_global_object = heap().allocate<GlobalObject>(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 \ |