Skip to main content

luaur_code_gen/functions/
set_native_execution_enabled.rs

1use crate::functions::get_code_gen_context::get_code_gen_context;
2use crate::functions::on_enter::on_enter;
3use crate::functions::on_enter_disabled::on_enter_disabled;
4use crate::type_aliases::lua_state::lua_State;
5
6#[inline]
7pub fn set_native_execution_enabled(L: *mut lua_State, enabled: bool) {
8    // SAFETY: native-only; caller must ensure L is a valid lua_State pointer.
9    // We must access the internal global state to modify the execution callbacks.
10    unsafe {
11        let context_ptr = get_code_gen_context(L);
12        if context_ptr.is_null() {
13            return;
14        }
15
16        // Access L->global->ecb.enter.
17        // We use the internal layout of lua_State from luau-vm as seen in examples.
18        let l_internal = L as *mut luaur_vm::records::lua_state::lua_State;
19        let global = (*l_internal).global;
20
21        // The callbacks in ecb.enter expect a specific function pointer type:
22        // unsafe extern "C" fn(*mut luaur_vm::records::lua_state::lua_State, *mut luaur_vm::records::proto::Proto) -> i32
23        // We transmute the imported on_enter/on_enter_disabled to match this expected VM signature.
24        type LuaEnterFn = unsafe extern "C" fn(
25            *mut luaur_vm::records::lua_state::lua_State,
26            *mut luaur_vm::records::proto::Proto,
27        ) -> i32;
28
29        (*global).ecb.enter = if enabled {
30            let enter_ptr: LuaEnterFn = core::mem::transmute(on_enter as *const ());
31            Some(enter_ptr)
32        } else {
33            let enter_disabled_ptr: LuaEnterFn =
34                core::mem::transmute(on_enter_disabled as *const ());
35            Some(enter_disabled_ptr)
36        };
37    }
38}