Add back explanatory comment about trying "return <expr>" before statement

Also run through rustfmt
This commit is contained in:
kyren 2017-07-19 20:10:11 -04:00
parent 4f2d894a52
commit 160c5405e1
3 changed files with 30 additions and 11 deletions

View file

@ -70,7 +70,8 @@ fn examples() -> LuaResult<()> {
for k, v in pairs(map_table) do
print(k, v)
end
"#, None
"#,
None,
)?;
// You can load lua functions
@ -118,14 +119,14 @@ fn examples() -> LuaResult<()> {
assert_eq!(
lua.eval::<bool>(
r#"check_equal({"a", "b", "c"}, {"a", "b", "c"})"#,
None
None,
)?,
true
);
assert_eq!(
lua.eval::<bool>(
r#"check_equal({"a", "b", "c"}, {"d", "e", "f"})"#,
None
None,
)?,
false
);
@ -158,7 +159,12 @@ fn examples() -> LuaResult<()> {
});
globals.set("vec2", vec2_constructor)?;
assert!(lua.eval::<f32>("(vec2(1, 2) + vec2(2, 2)):magnitude()", None)? - 5.0 < f32::EPSILON);
assert!(
lua.eval::<f32>(
"(vec2(1, 2) + vec2(2, 2)):magnitude()",
None,
)? - 5.0 < f32::EPSILON
);
Ok(())
}

View file

@ -1087,8 +1087,11 @@ impl Lua {
pub fn eval<'lua, R: FromLuaMulti<'lua>>(
&'lua self,
source: &str,
name: Option<&str>
name: Option<&str>,
) -> LuaResult<R> {
// First, try interpreting the lua as an expression by adding
// "return", then as a statement. This is the same thing the
// actual lua repl does.
self.load(&format!("return {}", source), name)
.or_else(|_| self.load(source, name))?
.call(())

View file

@ -323,8 +323,18 @@ fn test_metamethods() {
let globals = lua.globals();
globals.set("userdata1", UserData(7)).unwrap();
globals.set("userdata2", UserData(3)).unwrap();
assert_eq!(lua.eval::<UserData>("userdata1 + userdata2", None).unwrap().0, 10);
assert_eq!(lua.eval::<UserData>("userdata1 - userdata2", None).unwrap().0, 4);
assert_eq!(
lua.eval::<UserData>("userdata1 + userdata2", None)
.unwrap()
.0,
10
);
assert_eq!(
lua.eval::<UserData>("userdata1 - userdata2", None)
.unwrap()
.0,
4
);
assert_eq!(lua.eval::<i64>("userdata1:get()", None).unwrap(), 7);
assert_eq!(lua.eval::<i64>("userdata2.inner", None).unwrap(), 3);
assert!(lua.eval::<()>("userdata2.nonexist_field", None).is_err());
@ -610,7 +620,7 @@ fn test_thread() {
return sum
end
"#,
None
None,
).unwrap(),
);
@ -635,7 +645,7 @@ fn test_thread() {
end
end
"#,
None
None,
).unwrap(),
);
@ -655,7 +665,7 @@ fn test_thread() {
end
end)
"#,
None
None,
).unwrap();
assert_eq!(thread.status(), LuaThreadStatus::Active);
assert_eq!(thread.resume::<_, i64>(()).unwrap(), 42);
@ -669,7 +679,7 @@ fn test_thread() {
return 987
end)
"#,
None
None,
).unwrap();
assert_eq!(thread.resume::<_, u32>(42).unwrap(), 123);