Skip to content

Lightpanda: the headless browser designed for AI and automation

crash: prevent call_arena cleanup during callWithThisReport

Karl Seguindeecf9bb0776
diff --git a/src/browser/js/Function.zig b/src/browser/js/Function.zig
index c478a9bd5..3854c296b 100644
--- a/src/browser/js/Function.zig
+++ b/src/browser/js/Function.zig
@@ -139,6 +139,14 @@ pub fn callWithThisRethrow(self: *const Function, comptime T: type, this: anytyp
// An exception is reported on the global (e.g. window.reportError, which also
// dispatches the "error" event).
pub fn callWithThisReport(self: *const Function, this: anytype, args: anytype) !void {
+ // Reporting can callback into Zig (e.g. getting a DOMExceptions' message).
+ // If we're at call_depth == 0, that call will reset call_arena which our
+ // caller might be using.
+ const ctx = self.local.ctx;
+ const call_depth = ctx.call_depth;
+ ctx.call_depth = call_depth + 1;
+ defer ctx.call_depth = call_depth;
+
var try_catch: js.TryCatch = undefined;
try_catch.init(self.local);
defer try_catch.deinit();
@@ -148,7 +156,7 @@ pub fn callWithThisReport(self: *const Function, this: anytype, args: anytype) !
return err;
}
const exc = try_catch.exceptionValue() orelse return;
- try self.local.ctx.global.reportError(exc);
+ try ctx.global.reportError(exc);
};
}
diff --git a/src/browser/tests/intersection_observer/callback_throws.html b/src/browser/tests/intersection_observer/callback_throws.html
new file mode 100644
index 000000000..228f08ec1
--- /dev/null
+++ b/src/browser/tests/intersection_observer/callback_throws.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+<div id="target">Target Element</div>
+
+<script id=expectReportedErrors>
+ let reported = 0;
+ window.addEventListener('error', (e) => {
+ reported += 1;
+ e.preventDefault();
+ });
+ testing.expectEqual(0, reported);
+</script>
+
+<script id=callbackThrowsDOMException>
+ {
+ // Reporting reads the exception's message through a native accessor. The
+ // delivered entries must survive that call.
+ let calls = 0;
+ const io = new IntersectionObserver(() => {
+ calls += 1;
+ throw new DOMException('nope', 'SyntaxError');
+ });
+ io.observe(document.getElementById('target'));
+
+ testing.onload(() => {
+ testing.expectEqual(1, calls);
+ testing.expectEqual(1, reported);
+ io.disconnect();
+ });
+ }
+</script>