Lightpanda: the headless browser designed for AI and automation
webapi: allow timers to be called with any value
Karl Seguinca0ff7569be6
diff --git a/src/browser/tests/window/timers.html b/src/browser/tests/window/timers.htmlindex e34bd3043..8c58e26ec 100644--- a/src/browser/tests/window/timers.html+++ b/src/browser/tests/window/timers.html@@ -79,15 +79,20 @@testing.onload(() => testing.expectEqual(true, window.__si_string_ran >= 1));</script>-<script id=setTimeout-invalid-handler>- // Non-function, non-string handlers must throw a TypeError.- let threw = false;- try {- window.setTimeout(123, 1);- } catch (e) {- threw = true;- }- testing.expectEqual(true, threw);+<script id=setTimeout-non-string-handler>+ // A handler that isn't callable is converted to a string and compiled.+ testing.expectEqual('number', typeof window.setTimeout(undefined));+ testing.expectEqual('number', typeof window.setTimeout(null));+ testing.expectEqual('number', typeof window.setTimeout(123, 1));+ const ui_id = window.setInterval(undefined, 1000);+ testing.expectEqual('number', typeof ui_id);+ window.clearInterval(ui_id);++ window.__st_obj_ran = 0;+ window.setTimeout({ toString: () => 'window.__st_obj_ran = 7' }, 1);+ testing.onload(() => testing.expectEqual(7, window.__st_obj_ran));++ testing.expectError('TypeError', () => window.setTimeout(Symbol('x')));</script>diff --git a/src/browser/tests/worker/timers-worker.js b/src/browser/tests/worker/timers-worker.jsindex aa37bb8f3..2fc509afb 100644--- a/src/browser/tests/worker/timers-worker.js+++ b/src/browser/tests/worker/timers-worker.js@@ -71,11 +71,9 @@results.setInterval_string_ran = (self.__si_string_ran >= 1);}- // Non-function, non-string handlers must throw.+ // A handler that isn't callable is converted to a string and compiled.{- let threw = false;- try { setTimeout(123, 1); } catch (_) { threw = true; }- results.setTimeout_invalid_throws = threw;+ results.setTimeout_non_string_id_is_number = (typeof setTimeout(undefined) === 'number');}postMessage({ ok: true, results });diff --git a/src/browser/tests/worker/worker.html b/src/browser/tests/worker/worker.htmlindex dd6b39096..b541eda7f 100644--- a/src/browser/tests/worker/worker.html+++ b/src/browser/tests/worker/worker.html@@ -383,7 +383,7 @@testing.expectEqual(42, r.setTimeout_string_ran);testing.expectEqual(true, r.setInterval_string_ran);- testing.expectEqual(true, r.setTimeout_invalid_throws);+ testing.expectEqual(true, r.setTimeout_non_string_id_is_number);});}</script>diff --git a/src/browser/webapi/Timers.zig b/src/browser/webapi/Timers.zigindex ef01385d1..8d3b10e86 100644--- a/src/browser/webapi/Timers.zig+++ b/src/browser/webapi/Timers.zig@@ -165,18 +165,23 @@ pub fn clear(self: *Timers, id: u32) void {// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timerhandler-// TimerHandler = Function or DOMString. When a string is passed, it is-// compiled into an anonymous function body, matching how legacy browsers-// (and all current UAs) interpret `setTimeout("foo()", 100)`.+// TimerHandler = Function or DOMString. Anything that isn't callable is+// converted to a string (WebIDL union conversion), so `setTimeout(undefined)`+// compiles the script "undefined". The string is compiled into an anonymous+// function body, e.g. `setTimeout("foo()", 100)`.pub const LegacyHandler = union(enum) {function: js.Function.Global,- string: js.String,+ string: js.Value,pub fn resolve(handler: LegacyHandler, exec: *js.Execution) !js.Function.Global {switch (handler) {.function => |fun| return fun,- .string => |str| {- const fun = try exec.js.local.?.compileFunction(str, &.{}, &.{});+ .string => |value| {+ // Value.toString() uses a Symbol's description; ToString throws.+ if (value.isSymbol()) {+ return error.InvalidArgument;+ }+ const fun = try exec.js.local.?.compileFunction(try value.toString(), &.{}, &.{});return fun.persist();},}