Skip to content

Commit bad0fa9

Browse files
committed
Add get_global_table() and fix some testing showcase.
1 parent 5d2f15f commit bad0fa9

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua_actor"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <johnteee@gmail.com>"]
66
include = ["src/**/*.rs", "Cargo.toml"]

README.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,63 +57,88 @@ extern crate lua_actor;
5757

5858
fn main() {
5959

60-
use fp_rust::handler::{
61-
HandlerThread,
62-
};
6360
use rlua::{Variadic};
6461
use lua_actor::{actor::Actor, message::LuaMessage};
6562

63+
6664
fn test_actor(act: Actor) {
67-
let _ = act.exec_nowait(r#"
65+
let _ = act.exec_nowait(
66+
r#"
6867
i = 1
69-
"#, None);
68+
"#,
69+
None,
70+
);
7071
assert_eq!(Some(1), Option::from(act.get_global("i").ok().unwrap()));
7172

72-
let v = act.eval(r#"
73+
let v = act.eval(
74+
r#"
7375
3
74-
"#, None);
76+
"#,
77+
None,
78+
);
7579
assert_eq!(Some(3), Option::from(v.ok().unwrap()));
7680

77-
act.exec(r#"
81+
act.exec(
82+
r#"
7883
function testit (i)
7984
return i + 1
8085
end
81-
"#, None).ok().unwrap();
86+
"#,
87+
None,
88+
).ok().unwrap();
8289
match act.call("testit", LuaMessage::from(1)) {
8390
Ok(_v) => {
8491
assert_eq!(Some(2), Option::from(_v));
85-
},
92+
}
8693
Err(_err) => {
8794
println!("{:?}", _err);
8895
panic!(_err);
89-
},
96+
}
9097
}
9198

9299
{
93-
let vm = act.lua();
94-
let vm = vm.lock().unwrap();
95-
Actor::def_fn_with_name(&vm, |_, (list1, list2): (Vec<String>, Vec<String>)| {
100+
act.def_fn_with_name_nowait(|_, (list1, list2): (Vec<String>, Vec<String>)| {
96101
// This function just checks whether two string lists are equal, and in an inefficient way.
97102
// Lua callbacks return `rlua::Result`, an Ok value is a normal return, and an Err return
98103
// turns into a Lua 'error'. Again, any type that is convertible to lua may be returned.
99104
Ok(list1 == list2)
100105
}, "check_equal").ok().unwrap();
101-
Actor::def_fn_with_name(&vm, |_, strings: Variadic<String>| {
102-
// (This is quadratic!, it's just an example!)
103-
Ok(strings.iter().fold("".to_owned(), |a, b| a + b))
104-
}, "join").ok().unwrap();
106+
act.def_fn_with_name_nowait(
107+
|_, strings: Variadic<String>| {
108+
// (This is quadratic!, it's just an example!)
109+
Ok(strings.iter().fold("".to_owned(), |a, b| a + b))
110+
},
111+
"join",
112+
).ok()
113+
.unwrap();
105114
assert_eq!(
106-
vm.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#, None).ok().unwrap(),
115+
Option::<bool>::from(
116+
act.eval(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#, None)
117+
.ok()
118+
.unwrap()
119+
).unwrap(),
107120
true
108121
);
109122
assert_eq!(
110-
vm.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#, None).ok().unwrap(),
123+
Option::<bool>::from(
124+
act.eval(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#, None)
125+
.ok()
126+
.unwrap()
127+
).unwrap(),
111128
false
112129
);
113-
assert_eq!(vm.eval::<String>(r#"join("a", "b", "c")"#, None).ok().unwrap(), "abc");
130+
assert_eq!(
131+
Option::<String>::from(act.eval(r#"join("a", "b", "c")"#, None).ok().unwrap())
132+
.unwrap(),
133+
"abc"
134+
);
114135
}
115136

116-
act.set_global("arr1", LuaMessage::from(vec!(LuaMessage::from(1), LuaMessage::from(2)))).ok().unwrap();
137+
act.set_global(
138+
"arr1",
139+
LuaMessage::from(vec![LuaMessage::from(1), LuaMessage::from(2)]),
140+
).ok()
141+
.unwrap();
117142

118143
let v = Option::<Vec<LuaMessage>>::from(act.get_global("arr1").ok().unwrap());
119144
assert_eq!(LuaMessage::from(1), v.clone().unwrap()[0]);
@@ -122,7 +147,6 @@ fn main() {
122147
}
123148

124149
let _ = test_actor(Actor::new_with_handler(None));
125-
let _ = test_actor(Actor::new_with_handler(Some(HandlerThread::new_with_mutex())));
126150
let _ = test_actor(Actor::new());
127151
}
128152

src/actor.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,20 @@ impl Actor {
9090
let lua = self.lua.clone();
9191
_handler.lock().unwrap().post(RawFunc::new(move || {
9292
let lua = lua.clone();
93-
let _ = Self::_set_global(&lua, key, value.clone());
93+
let _ = Self::set_global_raw(&lua.lock().unwrap(), key, value.clone());
9494
}));
9595
Ok(())
9696
}
97-
None => Self::_set_global(&self.lua.clone(), key, value),
97+
None => Self::set_global_raw(&self.lua.lock().unwrap(), key, value),
9898
}
9999
}
100100
#[inline]
101-
fn _set_global(lua: &Arc<Mutex<Lua>>, key: &str, value: LuaMessage) -> Result<(), Error> {
102-
let vm = lua.lock().unwrap();
103-
let globals = vm.globals();
104-
globals.set(key, value)?;
105-
Ok(())
101+
pub fn set_global_raw<'lua, K: ToLua<'lua>, V: ToLua<'lua>>(
102+
lua: &'lua Lua,
103+
key: K,
104+
value: V,
105+
) -> Result<(), Error> {
106+
lua.globals().set::<_, V>(key, value)
106107
}
107108

108109
pub fn get_global(&self, key: &'static str) -> Result<LuaMessage, Error> {
@@ -150,6 +151,7 @@ impl Actor {
150151
#[inline]
151152
pub fn def_fn_with_name<'lua, 'callback, F, A, R>(
152153
lua: &'lua Lua,
154+
table: Table<'lua>,
153155
func: F,
154156
key: &str,
155157
) -> Result<Function<'lua>, Error>
@@ -159,8 +161,8 @@ impl Actor {
159161
F: 'static + Send + Fn(&'callback Lua, A) -> Result<R, Error>,
160162
{
161163
let def = Self::def_fn(lua, func)?;
162-
lua.globals().set(key, def)?;
163-
Self::get_global_function(lua, key)
164+
table.set(key, def)?;
165+
table.get(key)
164166
}
165167
pub fn def_fn_with_name_nowait<'callback, F, A, R>(
166168
&self,
@@ -177,12 +179,12 @@ impl Actor {
177179
let lua = self.lua.clone();
178180
_handler.lock().unwrap().post(RawFunc::new(move || {
179181
let lua = lua.lock().unwrap();
180-
let _ = Self::def_fn_with_name(&lua, func.clone(), key);
182+
let _ = Self::def_fn_with_name(&lua, lua.globals(), func.clone(), key);
181183
}));
182184
}
183185
None => {
184186
let lua = self.lua.lock().unwrap();
185-
Self::def_fn_with_name(&lua, func.clone(), key)?;
187+
Self::def_fn_with_name(&lua, lua.globals(), func.clone(), key)?;
186188
}
187189
}
188190

@@ -375,16 +377,13 @@ fn test_actor_new() {
375377
}
376378

377379
{
378-
let vm = act.lua();
379-
let vm = vm.lock().unwrap();
380-
Actor::def_fn_with_name(&vm, |_, (list1, list2): (Vec<String>, Vec<String>)| {
380+
act.def_fn_with_name_nowait(|_, (list1, list2): (Vec<String>, Vec<String>)| {
381381
// This function just checks whether two string lists are equal, and in an inefficient way.
382382
// Lua callbacks return `rlua::Result`, an Ok value is a normal return, and an Err return
383383
// turns into a Lua 'error'. Again, any type that is convertible to lua may be returned.
384384
Ok(list1 == list2)
385385
}, "check_equal").ok().unwrap();
386-
Actor::def_fn_with_name(
387-
&vm,
386+
act.def_fn_with_name_nowait(
388387
|_, strings: Variadic<String>| {
389388
// (This is quadratic!, it's just an example!)
390389
Ok(strings.iter().fold("".to_owned(), |a, b| a + b))
@@ -393,20 +392,23 @@ fn test_actor_new() {
393392
).ok()
394393
.unwrap();
395394
assert_eq!(
396-
vm.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#, None)
397-
.ok()
398-
.unwrap(),
395+
Option::<bool>::from(
396+
act.eval(r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#, None)
397+
.ok()
398+
.unwrap()
399+
).unwrap(),
399400
true
400401
);
401402
assert_eq!(
402-
vm.eval::<bool>(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#, None)
403-
.ok()
404-
.unwrap(),
403+
Option::<bool>::from(
404+
act.eval(r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#, None)
405+
.ok()
406+
.unwrap()
407+
).unwrap(),
405408
false
406409
);
407410
assert_eq!(
408-
vm.eval::<String>(r#"join("a", "b", "c")"#, None)
409-
.ok()
411+
Option::<String>::from(act.eval(r#"join("a", "b", "c")"#, None).ok().unwrap())
410412
.unwrap(),
411413
"abc"
412414
);

0 commit comments

Comments
 (0)