Skip to main content

luaur_code_gen/functions/
is_native_execution_enabled.rs

1use crate::functions::get_code_gen_context::get_code_gen_context;
2use crate::type_aliases::lua_state::lua_State;
3use luaur_vm::records::lua_state::lua_State as LuaStateInternal;
4
5#[inline]
6pub fn is_native_execution_enabled(L: *mut lua_State) -> bool {
7    // SAFETY: Accessing L->global->ecb.enter requires L to be a valid pointer.
8    // The C++ implementation checks if the context exists and if the enter callback
9    // matches the 'onEnter' function pointer.
10    unsafe {
11        let context = get_code_gen_context(L);
12        if context.is_null() {
13            return false;
14        }
15
16        let l_internal = L as *mut LuaStateInternal;
17        if l_internal.is_null() {
18            return false;
19        }
20
21        let global = (*l_internal).global;
22        if global.is_null() {
23            return false;
24        }
25
26        // The C++ source compares the function pointer directly.
27        // We assume 'on_enter' is the Rust translation of the 'onEnter' symbol.
28        // We compare the function pointer addresses.
29        let current_enter = (*global).ecb.enter;
30
31        // We need to compare against the address of the 'on_enter' function.
32        // Import it from the crate where it is defined.
33        let on_enter_ptr = crate::functions::on_enter::on_enter as *const ();
34
35        if let Some(enter_fn) = current_enter {
36            let enter_fn_ptr = enter_fn as *const ();
37            enter_fn_ptr == on_enter_ptr
38        } else {
39            false
40        }
41    }
42}