Skip to content

Commit acb6a30

Browse files
committed
Implmeent call_variadic()
1 parent b3ea4a3 commit acb6a30

File tree

4 files changed

+128
-17
lines changed

4 files changed

+128
-17
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua_actor"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <[email protected]>"]
66
include = ["src/**/*.rs", "Cargo.toml"]

src/actor.rs

+105-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use fp_rust::{
66
sync::{CountDownLatch, Will, WillAsync},
77
};
88
use 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)]
1214
pub 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]
333384
fn 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| {

src/message.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Credits(mainly, except Array & reverse conversions):
33
https://github.com/poga/actix-lua/blob/master/src/message.rs
44
*/
55

6-
use rlua::Result as LuaResult;
7-
use rlua::{Context, FromLua, ToLua, Value};
86
use std::collections::HashMap;
7+
use std::iter::FromIterator;
8+
9+
use rlua::Result as LuaResult;
10+
use rlua::{Context, FromLua, ToLua, Value, Variadic};
911

1012
#[derive(Debug, PartialEq, Clone)]
1113
pub enum LuaMessage {
@@ -64,6 +66,22 @@ impl From<LuaMessage> for Option<String> {
6466
}
6567
}
6668

69+
impl From<Variadic<LuaMessage>> for LuaMessage {
70+
fn from(s: Variadic<LuaMessage>) -> Self {
71+
LuaMessage::from(
72+
s.into_iter()
73+
.map(|v| LuaMessage::from(v.clone()))
74+
.collect::<Vec<LuaMessage>>(),
75+
)
76+
}
77+
}
78+
79+
impl Into<Variadic<LuaMessage>> for LuaMessage {
80+
fn into(self) -> Variadic<LuaMessage> {
81+
return Variadic::from_iter([self]);
82+
}
83+
}
84+
6785
macro_rules! lua_message_number_convert {
6886
($x:ty) => {
6987
impl From<LuaMessage> for Option<$x> {

test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# RUST_BACKTRACE=1 cargo test -- --color always --nocapture
2-
cargo test -- --color always --nocapture
1+
RUST_BACKTRACE=1 cargo test -- --color always --nocapture
2+
# cargo test -- --color always --nocapture

0 commit comments

Comments
 (0)