|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <francis@lightpanda.io> |
| 4 | +// Pierre Tachoire <pierre@lightpanda.io> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const std = @import("std"); |
| 20 | +const log = @import("../../log.zig"); |
| 21 | +const parser = @import("../netsurf.zig"); |
| 22 | +const Env = @import("../env.zig").Env; |
| 23 | +const Page = @import("../page.zig").Page; |
| 24 | +const Loop = @import("../../runtime/loop.zig").Loop; |
| 25 | +const EventTarget = @import("../dom/event_target.zig").EventTarget; |
| 26 | + |
| 27 | +pub const Interfaces = .{ |
| 28 | + AbortController, |
| 29 | + AbortSignal, |
| 30 | +}; |
| 31 | + |
| 32 | +const AbortController = @This(); |
| 33 | + |
| 34 | +signal: *AbortSignal, |
| 35 | + |
| 36 | +pub fn constructor(page: *Page) !AbortController { |
| 37 | + // Why do we allocate this rather than storing directly in the struct? |
| 38 | + // https://github.com/lightpanda-io/project/discussions/165 |
| 39 | + const signal = try page.arena.create(AbortSignal); |
| 40 | + signal.* = .init; |
| 41 | + |
| 42 | + return .{ |
| 43 | + .signal = signal, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +pub fn get_signal(self: *AbortController) *AbortSignal { |
| 48 | + return self.signal; |
| 49 | +} |
| 50 | + |
| 51 | +pub fn _abort(self: *AbortController, reason_: ?[]const u8) !void { |
| 52 | + return self.signal.abort(reason_); |
| 53 | +} |
| 54 | + |
| 55 | +pub const AbortSignal = struct { |
| 56 | + const DEFAULT_REASON = "AbortError"; |
| 57 | + |
| 58 | + pub const prototype = *EventTarget; |
| 59 | + proto: parser.EventTargetTBase = .{}, |
| 60 | + |
| 61 | + aborted: bool, |
| 62 | + reason: ?[]const u8, |
| 63 | + |
| 64 | + pub const init: AbortSignal = .{ |
| 65 | + .proto = .{}, |
| 66 | + .reason = null, |
| 67 | + .aborted = false, |
| 68 | + }; |
| 69 | + |
| 70 | + pub fn static_abort(reason_: ?[]const u8) AbortSignal { |
| 71 | + return .{ |
| 72 | + .aborted = true, |
| 73 | + .reason = reason_ orelse DEFAULT_REASON, |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + pub fn static_timeout(delay: u32, page: *Page) !*AbortSignal { |
| 78 | + const callback = try page.arena.create(TimeoutCallback); |
| 79 | + callback.* = .{ |
| 80 | + .signal = .init, |
| 81 | + .node = .{ .func = TimeoutCallback.run }, |
| 82 | + }; |
| 83 | + |
| 84 | + const delay_ms: u63 = @as(u63, delay) * std.time.ns_per_ms; |
| 85 | + _ = try page.loop.timeout(delay_ms, &callback.node); |
| 86 | + return &callback.signal; |
| 87 | + } |
| 88 | + |
| 89 | + pub fn get_aborted(self: *const AbortSignal) bool { |
| 90 | + return self.aborted; |
| 91 | + } |
| 92 | + |
| 93 | + fn abort(self: *AbortSignal, reason_: ?[]const u8) !void { |
| 94 | + self.aborted = true; |
| 95 | + self.reason = reason_ orelse DEFAULT_REASON; |
| 96 | + |
| 97 | + const abort_event = try parser.eventCreate(); |
| 98 | + try parser.eventSetInternalType(abort_event, .abort_signal); |
| 99 | + |
| 100 | + defer parser.eventDestroy(abort_event); |
| 101 | + try parser.eventInit(abort_event, "abort", .{}); |
| 102 | + _ = try parser.eventTargetDispatchEvent( |
| 103 | + parser.toEventTarget(AbortSignal, self), |
| 104 | + abort_event, |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + const Reason = union(enum) { |
| 109 | + reason: []const u8, |
| 110 | + undefined: void, |
| 111 | + }; |
| 112 | + pub fn get_reason(self: *const AbortSignal) Reason { |
| 113 | + if (self.reason) |r| { |
| 114 | + return .{ .reason = r }; |
| 115 | + } |
| 116 | + return .{ .undefined = {} }; |
| 117 | + } |
| 118 | + |
| 119 | + const ThrowIfAborted = union(enum) { |
| 120 | + exception: Env.Exception, |
| 121 | + undefined: void, |
| 122 | + }; |
| 123 | + pub fn _throwIfAborted(self: *const AbortSignal, page: *Page) ThrowIfAborted { |
| 124 | + if (self.aborted) { |
| 125 | + const ex = page.main_context.throw(self.reason orelse DEFAULT_REASON); |
| 126 | + return .{ .exception = ex }; |
| 127 | + } |
| 128 | + return .{ .undefined = {} }; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +const TimeoutCallback = struct { |
| 133 | + signal: AbortSignal, |
| 134 | + |
| 135 | + // This is the internal data that the event loop tracks. We'll get this |
| 136 | + // back in run and, from it, can get our TimeoutCallback instance |
| 137 | + node: Loop.CallbackNode = undefined, |
| 138 | + |
| 139 | + fn run(node: *Loop.CallbackNode, _: *?u63) void { |
| 140 | + const self: *TimeoutCallback = @fieldParentPtr("node", node); |
| 141 | + self.signal.abort("TimeoutError") catch |err| { |
| 142 | + log.warn(.app, "abort signal timeout", .{ .err = err }); |
| 143 | + }; |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +const testing = @import("../../testing.zig"); |
| 148 | +test "Browser.HTML.AbortController" { |
| 149 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); |
| 150 | + defer runner.deinit(); |
| 151 | + |
| 152 | + try runner.testCases(&.{ |
| 153 | + .{ "var called = 0", null }, |
| 154 | + .{ "var a1 = new AbortController()", null }, |
| 155 | + .{ "var s1 = a1.signal", null }, |
| 156 | + .{ "s1.throwIfAborted()", "undefined" }, |
| 157 | + .{ "s1.reason", "undefined" }, |
| 158 | + .{ "var target;", null }, |
| 159 | + .{ |
| 160 | + \\ s1.addEventListener('abort', (e) => { |
| 161 | + \\ called += 1; |
| 162 | + \\ target = e.target; |
| 163 | + \\ |
| 164 | + \\ }); |
| 165 | + , |
| 166 | + null, |
| 167 | + }, |
| 168 | + .{ "a1.abort()", null }, |
| 169 | + .{ "s1.aborted", "true" }, |
| 170 | + .{ "target == s1", "true" }, |
| 171 | + .{ "s1.reason", "AbortError" }, |
| 172 | + .{ "called", "1" }, |
| 173 | + }, .{}); |
| 174 | + |
| 175 | + try runner.testCases(&.{ |
| 176 | + .{ "var s2 = AbortSignal.abort('over 9000')", null }, |
| 177 | + .{ "s2.aborted", "true" }, |
| 178 | + .{ "s2.reason", "over 9000" }, |
| 179 | + .{ "AbortSignal.abort().reason", "AbortError" }, |
| 180 | + }, .{}); |
| 181 | + |
| 182 | + try runner.testCases(&.{ |
| 183 | + .{ "var s3 = AbortSignal.timeout(10)", null }, |
| 184 | + .{ "s3.aborted", "true" }, |
| 185 | + .{ "s3.reason", "TimeoutError" }, |
| 186 | + .{ "try { s3.throwIfAborted() } catch (e) { e }", "Error: TimeoutError" }, |
| 187 | + }, .{}); |
| 188 | +} |
0 commit comments