Lightpanda: the headless browser designed for AI and automation
js: a promise-returning operation rejects with the page's exception
orezraeye9b49cec8f56
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex 4e0527996..43168b67a 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -892,16 +892,57 @@ pub const Function = struct {}};- const js_value = _call(T, &caller.local, info, func, opts) catch |err| {- handleError(T, @TypeOf(func), &caller.local, err, info);- return;- };+ const js_value = if (comptime returnsPromise(@TypeOf(func)))+ callRejecting(T, &caller.local, info, func, opts) orelse return+ else+ _call(T, &caller.local, info, func, opts) catch |err| {+ handleError(T, @TypeOf(func), &caller.local, err, info);+ return;+ };if (comptime opts.cache) |cache| {cache_state.save(cache, js_value);}}+ /// `_call` for an operation that returns a promise, which WebIDL says must+ /// never throw: an exception the page's own code throws while it runs (a+ /// getter or toString during argument conversion, say) rejects the promise+ /// it returns instead. A terminating execution still propagates, and so+ /// does an exception left behind by a call that otherwise succeeded, as+ /// before. The returned value on success; null once the call has set its+ /// rejection (or thrown).+ fn callRejecting(comptime T: type, local: *const Local, info: FunctionCallbackInfo, func: anytype, comptime opts: Opts) ?js.Value {+ var try_catch: js.TryCatch = undefined;+ try_catch.init(local);+ defer try_catch.deinit();++ const js_value = _call(T, local, info, func, opts) catch |err| {+ if (try_catch.hasCaught()) {+ const terminating = local.ctx.env.terminatePending() or+ v8.v8__Isolate__IsExecutionTerminating(local.isolate.handle);+ if (!terminating) {+ if (try_catch.exceptionValue()) |exception| {+ const err_local = errorLocal(T, local, info);+ const resolver = js.PromiseResolver.init(&err_local);+ resolver.rejectValue(exception) catch |reject_err| {+ log.err(.bug, "callRejecting reject", .{ .err = reject_err });+ };+ info.getReturnValue().set(resolver.promise().toValue());+ return null;+ }+ }+ try_catch.rethrow();+ }+ handleError(T, @TypeOf(func), local, err, info);+ return null;+ };+ if (try_catch.hasCaught()) {+ try_catch.rethrow();+ }+ return js_value;+ }+fn _call(comptime T: type, local: *const Local, info: FunctionCallbackInfo, func: anytype, comptime opts: Opts) !js.Value {const F = @TypeOf(func);var args: ParameterTypes(F) = undefined;diff --git a/src/browser/tests/page/promise_rejections.html b/src/browser/tests/page/promise_rejections.htmlnew file mode 100644index 000000000..9efd12f8c--- /dev/null+++ b/src/browser/tests/page/promise_rejections.html@@ -0,0 +1,56 @@+<!DOCTYPE html>+<body></body>+<script src="../testing.js"></script>++<script id=promise-rejects-page-exceptions type=module>+ // An operation that returns a promise never throws (WebIDL): an exception+ // the page's own code throws while it runs, a getter or a toString during+ // its arguments' conversion, rejects the promise with that exception.+ const state = await testing.async();+ const subtle = crypto.subtle;+ const boom = new RangeError("boom");+ const fail = () => { throw boom; };+ const outcome = async (fn) => {+ let promise;+ try {+ promise = fn();+ } catch (err) {+ return "threw";+ }+ try {+ await promise;+ return "resolved";+ } catch (err) {+ return err === boom ? "rejected with it" : `rejected with ${err.name}`;+ }+ };+ const key = await subtle.generateKey({ name: "AES-GCM", length: 128 }, false, ["encrypt"]);++ const results = {+ digestNameGetter: await outcome(() => subtle.digest({ get name() { fail(); } }, new Uint8Array(1))),+ encryptNameGetter: await outcome(() => subtle.encrypt({ get name() { fail(); } }, key, new Uint8Array(1))),+ importKeyFormatToString: await outcome(() => subtle.importKey({ toString: fail }, new Uint8Array(16), "AES-GCM", false, ["encrypt"])),+ fetchInitGetter: await outcome(() => fetch("data:,x", { get method() { fail(); } })),+ // A Zig error still rejects as before, and a call that succeeds resolves.+ digestBadAlgorithm: await outcome(() => subtle.digest("SHA-999", new Uint8Array(1))),+ digestOk: await outcome(() => subtle.digest("SHA-256", new Uint8Array(1))),+ };+ // An operation that doesn't return a promise still throws.+ let createElement = "returned";+ try {+ document.createElement({ toString: fail });+ } catch (err) {+ createElement = err === boom ? "threw it" : `threw ${err.name}`;+ }++ state.resolve();+ await state.done(() => {+ testing.expectEqual("rejected with it", results.digestNameGetter);+ testing.expectEqual("rejected with it", results.encryptNameGetter);+ testing.expectEqual("rejected with it", results.importKeyFormatToString);+ testing.expectEqual("rejected with it", results.fetchInitGetter);+ testing.expectEqual("rejected with NotSupportedError", results.digestBadAlgorithm);+ testing.expectEqual("resolved", results.digestOk);+ testing.expectEqual("threw it", createElement);+ });+</script>