Skip to content

Commit

Permalink
LibWeb: Add basic support for script string argument to setInterval()
Browse files Browse the repository at this point in the history
Instead of passing a function it is also possible to pass a string,
which is then evaluated as a classic script.
  • Loading branch information
linusg committed Oct 4, 2021
1 parent 9f4ac38 commit 7ee3432
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,33 @@ 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 {};
if (!vm.argument_count()) {
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) {
Expand All @@ -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);
}

Expand All @@ -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 {
Expand Down

0 comments on commit 7ee3432

Please sign in to comment.