From e2a38f3abaf0f50ec28b94f221486fa65db7a721 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 21 Mar 2020 18:55:37 +0100 Subject: 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! --- Libraries/LibWeb/DOM/Document.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'Libraries/LibWeb') 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 #include #include +#include #include #include #include @@ -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& 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(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(static_cast(callback.cell()))->call(*m_interpreter, {}); + }).leak_ref(); + + return JS::js_undefined(); + }); + m_interpreter->global_object().put_native_property( "document", [this](JS::Object*) { -- cgit v1.2.3