luaur_compile_cli/functions/
get_codegen_assembly.rs1use alloc::string::String;
2use core::ffi::c_char;
3
4use luaur_code_gen::functions::get_assembly::get_assembly;
5use luaur_code_gen::records::assembly_options::AssemblyOptions;
6use luaur_code_gen::records::lowering_stats::LoweringStats;
7use luaur_vm::functions::lua_close::lua_close;
8use luaur_vm::functions::lua_l_newstate::lua_l_newstate;
9use luaur_vm::functions::luau_load::luau_load;
10use luaur_vm::type_aliases::lua_state::lua_State;
11
12struct LuaStateGuard(*mut lua_State);
13
14impl Drop for LuaStateGuard {
15 fn drop(&mut self) {
16 if !self.0.is_null() {
17 unsafe {
18 lua_close(self.0);
19 }
20 }
21 }
22}
23
24pub fn get_codegen_assembly(
25 name: *const c_char,
26 bytecode: &String,
27 options: AssemblyOptions,
28 stats: *mut LoweringStats,
29) -> String {
30 let global_state = LuaStateGuard(lua_l_newstate());
31 let l = global_state.0;
32
33 if unsafe {
34 luau_load(
35 l,
36 name,
37 bytecode.as_ptr() as *const c_char,
38 bytecode.len(),
39 0,
40 )
41 } == 0
42 {
43 unsafe { get_assembly(l, -1, options, stats) }
44 } else {
45 let name = unsafe { core::ffi::CStr::from_ptr(name).to_string_lossy() };
46 eprintln!("Error loading bytecode {}", name);
47 String::new()
48 }
49}