Skip to main content

luaur_analysis/functions/
get_write_parent.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_extern_type::TypeFunctionExternType;
6use crate::type_aliases::lua_state::lua_State;
7use luaur_vm::functions::lua_gettop::lua_gettop;
8use luaur_vm::functions::lua_l_error_l::lua_l_error_l;
9use luaur_vm::functions::lua_pushnil::lua_pushnil;
10
11#[allow(non_snake_case)]
12pub unsafe fn get_write_parent(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 != 1 {
16        lua_l_error_l(
17            vm_l,
18            c"%s".as_ptr(),
19            core::format_args!(
20                "type.parent: expected 1 arguments, but got {}",
21                argument_count
22            ),
23        );
24    }
25
26    let self_ty = get_type_user_data(l, 1);
27    let tfct = get_type_function_type_id::<TypeFunctionExternType>(self_ty);
28
29    if tfct.is_null() {
30        lua_l_error_l(
31            vm_l,
32            c"%s".as_ptr(),
33            core::format_args!(
34                "type.parent: expected self to be a class, but got {} instead",
35                get_tag(l, self_ty)
36            ),
37        );
38    }
39
40    if let Some(write_parent) = (*tfct).write_parent {
41        alloc_type_user_data(l, (*write_parent).type_variant.clone(), false);
42    } else {
43        lua_pushnil(vm_l);
44    }
45
46    1
47}