File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -428,6 +428,11 @@ impl Compiler {
428428}
429429
430430impl Chunk < ' _ > {
431+ /// Returns the name of this chunk.
432+ pub fn name ( & self ) -> & str {
433+ & self . name
434+ }
435+
431436 /// Sets the name of this chunk, which results in more informative error traces.
432437 ///
433438 /// Possible name prefixes:
@@ -439,6 +444,11 @@ impl Chunk<'_> {
439444 self
440445 }
441446
447+ /// Returns the environment of this chunk.
448+ pub fn environment ( & self ) -> Option < & Table > {
449+ self . env . as_ref ( ) . ok ( ) ?. as_ref ( )
450+ }
451+
442452 /// Sets the environment of the loaded chunk to the given value.
443453 ///
444454 /// In Lua >=5.2 main chunks always have exactly one upvalue, and this upvalue is used as the
@@ -455,6 +465,11 @@ impl Chunk<'_> {
455465 self
456466 }
457467
468+ /// Returns the mode (auto-detected by default) of this chunk.
469+ pub fn mode ( & self ) -> ChunkMode {
470+ self . detect_mode ( )
471+ }
472+
458473 /// Sets whether the chunk is text or binary (autodetected by default).
459474 ///
460475 /// 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 {
10331033 ) -> Chunk < ' a > {
10341034 Chunk {
10351035 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( ) ) ) ,
10371039 env : chunk. environment ( self ) ,
10381040 mode : chunk. mode ( ) ,
10391041 source : chunk. source ( ) ,
Original file line number Diff line number Diff line change 11use std:: { fs, io} ;
22
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+ }
422
523#[ test]
624fn test_chunk_path ( ) -> Result < ( ) > {
You can’t perform that action at this time.
0 commit comments