EventTarget constructor

This commit is contained in:
sjorsdonkers
2025-07-08 13:21:52 +02:00
committed by Sjors
parent d6ace3f695
commit 2873aa5f81
2 changed files with 13 additions and 1 deletions

View File

@@ -35,6 +35,9 @@ pub const EventTarget = struct {
pub const Self = parser.EventTarget; pub const Self = parser.EventTarget;
pub const Exception = DOMException; pub const Exception = DOMException;
// Extend libdom event target for pure zig struct.
base: parser.EventTargetTBase = parser.EventTargetTBase{},
pub fn toInterface(e: *parser.Event, et: *parser.EventTarget, page: *Page) !Union { pub fn toInterface(e: *parser.Event, et: *parser.EventTarget, page: *Page) !Union {
// libdom assumes that all event targets are libdom nodes. They are not. // libdom assumes that all event targets are libdom nodes. They are not.
@@ -63,6 +66,11 @@ pub const EventTarget = struct {
// JS funcs // JS funcs
// -------- // --------
pub fn constructor(page: *Page) !*parser.EventTarget {
const et = try page.arena.create(EventTarget);
return @ptrCast(&et.base);
}
pub fn _addEventListener( pub fn _addEventListener(
self: *parser.EventTarget, self: *parser.EventTarget,
typ: []const u8, typ: []const u8,
@@ -128,6 +136,10 @@ test "Browser.DOM.EventTarget" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{}); var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit(); defer runner.deinit();
try runner.testCases(&.{
.{ "new EventTarget()", "[object EventTarget]" },
}, .{});
try runner.testCases(&.{ try runner.testCases(&.{
.{ "let content = document.getElementById('content')", "undefined" }, .{ "let content = document.getElementById('content')", "undefined" },
.{ "let para = document.getElementById('para')", "undefined" }, .{ "let para = document.getElementById('para')", "undefined" },

View File

@@ -2131,7 +2131,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
const info = v8.FunctionCallbackInfo.initFromV8(raw_info); const info = v8.FunctionCallbackInfo.initFromV8(raw_info);
var caller = Caller(JsContext, State).init(info); var caller = Caller(JsContext, State).init(info);
defer caller.deinit(); defer caller.deinit();
// See comment above. We generateConstructor on all types // See comment above. We generateConstructor on all types
// in order to create the FunctionTemplate, but there might // in order to create the FunctionTemplate, but there might
// not be an actual "constructor" function. So if someone // not be an actual "constructor" function. So if someone
@@ -2139,6 +2138,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
// a constructor function, we'll return an error. // a constructor function, we'll return an error.
if (@hasDecl(Struct, "constructor") == false) { if (@hasDecl(Struct, "constructor") == false) {
const iso = caller.isolate; const iso = caller.isolate;
log.warn(.js, "Illegal constructor call", .{ .name = @typeName(Struct) });
const js_exception = iso.throwException(createException(iso, "Illegal Constructor")); const js_exception = iso.throwException(createException(iso, "Illegal Constructor"));
info.getReturnValue().set(js_exception); info.getReturnValue().set(js_exception);
return; return;