diff --git a/src/netsurf.zig b/src/netsurf.zig index 0071b089..7b995189 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -39,9 +39,9 @@ const c = @cImport({ // - node: the node interface instance inline fn getVtable(comptime VtableT: type, comptime NodeT: type, node: anytype) VtableT { // first align correctly the node interface - const node_aligned: *align(@alignOf([*c]c.dom_node)) NodeT = @alignCast(node); + const node_aligned: *align(@alignOf(NodeExternal)) NodeT = @alignCast(node); // then convert the node interface to a base node - const node_base = @as([*c]c.dom_node, @ptrCast(node_aligned)); + const node_base = @as(NodeExternal, @ptrCast(node_aligned)); // retrieve the vtable on the base node const vtable = node_base.*.vtable.?; @@ -347,24 +347,22 @@ pub fn namedNodeMapGetLength(nnm: *NamedNodeMap) !u32 { } pub fn namedNodeMapItem(nnm: *NamedNodeMap, index: u32) !?*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_item(nnm, index, &n); try DOMErr(err); if (n == null) return null; - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } pub fn namedNodeMapGetNamedItem(nnm: *NamedNodeMap, qname: []const u8) !?*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_get_named_item(nnm, try strFromData(qname), &n); try DOMErr(err); if (n == null) return null; - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } @@ -373,7 +371,7 @@ pub fn namedNodeMapGetNamedItemNS( namespace: []const u8, localname: []const u8, ) !?*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_get_named_item_ns( nnm, try strFromData(namespace), @@ -384,52 +382,42 @@ pub fn namedNodeMapGetNamedItemNS( if (n == null) return null; - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } -// Convert a parser pointer to a public dom_node pointer. -fn toDOMNode(comptime T: type, v: *T) [*c]c.dom_node { - const v_aligned: *align(@alignOf([*c]c.dom_node)) T = @alignCast(v); - return @ptrCast(v_aligned); -} - pub fn namedNodeMapSetNamedItem(nnm: *NamedNodeMap, attr: *Attribute) !?*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_set_named_item( nnm, - toDOMNode(Attribute, attr), + toNodeExternal(Attribute, attr), &n, ); try DOMErr(err); if (n == null) return null; - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } pub fn namedNodeMapSetNamedItemNS(nnm: *NamedNodeMap, attr: *Attribute) !?*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_set_named_item_ns( nnm, - toDOMNode(Attribute, attr), + toNodeExternal(Attribute, attr), &n, ); try DOMErr(err); if (n == null) return null; - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } pub fn namedNodeMapRemoveNamedItem(nnm: *NamedNodeMap, qname: []const u8) !*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_remove_named_item(nnm, try strFromData(qname), &n); try DOMErr(err); - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } @@ -438,7 +426,7 @@ pub fn namedNodeMapRemoveNamedItemNS( namespace: []const u8, localname: []const u8, ) !*Attribute { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_namednodemap_remove_named_item_ns( nnm, try strFromData(namespace), @@ -447,7 +435,6 @@ pub fn namedNodeMapRemoveNamedItemNS( ); try DOMErr(err); - // cast [*c]c.dom_node into *Attribute return @as(*Attribute, @ptrCast(n)); } @@ -479,16 +466,26 @@ pub fn nodeListLength(nodeList: *NodeList) !u32 { } pub fn nodeListItem(nodeList: *NodeList, index: u32) !?*Node { - var n: [*c]c.dom_node = undefined; + var n: NodeExternal = undefined; const err = c._dom_nodelist_item(nodeList, index, &n); try DOMErr(err); if (n == null) return null; - // cast [*c]c.dom_node into *Node return @as(*Node, @ptrCast(n)); } +// NodeExternal is the libdom public representation of a Node. +// Since we use the internal representation (dom_node_internal), we declare +// here a private version useful for some netsurf function call. +const NodeExternal = [*c]c.dom_node; + +// Convert a parser pointer to a NodeExternal pointer. +fn toNodeExternal(comptime T: type, v: *T) NodeExternal { + const v_aligned: *align(@alignOf(NodeExternal)) T = @alignCast(v); + return @ptrCast(v_aligned); +} + // Node pub const Node = c.dom_node_internal;