mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Guard against null object when trying to fetch a function
This commit is contained in:
@@ -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 },
|
||||
}, .{});
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user