Skip to content

Commit bbdb254

Browse files
authored
Merge pull request #746 from lightpanda-io/null_object_guard
Guard against null object when trying to fetch a function
2 parents 1ac23ce + 167fe5f commit bbdb254

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/browser/dom/event_target.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,10 @@ test "Browser.DOM.EventTarget" {
222222
.{ "content.dispatchEvent(new Event('he'));", null },
223223
.{ "obj1.calls", "1" },
224224
}, .{});
225+
226+
// doesn't crash on null receiver
227+
try runner.testCases(&.{
228+
.{ "content.addEventListener('he2', null);", null },
229+
.{ "content.dispatchEvent(new Event('he2'));", null },
230+
}, .{});
225231
}

src/runtime/js.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
14001400
}
14011401

14021402
pub fn getFunction(self: JsObject, name: []const u8) !?Function {
1403+
if (self.isNullOrUndefined()) {
1404+
return null;
1405+
}
14031406
const scope = self.scope;
1407+
14041408
const js_name = v8.String.initUtf8(scope.isolate, name);
14051409

14061410
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 {
14091413
}
14101414
return try scope.createFunction(js_value);
14111415
}
1416+
1417+
pub fn isNullOrUndefined(self: JsObject) bool {
1418+
return self.js_obj.toValue().isNullOrUndefined();
1419+
}
14121420
};
14131421

14141422
// This only exists so that we know whether a function wants the opaque

0 commit comments

Comments
 (0)