Skip to content

Lightpanda: the headless browser designed for AI and automation

apply the guard directly in reportError

Karl Seguin96f39e85d3c7
diff --git a/src/browser/js/Function.zig b/src/browser/js/Function.zig
index 3854c296b..c478a9bd5 100644
--- a/src/browser/js/Function.zig
+++ b/src/browser/js/Function.zig
@@ -139,14 +139,6 @@ 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();
@@ -156,7 +148,7 @@ pub fn callWithThisReport(self: *const Function, this: anytype, args: anytype) !
return err;
}
const exc = try_catch.exceptionValue() orelse return;
- try ctx.global.reportError(exc);
+ try self.local.ctx.global.reportError(exc);
};
}
diff --git a/src/browser/tests/element/html/slot.html b/src/browser/tests/element/html/slot.html
index 68475639f..3c2ed2e44 100644
--- a/src/browser/tests/element/html/slot.html
+++ b/src/browser/tests/element/html/slot.html
@@ -699,3 +699,37 @@
testing.expectTrue(flat[0] === fallback);
}
</script>
+
+<script id="Slot#slotchange_listener_throws">
+{
+ // Reporting reads the exception's message through a native accessor. The
+ // slots still waiting for their slotchange must survive that call.
+ let reported = 0;
+ const onError = (e) => { reported += 1; e.preventDefault(); };
+ window.addEventListener('error', onError);
+
+ const host = document.createElement('div');
+ const shadow = host.attachShadow({ mode: 'open' });
+ const calls = [];
+ for (const name of ['a', 'b', 'c']) {
+ const slot = document.createElement('slot');
+ slot.name = name;
+ slot.addEventListener('slotchange', () => {
+ calls.push(name);
+ throw new DOMException('nope', 'SyntaxError');
+ });
+ shadow.appendChild(slot);
+ }
+ for (const name of ['a', 'b', 'c']) {
+ const span = document.createElement('span');
+ span.slot = name;
+ host.appendChild(span);
+ }
+
+ testing.onload(() => {
+ window.removeEventListener('error', onError);
+ testing.expectEqual('a,b,c', calls.join(','));
+ testing.expectEqual(3, reported);
+ });
+}
+</script>
diff --git a/src/browser/tests/event/abort_controller.html b/src/browser/tests/event/abort_controller.html
index 0fb4187df..b3fb7eec7 100644
--- a/src/browser/tests/event/abort_controller.html
+++ b/src/browser/tests/event/abort_controller.html
@@ -343,3 +343,31 @@
testing.expectEqual(false, AbortSignal.timeout(2 ** 53 - 1).aborted);
testing.expectEqual(false, AbortSignal.timeout(-0.5).aborted);
</script>
+
+<script id=abortsignal_timeout_listener_throws type=module>
+ {
+ // Reporting reads the exception's message through a native accessor. The
+ // dependents still waiting for their abort event must survive that call.
+ let reported = 0;
+ const onError = (e) => { reported += 1; e.preventDefault(); };
+ window.addEventListener('error', onError);
+
+ const state = await testing.async();
+ const timeout = AbortSignal.timeout(1);
+ let dependents = 0;
+ for (let i = 0; i < 3; i++) {
+ AbortSignal.any([timeout]).addEventListener('abort', () => { dependents += 1; });
+ }
+ timeout.addEventListener('abort', () => {
+ throw new DOMException('nope', 'SyntaxError');
+ });
+ window.setTimeout(() => state.resolve(), 5);
+
+ await state.done(() => {
+ window.removeEventListener('error', onError);
+ testing.expectEqual(true, timeout.aborted);
+ testing.expectEqual(3, dependents);
+ testing.expectEqual(1, reported);
+ });
+ }
+</script>
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index 5a5b3e633..e369f92db 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -592,6 +592,13 @@ pub fn reportError(self: *Window, err: js.Value, frame: *Frame) !void {
return;
}
+ // Reporting calls back into Zig (e.g. a DOMException's message accessor).
+ // At call_depth 0 that call resets the call_arena, which the code reporting
+ // the error may still be using (e.g. a dispatch's snapshot of its targets).
+ const call_depth = frame.js.call_depth;
+ frame.js.call_depth = call_depth + 1;
+ defer frame.js.call_depth = call_depth;
+
frame.page.recordJsError(error.JsException);
const target = self.asEventTarget();
diff --git a/src/browser/webapi/WorkerGlobalScope.zig b/src/browser/webapi/WorkerGlobalScope.zig
index 4abbb39be..675476646 100644
--- a/src/browser/webapi/WorkerGlobalScope.zig
+++ b/src/browser/webapi/WorkerGlobalScope.zig
@@ -473,6 +473,11 @@ pub fn reportError(self: *WorkerGlobalScope, err: JS.Value) !void {
return;
}
+ // See Window.reportError: keeps the call_arena alive while reporting.
+ const call_depth = self.js.call_depth;
+ self.js.call_depth = call_depth + 1;
+ defer self.js.call_depth = call_depth;
+
self.page.recordJsError(error.JsException);
self._reporting_error = true;