Skip to content

Commit c52d33e

Browse files
authored
Merge pull request #822 from lightpanda-io/undefined_or
Add UndefinedOr(T) union
2 parents 1c6f4a7 + fd36606 commit c52d33e

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/browser/html/DataSet.zig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
const std = @import("std");
1919
const parser = @import("../netsurf.zig");
20+
const Env = @import("../env.zig").Env;
2021
const Page = @import("../page.zig").Page;
2122

2223
const Allocator = std.mem.Allocator;
@@ -25,16 +26,12 @@ const DataSet = @This();
2526

2627
element: *parser.Element,
2728

28-
const GetResult = union(enum) {
29-
value: []const u8,
30-
undefined: void,
31-
};
32-
pub fn named_get(self: *const DataSet, name: []const u8, _: *bool, page: *Page) !GetResult {
29+
pub fn named_get(self: *const DataSet, name: []const u8, _: *bool, page: *Page) !Env.UndefinedOr([]const u8) {
3330
const normalized_name = try normalize(page.call_arena, name);
3431
if (try parser.elementGetAttribute(self.element, normalized_name)) |value| {
3532
return .{ .value = value };
3633
}
37-
return .{ .undefined = {} };
34+
return .undefined;
3835
}
3936

4037
pub fn named_set(self: *DataSet, name: []const u8, value: []const u8, _: *bool, page: *Page) !void {

src/browser/html/error_event.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,11 @@ pub const ErrorEvent = struct {
7272
return self.colno;
7373
}
7474

75-
const ErrorValue = union(enum) {
76-
obj: Env.JsObject,
77-
undefined: void,
78-
};
79-
pub fn get_error(self: *const ErrorEvent) ErrorValue {
75+
pub fn get_error(self: *const ErrorEvent) Env.UndefinedOr(Env.JsObject) {
8076
if (self.@"error") |e| {
81-
return .{ .obj = e };
77+
return .{ .value = e };
8278
}
83-
return .{ .undefined = {} };
79+
return .undefined;
8480
}
8581
};
8682

src/runtime/js.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,13 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18671867
}
18681868
};
18691869

1870+
pub fn UndefinedOr(comptime T: type) type {
1871+
return union(enum) {
1872+
undefined: void,
1873+
value: T,
1874+
};
1875+
}
1876+
18701877
fn compileModule(isolate: v8.Isolate, src: []const u8, name: []const u8) !v8.Module {
18711878
// compile
18721879
const script_name = v8.String.initUtf8(isolate, name);

0 commit comments

Comments
 (0)