mlua_sys/luau/
luacode.rs

1//! Contains definitions from `luacode.h`.
2
3use std::marker::{PhantomData, PhantomPinned};
4use std::os::raw::{c_char, c_int, c_void};
5use std::{ptr, slice};
6
7#[repr(C)]
8#[non_exhaustive]
9pub struct lua_CompileOptions {
10    pub optimizationLevel: c_int,
11    pub debugLevel: c_int,
12    pub typeInfoLevel: c_int,
13    pub coverageLevel: c_int,
14    pub vectorLib: *const c_char,
15    pub vectorCtor: *const c_char,
16    pub vectorType: *const c_char,
17    pub mutableGlobals: *const *const c_char,
18    pub userdataTypes: *const *const c_char,
19    pub librariesWithKnownMembers: *const *const c_char,
20    pub libraryMemberTypeCallback: Option<lua_LibraryMemberTypeCallback>,
21    pub libraryMemberConstantCallback: Option<lua_LibraryMemberConstantCallback>,
22    pub disabledBuiltins: *const *const c_char,
23}
24
25impl Default for lua_CompileOptions {
26    fn default() -> Self {
27        Self {
28            optimizationLevel: 1,
29            debugLevel: 1,
30            typeInfoLevel: 0,
31            coverageLevel: 0,
32            vectorLib: ptr::null(),
33            vectorCtor: ptr::null(),
34            vectorType: ptr::null(),
35            mutableGlobals: ptr::null(),
36            userdataTypes: ptr::null(),
37            librariesWithKnownMembers: ptr::null(),
38            libraryMemberTypeCallback: None,
39            libraryMemberConstantCallback: None,
40            disabledBuiltins: ptr::null(),
41        }
42    }
43}
44
45#[repr(C)]
46pub struct lua_CompileConstant {
47    _data: [u8; 0],
48    _marker: PhantomData<(*mut u8, PhantomPinned)>,
49}
50
51/// Type table tags
52#[doc(hidden)]
53#[repr(i32)]
54#[non_exhaustive]
55pub enum luau_BytecodeType {
56    Nil = 0,
57    Boolean,
58    Number,
59    String,
60    Table,
61    Function,
62    Thread,
63    UserData,
64    Vector,
65    Buffer,
66
67    Any = 15,
68}
69
70pub type lua_LibraryMemberTypeCallback =
71    unsafe extern "C-unwind" fn(library: *const c_char, member: *const c_char) -> c_int;
72
73pub type lua_LibraryMemberConstantCallback = unsafe extern "C-unwind" fn(
74    library: *const c_char,
75    member: *const c_char,
76    constant: *mut lua_CompileConstant,
77);
78
79extern "C" {
80    pub fn luau_set_compile_constant_nil(cons: *mut lua_CompileConstant);
81    pub fn luau_set_compile_constant_boolean(cons: *mut lua_CompileConstant, b: c_int);
82    pub fn luau_set_compile_constant_number(cons: *mut lua_CompileConstant, n: f64);
83    pub fn luau_set_compile_constant_vector(cons: *mut lua_CompileConstant, x: f32, y: f32, z: f32, w: f32);
84    pub fn luau_set_compile_constant_string(cons: *mut lua_CompileConstant, s: *const c_char, l: usize);
85}
86
87extern "C-unwind" {
88    #[link_name = "luau_compile"]
89    pub fn luau_compile_(
90        source: *const c_char,
91        size: usize,
92        options: *mut lua_CompileOptions,
93        outsize: *mut usize,
94    ) -> *mut c_char;
95}
96
97extern "C" {
98    fn free(p: *mut c_void);
99}
100
101pub unsafe fn luau_compile(source: &[u8], mut options: lua_CompileOptions) -> Vec<u8> {
102    let mut outsize = 0;
103    let data_ptr = luau_compile_(
104        source.as_ptr() as *const c_char,
105        source.len(),
106        &mut options,
107        &mut outsize,
108    );
109    assert!(!data_ptr.is_null(), "luau_compile failed");
110    let data = slice::from_raw_parts(data_ptr as *mut u8, outsize).to_vec();
111    free(data_ptr as *mut c_void);
112    data
113}