Ver Fonte

LibJS+LibWeb: Move native JS functions into dedicated member functions

Instead of implementing every native function as a lambda function,
use static member functions instead.

This makes it easier to navigate the code + backtraces look nicer. :^)
Andreas Kling há 5 anos atrás
pai
commit
56936b97d0

+ 32 - 25
Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -35,36 +35,43 @@ namespace JS {
 
 ArrayPrototype::ArrayPrototype()
 {
-    put_native_function("shift", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        ASSERT(this_object->is_array());
-        return static_cast<Array*>(this_object)->shift();
-    });
+    put_native_function("shift", shift);
+    put_native_function("pop", pop);
+    put_native_function("push", push);
+}
 
-    put_native_function("pop", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        ASSERT(this_object->is_array());
-        return static_cast<Array*>(this_object)->pop();
-    });
+ArrayPrototype::~ArrayPrototype()
+{
+}
 
-    put_native_function("push", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        ASSERT(this_object->is_array());
-        if (interpreter.call_frame().arguments.is_empty())
-            return js_undefined();
-        static_cast<Array*>(this_object)->push(interpreter.call_frame().arguments[0]);
-        return Value(static_cast<const Array*>(this_object)->length());
-    });
+Value ArrayPrototype::push(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    ASSERT(this_object->is_array());
+    if (interpreter.call_frame().arguments.is_empty())
+        return js_undefined();
+    static_cast<Array*>(this_object)->push(interpreter.call_frame().arguments[0]);
+    return Value(static_cast<const Array*>(this_object)->length());
 }
 
-ArrayPrototype::~ArrayPrototype()
+Value ArrayPrototype::pop(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    ASSERT(this_object->is_array());
+    return static_cast<Array*>(this_object)->pop();
+}
+
+Value ArrayPrototype::shift(Interpreter& interpreter)
 {
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    ASSERT(this_object->is_array());
+    return static_cast<Array*>(this_object)->shift();
 }
 
 }

+ 4 - 0
Libraries/LibJS/Runtime/ArrayPrototype.h

@@ -37,6 +37,10 @@ public:
 
 private:
     virtual const char* class_name() const override { return "ArrayPrototype"; }
+
+    static Value push(Interpreter&);
+    static Value shift(Interpreter&);
+    static Value pop(Interpreter&);
 };
 
 }

+ 12 - 9
Libraries/LibJS/Runtime/ConsoleObject.cpp

@@ -34,19 +34,22 @@ namespace JS {
 
 ConsoleObject::ConsoleObject()
 {
-    put_native_function("log", [](Interpreter& interpreter) -> Value {
-        for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) {
-            printf("%s", interpreter.call_frame().arguments[i].to_string().characters());
-            if (i != interpreter.call_frame().arguments.size() - 1)
-                putchar(' ');
-        }
-        putchar('\n');
-        return js_undefined();
-    });
+    put_native_function("log", log);
 }
 
 ConsoleObject::~ConsoleObject()
 {
 }
 
+Value ConsoleObject::log(Interpreter& interpreter)
+{
+    for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) {
+        printf("%s", interpreter.call_frame().arguments[i].to_string().characters());
+        if (i != interpreter.call_frame().arguments.size() - 1)
+            putchar(' ');
+    }
+    putchar('\n');
+    return js_undefined();
+}
+
 }

+ 2 - 0
Libraries/LibJS/Runtime/ConsoleObject.h

@@ -37,6 +37,8 @@ public:
 
 private:
     virtual const char* class_name() const override { return "ConsoleObject"; }
+
+    static Value log(Interpreter&);
 };
 
 }

+ 17 - 10
Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -13,17 +13,10 @@ namespace JS {
 
 GlobalObject::GlobalObject()
 {
+    put_native_function("gc", gc);
+    put_native_function("isNaN", is_nan);
+
     put("console", heap().allocate<ConsoleObject>());
-    put_native_function("gc", [](Interpreter& interpreter) -> Value {
-        dbg() << "Forced garbage collection requested!";
-        interpreter.heap().collect_garbage();
-        return js_undefined();
-    });
-    put_native_function("isNaN", [](Interpreter& interpreter) -> Value {
-        if (interpreter.call_frame().arguments.size() < 1)
-            return js_undefined();
-        return Value(interpreter.call_frame().arguments[0].to_number().is_nan());
-    });
     put("Math", heap().allocate<MathObject>());
     put("Object", heap().allocate<ObjectConstructor>());
 }
@@ -32,4 +25,18 @@ GlobalObject::~GlobalObject()
 {
 }
 
+Value GlobalObject::gc(Interpreter& interpreter)
+{
+    dbg() << "Forced garbage collection requested!";
+    interpreter.heap().collect_garbage();
+    return js_undefined();
+}
+
+Value GlobalObject::is_nan(Interpreter& interpreter)
+{
+    if (interpreter.call_frame().arguments.size() < 1)
+        return js_undefined();
+    return Value(interpreter.call_frame().arguments[0].to_number().is_nan());
+}
+
 }

