Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Make DOMException structureCloneable

Karl Seguinf06d0047a4a1
diff --git a/src/browser/js/Value.zig b/src/browser/js/Value.zig
index 5f4ecde96..a2ab7fa6f 100644
--- a/src/browser/js/Value.zig
+++ b/src/browser/js/Value.zig
@@ -506,6 +506,7 @@ const cloneable_types = .{
@import("../webapi/DOMPoint.zig"),
@import("../webapi/DOMRectReadOnly.zig"),
@import("../webapi/DOMRect.zig"),
+ @import("../webapi/DOMException.zig"),
@import("../webapi/webcodecs/encoded_chunk.zig").EncodedAudioChunk,
@import("../webapi/webcodecs/encoded_chunk.zig").EncodedVideoChunk,
@import("../webapi/webcodecs/AudioData.zig"),
diff --git a/src/browser/tests/domexception.html b/src/browser/tests/domexception.html
index 8068d6737..dfd7b890a 100644
--- a/src/browser/tests/domexception.html
+++ b/src/browser/tests/domexception.html
@@ -146,3 +146,29 @@
testing.expectEqual(true, err instanceof Error);
}, () => link.appendChild(content));
</script>
+
+<script id=structured_clone>
+ {
+ const named = structuredClone(new DOMException('boom', 'NotFoundError'));
+ testing.expectEqual(true, named instanceof DOMException);
+ testing.expectEqual('NotFoundError', named.name);
+ testing.expectEqual('boom', named.message);
+ testing.expectEqual(8, named.code);
+
+ const plain = structuredClone(new DOMException());
+ testing.expectEqual('Error', plain.name);
+ testing.expectEqual('', plain.message);
+ testing.expectEqual(0, plain.code);
+
+ // An exception raised from Zig carries only a code; name and message
+ // still survive the round trip.
+ testing.withError((err) => {
+ const c = structuredClone({a: err, b: err});
+ testing.expectEqual(true, c.a === c.b);
+ testing.expectEqual(false, c.a === err);
+ testing.expectEqual('HierarchyRequestError', c.a.name);
+ testing.expectEqual(err.message, c.a.message);
+ testing.expectEqual(3, c.a.code);
+ }, () => link.appendChild(content));
+ }
+</script>
diff --git a/src/browser/webapi/DOMException.zig b/src/browser/webapi/DOMException.zig
index 9276d4769..6babf1efb 100644
--- a/src/browser/webapi/DOMException.zig
+++ b/src/browser/webapi/DOMException.zig
@@ -18,6 +18,7 @@
const std = @import("std");
const js = @import("../js/js.zig");
+const Page = @import("../Page.zig");
const DOMException = @This();
@@ -165,6 +166,18 @@ pub fn toString(self: *const DOMException, exec: *js.Execution) ![]const u8 {
return std.mem.print(exec.buf, "{s}: {s}", .{ self.getName(), msg }) catch return msg;
}
+pub fn structuredSerialize(self: *const DOMException, writer: *js.StructuredWriter) !void {
+ writer.writeBytes(self.getName());
+ writer.writeBytes(self.getMessage());
+}
+
+pub fn structuredDeserialize(reader: *js.StructuredReader, _: *Page) !DOMException {
+ const arena = reader.local.ctx.arena.allocator();
+ const name = try arena.dupe(u8, try reader.readBytes());
+ const message = try arena.dupe(u8, try reader.readBytes());
+ return init(message, name);
+}
+
const Code = enum(u8) {
none = 0,
index_size_error = 1,