@@ -8,39 +8,51 @@ use std::path::{Component, Path, PathBuf};
8
8
use std:: result:: Result as StdResult ;
9
9
use std:: { env, fmt, fs, mem, ptr} ;
10
10
11
- use crate :: error:: Result ;
11
+ use crate :: error:: { Error , Result } ;
12
12
use crate :: function:: Function ;
13
13
use crate :: state:: { callback_error_ext, Lua } ;
14
14
use crate :: table:: Table ;
15
15
use crate :: types:: MaybeSend ;
16
16
17
17
/// An error that can occur during navigation in the Luau `require` system.
18
- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
18
+ #[ cfg( any( feature = "luau" , doc) ) ]
19
+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
20
+ #[ derive( Debug , Clone ) ]
19
21
pub enum NavigateError {
20
22
Ambiguous ,
21
23
NotFound ,
24
+ Other ( Error ) ,
22
25
}
23
26
24
27
#[ cfg( feature = "luau" ) ]
25
28
trait IntoNavigateResult {
26
- fn into_nav_result ( self ) -> ffi:: luarequire_NavigateResult ;
29
+ fn into_nav_result ( self ) -> Result < ffi:: luarequire_NavigateResult > ;
27
30
}
28
31
29
32
#[ cfg( feature = "luau" ) ]
30
33
impl IntoNavigateResult for StdResult < ( ) , NavigateError > {
31
- fn into_nav_result ( self ) -> ffi:: luarequire_NavigateResult {
34
+ fn into_nav_result ( self ) -> Result < ffi:: luarequire_NavigateResult > {
32
35
match self {
33
- Ok ( ( ) ) => ffi:: luarequire_NavigateResult:: Success ,
34
- Err ( NavigateError :: Ambiguous ) => ffi:: luarequire_NavigateResult:: Ambiguous ,
35
- Err ( NavigateError :: NotFound ) => ffi:: luarequire_NavigateResult:: NotFound ,
36
+ Ok ( ( ) ) => Ok ( ffi:: luarequire_NavigateResult:: Success ) ,
37
+ Err ( NavigateError :: Ambiguous ) => Ok ( ffi:: luarequire_NavigateResult:: Ambiguous ) ,
38
+ Err ( NavigateError :: NotFound ) => Ok ( ffi:: luarequire_NavigateResult:: NotFound ) ,
39
+ Err ( NavigateError :: Other ( err) ) => Err ( err) ,
36
40
}
37
41
}
38
42
}
39
43
44
+ impl From < Error > for NavigateError {
45
+ fn from ( err : Error ) -> Self {
46
+ NavigateError :: Other ( err)
47
+ }
48
+ }
49
+
40
50
#[ cfg( feature = "luau" ) ]
41
51
type WriteResult = ffi:: luarequire_WriteResult ;
42
52
43
53
/// A trait for handling modules loading and navigation in the Luau `require` system.
54
+ #[ cfg( any( feature = "luau" , doc) ) ]
55
+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
44
56
pub trait Require : MaybeSend {
45
57
/// Returns `true` if "require" is permitted for the given chunk name.
46
58
fn is_require_allowed ( & self , chunk_name : & str ) -> bool ;
@@ -92,15 +104,17 @@ impl fmt::Debug for dyn Require {
92
104
}
93
105
94
106
/// The standard implementation of Luau `require` navigation.
107
+ #[ doc( hidden) ]
95
108
#[ derive( Default , Debug ) ]
96
- pub ( super ) struct TextRequirer {
109
+ pub struct TextRequirer {
97
110
abs_path : PathBuf ,
98
111
rel_path : PathBuf ,
99
112
module_path : PathBuf ,
100
113
}
101
114
102
115
impl TextRequirer {
103
- pub ( super ) fn new ( ) -> Self {
116
+ /// Creates a new `TextRequirer` instance.
117
+ pub fn new ( ) -> Self {
104
118
Self :: default ( )
105
119
}
106
120
@@ -308,12 +322,12 @@ macro_rules! try_borrow_mut {
308
322
}
309
323
310
324
#[ cfg( feature = "luau" ) ]
311
- pub ( super ) unsafe extern "C" fn init_config ( config : * mut ffi:: luarequire_Configuration ) {
325
+ pub ( super ) unsafe extern "C-unwind " fn init_config ( config : * mut ffi:: luarequire_Configuration ) {
312
326
if config. is_null ( ) {
313
327
return ;
314
328
}
315
329
316
- unsafe extern "C" fn is_require_allowed (
330
+ unsafe extern "C-unwind " fn is_require_allowed (
317
331
state : * mut ffi:: lua_State ,
318
332
ctx : * mut c_void ,
319
333
requirer_chunkname : * const c_char ,
@@ -327,50 +341,58 @@ pub(super) unsafe extern "C" fn init_config(config: *mut ffi::luarequire_Configu
327
341
this. is_require_allowed ( & chunk_name)
328
342
}
329
343
330
- unsafe extern "C" fn reset (
344
+ unsafe extern "C-unwind " fn reset (
331
345
state : * mut ffi:: lua_State ,
332
346
ctx : * mut c_void ,
333
347
requirer_chunkname : * const c_char ,
334
348
) -> ffi:: luarequire_NavigateResult {
335
349
let mut this = try_borrow_mut ! ( state, ctx) ;
336
350
let chunk_name = CStr :: from_ptr ( requirer_chunkname) . to_string_lossy ( ) ;
337
- this. reset ( & chunk_name) . into_nav_result ( )
351
+ callback_error_ext ( state, ptr:: null_mut ( ) , true , move |_, _| {
352
+ this. reset ( & chunk_name) . into_nav_result ( )
353
+ } )
338
354
}
339
355
340
- unsafe extern "C" fn jump_to_alias (
356
+ unsafe extern "C-unwind " fn jump_to_alias (
341
357
state : * mut ffi:: lua_State ,
342
358
ctx : * mut c_void ,
343
359
path : * const c_char ,
344
360
) -> ffi:: luarequire_NavigateResult {
345
361
let mut this = try_borrow_mut ! ( state, ctx) ;
346
362
let path = CStr :: from_ptr ( path) . to_string_lossy ( ) ;
347
- this. jump_to_alias ( & path) . into_nav_result ( )
363
+ callback_error_ext ( state, ptr:: null_mut ( ) , true , move |_, _| {
364
+ this. jump_to_alias ( & path) . into_nav_result ( )
365
+ } )
348
366
}
349
367
350
- unsafe extern "C" fn to_parent (
368
+ unsafe extern "C-unwind " fn to_parent (
351
369
state : * mut ffi:: lua_State ,
352
370
ctx : * mut c_void ,
353
371
) -> ffi:: luarequire_NavigateResult {
354
372
let mut this = try_borrow_mut ! ( state, ctx) ;
355
- this. to_parent ( ) . into_nav_result ( )
373
+ callback_error_ext ( state, ptr:: null_mut ( ) , true , move |_, _| {
374
+ this. to_parent ( ) . into_nav_result ( )
375
+ } )
356
376
}
357
377
358
- unsafe extern "C" fn to_child (
378
+ unsafe extern "C-unwind " fn to_child (
359
379
state : * mut ffi:: lua_State ,
360
380
ctx : * mut c_void ,
361
381
name : * const c_char ,
362
382
) -> ffi:: luarequire_NavigateResult {
363
383
let mut this = try_borrow_mut ! ( state, ctx) ;
364
384
let name = CStr :: from_ptr ( name) . to_string_lossy ( ) ;
365
- this. to_child ( & name) . into_nav_result ( )
385
+ callback_error_ext ( state, ptr:: null_mut ( ) , true , move |_, _| {
386
+ this. to_child ( & name) . into_nav_result ( )
387
+ } )
366
388
}
367
389
368
- unsafe extern "C" fn is_module_present ( state : * mut ffi:: lua_State , ctx : * mut c_void ) -> bool {
390
+ unsafe extern "C-unwind " fn is_module_present ( state : * mut ffi:: lua_State , ctx : * mut c_void ) -> bool {
369
391
let this = try_borrow ! ( state, ctx) ;
370
392
this. has_module ( )
371
393
}
372
394
373
- unsafe extern "C" fn get_chunkname (
395
+ unsafe extern "C-unwind " fn get_chunkname (
374
396
_state : * mut ffi:: lua_State ,
375
397
_ctx : * mut c_void ,
376
398
buffer : * mut c_char ,
@@ -380,7 +402,7 @@ pub(super) unsafe extern "C" fn init_config(config: *mut ffi::luarequire_Configu
380
402
write_to_buffer ( buffer, buffer_size, size_out, & [ ] )
381
403
}
382
404
383
- unsafe extern "C" fn get_loadname (
405
+ unsafe extern "C-unwind " fn get_loadname (
384
406
_state : * mut ffi:: lua_State ,
385
407
_ctx : * mut c_void ,
386
408
buffer : * mut c_char ,
@@ -390,7 +412,7 @@ pub(super) unsafe extern "C" fn init_config(config: *mut ffi::luarequire_Configu
390
412
write_to_buffer ( buffer, buffer_size, size_out, & [ ] )
391
413
}
392
414
393
- unsafe extern "C" fn get_cache_key (
415
+ unsafe extern "C-unwind " fn get_cache_key (
394
416
state : * mut ffi:: lua_State ,
395
417
ctx : * mut c_void ,
396
418
buffer : * mut c_char ,
@@ -402,22 +424,20 @@ pub(super) unsafe extern "C" fn init_config(config: *mut ffi::luarequire_Configu
402
424
write_to_buffer ( buffer, buffer_size, size_out, cache_key. as_bytes ( ) )
403
425
}
404
426
405
- unsafe extern "C" fn is_config_present ( state : * mut ffi:: lua_State , ctx : * mut c_void ) -> bool {
427
+ unsafe extern "C-unwind " fn is_config_present ( state : * mut ffi:: lua_State , ctx : * mut c_void ) -> bool {
406
428
let this = try_borrow ! ( state, ctx) ;
407
429
this. has_config ( )
408
430
}
409
431
410
- unsafe extern "C" fn get_config (
432
+ unsafe extern "C-unwind " fn get_config (
411
433
state : * mut ffi:: lua_State ,
412
434
ctx : * mut c_void ,
413
435
buffer : * mut c_char ,
414
436
buffer_size : usize ,
415
437
size_out : * mut usize ,
416
438
) -> WriteResult {
417
439
let this = try_borrow ! ( state, ctx) ;
418
- let Ok ( config) = this. config ( ) else {
419
- return WriteResult :: Failure ;
420
- } ;
440
+ let config = callback_error_ext ( state, ptr:: null_mut ( ) , true , move |_, _| Ok ( this. config ( ) ?) ) ;
421
441
write_to_buffer ( buffer, buffer_size, size_out, & config)
422
442
}
423
443
@@ -429,7 +449,7 @@ pub(super) unsafe extern "C" fn init_config(config: *mut ffi::luarequire_Configu
429
449
_loadname : * const c_char ,
430
450
) -> c_int {
431
451
let this = try_borrow ! ( state, ctx) ;
432
- callback_error_ext ( state, ptr:: null_mut ( ) , false , move |extra, _| {
452
+ callback_error_ext ( state, ptr:: null_mut ( ) , true , move |extra, _| {
433
453
let rawlua = ( * extra) . raw_lua ( ) ;
434
454
let loader = this. loader ( rawlua. lua ( ) ) ?;
435
455
rawlua. push ( loader) ?;
0 commit comments