Skip to main content

luaur_vm/functions/
lua_g_indexerror.rs

1//! Node: `cxx:Function:Luau.VM:VM/src/ldebug.cpp:286:luaG_indexerror`
2//! Source: `VM/src/ldebug.cpp:286-296` (hand-ported)
3
4use core::ffi::{c_char, CStr};
5
6use crate::functions::lua_t_objtypename::lua_t_objtypename;
7use crate::macros::getstr::getstr;
8use crate::macros::lua_g_runerror::lua_g_runerror;
9use crate::macros::tsvalue::tsvalue;
10use crate::macros::ttisstring::ttisstring;
11use crate::type_aliases::lua_state::lua_State;
12use crate::type_aliases::t_string::TString;
13use crate::type_aliases::t_value::TValue;
14
15#[allow(non_snake_case)]
16pub unsafe fn lua_g_indexerror(L: *mut lua_State, p1: *const TValue, p2: *const TValue) -> ! {
17    let t1: *const c_char = lua_t_objtypename(L, p1);
18    let t2: *const c_char = lua_t_objtypename(L, p2);
19    let key: *const TString = if ttisstring!(p2) {
20        tsvalue!(p2)
21    } else {
22        core::ptr::null()
23    };
24
25    // limit length to make sure we don't generate very long error messages for very long keys
26    if !key.is_null() && (*key).len <= 64 {
27        lua_g_runerror!(
28            L,
29            "attempt to index {} with '{}'",
30            CStr::from_ptr(t1).to_string_lossy(),
31            CStr::from_ptr(getstr(key)).to_string_lossy()
32        )
33    } else {
34        lua_g_runerror!(
35            L,
36            "attempt to index {} with {}",
37            CStr::from_ptr(t1).to_string_lossy(),
38            CStr::from_ptr(t2).to_string_lossy()
39        )
40    }
41}
42
43#[allow(non_snake_case)]
44pub unsafe fn luaG_indexerror(L: *mut lua_State, p1: *const TValue, p2: *const TValue) -> ! {
45    lua_g_indexerror(L, p1, p2)
46}