Skip to main content

luaur_analysis/functions/
get_generic_name.rs

1use crate::functions::get_tag::get_tag;
2use crate::functions::get_type_function_runtime_alt_o::get_type_function_type_id;
3use crate::functions::get_type_user_data::get_type_user_data;
4use crate::records::type_function_generic_type::TypeFunctionGenericType;
5use crate::type_aliases::lua_state::lua_State;
6use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
7use luaur_vm::functions::lua_pushlstring::lua_pushlstring;
8use luaur_vm::functions::lua_pushnil::lua_pushnil;
9
10pub unsafe fn get_generic_name(l: *mut lua_State) -> core::ffi::c_int {
11    let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
12    let self_ty = get_type_user_data(l, 1);
13
14    let tfgt = get_type_function_type_id::<TypeFunctionGenericType>(self_ty);
15    if tfgt.is_null() {
16        lua_l_error_l(
17            vm_l,
18            c"%s".as_ptr(),
19            core::format_args!(
20                "type.name: expected self to be a generic, but got {} instead",
21                get_tag(l, self_ty)
22            ),
23        );
24    }
25
26    if (*tfgt).is_named {
27        lua_pushlstring(
28            vm_l,
29            {
30                let n = &(*tfgt).name;
31                n.as_ptr() as *const core::ffi::c_char
32            },
33            {
34                let n = &(*tfgt).name;
35                n.len()
36            },
37        );
38    } else {
39        lua_pushnil(vm_l);
40    }
41
42    1
43}