+ 3 - 0
Libraries/LibJS/Runtime/GlobalObject.h

@@ -11,6 +11,9 @@ public:
 
 private:
     virtual const char* class_name() const override { return "GlobalObject"; }
+
+    static Value gc(Interpreter&);
+    static Value is_nan(Interpreter&);
 };
 
 }

+ 11 - 8
Libraries/LibJS/Runtime/MathObject.cpp

@@ -32,18 +32,21 @@ namespace JS {
 
 MathObject::MathObject()
 {
-    put_native_function("random", [](Interpreter&) {
-#ifdef __serenity__
-        double r = (double)arc4random() / (double)UINT32_MAX;
-#else
-        double r = (double)rand() / (double)RAND_MAX;
-#endif
-        return Value(r);
-    });
+    put_native_function("random", random);
 }
 
 MathObject::~MathObject()
 {
 }
 
+Value MathObject::random(Interpreter&)
+{
+#ifdef __serenity__
+    double r = (double)arc4random() / (double)UINT32_MAX;
+#else
+    double r = (double)rand() / (double)RAND_MAX;
+#endif
+    return Value(r);
+}
+
 }

+ 2 - 0
Libraries/LibJS/Runtime/MathObject.h

@@ -37,6 +37,8 @@ public:
 
 private:
     virtual const char* class_name() const override { return "MathObject"; }
+
+    static Value random(Interpreter&);
 };
 
 }

+ 28 - 21
Libraries/LibJS/Runtime/ObjectPrototype.cpp

@@ -37,32 +37,39 @@ ObjectPrototype::ObjectPrototype()
 {
     set_prototype(nullptr);
 
-    put_native_function("hasOwnProperty", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        if (interpreter.call_frame().arguments.is_empty())
-            return js_undefined();
-        return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string()));
-    });
+    put_native_function("hasOwnProperty", has_own_property);
+    put_native_function("toString", to_string);
+    put_native_function("valueOf", value_of);
+}
 
-    put_native_function("toString", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        return Value(this_object->to_string());
-    });
+ObjectPrototype::~ObjectPrototype()
+{
+}
 
-    put_native_function("valueOf", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        return this_object->value_of();
-    });
+Value ObjectPrototype::has_own_property(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    if (interpreter.call_frame().arguments.is_empty())
+        return js_undefined();
+    return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string()));
 }
 
-ObjectPrototype::~ObjectPrototype()
+Value ObjectPrototype::to_string(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    return Value(this_object->to_string());
+}
+
+Value ObjectPrototype::value_of(Interpreter& interpreter)
 {
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    return this_object->value_of();
 }
 
 }

+ 4 - 0
Libraries/LibJS/Runtime/ObjectPrototype.h

@@ -37,6 +37,10 @@ public:
 
 private:
     virtual const char* class_name() const override { return "ObjectPrototype"; }
+
+    static Value has_own_property(Interpreter&);
+    static Value value_of(Interpreter&);
+    static Value to_string(Interpreter&);
 };
 
 }

+ 38 - 32
Libraries/LibJS/Runtime/StringPrototype.cpp

@@ -44,42 +44,48 @@ StringPrototype::StringPrototype()
             return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
         },
         nullptr);
-    put_native_function("charAt", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        i32 index = 0;
-        if (!interpreter.call_frame().arguments.is_empty())
-            index = interpreter.call_frame().arguments[0].to_i32();
-        ASSERT(this_object->is_string_object());
-        auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
-        if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
-            return js_string(this_object->heap(), String::empty());
-        return js_string(this_object->heap(), underlying_string.substring(index, 1));
-    });
-    put_native_function("repeat", [](Interpreter& interpreter) -> Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-        ASSERT(this_object->is_string_object());
-        if (interpreter.call_frame().arguments.is_empty())
-            return js_string(this_object->heap(), String::empty());
-        i32 count = 0;
-        count = interpreter.call_frame().arguments[0].to_i32();
-        if (count < 0) {
-            // FIXME: throw RangeError
-            return js_undefined();
-        }
-        auto* string_object = static_cast<StringObject*>(this_object);
-        StringBuilder builder;
-        for (i32 i = 0; i < count; ++i)
-            builder.append(string_object->primitive_string()->string());
-        return js_string(this_object->heap(), builder.to_string());
-    });
+    put_native_function("charAt", char_at);
+    put_native_function("repeat", repeat);
 }
 
 StringPrototype::~StringPrototype()
 {
 }
 
