Files
browser/src/browser/webapi/event/WheelEvent.zig
Karl Seguin 01ecb296e5 Rework finalizers
This commit involves a number of changes to finalizers, all aimed towards
better consistency and reliability.

A big part of this has to do with v8::Inspector's ability to move objects
across IsolatedWorlds. There has been a few previous efforts on this, the most
significant being https://github.com/lightpanda-io/browser/pull/1901. To recap,
a Zig instance can map to 0-N v8::Objects. Where N is the total number of
IsolatedWorlds. Generally, IsolatedWorlds between origins are...isolated...but
the v8::Inspector isn't bound by this. So a Zig instance cannot be tied to a
Context/Identity/IsolatedWorld...it has to live until all references, possibly
from different IsolatedWorlds, are released (or the page is reset).

Finalizers could previously be managed via reference counting or explicitly
toggling the instance as weak/strong. Now, only reference counting is supported.
weak/strong can essentially be seen as an acquireRef (rc += 1) and
releaseRef (rc -= 1). Explicit setting did make some things easier, like not
having to worry so much about double-releasing (e.g. XHR abort being called
multiple times), but it was only used in a few places AND it simply doesn't work
with objects shared between IsolatedWorlds. It is never a boolean now, as 3
different IsolatedWorlds can each hold a reference.

Temps and Globals are tracked on the Session. Previously, they were tracked on
the Identity, but that makes no sense. If a Zig instance can outlive an Identity,
then any of its Temp references can too. This hasn't been a problem because we've
only seen MutationObserver and IntersectionObserver be used cross-origin,
but the right CDP script can make this crash with a use-after-free (e.g.
`MessageEvent.data` is released when the Identity is done, but `MessageEvent` is
still referenced by a different IsolateWorld).

Rather than deinit with a `comptime shutdown: bool`, there is now an explicit
`releaseRef` and `deinit`.

Bridge registration has been streamlined. Previously, types had to register
their finalizer AND acquireRef/releaseRef/deinit had to be declared on the entire
prototype chain, even if these methods just delegated to their proto. Finalizers
are now automatically enabled if a type has a `acquireRef` function. If a type
has an `acquireRef`, then it must have a `releaseRef` and a `deinit`. So if
there's custom cleanup to do in `deinit`, then you also have to define
`acquireRef` and `releaseRef` which will just delegate to the _proto.

Furthermore these finalizer methods can be defined anywhere on the chain.

Previously:

```zig
const KeywboardEvent = struct {
  _proto: *Event,
  ...

  pub fn deinit(self: *KeyboardEvent, session: *Session) void {
    self._proto.deinit(session);
  }

  pub fn releaseRef(self: *KeyboardEvent, session: *Session) void {
    self._proto.releaseRef(session);
  }
}
```

```zig
const KeyboardEvent = struct {
  _proto: *Event,
  ...
  // no deinit, releaseRef, acquireref
}
```

Since the `KeyboardEvent` doesn't participate in finalization directly, it
doesn't have to define anything. The bridge will detect the most specific place
they are defined and call them there.
2026-03-28 21:11:23 +08:00

133 lines
4.2 KiB
Zig

// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const String = @import("../../../string.zig").String;
const Page = @import("../../Page.zig");
const Session = @import("../../Session.zig");
const js = @import("../../js/js.zig");
const Event = @import("../Event.zig");
const MouseEvent = @import("MouseEvent.zig");
const WheelEvent = @This();
_proto: *MouseEvent,
_delta_x: f64,
_delta_y: f64,
_delta_z: f64,
_delta_mode: u32,
pub const DOM_DELTA_PIXEL: u32 = 0x00;
pub const DOM_DELTA_LINE: u32 = 0x01;
pub const DOM_DELTA_PAGE: u32 = 0x02;
pub const WheelEventOptions = struct {
deltaX: f64 = 0.0,
deltaY: f64 = 0.0,
deltaZ: f64 = 0.0,
deltaMode: u32 = 0,
};
pub const Options = Event.inheritOptions(
WheelEvent,
WheelEventOptions,
);
pub fn init(typ: []const u8, _opts: ?Options, page: *Page) !*WheelEvent {
const arena = try page.getArena(.{ .debug = "WheelEvent" });
errdefer page.releaseArena(arena);
const type_string = try String.init(arena, typ, .{});
const opts = _opts orelse Options{};
const event = try page._factory.mouseEvent(
arena,
type_string,
MouseEvent{
._type = .{ .wheel_event = undefined },
._proto = undefined,
._screen_x = opts.screenX,
._screen_y = opts.screenY,
._client_x = opts.clientX,
._client_y = opts.clientY,
._ctrl_key = opts.ctrlKey,
._shift_key = opts.shiftKey,
._alt_key = opts.altKey,
._meta_key = opts.metaKey,
._button = std.meta.intToEnum(MouseEvent.MouseButton, opts.button) catch return error.TypeError,
._buttons = opts.buttons,
._related_target = opts.relatedTarget,
},
WheelEvent{
._proto = undefined,
._delta_x = opts.deltaX,
._delta_y = opts.deltaY,
._delta_z = opts.deltaZ,
._delta_mode = opts.deltaMode,
},
);
Event.populatePrototypes(event, opts, false);
return event;
}
pub fn asEvent(self: *WheelEvent) *Event {
return self._proto.asEvent();
}
pub fn getDeltaX(self: *const WheelEvent) f64 {
return self._delta_x;
}
pub fn getDeltaY(self: *const WheelEvent) f64 {
return self._delta_y;
}
pub fn getDeltaZ(self: *const WheelEvent) f64 {
return self._delta_z;
}
pub fn getDeltaMode(self: *const WheelEvent) u32 {
return self._delta_mode;
}
pub const JsApi = struct {
pub const bridge = js.Bridge(WheelEvent);
pub const Meta = struct {
pub const name = "WheelEvent";
pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined;
};
pub const constructor = bridge.constructor(WheelEvent.init, .{});
pub const deltaX = bridge.accessor(WheelEvent.getDeltaX, null, .{});
pub const deltaY = bridge.accessor(WheelEvent.getDeltaY, null, .{});
pub const deltaZ = bridge.accessor(WheelEvent.getDeltaZ, null, .{});
pub const deltaMode = bridge.accessor(WheelEvent.getDeltaMode, null, .{});
pub const DOM_DELTA_PIXEL = bridge.property(WheelEvent.DOM_DELTA_PIXEL, .{ .template = true });
pub const DOM_DELTA_LINE = bridge.property(WheelEvent.DOM_DELTA_LINE, .{ .template = true });
pub const DOM_DELTA_PAGE = bridge.property(WheelEvent.DOM_DELTA_PAGE, .{ .template = true });
};
const testing = @import("../../../testing.zig");
test "WebApi: WheelEvent" {
try testing.htmlRunner("event/wheel.html", .{});
}