diff --git a/src/html/window.zig b/src/html/window.zig index 5a34ae06..b81c91ba 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; @@ -39,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 "", @@ -82,4 +91,26 @@ pub const Window = struct { if (self.storageShelf == null) return parser.DOMError.NotSupported; return &self.storageShelf.?.bucket.session; } + + // TODO handle callback arguments. + 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; + 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(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); + } }; 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