LibWeb: Add a naive implementation of setInterval()

This implementation leaks a Core::Timer whenever you call setInterval()
so it will definitely need some improvements, but it does kinda work!
This commit is contained in:
Andreas Kling 2020-03-21 18:55:37 +01:00
parent 81e18a9b26
commit e2a38f3aba
Notes: sideshowbarker 2024-07-19 08:11:49 +09:00

View file

@ -30,6 +30,7 @@
#include <LibGUI/Application.h>
#include <LibGUI/MessageBox.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/CSS/StyleResolver.h>
@ -348,6 +349,22 @@ JS::Interpreter& Document::interpreter()
return JS::js_undefined();
});
m_interpreter->global_object().put_native_function("setInterval", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
if (arguments.size() < 2)
return JS::js_undefined();
ASSERT(arguments[0].is_object());
ASSERT(arguments[0].as_object()->is_function());
auto callback = make_handle(const_cast<JS::Object*>(arguments[0].as_object()));
// FIXME: This timer should not be leaked! It should also be removable with clearInterval()!
(void)Core::Timer::construct(
arguments[1].to_i32(), [this, callback] {
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter, {});
}).leak_ref();
return JS::js_undefined();
});
m_interpreter->global_object().put_native_property(
"document",
[this](JS::Object*) {