Skip to content

Commit 2873aa5

Browse files
committed
EventTarget constructor
1 parent d6ace3f commit 2873aa5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,14 +2131,14 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
21312131
const info = v8.FunctionCallbackInfo.initFromV8(raw_info);
21322132
var caller = Caller(JsContext, State).init(info);
21332133
defer caller.deinit();
2134-
21352134
// See comment above. We generateConstructor on all types
21362135
// in order to create the FunctionTemplate, but there might
21372136
// not be an actual "constructor" function. So if someone
21382137
// does `new ClassName()` where ClassName doesn't have
21392138
// a constructor function, we'll return an error.
21402139
if (@hasDecl(Struct, "constructor") == false) {
21412140
const iso = caller.isolate;
2141+
log.warn(.js, "Illegal constructor call", .{ .name = @typeName(Struct) });
21422142
const js_exception = iso.throwException(createException(iso, "Illegal Constructor"));
21432143
info.getReturnValue().set(js_exception);
21442144
return;

0 commit comments

Comments
 (0)