Skip to main content

luaur_analysis/functions/
read_table_prop.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_o::get_type_function_type_id;
4use crate::functions::get_type_user_data::get_type_user_data;
5use crate::records::type_function_singleton_type::TypeFunctionSingletonType;
6use crate::records::type_function_table_type::TypeFunctionTableType;
7use crate::type_aliases::lua_state::lua_State;
8use luaur_vm::functions::lua_gettop::lua_gettop;
9use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
10use luaur_vm::functions::lua_pushnil::lua_pushnil;
11
12pub unsafe fn read_table_prop(l: *mut lua_State) -> core::ffi::c_int {
13    let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
14    let argument_count = lua_gettop(vm_l);
15    if argument_count != 2 {
16        lua_l_error_l(
17            vm_l,
18            c"%s".as_ptr(),
19            core::format_args!(
20                "type.readproperty: expected 2 arguments, but got {}",
21                argument_count
22            ),
23        );
24    }
25
26    let self_ty = get_type_user_data(l, 1);
27    let tftt = get_type_function_type_id::<TypeFunctionTableType>(self_ty);
28    if tftt.is_null() {
29        lua_l_error_l(
30            vm_l,
31            c"%s".as_ptr(),
32            core::format_args!(
33                "type.readproperty: expected self to be either a table, but got {} instead",
34                get_tag(l, self_ty)
35            ),
36        );
37    }
38
39    let key = get_type_user_data(l, 2);
40    let tfst = get_type_function_type_id::<TypeFunctionSingletonType>(key);
41    if tfst.is_null() {
42        lua_l_error_l(
43            vm_l,
44            c"%s".as_ptr(),
45            core::format_args!(
46                "type.readproperty: expected to be given a singleton type, but got {} instead",
47                get_tag(l, key)
48            ),
49        );
50    }
51
52    let tfsst = (*tfst).variant.get_if_1();
53    if tfsst.is_none() {
54        lua_l_error_l(
55            vm_l,
56            c"%s".as_ptr(),
57            core::format_args!(
58                "type.readproperty: expected to be given a string singleton type, but got {} instead",
59                get_tag(l, key)
60            ),
61        );
62    }
63
64    let key_name = &tfsst.unwrap().value;
65    let prop = (*tftt).props.get(key_name);
66    if prop.is_none() {
67        lua_pushnil(vm_l);
68        return 1;
69    }
70
71    if let Some(read_ty) = prop.unwrap().read_ty {
72        alloc_type_user_data(l, (*read_ty).type_variant.clone(), false);
73    } else {
74        lua_pushnil(vm_l);
75    }
76
77    1
78}