1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:38:10 +00:00

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. :^)
This commit is contained in:
Andreas Kling 2020-03-28 23:10:37 +01:00
parent 7c4e53f31e
commit 56936b97d0
20 changed files with 233 additions and 149 deletions

View file

@ -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));
}
}
}