summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Libraries/LibJS/GlobalObject.cpp30
-rw-r--r--Libraries/LibJS/GlobalObject.h16
-rw-r--r--Libraries/LibJS/Interpreter.cpp14
-rw-r--r--Libraries/LibJS/Makefile1
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 \