Skip to main content

luaur_analysis/functions/
get_singleton_value.rs

1use crate::functions::get_tag::get_tag;
2use crate::functions::get_type_function_runtime_alt_o::get_type_function_type_id;
3use crate::functions::get_type_user_data::get_type_user_data;
4use crate::records::type_function_boolean_singleton::TypeFunctionBooleanSingleton;
5use crate::records::type_function_primitive_type::TypeFunctionPrimitiveType;
6use crate::records::type_function_singleton_type::TypeFunctionSingletonType;
7use crate::records::type_function_string_singleton::TypeFunctionStringSingleton;
8use crate::type_aliases::lua_state::lua_State;
9use core::ffi::c_int;
10use luaur_vm::functions::lua_gettop::lua_gettop;
11use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
12use luaur_vm::functions::lua_pushboolean::lua_pushboolean;
13use luaur_vm::functions::lua_pushlstring::lua_pushlstring;
14use luaur_vm::functions::lua_pushnil::lua_pushnil;
15
16pub unsafe fn get_singleton_value(l: *mut lua_State) -> c_int {
17    let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
18    let argument_count = lua_gettop(vm_l);
19    if argument_count != 1 {
20        lua_l_error_l(
21            vm_l,
22            c"%s".as_ptr(),
23            core::format_args!(
24                "type.value: expected 1 argument, but got {}",
25                argument_count
26            ),
27        );
28    }
29
30    let self_ty = get_type_user_data(l, 1);
31    let tfpt = get_type_function_type_id::<TypeFunctionPrimitiveType>(self_ty);
32    if !tfpt.is_null() {
33        if (*tfpt).r#type != crate::enums::type_type_function_runtime::Type::NilType {
34            lua_l_error_l(
35                vm_l,
36                c"%s".as_ptr(),
37                core::format_args!(
38                    "type.value: expected self to be a singleton, but got {} instead",
39                    get_tag(l, self_ty)
40                ),
41            );
42        }
43
44        lua_pushnil(vm_l);
45        return 1;
46    }
47
48    let tfst = get_type_function_type_id::<TypeFunctionSingletonType>(self_ty);
49    if tfst.is_null() {
50        lua_l_error_l(
51            vm_l,
52            c"%s".as_ptr(),
53            core::format_args!(
54                "type.value: expected self to be a singleton, but got {} instead",
55                get_tag(l, self_ty)
56            ),
57        );
58    }
59
60    if let Some(tfbst) = (*tfst).variant.get_if::<TypeFunctionBooleanSingleton>() {
61        lua_pushboolean(vm_l, tfbst.value as c_int);
62        return 1;
63    }
64
65    if let Some(tfsst) = (*tfst).variant.get_if::<TypeFunctionStringSingleton>() {
66        let s = &tfsst.value;
67        lua_pushlstring(vm_l, s.as_ptr() as *const core::ffi::c_char, s.len());
68        return 1;
69    }
70
71    lua_l_error_l(
72        vm_l,
73        c"%s".as_ptr(),
74        core::format_args!(
75            "type.value: can't call `value` method on `{}` type",
76            get_tag(l, self_ty)
77        ),
78    );
79    0
80}