mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 14:43:28 +00:00
add ENUM_JS_USE_TAG for enums
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
@@ -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 => {},
|
||||||
|
|||||||
@@ -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 => {},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user