Skip to content

Commit

Permalink
events: add EventTargetBase
Browse files Browse the repository at this point in the history
EventTargetBase implements event target vtable for pure zig struct
  • Loading branch information
krichprollsch committed Jan 31, 2024
1 parent 85a8dec commit 160a8e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/html/window.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const std = @import("std");

const parser = @import("../netsurf.zig");
const c = @cImport({
@cInclude("events/event_target.h");
});

const EventTarget = @import("../dom/event_target.zig").EventTarget;

Expand All @@ -10,6 +13,9 @@ pub const Window = struct {
pub const prototype = *EventTarget;
pub const mem_guarantied = true;

// Extend libdom event target for pure zig struct.
base: parser.EventTargetTBase = parser.EventTargetTBase{},

document: ?*parser.Document = null,
target: []const u8,

Expand Down Expand Up @@ -42,6 +48,4 @@ pub const Window = struct {
pub fn get_name(self: *Window) []const u8 {
return self.target;
}

// TODO we need to re-implement EventTarget interface.
};
30 changes: 29 additions & 1 deletion src/netsurf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const std = @import("std");
const c = @cImport({
@cInclude("dom/dom.h");
@cInclude("dom/bindings/hubbub/parser.h");
@cInclude("events/event_target.h");
});

const Callback = @import("jsruntime").Callback;
Expand Down Expand Up @@ -622,6 +623,33 @@ pub fn eventTargetDispatchEvent(et: *EventTarget, event: *Event) !bool {
return res;
}

// EventTargetBase is used to impement EventTarget for pure zig struct.
pub const EventTargetTBase = struct {
const Self = @This();

vtable: ?*const c.struct_dom_event_target_vtable = &c.struct_dom_event_target_vtable{
.dispatch_event = dispatch_event,
.remove_event_listener = remove_event_listener,
.add_event_listener = add_event_listener,
},
eti: c.dom_event_target_internal = c.dom_event_target_internal{ .listeners = null },

pub fn add_event_listener(et: [*c]c.dom_event_target, t: [*c]c.dom_string, l: ?*c.struct_dom_event_listener, capture: bool) callconv(.C) c.dom_exception {
const self = @as(*Self, @ptrCast(et));
return c._dom_event_target_add_event_listener(&self.eti, t, l, capture);
}

pub fn dispatch_event(et: [*c]c.dom_event_target, evt: ?*c.struct_dom_event, res: [*c]bool) callconv(.C) c.dom_exception {
const self = @as(*Self, @ptrCast(et));
return c._dom_event_target_dispatch(et, &self.eti, evt, c.DOM_BUBBLING_PHASE, res);
}

pub fn remove_event_listener(et: [*c]c.dom_event_target, t: [*c]c.dom_string, l: ?*c.struct_dom_event_listener, capture: bool) callconv(.C) c.dom_exception {
const self = @as(*Self, @ptrCast(et));
return c._dom_event_target_add_event_listener(&self.eti, t, l, capture);
}
};

// NodeType

pub const NodeType = enum(u4) {
Expand Down Expand Up @@ -1804,4 +1832,4 @@ pub inline fn documentHTMLGetTitle(doc: *DocumentHTML) ![]const u8 {
pub inline fn documentHTMLSetTitle(doc: *DocumentHTML, v: []const u8) !void {
const err = documentHTMLVtable(doc).set_title.?(doc, try strFromData(v));
try DOMErr(err);
}
}
11 changes: 11 additions & 0 deletions src/run_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const generate = @import("generate.zig");

const parser = @import("netsurf.zig");
const apiweb = @import("apiweb.zig");
const Window = @import("html/window.zig").Window;

const documentTestExecFn = @import("dom/document.zig").testExecFn;
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
Expand Down Expand Up @@ -135,3 +136,13 @@ test "run browser tests" {

try dump.HTMLFileTestFn(out);
}

test "Window is a libdom event target" {
var window = Window.create(null);

const event = try parser.eventCreate();
try parser.eventInit(event, "foo", .{});

const et = @as(*parser.EventTarget, @ptrCast(&window));
_ = try parser.eventTargetDispatchEvent(et, event);
}

0 comments on commit 160a8e4

Please sign in to comment.