@@ -6,7 +6,9 @@ use fp_rust::{
66 sync:: { CountDownLatch , Will , WillAsync } ,
77} ;
88use message:: LuaMessage ;
9- use rlua:: { Chunk , Context , Error , FromLua , FromLuaMulti , Function , Lua , Table , ToLua , ToLuaMulti } ;
9+ use rlua:: {
10+ Chunk , Context , Error , FromLua , FromLuaMulti , Function , Lua , Table , ToLua , ToLuaMulti , Variadic ,
11+ } ;
1012
1113#[ derive( Clone ) ]
1214pub struct Actor {
@@ -282,43 +284,91 @@ impl Actor {
282284 lua. load ( source) . eval ( )
283285 }
284286
285- pub fn call ( & self , name : & ' static str , args : LuaMessage ) -> Result < LuaMessage , Error > {
287+ pub fn call_one ( & self , name : & ' static str , args : LuaMessage ) -> Result < LuaMessage , Error > {
288+ match self . handler . clone ( ) {
289+ Some ( _handler) => {
290+ let lua = self . lua . clone ( ) ;
291+ self . wait_async_lua_message_result ( & _handler, move || {
292+ Self :: _call_one ( & lua, name, args. clone ( ) )
293+ } )
294+ }
295+ None => Self :: _call_one ( & self . lua . clone ( ) , name, args) ,
296+ }
297+ }
298+ pub fn call_one_nowait ( & self , name : & ' static str , args : LuaMessage ) -> Result < ( ) , Error > {
299+ match self . handler . clone ( ) {
300+ Some ( _handler) => {
301+ let lua = self . lua . clone ( ) ;
302+ _handler. lock ( ) . unwrap ( ) . post ( RawFunc :: new ( move || {
303+ let _ = Self :: _call_one ( & lua. clone ( ) , name, args. clone ( ) ) ;
304+ } ) ) ;
305+ }
306+ None => {
307+ Self :: _call_one ( & self . lua . clone ( ) , name, args) ?;
308+ }
309+ }
310+ Ok ( ( ) )
311+ }
312+ #[ inline]
313+ fn _call_one ( lua : & Arc < Mutex < Lua > > , name : & str , args : LuaMessage ) -> Result < LuaMessage , Error > {
314+ let vm = lua. lock ( ) . unwrap ( ) ;
315+ vm. context ( |lua| {
316+ lua. globals ( )
317+ . get :: < _ , Function > ( name)
318+ . unwrap ( )
319+ . call :: < _ , _ > ( args)
320+ } )
321+ }
322+ pub fn call_variadic (
323+ & self ,
324+ name : & ' static str ,
325+ args : Variadic < LuaMessage > ,
326+ ) -> Result < LuaMessage , Error > {
286327 match self . handler . clone ( ) {
287328 Some ( _handler) => {
288329 let lua = self . lua . clone ( ) ;
289330 self . wait_async_lua_message_result ( & _handler, move || {
290- Self :: _call ( & lua, name, args. clone ( ) )
331+ Self :: _call_variadic ( & lua, name, args. clone ( ) )
291332 } )
292333 }
293- None => Self :: _call ( & self . lua . clone ( ) , name, args) ,
334+ None => Self :: _call_variadic ( & self . lua . clone ( ) , name, args) ,
294335 }
295336 }
296- pub fn call_nowait ( & self , name : & ' static str , args : LuaMessage ) -> Result < ( ) , Error > {
337+ pub fn call_variadic_nowait (
338+ & self ,
339+ name : & ' static str ,
340+ args : Variadic < LuaMessage > ,
341+ ) -> Result < ( ) , Error > {
297342 match self . handler . clone ( ) {
298343 Some ( _handler) => {
299344 let lua = self . lua . clone ( ) ;
300345 _handler. lock ( ) . unwrap ( ) . post ( RawFunc :: new ( move || {
301- let _ = Self :: _call ( & lua. clone ( ) , name, args. clone ( ) ) ;
346+ let _ = Self :: _call_variadic ( & lua. clone ( ) , name, args. clone ( ) ) ;
302347 } ) ) ;
303348 }
304349 None => {
305- Self :: _call ( & self . lua . clone ( ) , name, args) ?;
350+ Self :: _call_variadic ( & self . lua . clone ( ) , name, args) ?;
306351 }
307352 }
308353 Ok ( ( ) )
309354 }
310355 #[ inline]
311- fn _call ( lua : & Arc < Mutex < Lua > > , name : & str , args : LuaMessage ) -> Result < LuaMessage , Error > {
356+ fn _call_variadic (
357+ lua : & Arc < Mutex < Lua > > ,
358+ name : & str ,
359+ args : Variadic < LuaMessage > ,
360+ ) -> Result < LuaMessage , Error > {
312361 let vm = lua. lock ( ) . unwrap ( ) ;
313362 vm. context ( |lua| {
314363 lua. globals ( )
315364 . get :: < _ , Function > ( name)
316365 . unwrap ( )
317- . call :: < _ , LuaMessage > ( args)
366+ . call :: < _ , _ > ( args)
318367 } )
319368 }
369+ /*
320370 #[inline]
321- pub fn call_multi < ' lua , A , R > ( lua : Context < ' lua > , name : & str , args : A ) -> Result < R , Error >
371+ pub fn call_variadic <'lua, A, R>(lua: Context<'lua>, name: &str, args: A) -> Result<R, Error>
322372 where
323373 A: ToLuaMulti<'lua> + Send + Sync + Clone + 'static,
324374 R: FromLuaMulti<'lua>,
@@ -327,11 +377,12 @@ impl Actor {
327377
328378 func.call::<_, R>(args)
329379 }
380+ // */
330381}
331382
332383#[ test]
333384fn test_actor_new ( ) {
334- use rlua :: Variadic ;
385+ use std :: iter :: FromIterator ;
335386
336387 fn test_actor ( act : Actor ) {
337388 let _ = act. exec_nowait (
@@ -358,7 +409,7 @@ fn test_actor_new() {
358409 )
359410 . ok ( )
360411 . unwrap ( ) ;
361- match act. call ( "testit" , LuaMessage :: from ( 1 ) ) {
412+ match act. call_one ( "testit" , LuaMessage :: from ( 1 ) ) {
362413 Ok ( _v) => {
363414 assert_eq ! ( Some ( 2 ) , Option :: from( _v) ) ;
364415 }
@@ -367,6 +418,48 @@ fn test_actor_new() {
367418 std:: panic:: panic_any ( _err) ;
368419 }
369420 }
421+ act. exec (
422+ r#"
423+ function testlist (vlist)
424+ return #vlist
425+ end
426+ "# ,
427+ )
428+ . ok ( )
429+ . unwrap ( ) ;
430+ match act. call_one (
431+ "testlist" ,
432+ Vec :: < LuaMessage > :: from_iter ( [ 3 . into ( ) , 2 . into ( ) ] ) . into ( ) ,
433+ ) {
434+ Ok ( _v) => {
435+ assert_eq ! ( Some ( 2 ) , Option :: from( _v) ) ;
436+ }
437+ Err ( _err) => {
438+ println ! ( "{:?}" , _err) ;
439+ std:: panic:: panic_any ( _err) ;
440+ }
441+ } ;
442+ act. exec (
443+ r#"
444+ function testvargs (v1, v2)
445+ return v1 + v2
446+ end
447+ "# ,
448+ )
449+ . ok ( )
450+ . unwrap ( ) ;
451+ match act. call_variadic (
452+ "testvargs" ,
453+ Variadic :: < LuaMessage > :: from_iter ( [ 6 . into ( ) , 7 . into ( ) ] ) . into ( ) ,
454+ ) {
455+ Ok ( _v) => {
456+ assert_eq ! ( Some ( 13 ) , Option :: from( _v) ) ;
457+ }
458+ Err ( _err) => {
459+ println ! ( "{:?}" , _err) ;
460+ std:: panic:: panic_any ( _err) ;
461+ }
462+ } ;
370463
371464 {
372465 act. lua . lock ( ) . unwrap ( ) . context ( |lua| {
0 commit comments