luaur_analysis/functions/
create_generic.rs1use crate::functions::alloc_type_user_data::alloc_type_user_data;
2use crate::records::type_function_generic_type::TypeFunctionGenericType;
3use crate::type_aliases::lua_state::lua_State;
4use crate::type_aliases::type_function_type_variant::TypeFunctionTypeVariant;
5use core::ffi::c_int;
6use core::ffi::CStr;
7use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
8use luaur_vm::functions::lua_l_optboolean::lua_l_optboolean;
9use luaur_vm::macros::lua_l_checkstring::luaL_checkstring;
10
11pub unsafe fn create_generic(l: *mut lua_State) -> c_int {
12 let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
13 let name_ptr = luaL_checkstring!(vm_l, 1);
14 let is_pack = lua_l_optboolean(vm_l, 2, false);
15
16 let name_cstr = CStr::from_ptr(name_ptr);
17 if name_cstr.to_bytes().is_empty() {
18 lua_l_error_l(
19 vm_l,
20 c"%s".as_ptr(),
21 core::format_args!("types.generic: generic name cannot be empty"),
22 );
23 }
24
25 let generic_type = TypeFunctionGenericType {
26 is_named: true,
27 is_pack,
28 name: name_cstr.to_string_lossy().into_owned(),
29 };
30
31 alloc_type_user_data(l, TypeFunctionTypeVariant::Generic(generic_type), false);
32
33 1
34}