add ENUM_JS_USE_TAG for enums

This commit is contained in:
Muki Kiboigo
2025-10-12 15:45:40 -07:00
parent 186655e614
commit 8ab9364f19
3 changed files with 19 additions and 19 deletions

View File

@@ -33,22 +33,10 @@ const HistoryEntry = struct {
}; };
const ScrollRestorationMode = enum { const ScrollRestorationMode = enum {
pub const ENUM_JS_USE_TAG = true;
auto, auto,
manual, manual,
pub fn fromString(str: []const u8) ?ScrollRestorationMode {
for (std.enums.values(ScrollRestorationMode)) |mode| {
if (std.ascii.eqlIgnoreCase(str, @tagName(mode))) {
return mode;
}
} else {
return null;
}
}
pub fn toString(self: ScrollRestorationMode) []const u8 {
return @tagName(self);
}
}; };
scroll_restoration: ScrollRestorationMode = .auto, scroll_restoration: ScrollRestorationMode = .auto,
@@ -63,8 +51,8 @@ pub fn get_scrollRestoration(self: *History) ScrollRestorationMode {
return self.scroll_restoration; return self.scroll_restoration;
} }
pub fn set_scrollRestoration(self: *History, mode: []const u8) void { pub fn set_scrollRestoration(self: *History, mode: ScrollRestorationMode) void {
self.scroll_restoration = ScrollRestorationMode.fromString(mode) orelse self.scroll_restoration; self.scroll_restoration = mode;
} }
pub fn get_state(self: *History, page: *Page) !?js.Value { pub fn get_state(self: *History, page: *Page) !?js.Value {

View File

@@ -750,9 +750,16 @@ pub fn jsValueToZig(self: *Context, comptime named_function: NamedFunction, comp
unreachable; unreachable;
}, },
.@"enum" => |e| { .@"enum" => |e| {
switch (@typeInfo(e.tag_type)) { if (@hasDecl(T, "ENUM_JS_USE_TAG")) {
.int => return std.meta.intToEnum(T, try jsIntToZig(e.tag_type, js_value, self.v8_context)), const str = try self.jsValueToZig(named_function, []const u8, js_value);
else => @compileError(named_function.full_name ++ " has an unsupported enum parameter type: " ++ @typeName(T)), return std.meta.stringToEnum(T, str) orelse return error.InvalidEnumValue;
} else {
switch (@typeInfo(e.tag_type)) {
.int => return std.meta.intToEnum(T, try jsIntToZig(e.tag_type, js_value, self.v8_context)),
else => {
@compileError(named_function.full_name ++ " has an unsupported enum parameter type: " ++ @typeName(T));
},
}
} }
}, },
else => {}, else => {},

View File

@@ -378,8 +378,13 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
.@"enum" => { .@"enum" => {
const T = @TypeOf(value); const T = @TypeOf(value);
if (@hasDecl(T, "toString")) { if (@hasDecl(T, "toString")) {
// This should be deprecated in favor of the ENUM_JS_USE_TAG.
return simpleZigValueToJs(isolate, value.toString(), fail); return simpleZigValueToJs(isolate, value.toString(), fail);
} }
if (@hasDecl(T, "ENUM_JS_USE_TAG")) {
return simpleZigValueToJs(isolate, @tagName(value), fail);
}
}, },
else => {}, else => {},
} }