Skip to main content

rs_luau/ffi/
luaucode.rs

1use std::ffi::{c_char, c_int};
2use std::ffi::{c_double, c_float, c_void};
3use std::ptr::{self, null};
4
5#[allow(non_camel_case_types)]
6#[repr(C)]
7#[derive(Debug, Clone, Copy)]
8pub enum LuauBytecodeType {
9    LBC_TYPE_NIL = 0,
10    LBC_TYPE_BOOLEAN,
11    LBC_TYPE_NUMBER,
12    LBC_TYPE_STRING,
13    LBC_TYPE_TABLE,
14    LBC_TYPE_FUNCTION,
15    LBC_TYPE_THREAD,
16    LBC_TYPE_USERDATA,
17    LBC_TYPE_VECTOR,
18    LBC_TYPE_BUFFER,
19
20    LBC_TYPE_ANY = 15,
21
22    LBC_TYPE_TAGGED_USERDATA_BASE = 64,
23    LBC_TYPE_TAGGED_USERDATA_END = 64 + 32,
24
25    LBC_TYPE_OPTIONAL_BIT = 1 << 7,
26
27    LBC_TYPE_INVALID = 256,
28}
29
30pub type LuauCompilerConstant = *mut c_void;
31
32/// return a type identifier for a global library member
33pub type LuauLibraryMemberTypeCallback =
34    unsafe extern "C-unwind" fn(library: *const c_char, member: *const c_char) -> LuauBytecodeType;
35
36// setup a value of a constant for a global library member
37// use luau_set_compile_constant_*** set of functions for values
38pub type LuauLibraryMemberConstantCallback = unsafe extern "C-unwind" fn(
39    library: *const c_char,
40    member: *const c_char,
41    constant: LuauCompilerConstant,
42);
43
44#[repr(C)]
45#[allow(non_snake_case)]
46pub struct LuauCompileOptions {
47    /// Determines the degree of optimizations the compiler will do
48    ///
49    /// 0. No optimizations
50    /// 1. Optimizations which will not impact debuggability
51    /// 2. All optimizations in level 1 plus optimizations that harm debuggability such as inlining
52    pub optimizationLevel: c_int,
53
54    /// Determiens the degree to which debugging information will be included
55    ///
56    /// 0. No debug information
57    /// 1. Line info & function names only; sufficient for backtraces
58    /// 2. Full debug info with local & upvalue names; necessary for debugger
59    pub debugLevel: c_int,
60
61    /// Type information is used to guide native code generation decisions
62    ///
63    /// Information includes testable types for function arguments, locals, upvalues and some temporaries
64    ///
65    /// 0. generate for native modules
66    /// 1. generate for all modules
67    pub typeInfoLevel: c_int,
68
69    /// Determines the degree to which converage information should be included into the bytecode
70    pub coverageLevel: c_int,
71
72    /// Global library to construct vectors; disabled by default
73    pub vectorLib: *const c_char,
74
75    /// Global builtin to construct vectors; disabled by default
76    pub vectorCtor: *const c_char,
77
78    /// Vector typename for type tables; disabled by default
79    pub vectorType: *const c_char,
80
81    /// `NULL`-terminated array of globals that are mutable; disables import optimizations for fields accessed through these
82    pub mutableGlobals: *const *const c_char,
83
84    /// `NULL`-terminated array of userdata types which will be included in the type information
85    pub userdataTypes: *const *const c_char,
86
87    /// null-terminated array of globals which act as libraries and have members with known type and/or constant value
88    /// when an import of one of these libraries is accessed, library_member_type_callback and library_member_constant_callback below will be called to receive that information
89    pub librariesWithKnownMembers: *const *const c_char,
90    pub libraryMemberTypeCallback: Option<LuauLibraryMemberTypeCallback>,
91    pub libraryMemberConstantCallback: Option<LuauLibraryMemberConstantCallback>,
92
93    /// `NULL`-terminated array of builtins which will not be compiled into a fastcall ("name", "lib.name")
94    pub disabledBuiltins: *const *const c_char,
95}
96
97impl Default for LuauCompileOptions {
98    fn default() -> Self {
99        Self {
100            optimizationLevel: 1,
101            debugLevel: 1,
102            typeInfoLevel: 0,
103            coverageLevel: 0,
104            vectorLib: ptr::null(),
105            vectorCtor: ptr::null(),
106            vectorType: ptr::null(),
107            mutableGlobals: ptr::null(),
108            userdataTypes: ptr::null(),
109            librariesWithKnownMembers: null(),
110            libraryMemberTypeCallback: None,
111            libraryMemberConstantCallback: None,
112            disabledBuiltins: null(),
113        }
114    }
115}
116
117extern "C-unwind" {
118    /// Compiles Luau source into Luau bytecode.
119    ///
120    /// This code will not hold references to the source code after calling allowing it to be freed.
121    ///
122    /// This code will set the value at `outsize` if it is not NULL
123    ///
124    /// This will return NULL on an allocation error and an error encoded in the resultant bytecode string on a compilation error
125    pub fn luau_compile(
126        source: *const c_char,
127        size: usize,
128        options: *mut LuauCompileOptions,
129        outsize: *mut usize,
130    ) -> *mut c_char;
131}
132
133extern "C-unwind" {
134    /// Sets a constant nil
135    pub fn luau_set_compile_constant_nil(constant: LuauCompilerConstant);
136    /// Sets a constant boolean
137    pub fn luau_set_compile_constant_boolean(constant: LuauCompilerConstant, b: c_int);
138    /// Sets a constant number
139    pub fn luau_set_compile_constant_number(constant: LuauCompilerConstant, n: c_double);
140    /// Sets a constant vector
141    ///
142    /// Vector component 'w' is not visible to VM runtime configured with LUA_VECTOR_SIZE == 3, but can affect constant folding during compilation
143    pub fn luau_set_compile_constant_vector(
144        constant: LuauCompilerConstant,
145        x: c_float,
146        y: c_float,
147        z: c_float,
148        w: c_float,
149    );
150    /// String storage must outlive the invocation of 'luau_compile' which used the callback
151    pub fn luau_set_compile_constant_string(
152        constant: LuauCompilerConstant,
153        s: *const c_char,
154        l: usize,
155    );
156}
157
158extern "C" {
159    #[link_name = "free"]
160    pub fn cstdlib_free(ptr: *mut c_void);
161}