From 0559fb9365b6f618fa75412b894622d71a8dd910 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 25 Apr 2024 10:49:43 +0200 Subject: [PATCH 1/4] dom: first draft for window setTimeout --- src/html/window.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/html/window.zig b/src/html/window.zig index 5a34ae06..900e5ef5 100644 --- a/src/html/window.zig +++ b/src/html/window.zig @@ -19,6 +19,10 @@ const std = @import("std"); const parser = @import("netsurf"); +const jsruntime = @import("jsruntime"); +const Callback = jsruntime.Callback; +const CallbackArg = jsruntime.CallbackArg; +const Loop = jsruntime.Loop; const EventTarget = @import("../dom/event_target.zig").EventTarget; @@ -82,4 +86,16 @@ pub const Window = struct { if (self.storageShelf == null) return parser.DOMError.NotSupported; return &self.storageShelf.?.bucket.session; } + + // TODO handle callback arguments. + pub fn _setTimeout(_: *Window, loop: *Loop, cbk: Callback, delay: ?u32) !u32 { + const ddelay: u63 = delay orelse 0; + loop.timeout(ddelay * std.time.ns_per_ms, cbk); + // TODO handle timeout ID + return 1; + } + + pub fn _clearTimeout(_: *Window, _: *Loop, id: u32) !void { + _ = id; + } }; From e8a2ce361424dbd72fba3c2578d02fde10884683 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 21 May 2024 17:39:58 +0200 Subject: [PATCH 2/4] upgrade zig-js-runtime lib --- vendor/zig-js-runtime | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/zig-js-runtime b/vendor/zig-js-runtime index ade128f8..f2a6e94a 160000 --- a/vendor/zig-js-runtime +++ b/vendor/zig-js-runtime @@ -1 +1 @@ -Subproject commit ade128f8b6f1cdf7457efe9fc622448d1901afa7 +Subproject commit f2a6e94a18488cd2d5ae296b74ce70ba4af0afbf From be27359109ffb244bf07f5d5f320bf4250a1b593 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 21 May 2024 17:40:18 +0200 Subject: [PATCH 3/4] dom: implement clearTimeout --- src/html/window.zig | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/html/window.zig b/src/html/window.zig index 900e5ef5..251a1cf5 100644 --- a/src/html/window.zig +++ b/src/html/window.zig @@ -43,6 +43,11 @@ pub const Window = struct { storageShelf: ?*storage.Shelf = null, + // store a map between internal timeouts ids and pointers to uint. + // the maximum number of possible timeouts is fixed. + timeoutid: u32 = 0, + timeoutids: [512]u64 = undefined, + pub fn create(target: ?[]const u8) Window { return Window{ .target = target orelse "", @@ -88,14 +93,20 @@ pub const Window = struct { } // TODO handle callback arguments. - pub fn _setTimeout(_: *Window, loop: *Loop, cbk: Callback, delay: ?u32) !u32 { + pub fn _setTimeout(self: *Window, loop: *Loop, cbk: Callback, delay: ?u32) !u32 { + if (self.timeoutid >= self.timeoutids.len) return error.TooMuchTimeout; + const ddelay: u63 = delay orelse 0; - loop.timeout(ddelay * std.time.ns_per_ms, cbk); - // TODO handle timeout ID - return 1; + const id = loop.timeout(ddelay * std.time.ns_per_ms, cbk); + + self.timeoutids[self.timeoutid] = id; + defer self.timeoutid += 1; + + return self.timeoutid; } - pub fn _clearTimeout(_: *Window, _: *Loop, id: u32) !void { - _ = id; + pub fn _clearTimeout(self: *Window, loop: *Loop, id: u32) !void { + if (id >= self.timeoutid) return error.InvalidTimeoutId; + loop.cancel(self.timeoutids[id], null); } }; From aeaa745600c1c44a63f2ed8c419d0d22219005da Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 22 May 2024 08:51:11 +0200 Subject: [PATCH 4/4] clearTimeout: ignore invalid timeout ids --- src/html/window.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/html/window.zig b/src/html/window.zig index 251a1cf5..b81c91ba 100644 --- a/src/html/window.zig +++ b/src/html/window.zig @@ -105,8 +105,12 @@ pub const Window = struct { return self.timeoutid; } - pub fn _clearTimeout(self: *Window, loop: *Loop, id: u32) !void { - if (id >= self.timeoutid) return error.InvalidTimeoutId; + pub fn _clearTimeout(self: *Window, loop: *Loop, id: u32) void { + // I do would prefer return an error in this case, but it seems some JS + // uses invalid id, in particular id 0. + // So we silently ignore invalid id for now. + if (id >= self.timeoutid) return; + loop.cancel(self.timeoutids[id], null); } };