From 16a30fa3b765099a368552023cced8446ac52994 Mon Sep 17 00:00:00 2001 From: sjorsdonkers <72333389+sjorsdonkers@users.noreply.github.com> Date: Fri, 18 Apr 2025 12:03:43 +0200 Subject: [PATCH] enum as subtype --- src/browser/dom/attribute.zig | 2 +- src/browser/dom/cdata_section.zig | 2 +- src/browser/dom/character_data.zig | 2 +- src/browser/dom/comment.zig | 2 +- src/browser/dom/document.zig | 2 +- src/browser/dom/document_fragment.zig | 2 +- src/browser/dom/document_type.zig | 2 +- src/browser/dom/element.zig | 2 +- src/browser/dom/node.zig | 2 +- src/browser/dom/processing_instruction.zig | 2 +- src/browser/dom/text.zig | 2 +- src/browser/html/document.zig | 2 +- src/browser/html/elements.zig | 136 ++++++++++----------- src/runtime/js.zig | 4 +- 14 files changed, 81 insertions(+), 83 deletions(-) diff --git a/src/browser/dom/attribute.zig b/src/browser/dom/attribute.zig index 19138677..a8ebc3c2 100644 --- a/src/browser/dom/attribute.zig +++ b/src/browser/dom/attribute.zig @@ -27,7 +27,7 @@ const DOMException = @import("exceptions.zig").DOMException; pub const Attr = struct { pub const Self = parser.Attribute; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_namespaceURI(self: *parser.Attribute) !?[]const u8 { return try parser.nodeGetNamespace(parser.attributeToNode(self)); diff --git a/src/browser/dom/cdata_section.zig b/src/browser/dom/cdata_section.zig index 4e91535d..1b3ac6df 100644 --- a/src/browser/dom/cdata_section.zig +++ b/src/browser/dom/cdata_section.zig @@ -26,5 +26,5 @@ const Text = @import("text.zig").Text; pub const CDATASection = struct { pub const Self = parser.CDATASection; pub const prototype = *Text; - pub const subtype = "node"; + pub const subtype = .node; }; diff --git a/src/browser/dom/character_data.zig b/src/browser/dom/character_data.zig index 621aa6aa..a3c35a4b 100644 --- a/src/browser/dom/character_data.zig +++ b/src/browser/dom/character_data.zig @@ -38,7 +38,7 @@ pub const Interfaces = .{ pub const CharacterData = struct { pub const Self = parser.CharacterData; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; // JS funcs // -------- diff --git a/src/browser/dom/comment.zig b/src/browser/dom/comment.zig index 2e0692b3..5a7bcce7 100644 --- a/src/browser/dom/comment.zig +++ b/src/browser/dom/comment.zig @@ -27,7 +27,7 @@ const SessionState = @import("../env.zig").SessionState; pub const Comment = struct { pub const Self = parser.Comment; pub const prototype = *CharacterData; - pub const subtype = "node"; + pub const subtype = .node; pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Comment { return parser.documentCreateComment( diff --git a/src/browser/dom/document.zig b/src/browser/dom/document.zig index 6f7c1b58..7d9c4ec0 100644 --- a/src/browser/dom/document.zig +++ b/src/browser/dom/document.zig @@ -39,7 +39,7 @@ const DOMImplementation = @import("implementation.zig").DOMImplementation; pub const Document = struct { pub const Self = parser.Document; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub fn constructor(state: *const SessionState) !*parser.DocumentHTML { const doc = try parser.documentCreateDocument( diff --git a/src/browser/dom/document_fragment.zig b/src/browser/dom/document_fragment.zig index 929960ba..0288dcb5 100644 --- a/src/browser/dom/document_fragment.zig +++ b/src/browser/dom/document_fragment.zig @@ -27,7 +27,7 @@ const Node = @import("node.zig").Node; pub const DocumentFragment = struct { pub const Self = parser.DocumentFragment; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub fn constructor(state: *const SessionState) !*parser.DocumentFragment { return parser.documentCreateDocumentFragment( diff --git a/src/browser/dom/document_type.zig b/src/browser/dom/document_type.zig index 3841094d..6cdc4a45 100644 --- a/src/browser/dom/document_type.zig +++ b/src/browser/dom/document_type.zig @@ -26,7 +26,7 @@ const Node = @import("node.zig").Node; pub const DocumentType = struct { pub const Self = parser.DocumentType; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_name(self: *parser.DocumentType) ![]const u8 { return try parser.documentTypeGetName(self); diff --git a/src/browser/dom/element.zig b/src/browser/dom/element.zig index fa545e05..58016167 100644 --- a/src/browser/dom/element.zig +++ b/src/browser/dom/element.zig @@ -37,7 +37,7 @@ const DOMException = @import("exceptions.zig").DOMException; pub const Element = struct { pub const Self = parser.Element; pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub const DOMRect = struct { x: f64, diff --git a/src/browser/dom/node.zig b/src/browser/dom/node.zig index 6db7c1d5..16455ff8 100644 --- a/src/browser/dom/node.zig +++ b/src/browser/dom/node.zig @@ -60,7 +60,7 @@ pub const Union = generate.Union(Interfaces); pub const Node = struct { pub const Self = parser.Node; pub const prototype = *EventTarget; - pub const subtype = "node"; + pub const subtype = .node; pub fn toInterface(node: *parser.Node) !Union { return switch (try parser.nodeType(node)) { diff --git a/src/browser/dom/processing_instruction.zig b/src/browser/dom/processing_instruction.zig index e2307085..baf426cb 100644 --- a/src/browser/dom/processing_instruction.zig +++ b/src/browser/dom/processing_instruction.zig @@ -28,7 +28,7 @@ pub const ProcessingInstruction = struct { // TODO for libdom processing instruction inherit from node. // But the spec says it must inherit from CDATA. pub const prototype = *Node; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_target(self: *parser.ProcessingInstruction) ![]const u8 { // libdom stores the ProcessingInstruction target in the node's name. diff --git a/src/browser/dom/text.zig b/src/browser/dom/text.zig index 274e0809..0ffbf9d9 100644 --- a/src/browser/dom/text.zig +++ b/src/browser/dom/text.zig @@ -32,7 +32,7 @@ pub const Interfaces = .{ pub const Text = struct { pub const Self = parser.Text; pub const prototype = *CharacterData; - pub const subtype = "node"; + pub const subtype = .node; pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Text { return parser.documentCreateTextNode( diff --git a/src/browser/html/document.zig b/src/browser/html/document.zig index d61b5c00..f708dc1c 100644 --- a/src/browser/html/document.zig +++ b/src/browser/html/document.zig @@ -35,7 +35,7 @@ const Cookie = @import("../storage/cookie.zig").Cookie; pub const HTMLDocument = struct { pub const Self = parser.DocumentHTML; pub const prototype = *Document; - pub const subtype = "node"; + pub const subtype = .node; // JS funcs // -------- diff --git a/src/browser/html/elements.zig b/src/browser/html/elements.zig index 2bdfef67..cab33617 100644 --- a/src/browser/html/elements.zig +++ b/src/browser/html/elements.zig @@ -107,7 +107,7 @@ const CSSProperties = struct {}; pub const HTMLElement = struct { pub const Self = parser.ElementHTML; pub const prototype = *Element; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_style(_: *parser.ElementHTML) CSSProperties { return .{}; @@ -143,7 +143,7 @@ pub const HTMLElement = struct { pub const HTMLMediaElement = struct { pub const Self = parser.MediaElement; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; // HTML elements @@ -152,14 +152,14 @@ pub const HTMLMediaElement = struct { pub const HTMLUnknownElement = struct { pub const Self = parser.Unknown; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; // https://html.spec.whatwg.org/#the-a-element pub const HTMLAnchorElement = struct { pub const Self = parser.Anchor; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_target(self: *parser.Anchor) ![]const u8 { return try parser.anchorGetTarget(self); @@ -405,284 +405,284 @@ pub const HTMLAnchorElement = struct { pub const HTMLAppletElement = struct { pub const Self = parser.Applet; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLAreaElement = struct { pub const Self = parser.Area; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLAudioElement = struct { pub const Self = parser.Audio; pub const prototype = *HTMLMediaElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLBRElement = struct { pub const Self = parser.BR; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLBaseElement = struct { pub const Self = parser.Base; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLBodyElement = struct { pub const Self = parser.Body; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLButtonElement = struct { pub const Self = parser.Button; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLCanvasElement = struct { pub const Self = parser.Canvas; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDListElement = struct { pub const Self = parser.DList; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDataElement = struct { pub const Self = parser.Data; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDataListElement = struct { pub const Self = parser.DataList; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDialogElement = struct { pub const Self = parser.Dialog; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDirectoryElement = struct { pub const Self = parser.Directory; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLDivElement = struct { pub const Self = parser.Div; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLEmbedElement = struct { pub const Self = parser.Embed; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLFieldSetElement = struct { pub const Self = parser.FieldSet; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLFontElement = struct { pub const Self = parser.Font; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLFormElement = struct { pub const Self = parser.Form; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLFrameElement = struct { pub const Self = parser.Frame; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLFrameSetElement = struct { pub const Self = parser.FrameSet; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLHRElement = struct { pub const Self = parser.HR; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLHeadElement = struct { pub const Self = parser.Head; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLHeadingElement = struct { pub const Self = parser.Heading; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLHtmlElement = struct { pub const Self = parser.Html; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLIFrameElement = struct { pub const Self = parser.IFrame; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLImageElement = struct { pub const Self = parser.Image; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLInputElement = struct { pub const Self = parser.Input; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLLIElement = struct { pub const Self = parser.LI; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLLabelElement = struct { pub const Self = parser.Label; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLLegendElement = struct { pub const Self = parser.Legend; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLLinkElement = struct { pub const Self = parser.Link; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLMapElement = struct { pub const Self = parser.Map; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLMetaElement = struct { pub const Self = parser.Meta; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLMeterElement = struct { pub const Self = parser.Meter; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLModElement = struct { pub const Self = parser.Mod; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLOListElement = struct { pub const Self = parser.OList; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLObjectElement = struct { pub const Self = parser.Object; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLOptGroupElement = struct { pub const Self = parser.OptGroup; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLOptionElement = struct { pub const Self = parser.Option; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLOutputElement = struct { pub const Self = parser.Output; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLParagraphElement = struct { pub const Self = parser.Paragraph; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLParamElement = struct { pub const Self = parser.Param; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLPictureElement = struct { pub const Self = parser.Picture; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLPreElement = struct { pub const Self = parser.Pre; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLProgressElement = struct { pub const Self = parser.Progress; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLQuoteElement = struct { pub const Self = parser.Quote; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; // https://html.spec.whatwg.org/#the-script-element pub const HTMLScriptElement = struct { pub const Self = parser.Script; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; pub fn get_src(self: *parser.Script) !?[]const u8 { return try parser.elementGetAttribute( @@ -797,103 +797,103 @@ pub const HTMLScriptElement = struct { pub const HTMLSelectElement = struct { pub const Self = parser.Select; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLSourceElement = struct { pub const Self = parser.Source; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLSpanElement = struct { pub const Self = parser.Span; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLStyleElement = struct { pub const Self = parser.Style; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableElement = struct { pub const Self = parser.Table; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableCaptionElement = struct { pub const Self = parser.TableCaption; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableCellElement = struct { pub const Self = parser.TableCell; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableColElement = struct { pub const Self = parser.TableCol; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableRowElement = struct { pub const Self = parser.TableRow; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTableSectionElement = struct { pub const Self = parser.TableSection; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTemplateElement = struct { pub const Self = parser.Template; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTextAreaElement = struct { pub const Self = parser.TextArea; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTimeElement = struct { pub const Self = parser.Time; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTitleElement = struct { pub const Self = parser.Title; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLTrackElement = struct { pub const Self = parser.Track; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLUListElement = struct { pub const Self = parser.UList; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub const HTMLVideoElement = struct { pub const Self = parser.Video; pub const prototype = *HTMLElement; - pub const subtype = "node"; + pub const subtype = .node; }; pub fn toInterface(comptime T: type, e: *parser.Element) !T { diff --git a/src/runtime/js.zig b/src/runtime/js.zig index 450d3d9c..4a267e75 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -96,9 +96,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type { @compileError(std.fmt.comptimePrint("Prototype '{s}' for type '{s} must be a pointer", .{ @typeName(Struct.prototype), @typeName(Struct) })); } - @setEvalBranchQuota(40_000); // The number of branches increases as we get more structs with the subtype field - const subtype: ?SubType = - if (@hasDecl(Struct, "subtype")) std.meta.stringToEnum(SubType, Struct.subtype) else null; + const subtype: ?SubType = if (@hasDecl(Struct, "subtype")) Struct.subtype else null; const R = Receiver(@field(types, s.name)); fields[i] = .{