1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

LibJS: Add Object::put_native_function() for convenience

This makes it a little bit nicer to add native function properties
to JavaScript objects.

Thanks to Sergey for suggesting it! :^)
This commit is contained in:
Andreas Kling 2020-03-13 11:06:32 +01:00
parent 6089d6566b
commit 9f38f4dbfb
5 changed files with 16 additions and 7 deletions

View file

@ -9,18 +9,18 @@
namespace JS { namespace JS {
GlobalObject::GlobalObject(Heap& heap) GlobalObject::GlobalObject()
{ {
put("print", heap.allocate<NativeFunction>([](Interpreter&, Vector<Value> arguments) -> Value { put_native_function("print", [](Interpreter&, Vector<Value> arguments) -> Value {
for (auto& argument : arguments) for (auto& argument : arguments)
printf("%s ", argument.to_string().characters()); printf("%s ", argument.to_string().characters());
return js_undefined(); return js_undefined();
})); });
put("gc", heap.allocate<NativeFunction>([](Interpreter& interpreter, Vector<Value>) -> Value { put_native_function("gc", [](Interpreter& interpreter, Vector<Value>) -> Value {
dbg() << "Forced garbage collection requested!"; dbg() << "Forced garbage collection requested!";
interpreter.heap().collect_garbage(); interpreter.heap().collect_garbage();
return js_undefined(); return js_undefined();
})); });
} }
GlobalObject::~GlobalObject() GlobalObject::~GlobalObject()

View file

@ -6,7 +6,7 @@ namespace JS {
class GlobalObject final : public Object { class GlobalObject final : public Object {
public: public:
explicit GlobalObject(Heap&); explicit GlobalObject();
virtual ~GlobalObject() override; virtual ~GlobalObject() override;
private: private:

View file

@ -37,7 +37,7 @@ namespace JS {
Interpreter::Interpreter() Interpreter::Interpreter()
: m_heap(*this) : m_heap(*this)
{ {
m_global_object = heap().allocate<GlobalObject>(heap()); m_global_object = heap().allocate<GlobalObject>();
} }
Interpreter::~Interpreter() Interpreter::~Interpreter()

View file

@ -25,6 +25,8 @@
*/ */
#include <AK/String.h> #include <AK/String.h>
#include <LibJS/Heap.h>
#include <LibJS/NativeFunction.h>
#include <LibJS/Object.h> #include <LibJS/Object.h>
#include <LibJS/Value.h> #include <LibJS/Value.h>
@ -48,6 +50,11 @@ void Object::put(String property_name, Value value)
m_properties.set(property_name, move(value)); m_properties.set(property_name, move(value));
} }
void Object::put_native_function(String property_name, AK::Function<Value(Interpreter&, Vector<Value>)> native_function)
{
put(property_name, heap().allocate<NativeFunction>(move(native_function)));
}
void Object::visit_children(Cell::Visitor& visitor) void Object::visit_children(Cell::Visitor& visitor)
{ {
Cell::visit_children(visitor); Cell::visit_children(visitor);

View file

@ -41,6 +41,8 @@ public:
Value get(String property_name) const; Value get(String property_name) const;
void put(String property_name, Value); void put(String property_name, Value);
void put_native_function(String property_name, AK::Function<Value(Interpreter&, Vector<Value>)>);
virtual bool is_function() const { return false; } virtual bool is_function() const { return false; }
virtual bool is_native_function() const { return false; } virtual bool is_native_function() const { return false; }