Skip to main content

luaur_analysis/functions/
get_function_generics.rs

1use crate::functions::alloc_type_user_data::alloc_type_user_data;
2use crate::functions::get_tag::get_tag;
3use crate::functions::get_type_function_runtime_alt_n::get_type_function_type_pack_id;
4use crate::functions::get_type_function_runtime_alt_o::get_type_function_type_id;
5use crate::functions::get_type_user_data::get_type_user_data;
6use crate::records::type_function_function_type::TypeFunctionFunctionType;
7use crate::records::type_function_generic_type::TypeFunctionGenericType;
8use crate::records::type_function_generic_type_pack::TypeFunctionGenericTypePack;
9use crate::type_aliases::lua_state::lua_State;
10use crate::type_aliases::type_function_type_variant::TypeFunctionTypeVariant;
11use luaur_common::macros::luau_assert::LUAU_ASSERT;
12use luaur_vm::functions::lua_createtable::lua_createtable;
13use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
14use luaur_vm::functions::lua_rawseti::lua_rawseti;
15
16pub unsafe fn get_function_generics(l: *mut lua_State) -> core::ffi::c_int {
17    let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
18    let self_ty = get_type_user_data(l, 1);
19
20    let tfft = get_type_function_type_id::<TypeFunctionFunctionType>(self_ty);
21    if tfft.is_null() {
22        lua_l_error_l(
23            vm_l,
24            c"%s".as_ptr(),
25            core::format_args!(
26                "type.generics: expected self to be a function, but got {} instead",
27                get_tag(l, self_ty)
28            ),
29        );
30    }
31
32    lua_createtable(
33        vm_l,
34        ((*tfft).generics.len() + (*tfft).generic_packs.len()) as i32,
35        0,
36    );
37
38    let mut pos: i32 = 1;
39
40    for el in &(*tfft).generics {
41        alloc_type_user_data(l, (*(*el)).type_variant.clone(), false);
42        lua_rawseti(vm_l, -2, pos);
43        pos += 1;
44    }
45
46    for el in &(*tfft).generic_packs {
47        let gty = get_type_function_type_pack_id::<TypeFunctionGenericTypePack>(*el);
48        LUAU_ASSERT!(!gty.is_null());
49        alloc_type_user_data(
50            l,
51            TypeFunctionTypeVariant::Generic(TypeFunctionGenericType {
52                is_named: (*gty).is_named,
53                is_pack: true,
54                name: (*gty).name.clone(),
55            }),
56            false,
57        );
58        lua_rawseti(vm_l, -2, pos);
59        pos += 1;
60    }
61
62    1
63}