summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-10-04 00:15:58 +0100
committerLinus Groh <mail@linusgroh.de>2021-10-04 09:54:24 +0100
commit7ee3432ab6c3b9e4f46d5cae83ad1edd345e9abf (patch)
tree61d972987278f788406b54d20e95dd44122b6ec0 /Userland
parent9f4ac38f08a077c24f67de4f1663c83d9d477406 (diff)
downloadserenity-7ee3432ab6c3b9e4f46d5cae83ad1edd345e9abf.zip
LibWeb: Add basic support for script string argument to setInterval()
Instead of passing a function it is also possible to pass a string, which is then evaluated as a classic script.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/Bindings/WindowObject.cpp30
1 files changed, 21 insertions, 9 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
index e0bc3815fd..146cf27129 100644
--- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -231,8 +231,11 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
return JS::js_string(vm, response);
}
+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
{
+ // FIXME: Ideally this would share more code with setTimeout() using the "timer initialization steps"
+ // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
@@ -240,12 +243,21 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
return {};
}
- auto* callback_object = vm.argument(0).to_object(global_object);
- if (!callback_object)
- return {};
- if (!callback_object->is_function()) {
- vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
- return {};
+ JS::FunctionObject* callback;
+ if (vm.argument(0).is_function()) {
+ callback = &vm.argument(0).as_function();
+ } else {
+ auto script_source = vm.argument(0).to_string(global_object);
+ if (vm.exception())
+ return {};
+ // FIXME: This needs more work once we have a environment settings object.
+ // The spec wants us to use a task for the "run function or script string" part,
+ // using a NativeFunction for the latter is a workaround so that we can reuse the
+ // DOM::Timer API unaltered (always expects a JS::FunctionObject).
+ callback = JS::NativeFunction::create(global_object, "", [impl, script_source = move(script_source)](auto&, auto&) mutable {
+ auto script = HTML::ClassicScript::create(impl->associated_document().url().to_string(), script_source, impl->associated_document().realm(), AK::URL());
+ return script->run();
+ });
}
i32 interval = 0;
if (vm.argument_count() >= 2) {
@@ -255,8 +267,8 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
if (interval < 0)
interval = 0;
}
-
- auto timer_id = impl->set_interval(*static_cast<JS::FunctionObject*>(callback_object), interval);
+ // FIXME: Pass ...arguments to the callback function when it's invoked
+ auto timer_id = impl->set_interval(*callback, interval);
return JS::Value(timer_id);
}
@@ -280,7 +292,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
if (vm.exception())
return {};
// FIXME: This needs more work once we have a environment settings object.
- // The script wants us to use a task for the "run function or script string" part,
+ // The spec wants us to use a task for the "run function or script string" part,
// using a NativeFunction for the latter is a workaround so that we can reuse the
// DOM::Timer API unaltered (always expects a JS::FunctionObject).
callback = JS::NativeFunction::create(global_object, "", [impl, script_source = move(script_source)](auto&, auto&) mutable {