mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
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:
parent
81e18a9b26
commit
e2a38f3aba
Notes:
sideshowbarker
2024-07-19 08:11:49 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/e2a38f3abaf
1 changed files with 17 additions and 0 deletions
|
@ -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*) {
|
||||
|
|
Loading…
Reference in a new issue