File tree 3 files changed +37
-2
lines changed
3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -428,6 +428,11 @@ impl Compiler {
428
428
}
429
429
430
430
impl Chunk < ' _ > {
431
+ /// Returns the name of this chunk.
432
+ pub fn name ( & self ) -> & str {
433
+ & self . name
434
+ }
435
+
431
436
/// Sets the name of this chunk, which results in more informative error traces.
432
437
///
433
438
/// Possible name prefixes:
@@ -439,6 +444,11 @@ impl Chunk<'_> {
439
444
self
440
445
}
441
446
447
+ /// Returns the environment of this chunk.
448
+ pub fn environment ( & self ) -> Option < & Table > {
449
+ self . env . as_ref ( ) . ok ( ) ?. as_ref ( )
450
+ }
451
+
442
452
/// Sets the environment of the loaded chunk to the given value.
443
453
///
444
454
/// In Lua >=5.2 main chunks always have exactly one upvalue, and this upvalue is used as the
@@ -455,6 +465,11 @@ impl Chunk<'_> {
455
465
self
456
466
}
457
467
468
+ /// Returns the mode (auto-detected by default) of this chunk.
469
+ pub fn mode ( & self ) -> ChunkMode {
470
+ self . detect_mode ( )
471
+ }
472
+
458
473
/// Sets whether the chunk is text or binary (autodetected by default).
459
474
///
460
475
/// Be aware, Lua does not check the consistency of the code inside binary chunks.
Original file line number Diff line number Diff line change @@ -1033,7 +1033,9 @@ impl Lua {
1033
1033
) -> Chunk < ' a > {
1034
1034
Chunk {
1035
1035
lua : self . weak ( ) ,
1036
- name : chunk. name ( ) . unwrap_or_else ( || location. to_string ( ) ) ,
1036
+ name : chunk
1037
+ . name ( )
1038
+ . unwrap_or_else ( || format ! ( "@{}:{}" , location. file( ) , location. line( ) ) ) ,
1037
1039
env : chunk. environment ( self ) ,
1038
1040
mode : chunk. mode ( ) ,
1039
1041
source : chunk. source ( ) ,
Original file line number Diff line number Diff line change 1
1
use std:: { fs, io} ;
2
2
3
- use mlua:: { Chunk , Lua , Result } ;
3
+ use mlua:: { Chunk , ChunkMode , Lua , Result } ;
4
+
5
+ #[ test]
6
+ fn test_chunk_methods ( ) -> Result < ( ) > {
7
+ let lua = Lua :: new ( ) ;
8
+
9
+ #[ cfg( unix) ]
10
+ assert ! ( lua. load( "return 123" ) . name( ) . starts_with( "@tests/chunk.rs" ) ) ;
11
+ let chunk2 = lua. load ( "return 123" ) . set_name ( "@new_name" ) ;
12
+ assert_eq ! ( chunk2. name( ) , "@new_name" ) ;
13
+
14
+ let env = lua. create_table_from ( [ ( "a" , 987 ) ] ) ?;
15
+ let chunk3 = lua. load ( "return a" ) . set_environment ( env. clone ( ) ) ;
16
+ assert_eq ! ( chunk3. environment( ) . unwrap( ) , & env) ;
17
+ assert_eq ! ( chunk3. mode( ) , ChunkMode :: Text ) ;
18
+ assert_eq ! ( chunk3. call:: <i32 >( ( ) ) ?, 987 ) ;
19
+
20
+ Ok ( ( ) )
21
+ }
4
22
5
23
#[ test]
6
24
fn test_chunk_path ( ) -> Result < ( ) > {
You can’t perform that action at this time.
0 commit comments