Skip to content

Commit 6b41677

Browse files
committed
EventTarget constructor
1 parent 7b46fe9 commit 6b41677

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/browser/dom/event_target.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub const EventTarget = struct {
3535
pub const Self = parser.EventTarget;
3636
pub const Exception = DOMException;
3737

38+
// Extend libdom event target for pure zig struct.
39+
base: parser.EventTargetTBase = parser.EventTargetTBase{},
40+
3841
pub fn toInterface(e: *parser.Event, et: *parser.EventTarget, page: *Page) !Union {
3942
// libdom assumes that all event targets are libdom nodes. They are not.
4043

@@ -63,6 +66,11 @@ pub const EventTarget = struct {
6366

6467
// JS funcs
6568
// --------
69+
pub fn constructor(page: *Page) !*parser.EventTarget {
70+
const et = try page.arena.create(EventTarget);
71+
return @ptrCast(&et.base);
72+
}
73+
6674
pub fn _addEventListener(
6775
self: *parser.EventTarget,
6876
typ: []const u8,
@@ -128,6 +136,10 @@ test "Browser.DOM.EventTarget" {
128136
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
129137
defer runner.deinit();
130138

139+
try runner.testCases(&.{
140+
.{ "new EventTarget()", "[object EventTarget]" },
141+
}, .{});
142+
131143
try runner.testCases(&.{
132144
.{ "let content = document.getElementById('content')", "undefined" },
133145
.{ "let para = document.getElementById('para')", "undefined" },

src/runtime/js.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,14 +2002,14 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
20022002
const info = v8.FunctionCallbackInfo.initFromV8(raw_info);
20032003
var caller = Caller(Self, State).init(info);
20042004
defer caller.deinit();
2005-
20062005
// See comment above. We generateConstructor on all types
20072006
// in order to create the FunctionTemplate, but there might
20082007
// not be an actual "constructor" function. So if someone
20092008
// does `new ClassName()` where ClassName doesn't have
20102009
// a constructor function, we'll return an error.
20112010
if (@hasDecl(Struct, "constructor") == false) {
20122011
const iso = caller.isolate;
2012+
log.warn(.js, "Illegal constructor call", .{ .name = @typeName(Struct) });
20132013
const js_exception = iso.throwException(createException(iso, "Illegal Constructor"));
20142014
info.getReturnValue().set(js_exception);
20152015
return;
@@ -3420,7 +3420,7 @@ fn valueToDetailString(arena: Allocator, value: v8.Value, isolate: v8.Isolate, v
34203420
if (debugValueToString(arena, value.castTo(v8.Object), isolate, v8_context)) |ds| {
34213421
return ds;
34223422
} else |err| {
3423-
log.err(.js, "debug serialize value", .{.err = err});
3423+
log.err(.js, "debug serialize value", .{ .err = err });
34243424
}
34253425
}
34263426
}

0 commit comments

Comments
 (0)