+Value StringPrototype::char_at(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    i32 index = 0;
+    if (!interpreter.call_frame().arguments.is_empty())
+        index = interpreter.call_frame().arguments[0].to_i32();
+    ASSERT(this_object->is_string_object());
+    auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
+    if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
+        return js_string(interpreter.heap(), String::empty());
+    return js_string(interpreter.heap(), underlying_string.substring(index, 1));
+}
+
+Value StringPrototype::repeat(Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    ASSERT(this_object->is_string_object());
+    if (interpreter.call_frame().arguments.is_empty())
+        return js_string(interpreter.heap(), String::empty());
+    i32 count = 0;
+    count = interpreter.call_frame().arguments[0].to_i32();
+    if (count < 0) {
+        // FIXME: throw RangeError
+        return js_undefined();
+    }
+    auto* string_object = static_cast<StringObject*>(this_object);
+    StringBuilder builder;
+    for (i32 i = 0; i < count; ++i)
+        builder.append(string_object->primitive_string()->string());
+    return js_string(interpreter.heap(), builder.to_string());
+}
+
 }

+ 3 - 0
Libraries/LibJS/Runtime/StringPrototype.h

@@ -37,6 +37,9 @@ public:
 
 private:
     virtual const char* class_name() const override { return "StringPrototype"; }
+
+    static Value char_at(Interpreter&);
+    static Value repeat(Interpreter&);
 };
 
 }

+ 14 - 7
Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp

@@ -51,18 +51,25 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
         [this](JS::Object*, JS::Value value) {
             m_impl->set_fill_style(value.to_string());
         });
-    put_native_function("fillRect", [this](JS::Interpreter& interpreter) {
-        auto& arguments = interpreter.call_frame().arguments;
-        if (arguments.size() >= 4) {
-            m_impl->fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
-        }
-        return JS::js_undefined();
-    });
+    put_native_function("fillRect", fill_rect);
 }
 
 CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()
 {
 }
 
+JS::Value CanvasRenderingContext2DWrapper::fill_rect(JS::Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    // FIXME: Verify that it's a CanvasRenderingContext2DWrapper somehow!
+    auto& impl = static_cast<CanvasRenderingContext2DWrapper*>(this_object)->impl();
+    auto& arguments = interpreter.call_frame().arguments;
+    if (arguments.size() >= 4)
+        impl.fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
+    return JS::js_undefined();
+}
+
 }
 }

+ 2 - 0
Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h

@@ -42,6 +42,8 @@ public:
 private:
     virtual const char* class_name() const override { return "CanvasRenderingContext2DWrapper"; }
 
+    static JS::Value fill_rect(JS::Interpreter&);
+
     NonnullRefPtr<CanvasRenderingContext2D> m_impl;
 };
 

+ 18 - 10
Libraries/LibWeb/Bindings/DocumentWrapper.cpp

@@ -38,16 +38,7 @@ namespace Bindings {
 DocumentWrapper::DocumentWrapper(Document& document)
     : NodeWrapper(document)
 {
-    put_native_function("getElementById", [this](JS::Interpreter& interpreter) -> JS::Value {
-        auto& arguments = interpreter.call_frame().arguments;
-        if (arguments.is_empty())
-            return JS::js_null();
-        auto id = arguments[0].to_string();
-        auto* element = node().get_element_by_id(id);
-        if (!element)
-            return JS::js_null();
-        return wrap(heap(), const_cast<Element&>(*element));
-    });
+    put_native_function("getElementById", get_element_by_id);
 }
 
 DocumentWrapper::~DocumentWrapper()
@@ -64,5 +55,22 @@ const Document& DocumentWrapper::node() const
     return static_cast<const Document&>(NodeWrapper::node());
 }
 
+JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    // FIXME: Verify that it's a DocumentWrapper somehow!
+    auto& node = static_cast<DocumentWrapper*>(this_object)->node();
+    auto& arguments = interpreter.call_frame().arguments;
+    if (arguments.is_empty())
+        return JS::js_null();
+    auto id = arguments[0].to_string();
+    auto* element = node.get_element_by_id(id);
+    if (!element)
+        return JS::js_null();
+    return wrap(interpreter.heap(), const_cast<Element&>(*element));
+}
+
 }
 }

