luaur_analysis/functions/
get_props.rs1use 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_extern_type::TypeFunctionExternType;
6use crate::records::type_function_property::TypeFunctionProperty;
7use crate::records::type_function_singleton_type::TypeFunctionSingletonType;
8use crate::records::type_function_string_singleton::TypeFunctionStringSingleton;
9use crate::records::type_function_table_type::TypeFunctionTableType;
10use crate::type_aliases::lua_state::lua_State;
11use crate::type_aliases::type_function_singleton_variant::TypeFunctionSingletonVariant;
12use crate::type_aliases::type_function_type_variant::TypeFunctionTypeVariant;
13use alloc::collections::BTreeMap;
14use alloc::string::String;
15use luaur_vm::functions::lua_createtable::lua_createtable;
16use luaur_vm::functions::lua_gettop::lua_gettop;
17use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
18use luaur_vm::functions::lua_setfield::lua_setfield;
19use luaur_vm::functions::lua_settable::lua_settable;
20
21pub unsafe fn get_props(l: *mut lua_State) -> core::ffi::c_int {
22 let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
23 let argument_count = lua_gettop(vm_l);
24 if argument_count != 1 {
25 lua_l_error_l(
26 vm_l,
27 c"%s".as_ptr(),
28 core::format_args!(
29 "type.properties: expected 1 arguments, but got {}",
30 argument_count
31 ),
32 );
33 }
34
35 let self_ty = get_type_user_data(l, 1);
36
37 let tftt = get_type_function_type_id::<TypeFunctionTableType>(self_ty);
38 if !tftt.is_null() {
39 push_props(l, vm_l, &(*tftt).props);
40 return 1;
41 }
42
43 let tfct = get_type_function_type_id::<TypeFunctionExternType>(self_ty);
44 if !tfct.is_null() {
45 push_props(l, vm_l, &(*tfct).props);
46 return 1;
47 }
48
49 lua_l_error_l(
50 vm_l,
51 c"%s".as_ptr(),
52 core::format_args!(
53 "type.properties: expected self to be either a table or class, but got {} instead",
54 get_tag(l, self_ty)
55 ),
56 );
57 0
58}
59
60unsafe fn push_props(
61 l: *mut lua_State,
62 vm_l: *mut luaur_vm::records::lua_state::lua_State,
63 props: &BTreeMap<String, TypeFunctionProperty>,
64) {
65 lua_createtable(vm_l, props.len() as core::ffi::c_int, 0);
66
67 for (name, prop) in props {
68 alloc_type_user_data(
69 l,
70 TypeFunctionTypeVariant::Singleton(TypeFunctionSingletonType {
71 variant: TypeFunctionSingletonVariant::V1(TypeFunctionStringSingleton {
72 value: name.clone(),
73 }),
74 }),
75 false,
76 );
77
78 let mut size: core::ffi::c_int = 0;
79 if prop.read_ty.is_some() {
80 size += 1;
81 }
82 if prop.write_ty.is_some() {
83 size += 1;
84 }
85
86 lua_createtable(vm_l, 0, size);
87
88 if let Some(read_ty) = prop.read_ty {
89 alloc_type_user_data(l, (*read_ty).type_variant.clone(), false);
90 lua_setfield(vm_l, -2, c"read".as_ptr());
91 }
92
93 if let Some(write_ty) = prop.write_ty {
94 alloc_type_user_data(l, (*write_ty).type_variant.clone(), false);
95 lua_setfield(vm_l, -2, c"write".as_ptr());
96 }
97
98 lua_settable(vm_l, -3);
99 }
100}