Skip to main content

luaur_vm/functions/
buffer_fill.rs

1use crate::functions::lua_l_checkbuffer::lua_l_checkbuffer;
2use crate::functions::lua_l_checkinteger::lua_l_checkinteger;
3use crate::functions::lua_l_checkunsigned::lua_l_checkunsigned;
4use crate::functions::lua_l_optinteger::lua_l_optinteger;
5use crate::macros::isoutofbounds::isoutofbounds;
6use crate::macros::lua_l_error::luaL_error;
7use crate::type_aliases::lua_state::lua_State;
8
9pub fn buffer_fill(L: *mut lua_State) -> core::ffi::c_int {
10    let mut len: usize = 0;
11    let buf = lua_l_checkbuffer(L, 1, &mut len);
12    let offset = lua_l_checkinteger(L, 2);
13    let value = lua_l_checkunsigned(L, 3);
14    let size = lua_l_optinteger(L, 4, (len as core::ffi::c_int) - offset);
15
16    if size < 0 {
17        luaL_error!(L, "buffer access out of bounds");
18    }
19
20    if isoutofbounds(offset, len, size as usize) {
21        luaL_error!(L, "buffer access out of bounds");
22    }
23
24    unsafe {
25        core::ptr::write_bytes(
26            (buf as *mut core::ffi::c_char).offset(offset as isize),
27            (value & 0xff) as u8,
28            size as usize,
29        );
30    }
31
32    0
33}