@@ -128,6 +128,7 @@ pub struct Compiler {
128128 vector_ctor : Option < String > ,
129129 vector_type : Option < String > ,
130130 mutable_globals : Vec < String > ,
131+ userdata_types : Vec < String > ,
131132}
132133
133134#[ cfg( any( feature = "luau" , doc) ) ]
@@ -151,6 +152,7 @@ impl Compiler {
151152 vector_ctor : None ,
152153 vector_type : None ,
153154 mutable_globals : Vec :: new ( ) ,
155+ userdata_types : Vec :: new ( ) ,
154156 }
155157 }
156158
@@ -230,6 +232,13 @@ impl Compiler {
230232 self
231233 }
232234
235+ /// Sets a list of userdata types that will be included in the type information.
236+ #[ must_use]
237+ pub fn set_userdata_types ( mut self , types : Vec < String > ) -> Self {
238+ self . userdata_types = types;
239+ self
240+ }
241+
233242 /// Compiles the `source` into bytecode.
234243 pub fn compile ( & self , source : impl AsRef < [ u8 ] > ) -> Vec < u8 > {
235244 use std:: os:: raw:: c_int;
@@ -245,22 +254,26 @@ impl Compiler {
245254 let vector_type = vector_type. and_then ( |t| CString :: new ( t) . ok ( ) ) ;
246255 let vector_type = vector_type. as_ref ( ) ;
247256
248- let mutable_globals = self
249- . mutable_globals
250- . iter ( )
251- . map ( |name| CString :: new ( name. clone ( ) ) . ok ( ) )
252- . collect :: < Option < Vec < _ > > > ( )
253- . unwrap_or_default ( ) ;
254- let mut mutable_globals = mutable_globals
255- . iter ( )
256- . map ( |s| s. as_ptr ( ) )
257- . collect :: < Vec < _ > > ( ) ;
258- let mut mutable_globals_ptr = ptr:: null ( ) ;
259- if !mutable_globals. is_empty ( ) {
260- mutable_globals. push ( ptr:: null ( ) ) ;
261- mutable_globals_ptr = mutable_globals. as_ptr ( ) ;
257+ macro_rules! vec2cstring_ptr {
258+ ( $name: ident, $name_ptr: ident) => {
259+ let $name = self
260+ . $name
261+ . iter( )
262+ . map( |name| CString :: new( name. clone( ) ) . ok( ) )
263+ . collect:: <Option <Vec <_>>>( )
264+ . unwrap_or_default( ) ;
265+ let mut $name = $name. iter( ) . map( |s| s. as_ptr( ) ) . collect:: <Vec <_>>( ) ;
266+ let mut $name_ptr = ptr:: null( ) ;
267+ if !$name. is_empty( ) {
268+ $name. push( ptr:: null( ) ) ;
269+ $name_ptr = $name. as_ptr( ) ;
270+ }
271+ } ;
262272 }
263273
274+ vec2cstring_ptr ! ( mutable_globals, mutable_globals_ptr) ;
275+ vec2cstring_ptr ! ( userdata_types, userdata_types_ptr) ;
276+
264277 unsafe {
265278 let mut options = ffi:: lua_CompileOptions:: default ( ) ;
266279 options. optimizationLevel = self . optimization_level as c_int ;
@@ -271,6 +284,7 @@ impl Compiler {
271284 options. vectorCtor = vector_ctor. map_or ( ptr:: null ( ) , |s| s. as_ptr ( ) ) ;
272285 options. vectorType = vector_type. map_or ( ptr:: null ( ) , |s| s. as_ptr ( ) ) ;
273286 options. mutableGlobals = mutable_globals_ptr;
287+ options. userdataTypes = userdata_types_ptr;
274288 ffi:: luau_compile ( source. as_ref ( ) , options)
275289 }
276290 }
0 commit comments