Fix a bug in Function::bind when args and binds are empty

This leads to a Lua assertion due to using wrong stack index
This commit is contained in:
Alex Orlenko 2022-07-22 00:24:53 +01:00
parent d3b48cf2f3
commit 40fe937878
No known key found for this signature in database
GPG key ID: 4C150C250863B96D
2 changed files with 9 additions and 1 deletions

View file

@ -198,7 +198,9 @@ impl<'lua> Function<'lua> {
for i in 0..nbinds {
ffi::lua_pushvalue(state, ffi::lua_upvalueindex(i + 2));
}
ffi::lua_rotate(state, 1, nbinds);
if nargs > 0 {
ffi::lua_rotate(state, 1, nbinds);
}
nargs + nbinds
}

View file

@ -42,11 +42,17 @@ fn test_bind() -> Result<()> {
concat = concat.bind("foo")?;
concat = concat.bind("bar")?;
concat = concat.bind(("baz", "baf"))?;
assert_eq!(concat.call::<_, String>(())?, "foobarbazbaf");
assert_eq!(
concat.call::<_, String>(("hi", "wut"))?,
"foobarbazbafhiwut"
);
let mut concat2 = globals.get::<_, Function>("concat")?;
concat2 = concat2.bind(())?;
assert_eq!(concat2.call::<_, String>(())?, "");
assert_eq!(concat2.call::<_, String>(("ab", "cd"))?, "abcd");
Ok(())
}