From 167fe5f758b135bcf8750779e6d3c389199ac5f1 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 2 Jun 2025 23:27:29 +0800 Subject: [PATCH] Guard against null object when trying to fetch a function --- src/browser/dom/event_target.zig | 6 ++++++ src/runtime/js.zig | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/browser/dom/event_target.zig b/src/browser/dom/event_target.zig index acb6e934..34836fba 100644 --- a/src/browser/dom/event_target.zig +++ b/src/browser/dom/event_target.zig @@ -222,4 +222,10 @@ test "Browser.DOM.EventTarget" { .{ "content.dispatchEvent(new Event('he'));", null }, .{ "obj1.calls", "1" }, }, .{}); + + // doesn't crash on null receiver + try runner.testCases(&.{ + .{ "content.addEventListener('he2', null);", null }, + .{ "content.dispatchEvent(new Event('he2'));", null }, + }, .{}); } diff --git a/src/runtime/js.zig b/src/runtime/js.zig index a9ee587c..89fb4aff 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -1400,7 +1400,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { } pub fn getFunction(self: JsObject, name: []const u8) !?Function { + if (self.isNullOrUndefined()) { + return null; + } const scope = self.scope; + const js_name = v8.String.initUtf8(scope.isolate, name); const js_value = try self.js_obj.getValue(scope.context, js_name.toName()); @@ -1409,6 +1413,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { } return try scope.createFunction(js_value); } + + pub fn isNullOrUndefined(self: JsObject) bool { + return self.js_obj.toValue().isNullOrUndefined(); + } }; // This only exists so that we know whether a function wants the opaque