Skip to main content

luaur_analysis/functions/
set_function_returns.rs

1use crate::functions::create_function::get_type_pack_runtime;
2use crate::functions::get_mutable_type_function_runtime_alt_g::get_mutable_type_function_type_id;
3use crate::functions::get_tag::get_tag;
4use crate::functions::get_type_user_data::get_type_user_data;
5use crate::records::type_function_function_type::TypeFunctionFunctionType;
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;
9
10#[allow(non_snake_case)]
11pub unsafe fn set_function_returns(l: *mut lua_State) -> core::ffi::c_int {
12    let vm_l = l as *mut luaur_vm::records::lua_state::lua_State;
13    let argument_count = lua_gettop(vm_l);
14
15    if argument_count < 2 || argument_count > 3 {
16        lua_l_error_l(
17            vm_l,
18            c"%s".as_ptr(),
19            core::format_args!(
20                "type.setreturns: expected 1-3 arguments, but got {}",
21                argument_count
22            ),
23        );
24    }
25
26    let self_ty = get_type_user_data(l, 1);
27    let tfft = get_mutable_type_function_type_id::<TypeFunctionFunctionType>(self_ty);
28    if tfft.is_null() {
29        lua_l_error_l(
30            vm_l,
31            c"%s".as_ptr(),
32            core::format_args!(
33                "type.setreturns: expected self to be a function, but got {} instead",
34                get_tag(l, self_ty)
35            ),
36        );
37    }
38
39    if luaur_common::FFlag::LuauTypeFunctionSupportsFrozen.get() && (*self_ty).frozen {
40        lua_l_error_l(
41            vm_l,
42            c"%s".as_ptr(),
43            core::format_args!(
44                "type.setreturns: cannot be called to mutate a frozen type, use `types.copy` to make a copy"
45            ),
46        );
47    }
48
49    (*tfft).ret_types = get_type_pack_runtime(l, 2, 3);
50
51    0
52}