1- use mlua:: { Error , Function , Lua , Result , String , Table } ;
1+ use mlua:: { Error , Function , Lua , Result , String , Table , Variadic } ;
22
33#[ test]
4- fn test_function ( ) -> Result < ( ) > {
4+ fn test_function_call ( ) -> Result < ( ) > {
55 let lua = Lua :: new ( ) ;
66
7- let globals = lua. globals ( ) ;
8- lua. load (
9- r#"
10- function concat(arg1, arg2)
11- return arg1 .. arg2
12- end
13- "# ,
14- )
15- . exec ( ) ?;
16-
17- let concat = globals. get :: < Function > ( "concat" ) ?;
7+ let concat = lua
8+ . load ( r#"function(arg1, arg2) return arg1 .. arg2 end"# )
9+ . eval :: < Function > ( ) ?;
1810 assert_eq ! ( concat. call:: <String >( ( "foo" , "bar" ) ) ?, "foobar" ) ;
1911
2012 Ok ( ( ) )
2113}
2214
2315#[ test]
24- fn test_bind ( ) -> Result < ( ) > {
16+ fn test_function_call_error ( ) -> Result < ( ) > {
17+ let lua = Lua :: new ( ) ;
18+
19+ let concat_err = lua
20+ . load ( r#"function(arg1, arg2) error("concat error") end"# )
21+ . eval :: < Function > ( ) ?;
22+ match concat_err. call :: < String > ( ( "foo" , "bar" ) ) {
23+ Err ( Error :: RuntimeError ( msg) ) if msg. contains ( "concat error" ) => { }
24+ other => panic ! ( "unexpected result: {other:?}" ) ,
25+ }
26+
27+ Ok ( ( ) )
28+ }
29+
30+ #[ test]
31+ fn test_function_bind ( ) -> Result < ( ) > {
2532 let lua = Lua :: new ( ) ;
2633
2734 let globals = lua. globals ( ) ;
@@ -54,74 +61,29 @@ fn test_bind() -> Result<()> {
5461}
5562
5663#[ test]
57- fn test_rust_function ( ) -> Result < ( ) > {
64+ #[ cfg( not( target_arch = "wasm32" ) ) ]
65+ fn test_function_bind_error ( ) -> Result < ( ) > {
5866 let lua = Lua :: new ( ) ;
5967
60- let globals = lua. globals ( ) ;
61- lua. load (
62- r#"
63- function lua_function()
64- return rust_function()
65- end
66-
67- -- Test to make sure chunk return is ignored
68- return 1
69- "# ,
70- )
71- . exec ( ) ?;
72-
73- let lua_function = globals. get :: < Function > ( "lua_function" ) ?;
74- let rust_function = lua. create_function ( |_, ( ) | Ok ( "hello" ) ) ?;
75-
76- globals. set ( "rust_function" , rust_function) ?;
77- assert_eq ! ( lua_function. call:: <String >( ( ) ) ?, "hello" ) ;
78-
79- Ok ( ( ) )
80- }
81-
82- #[ test]
83- fn test_c_function ( ) -> Result < ( ) > {
84- let lua = Lua :: new ( ) ;
85-
86- unsafe extern "C-unwind" fn c_function ( state : * mut mlua:: lua_State ) -> std:: os:: raw:: c_int {
87- ffi:: lua_pushboolean ( state, 1 ) ;
88- ffi:: lua_setglobal ( state, b"c_function\0 " as * const _ as * const _ ) ;
89- 0
90- }
91-
92- let func = unsafe { lua. create_c_function ( c_function) ? } ;
93- func. call :: < ( ) > ( ( ) ) ?;
94- assert_eq ! ( lua. globals( ) . get:: <bool >( "c_function" ) ?, true ) ;
95-
96- Ok ( ( ) )
97- }
98-
99- #[ cfg( not( feature = "luau" ) ) ]
100- #[ test]
101- fn test_dump ( ) -> Result < ( ) > {
102- let lua = unsafe { Lua :: unsafe_new ( ) } ;
103-
104- let concat_lua = lua
105- . load ( r#"function(arg1, arg2) return arg1 .. arg2 end"# )
106- . eval :: < Function > ( ) ?;
107- let concat = lua. load ( & concat_lua. dump ( false ) ) . into_function ( ) ?;
108-
109- assert_eq ! ( concat. call:: <String >( ( "foo" , "bar" ) ) ?, "foobar" ) ;
68+ let func = lua. load ( r#"function(...) end"# ) . eval :: < Function > ( ) ?;
69+ assert ! ( func. bind( Variadic :: from_iter( 1 ..1000000 ) ) . is_err( ) ) ;
70+ assert ! ( func. call:: <( ) >( Variadic :: from_iter( 1 ..1000000 ) ) . is_err( ) ) ;
11071
11172 Ok ( ( ) )
11273}
11374
11475#[ test]
11576fn test_function_environment ( ) -> Result < ( ) > {
11677 let lua = Lua :: new ( ) ;
78+ let globals = lua. globals ( ) ;
11779
11880 // We must not get or set environment for C functions
11981 let rust_func = lua. create_function ( |_, ( ) | Ok ( "hello" ) ) ?;
12082 assert_eq ! ( rust_func. environment( ) , None ) ;
121- assert_eq ! ( rust_func. set_environment( lua . globals( ) ) . ok( ) , Some ( false ) ) ;
83+ assert_eq ! ( rust_func. set_environment( globals. clone ( ) ) . ok( ) , Some ( false ) ) ;
12284
12385 // Test getting Lua function environment
124- lua . globals ( ) . set ( "hello" , "global" ) ?;
86+ globals. set ( "hello" , "global" ) ?;
12587 let lua_func = lua
12688 . load (
12789 r#"
@@ -135,7 +97,7 @@ fn test_function_environment() -> Result<()> {
13597 . eval :: < Function > ( ) ?;
13698 let lua_func2 = lua. load ( "return hello" ) . into_function ( ) ?;
13799 assert_eq ! ( lua_func. call:: <String >( ( ) ) ?, "global" ) ;
138- assert_eq ! ( lua_func. environment( ) , Some ( lua . globals( ) ) ) ;
100+ assert_eq ! ( lua_func. environment( ) . as_ref ( ) , Some ( & globals) ) ;
139101
140102 // Test changing the environment
141103 let env = lua. create_table_from ( [ ( "hello" , "local" ) ] ) ?;
@@ -154,9 +116,9 @@ fn test_function_environment() -> Result<()> {
154116 "# ,
155117 )
156118 . exec ( ) ?;
157- let lucky = lua . globals ( ) . get :: < Function > ( "lucky" ) ?;
119+ let lucky = globals. get :: < Function > ( "lucky" ) ?;
158120 assert_eq ! ( lucky. call:: <String >( ( ) ) ?, "number is 15" ) ;
159- let new_env = lua . globals ( ) . get :: < Table > ( "new_env" ) ?;
121+ let new_env = globals. get :: < Table > ( "new_env" ) ?;
160122 lucky. set_environment ( new_env) ?;
161123 assert_eq ! ( lucky. call:: <String >( ( ) ) ?, "15" ) ;
162124
@@ -235,6 +197,95 @@ fn test_function_info() -> Result<()> {
235197 Ok ( ( ) )
236198}
237199
200+ #[ cfg( not( feature = "luau" ) ) ]
201+ #[ test]
202+ fn test_function_dump ( ) -> Result < ( ) > {
203+ let lua = unsafe { Lua :: unsafe_new ( ) } ;
204+
205+ let concat_lua = lua
206+ . load ( r#"function(arg1, arg2) return arg1 .. arg2 end"# )
207+ . eval :: < Function > ( ) ?;
208+ let concat = lua. load ( & concat_lua. dump ( false ) ) . into_function ( ) ?;
209+
210+ assert_eq ! ( concat. call:: <String >( ( "foo" , "bar" ) ) ?, "foobar" ) ;
211+
212+ Ok ( ( ) )
213+ }
214+
215+ #[ cfg( feature = "luau" ) ]
216+ #[ test]
217+ fn test_finction_coverage ( ) -> Result < ( ) > {
218+ let lua = Lua :: new ( ) ;
219+
220+ lua. set_compiler ( mlua:: Compiler :: default ( ) . set_coverage_level ( 1 ) ) ;
221+
222+ let f = lua
223+ . load (
224+ r#"local s = "abc"
225+ assert(#s == 3)
226+
227+ function abc(i)
228+ if i < 5 then
229+ return 0
230+ else
231+ return 1
232+ end
233+ end
234+
235+ (function()
236+ (function() abc(10) end)()
237+ end)()
238+ "# ,
239+ )
240+ . into_function ( ) ?;
241+
242+ f. call :: < ( ) > ( ( ) ) ?;
243+
244+ let mut report = Vec :: new ( ) ;
245+ f. coverage ( |cov| {
246+ report. push ( cov) ;
247+ } ) ;
248+
249+ assert_eq ! (
250+ report[ 0 ] ,
251+ mlua:: CoverageInfo {
252+ function: None ,
253+ line_defined: 1 ,
254+ depth: 0 ,
255+ hits: vec![ -1 , 1 , 1 , -1 , 1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 1 , -1 , -1 , -1 ] ,
256+ }
257+ ) ;
258+ assert_eq ! (
259+ report[ 1 ] ,
260+ mlua:: CoverageInfo {
261+ function: Some ( "abc" . into( ) ) ,
262+ line_defined: 4 ,
263+ depth: 1 ,
264+ hits: vec![ -1 , -1 , -1 , -1 , -1 , 1 , 0 , -1 , 1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ] ,
265+ }
266+ ) ;
267+ assert_eq ! (
268+ report[ 2 ] ,
269+ mlua:: CoverageInfo {
270+ function: None ,
271+ line_defined: 12 ,
272+ depth: 1 ,
273+ hits: vec![ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 1 , -1 , -1 ] ,
274+ }
275+ ) ;
276+ assert_eq ! (
277+ report[ 3 ] ,
278+ mlua:: CoverageInfo {
279+ function: None ,
280+ line_defined: 13 ,
281+ depth: 2 ,
282+ hits: vec![ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 1 , -1 , -1 ] ,
283+ }
284+ ) ;
285+
286+ Ok ( ( ) )
287+ }
288+
238289#[ test]
239290fn test_function_pointer ( ) -> Result < ( ) > {
240291 let lua = Lua :: new ( ) ;
0 commit comments