Skip to main content

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