Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub const Interfaces = .{
HTMLHtmlElement,
HTMLIFrameElement,
HTMLImageElement,
HTMLImageElement.Factory,
HTMLInputElement,
HTMLLIElement,
HTMLLabelElement,
Expand Down Expand Up @@ -565,15 +566,6 @@ pub const HTMLImageElement = struct {
pub const Self = parser.Image;
pub const prototype = *HTMLElement;
pub const subtype = .node;
pub const js_name = "Image";

pub fn constructor(width: ?u32, height: ?u32, page: *const Page) !*parser.Image {
const element = try parser.documentCreateElement(parser.documentHTMLToDocument(page.window.document), "img");
const image: *parser.Image = @ptrCast(element);
if (width) |width_| try parser.imageSetWidth(image, width_);
if (height) |height_| try parser.imageSetHeight(image, height_);
return image;
}

pub fn get_alt(self: *parser.Image) ![]const u8 {
return try parser.imageGetAlt(self);
Expand Down Expand Up @@ -611,6 +603,21 @@ pub const HTMLImageElement = struct {
pub fn set_isMap(self: *parser.Image, is_map: bool) !void {
try parser.imageSetIsMap(self, is_map);
}

pub const Factory = struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling it Alias?

Suggested change
pub const Factory = struct {
pub const Alias = struct {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite an alias though, not per the spec and not how it's implemented. The most obvious practical difference is:

new Image() // valid;
new HTMLImageElement() // invalid...which I find ridiculous, but...

But, also, HTMLImageElement != Image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it's a strong typedef/alias, I also have seen it be called distinct variant, but I don't like that one.
Factory is somewhat implying it is creating an HTMLImageElement.
Up to you :) 🤷 I'd probably go with StrongAlias or Alias (and have the strong part be implicit). Probably StrongAlias as that would encode the knowledge of your reply.

pub const js_name = "Image";
pub const subtype = .node;
pub const js_legacy_factory = true;
pub const prototype = *HTMLImageElement;

pub fn constructor(width: ?u32, height: ?u32, page: *const Page) !*parser.Image {
const element = try parser.documentCreateElement(parser.documentHTMLToDocument(page.window.document), "img");
const image: *parser.Image = @ptrCast(element);
if (width) |width_| try parser.imageSetWidth(image, width_);
if (height) |height_| try parser.imageSetHeight(image, height_);
return image;
}
};
};

pub const HTMLInputElement = struct {
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2222,8 +2222,14 @@ const TypeMeta = struct {
subtype: ?SubType,
};

// When we map a Zig instance into a JsObject, we'll normally store the a
// TaggedAnyOpaque (TAO) inside of the JsObject's internal field. This requires
// ensuring that the instance template has an InternalFieldCount of 1. However,
// for empty objects, we don't need to store the TAO, because we can't just cast
// one empty object to another, so for those, as an optimization, we do not set
// the InternalFieldCount.
fn isEmpty(comptime T: type) bool {
return @typeInfo(T) != .@"opaque" and @sizeOf(T) == 0;
return @typeInfo(T) != .@"opaque" and @sizeOf(T) == 0 and @hasDecl(T, "js_legacy_factory") == false;
}

// Responsible for calling Zig functions from JS invokations. This could
Expand Down