Skip to content

Commit b086729

Browse files
committed
add ENUM_JS_USE_TAG for enums
1 parent 2b613a4 commit b086729

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/browser/html/History.zig

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,10 @@ const HistoryEntry = struct {
3333
};
3434

3535
const ScrollRestorationMode = enum {
36+
pub const ENUM_JS_USE_TAG = true;
37+
3638
auto,
3739
manual,
38-
39-
pub fn fromString(str: []const u8) ?ScrollRestorationMode {
40-
for (std.enums.values(ScrollRestorationMode)) |mode| {
41-
if (std.ascii.eqlIgnoreCase(str, @tagName(mode))) {
42-
return mode;
43-
}
44-
} else {
45-
return null;
46-
}
47-
}
48-
49-
pub fn toString(self: ScrollRestorationMode) []const u8 {
50-
return @tagName(self);
51-
}
5240
};
5341

5442
scroll_restoration: ScrollRestorationMode = .auto,
@@ -63,8 +51,8 @@ pub fn get_scrollRestoration(self: *History) ScrollRestorationMode {
6351
return self.scroll_restoration;
6452
}
6553

66-
pub fn set_scrollRestoration(self: *History, mode: []const u8) void {
67-
self.scroll_restoration = ScrollRestorationMode.fromString(mode) orelse self.scroll_restoration;
54+
pub fn set_scrollRestoration(self: *History, mode: ScrollRestorationMode) void {
55+
self.scroll_restoration = mode;
6856
}
6957

7058
pub fn get_state(self: *History, page: *Page) !?js.Value {

src/browser/js/Context.zig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,16 @@ pub fn jsValueToZig(self: *Context, comptime named_function: NamedFunction, comp
739739
unreachable;
740740
},
741741
.@"enum" => |e| {
742-
switch (@typeInfo(e.tag_type)) {
743-
.int => return std.meta.intToEnum(T, try jsIntToZig(e.tag_type, js_value, self.v8_context)),
744-
else => @compileError(named_function.full_name ++ " has an unsupported enum parameter type: " ++ @typeName(T)),
742+
if (@hasDecl(T, "ENUM_JS_USE_TAG")) {
743+
const str = try self.jsValueToZig(named_function, []const u8, js_value);
744+
return std.meta.stringToEnum(T, str) orelse return error.InvalidEnumValue;
745+
} else {
746+
switch (@typeInfo(e.tag_type)) {
747+
.int => return std.meta.intToEnum(T, try jsIntToZig(e.tag_type, js_value, self.v8_context)),
748+
else => {
749+
@compileError(named_function.full_name ++ " has an unsupported enum parameter type: " ++ @typeName(T));
750+
},
751+
}
745752
}
746753
},
747754
else => {},

src/browser/js/js.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,13 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
378378
.@"enum" => {
379379
const T = @TypeOf(value);
380380
if (@hasDecl(T, "toString")) {
381+
// This should be deprecated in favor of the ENUM_JS_USE_TAG.
381382
return simpleZigValueToJs(isolate, value.toString(), fail);
382383
}
384+
385+
if (@hasDecl(T, "ENUM_JS_USE_TAG")) {
386+
return simpleZigValueToJs(isolate, @tagName(value), fail);
387+
}
383388
},
384389
else => {},
385390
}

0 commit comments

Comments
 (0)