diff --git a/Libraries/LibJS/GlobalObject.cpp b/Libraries/LibJS/GlobalObject.cpp index 788d6ccd71..d537ad3b38 100644 --- a/Libraries/LibJS/GlobalObject.cpp +++ b/Libraries/LibJS/GlobalObject.cpp @@ -9,18 +9,18 @@ namespace JS { -GlobalObject::GlobalObject(Heap& heap) +GlobalObject::GlobalObject() { - put("print", heap.allocate([](Interpreter&, Vector arguments) -> Value { + put_native_function("print", [](Interpreter&, Vector arguments) -> Value { for (auto& argument : arguments) printf("%s ", argument.to_string().characters()); return js_undefined(); - })); - put("gc", heap.allocate([](Interpreter& interpreter, Vector) -> Value { + }); + put_native_function("gc", [](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 index 4bbb21fdde..2efdbb8ad4 100644 --- a/Libraries/LibJS/GlobalObject.h +++ b/Libraries/LibJS/GlobalObject.h @@ -6,7 +6,7 @@ namespace JS { class GlobalObject final : public Object { public: - explicit GlobalObject(Heap&); + explicit GlobalObject(); virtual ~GlobalObject() override; private: diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index e25e31d61f..91153a538f 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -37,7 +37,7 @@ namespace JS { Interpreter::Interpreter() : m_heap(*this) { - m_global_object = heap().allocate(heap()); + m_global_object = heap().allocate(); } Interpreter::~Interpreter() diff --git a/Libraries/LibJS/Object.cpp b/Libraries/LibJS/Object.cpp index 2d4f021a9f..ae1d43397a 100644 --- a/Libraries/LibJS/Object.cpp +++ b/Libraries/LibJS/Object.cpp @@ -25,6 +25,8 @@ */ #include +#include +#include #include #include @@ -48,6 +50,11 @@ void Object::put(String property_name, Value value) m_properties.set(property_name, move(value)); } +void Object::put_native_function(String property_name, AK::Function)> native_function) +{ + put(property_name, heap().allocate(move(native_function))); +} + void Object::visit_children(Cell::Visitor& visitor) { Cell::visit_children(visitor); diff --git a/Libraries/LibJS/Object.h b/Libraries/LibJS/Object.h index 626396ff01..30cc74bf6e 100644 --- a/Libraries/LibJS/Object.h +++ b/Libraries/LibJS/Object.h @@ -41,6 +41,8 @@ public: Value get(String property_name) const; void put(String property_name, Value); + void put_native_function(String property_name, AK::Function)>); + virtual bool is_function() const { return false; } virtual bool is_native_function() const { return false; }