@@ -128,6 +128,7 @@ pub struct Compiler {
128
128
vector_ctor : Option < String > ,
129
129
vector_type : Option < String > ,
130
130
mutable_globals : Vec < String > ,
131
+ userdata_types : Vec < String > ,
131
132
}
132
133
133
134
#[ cfg( any( feature = "luau" , doc) ) ]
@@ -151,6 +152,7 @@ impl Compiler {
151
152
vector_ctor : None ,
152
153
vector_type : None ,
153
154
mutable_globals : Vec :: new ( ) ,
155
+ userdata_types : Vec :: new ( ) ,
154
156
}
155
157
}
156
158
@@ -230,6 +232,13 @@ impl Compiler {
230
232
self
231
233
}
232
234
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
+
233
242
/// Compiles the `source` into bytecode.
234
243
pub fn compile ( & self , source : impl AsRef < [ u8 ] > ) -> Vec < u8 > {
235
244
use std:: os:: raw:: c_int;
@@ -245,22 +254,26 @@ impl Compiler {
245
254
let vector_type = vector_type. and_then ( |t| CString :: new ( t) . ok ( ) ) ;
246
255
let vector_type = vector_type. as_ref ( ) ;
247
256
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
+ } ;
262
272
}
263
273
274
+ vec2cstring_ptr ! ( mutable_globals, mutable_globals_ptr) ;
275
+ vec2cstring_ptr ! ( userdata_types, userdata_types_ptr) ;
276
+
264
277
unsafe {
265
278
let mut options = ffi:: lua_CompileOptions:: default ( ) ;
266
279
options. optimizationLevel = self . optimization_level as c_int ;
@@ -271,6 +284,7 @@ impl Compiler {
271
284
options. vectorCtor = vector_ctor. map_or ( ptr:: null ( ) , |s| s. as_ptr ( ) ) ;
272
285
options. vectorType = vector_type. map_or ( ptr:: null ( ) , |s| s. as_ptr ( ) ) ;
273
286
options. mutableGlobals = mutable_globals_ptr;
287
+ options. userdataTypes = userdata_types_ptr;
274
288
ffi:: luau_compile ( source. as_ref ( ) , options)
275
289
}
276
290
}
0 commit comments