add more safety in the lua api

This commit is contained in:
Squibid 2025-12-13 09:03:10 -05:00
parent bfdc8b8c9d
commit 7537778e23
Signed by: squibid
GPG key ID: BECE5684D3C4005D
2 changed files with 18 additions and 8 deletions

View file

@ -3,6 +3,7 @@ const zlua = @import("zlua");
const wlr = @import("wlroots"); const wlr = @import("wlroots");
const Utils = @import("../Utils.zig"); const Utils = @import("../Utils.zig");
const LuaUtils = @import("LuaUtils.zig");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
@ -36,7 +37,11 @@ pub fn exit(L: *zlua.Lua) i32 {
/// ---Change to a different virtual terminal /// ---Change to a different virtual terminal
/// ---@param vt_num integer virtual terminal number to switch to /// ---@param vt_num integer virtual terminal number to switch to
pub fn change_vt(L: *zlua.Lua) i32 { pub fn change_vt(L: *zlua.Lua) i32 {
const vt_num: c_uint = @intCast(L.checkInteger(1)); const vt_num = num: {
const res = LuaUtils.coerceInteger(c_uint, L.checkInteger(1)) catch |err| break :num err;
if (res < 1) break :num error.InvalidInteger;
break :num res;
} catch L.raiseErrorStr("The vt number must be >= 1 and < inf", .{});
if (server.session) |session| { if (server.session) |session| {
std.log.debug("Changing virtual terminal to {d}", .{vt_num}); std.log.debug("Changing virtual terminal to {d}", .{vt_num});

View file

@ -2,9 +2,14 @@ const std = @import("std");
const zlua = @import("zlua"); const zlua = @import("zlua");
const Output = @import("../Output.zig"); const Output = @import("../Output.zig");
const LuaUtils = @import("LuaUtils.zig");
const server = &@import("../main.zig").server; const server = &@import("../main.zig").server;
fn output_id_err(L: *zlua.Lua) noreturn {
L.raiseErrorStr("The output id must be >= 0 and < inf", .{});
}
/// ---@alias output_id integer /// ---@alias output_id integer
/// ---Get the ids for all available outputs /// ---Get the ids for all available outputs
@ -44,7 +49,7 @@ pub fn get_focused_id(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return integer? /// ---@return integer?
pub fn get_rate(L: *zlua.Lua) i32 { pub fn get_rate(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -60,7 +65,7 @@ pub fn get_rate(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return { width: integer, height: integer }? /// ---@return { width: integer, height: integer }?
pub fn get_resolution(L: *zlua.Lua) i32 { pub fn get_resolution(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -85,7 +90,7 @@ pub fn get_resolution(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return string? /// ---@return string?
pub fn get_serial(L: *zlua.Lua) i32 { pub fn get_serial(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -106,7 +111,7 @@ pub fn get_serial(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return string? /// ---@return string?
pub fn get_make(L: *zlua.Lua) i32 { pub fn get_make(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -127,7 +132,7 @@ pub fn get_make(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return stirng? /// ---@return stirng?
pub fn get_model(L: *zlua.Lua) i32 { pub fn get_model(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -148,7 +153,7 @@ pub fn get_model(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return stirng? /// ---@return stirng?
pub fn get_description(L: *zlua.Lua) i32 { pub fn get_description(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {
@ -169,7 +174,7 @@ pub fn get_description(L: *zlua.Lua) i32 {
/// ---@param output_id output_id 0 maps to focused output /// ---@param output_id output_id 0 maps to focused output
/// ---@return stirng /// ---@return stirng
pub fn get_name(L: *zlua.Lua) i32 { pub fn get_name(L: *zlua.Lua) i32 {
const output_id: u64 = @intCast(L.checkInteger(1)); const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L);
const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
if(output) |o| { if(output) |o| {