Lightpanda: the headless browser designed for AI and automation
remove _proto from Node and CData
Karl Seguinc78c8b7e22fe
diff --git a/src/browser/Factory.zig b/src/browser/Factory.zigindex f48477d38..12a79ad62 100644--- a/src/browser/Factory.zig+++ b/src/browser/Factory.zig@@ -189,14 +189,22 @@ fn PrototypeChain(comptime types: []const type) type {assert(index < types.len);const ptr = self.get(index);- ptr.* = .{ ._proto = self.get(index - 1), ._type = unionInit(T, self.get(index + 1)) };+ ptr.* = if (comptime @hasField(types[index], "_proto"))+ .{ ._proto = undefined, ._type = unionInit(T, self.get(index + 1)) }+ else+ .{ ._type = unionInit(T, self.get(index + 1)) };+ setProto(ptr, self.get(index - 1));}fn setMiddleWithValue(self: *const Self, comptime index: usize, comptime T: type, value: anytype) void {assert(index >= 1);const ptr = self.get(index);- ptr.* = .{ ._proto = self.get(index - 1), ._type = unionInit(T, value) };+ ptr.* = if (comptime @hasField(types[index], "_proto"))+ .{ ._proto = undefined, ._type = unionInit(T, value) }+ else+ .{ ._type = unionInit(T, value) };+ setProto(ptr, self.get(index - 1));}fn setLeaf(self: *const Self, comptime index: usize, value: anytype) void {@@ -204,7 +212,7 @@ fn PrototypeChain(comptime types: []const type) type {const ptr = self.get(index);ptr.* = value;- ptr._proto = self.get(index - 1);+ setProto(ptr, self.get(index - 1));}};}@@ -325,7 +333,7 @@ pub fn cdataNode(self: *Factory, cd: Node.CData, leaf: anytype) !*Node.CData {const cd_ptr = chain.get(2);cd_ptr.* = cd;- cd_ptr._proto = chain.get(1);+ setProto(cd_ptr, chain.get(1));// only the CDATASection chain has a middle here (its Text)inline for (3..types.len - 1) |i| {@@ -363,13 +371,37 @@ pub fn chainOffsetOf(comptime Leaf: type, comptime T: type) usize {}}-// Byte delta from T down to its Proto within any chain allocation containing-// them (the prefix layout is the same for every leaf).+// Offset from T to its Protopub fn protoOffset(comptime T: type) usize {const P = reflect.Proto(T).?;return chainOffsetOf(T, T) - chainOffsetOf(T, P);}+// Get the Proto of a value.+pub fn protoOf(value: anytype) *reflect.Proto(reflect.Struct(@TypeOf(value))).? {+ const T = reflect.Struct(@TypeOf(value));+ const proto: *reflect.Proto(T).? = @ptrFromInt(@intFromPtr(value) - comptime protoOffset(T));+ if (comptime IS_DEBUG) {+ // In debug, we'll assert that our offset and the _proto field agree.+ std.debug.assert(proto == if (comptime hasStoredProto(T)) value._proto else value._proto_canary);+ }+ return proto;+}++// Assigns a chain member's stored `_proto`+fn setProto(ptr: anytype, proto: anytype) void {+ const T = reflect.Struct(@TypeOf(ptr));+ if (comptime hasStoredProto(T)) {+ ptr._proto = proto;+ } else if (comptime IS_DEBUG and @hasField(T, "_proto_canary")) {+ ptr._proto_canary = proto;+ }+}++fn hasStoredProto(comptime T: type) bool {+ return @hasField(T, "_proto") and @FieldType(T, "_proto") != void;+}+// Generic contiguous chain from explicit values, for chains whose middles// can't be default-initialized by AutoPrototypeChain. Callers pass every// level's value in root-to-leaf order, with `undefined` for `_proto` and for@@ -399,7 +431,7 @@ pub fn chainedWithAllocator(allocator: Allocator, values: anytype) !*ChainedLeafconst ptr = chain.get(i);ptr.* = values[i];if (i > 0) {- ptr._proto = chain.get(i - 1);+ setProto(ptr, chain.get(i - 1));}}return chain.get(types.len - 1);@@ -532,7 +564,7 @@ fn destroyChain(const new_align = std.mem.Alignment.max(old_align, std.mem.Alignment.of(S));if (comptime reflect.Proto(S) != null) {- self.destroyChain(value._proto, new_size, new_align);+ self.destroyChain(protoOf(value), new_size, new_align);} else {// no proto so this is the head of the chain.// we use this as the ptr to the start of the chain.diff --git a/src/browser/frame/node_factory.zig b/src/browser/frame/node_factory.zigindex 361812b45..6fe292cb7 100644--- a/src/browser/frame/node_factory.zig+++ b/src/browser/frame/node_factory.zig@@ -1052,7 +1052,6 @@ pub fn constructCustomElement(frame: *Frame, new_target: JS.Function) !*Elementpub fn createTextNode(frame: *Frame, text: []const u8) !*Node {const cd = try frame._factory.cdataNode(.{- ._proto = undefined,._type = .text,._data = try frame.dupeSSO(text),}, CData.Text{ ._proto = undefined });@@ -1061,7 +1060,6 @@ pub fn createTextNode(frame: *Frame, text: []const u8) !*Node {pub fn createComment(frame: *Frame, text: []const u8) !*Node {const cd = try frame._factory.cdataNode(.{- ._proto = undefined,._type = .comment,._data = try frame.dupeSSO(text),}, CData.Comment{ ._proto = undefined });@@ -1075,7 +1073,6 @@ pub fn createCDATASection(frame: *Frame, data: []const u8) !*Node {}const cd = try frame._factory.cdataNode(.{- ._proto = undefined,._type = .cdata_section,._data = try frame.dupeSSO(data),}, CData.CDATASection{ ._proto = undefined });@@ -1097,7 +1094,6 @@ pub fn createProcessingInstruction(frame: *Frame, target: []const u8, data: []coconst owned_target = try frame.dupeString(target);const cd = try frame._factory.cdataNode(.{- ._proto = undefined,._type = .processing_instruction,._data = try frame.dupeSSO(data),}, CData.ProcessingInstruction{diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex b59ea4368..49c34b491 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -22,6 +22,7 @@ const string = @import("../../string.zig");const Page = @import("../Page.zig");const Frame = @import("../Frame.zig");+const Factory = @import("../Factory.zig");const reflect = @import("../reflect.zig");const js = @import("js.zig");@@ -555,7 +556,7 @@ fn protoNode(comptime T: type, instance: *T) *@import("../webapi/Node.zig") {if (T == @import("../webapi/Node.zig")) {return instance;}- return protoNode(reflect.Proto(T).?, instance._proto);+ return protoNode(reflect.Proto(T).?, Factory.protoOf(instance));}fn handleError(comptime T: type, comptime F: type, local: *const Local, err: anyerror, info: anytype) void {diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zigindex cc1343704..e8d7bcab7 100644--- a/src/browser/js/Local.zig+++ b/src/browser/js/Local.zig@@ -1390,13 +1390,12 @@ fn resolveT(comptime T: type, value: *T) Resolved {};}-// Every wrapped chain must be contiguous — the TaggedOpaque walk upcasts by-// pointer arithmetic. The stored _proto fields double as the canary.+// Debug-only check!+// Walk the full chain at wrap time; protoOf's Debug assert verifies each+// hop's _proto (or _proto_canary) against the layout arithmetic.fn assertChainContiguity(comptime T: type, value: *T) void {if (comptime reflect.Proto(T) != null) {- const P = comptime reflect.Proto(T).?;- std.debug.assert(@intFromPtr(value._proto) == @intFromPtr(value) - comptime Factory.protoOffset(T));- assertChainContiguity(P, value._proto);+ assertChainContiguity(reflect.Proto(T).?, Factory.protoOf(value));}}@@ -1424,7 +1423,7 @@ fn finalizerPtrGetter(comptime T: type, comptime FT: type) *const fn (*T) *FT {const childGetter = comptime finalizerPtrGetter(P, FT);return struct {fn get(v: *T) *FT {- return childGetter(v._proto);+ return childGetter(Factory.protoOf(v));}}.get;}diff --git a/src/browser/reflect.zig b/src/browser/reflect.zigindex c4bf6f062..bfa190817 100644--- a/src/browser/reflect.zig+++ b/src/browser/reflect.zig@@ -17,14 +17,18 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.// The prototype ("parent") type of T, as declared by its `pub const Proto`.-// This decl is the single source of truth for prototype-chain discovery;-// a `_proto` field, where one exists, is only storage and must agree.+// This decl is the single source of truth for prototype-chain discovery.pub fn Proto(comptime T: type) ?type {if (!@hasDecl(T, "Proto")) {return null;}- if (@hasField(T, "_proto") and @FieldType(T, "_proto") != *T.Proto) {- @compileError(@typeName(T) ++ ": _proto field and Proto decl disagree");++ if (@hasField(T, "_proto")) {+ // If the field also has a _proto: *Parent field, it must be the same type+ const F = @FieldType(T, "_proto");+ if (F != *T.Proto and F != void) {+ @compileError(@typeName(T) ++ ": _proto field and Proto decl disagree");+ }}return T.Proto;}diff --git a/src/browser/webapi/CData.zig b/src/browser/webapi/CData.zigindex 9ce8879ed..7a8f3bc20 100644--- a/src/browser/webapi/CData.zig+++ b/src/browser/webapi/CData.zig@@ -37,8 +37,11 @@ const CData = @This();pub const Proto = Node;_type: Type,-_proto: *Node,_data: String = .empty,+// In debug, set so that we can check that we have a proper contiguous block+// of memory for the entire chain (and thus, simple pointer arithmetics will+// work to resolve the proto).+_proto_canary: if (IS_DEBUG) *Node else void = undefined,// The subtype's struct is not a payload here: it's the member laid out after// the CData in the factory chain (see Factory.cdataNode), reachable via@@ -227,7 +230,7 @@ fn utf16RangeToUtf8(data: []const u8, utf16_start: usize, utf16_end: usize) !str}pub fn asNode(self: *CData) *Node {- return self._proto;+ return Factory.protoOf(self);}pub fn is(self: *CData, comptime T: type) ?*T {@@ -551,7 +554,6 @@ test "WebApi: CData.render" {const cdata = CData{._type = .text,- ._proto = undefined,._data = .wrap(test_case.value),};diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zigindex d310c69af..3c905188d 100644--- a/src/browser/webapi/Node.zig+++ b/src/browser/webapi/Node.zig@@ -22,6 +22,7 @@ const lp = @import("lightpanda");const js = @import("../js/js.zig");const Frame = @import("../Frame.zig");const URL = @import("../URL.zig");+const Factory = @import("../Factory.zig");const reflect = @import("../reflect.zig");const EventTarget = @import("EventTarget.zig");@@ -37,6 +38,7 @@ pub const ShadowRoot = @import("ShadowRoot.zig");const String = lp.String;const Allocator = std.mem.Allocator;+const IS_DEBUG = @import("builtin").mode == .Debug;pub const AssignedSlotLookup = std.AutoHashMapUnmanaged(*Node, *Element.Html.Slot);@@ -45,13 +47,16 @@ const Node = @This();pub const Proto = EventTarget;_type: Type,-_proto: *EventTarget,_parent: ?*Node = null,// The first child's `_prev` points at the LAST child (keeping lastChild// O(1)); the last child's `_next` is null. A detached node has null links._first_child: ?*Node = null,_next: ?*Node = null,_prev: ?*Node = null,+// In debug, set so that we can check that we have a proper contiguous block+// of memory for the entire chain (and thus, simple pointer arithmetics will+// work to resolve the proto).+_proto_canary: if (IS_DEBUG) *EventTarget else void = undefined,// Lookup for nodes that have a different owner document than frame.documentpub const OwnerDocumentLookup = std.AutoHashMapUnmanaged(*Node, *Document);@@ -66,7 +71,7 @@ pub const Type = union(enum) {};pub fn asEventTarget(self: *Node) *EventTarget {- return self._proto;+ return Factory.protoOf(self);}// Returns the node as a more specific type. Will crash if node is not a `T`.diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zigindex 1522cd8d5..a4154cdd1 100644--- a/src/browser/webapi/element/Html.zig+++ b/src/browser/webapi/element/Html.zig@@ -224,7 +224,7 @@ pub fn asNode(self: *HtmlElement) *Node {}pub fn asEventTarget(self: *HtmlElement) *@import("../EventTarget.zig") {- return self._proto._proto._proto;+ return self.asNode().asEventTarget();}pub fn getInnerText(self: *HtmlElement, writer: *std.Io.Writer, frame: *Frame) !void {