+ 2 - 0
Libraries/LibWeb/Bindings/DocumentWrapper.h

@@ -41,6 +41,8 @@ public:
 
 private:
     virtual const char* class_name() const override { return "DocumentWrapper"; }
+
+    static JS::Value get_element_by_id(JS::Interpreter&);
 };
 
 }

+ 18 - 17
Libraries/LibWeb/Bindings/EventTargetWrapper.cpp

@@ -39,28 +39,29 @@ namespace Bindings {
 EventTargetWrapper::EventTargetWrapper(EventTarget& impl)
     : m_impl(impl)
 {
-    put_native_function("addEventListener", [](JS::Interpreter& interpreter) -> JS::Value {
-        auto* this_object = interpreter.this_value().to_object(interpreter.heap());
-        if (!this_object)
-            return {};
-
-        auto& arguments = interpreter.call_frame().arguments;
-        if (arguments.size() < 2)
-            return JS::js_undefined();
-
-        auto event_name = arguments[0].to_string();
-        ASSERT(arguments[1].is_object());
-        ASSERT(arguments[1].as_object()->is_function());
-        auto* function = static_cast<JS::Function*>(const_cast<Object*>(arguments[1].as_object()));
-        auto listener = adopt(*new EventListener(JS::make_handle(function)));
-        static_cast<EventTargetWrapper*>(this_object)->impl().add_event_listener(event_name, move(listener));
-        return JS::js_undefined();
-    });
+    put_native_function("addEventListener", add_event_listener);
 }
 
 EventTargetWrapper::~EventTargetWrapper()
 {
 }
 
+JS::Value EventTargetWrapper::add_event_listener(JS::Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    auto& arguments = interpreter.call_frame().arguments;
+    if (arguments.size() < 2)
+        return JS::js_undefined();
+    auto event_name = arguments[0].to_string();
+    ASSERT(arguments[1].is_object());
+    ASSERT(arguments[1].as_object()->is_function());
+    auto* function = static_cast<JS::Function*>(const_cast<Object*>(arguments[1].as_object()));
+    auto listener = adopt(*new EventListener(JS::make_handle(function)));
+    static_cast<EventTargetWrapper*>(this_object)->impl().add_event_listener(event_name, move(listener));
+    return JS::js_undefined();
+}
+
 }
 }

+ 2 - 0
Libraries/LibWeb/Bindings/EventTargetWrapper.h

@@ -42,6 +42,8 @@ public:
 private:
     virtual const char* class_name() const override { return "EventTargetWrapper"; }
 
+    static JS::Value add_event_listener(JS::Interpreter&);
+
     NonnullRefPtr<EventTarget> m_impl;
 };
 

+ 17 - 8
Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp

@@ -40,14 +40,8 @@ namespace Bindings {
 HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element)
     : ElementWrapper(element)
 {
-    put_native_function("getContext", [this](JS::Interpreter& interpreter) -> JS::Value {
-        auto& arguments = interpreter.call_frame().arguments;
-        if (arguments.size() >= 1) {
-            auto* context = node().get_context(arguments[0].to_string());
-            return wrap(heap(), *context);
-        }
-        return JS::js_undefined();
-    });
+    put_native_function("getContext", get_context);
+
     put_native_property(
         "width",
         [this](JS::Object*) {
@@ -76,5 +70,20 @@ const HTMLCanvasElement& HTMLCanvasElementWrapper::node() const
     return static_cast<const HTMLCanvasElement&>(NodeWrapper::node());
 }
 
+JS::Value HTMLCanvasElementWrapper::get_context(JS::Interpreter& interpreter)
+{
+    auto* this_object = interpreter.this_value().to_object(interpreter.heap());
+    if (!this_object)
+        return {};
+    // FIXME: Verify that it's an HTMLCanvasElementWrapper somehow!
+    auto& node = static_cast<HTMLCanvasElementWrapper*>(this_object)->node();
+    auto& arguments = interpreter.call_frame().arguments;
+    if (arguments.size() >= 1) {
+        auto* context = node.get_context(arguments[0].to_string());
+        return wrap(interpreter.heap(), *context);
+    }
+    return JS::js_undefined();
+}
+
 }
 }

+ 2 - 0
Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.h

@@ -41,6 +41,8 @@ public:
 
 private:
     virtual const char* class_name() const override { return "HTMLCanvasElementWrapper"; }
+
+    static JS::Value get_context(JS::Interpreter&);
 };
 
 }