summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-21 18:55:37 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-21 18:55:37 +0100
commite2a38f3abaf0f50ec28b94f221486fa65db7a721 (patch)
tree15f7d48db97eb053f2b5b40e28edf908ff611e58 /Libraries/LibWeb
parent81e18a9b26c010984e6362ff2485eca9aeaca77c (diff)
downloadserenity-e2a38f3abaf0f50ec28b94f221486fa65db7a721.zip
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!
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r--Libraries/LibWeb/DOM/Document.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 518f2a653e..f4a120bc6e 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -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*) {