From 5b1483bd565fc44263e88ccc651dcdad1a3258d7 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 28 Sep 2021 18:47:08 +0100 Subject: [PATCH] Change syntax of `protect_lua` macro --- src/function.rs | 2 +- src/lua.rs | 18 +++++++++--------- src/macros.rs | 2 +- src/serde/mod.rs | 2 +- src/serde/ser.rs | 2 +- src/table.rs | 8 ++++---- src/util.rs | 8 ++++---- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/function.rs b/src/function.rs index 7b8ef02..966533c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -198,7 +198,7 @@ impl<'lua> Function<'lua> { for arg in args { lua.push_value(arg)?; } - protect_lua!(lua.state, nargs + 2, 1, state => { + protect_lua!(lua.state, nargs + 2, 1, fn(state) { ffi::lua_pushcclosure(state, bind_call_impl, ffi::lua_gettop(state)); })?; diff --git a/src/lua.rs b/src/lua.rs index 28eb22b..5e158f9 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -414,7 +414,7 @@ impl Lua { // Create empty Waker slot push_gc_userdata::>(state, None)?; - protect_lua!(state, 1, 0, state => { + protect_lua!(state, 1, 0, fn(state) { let waker_key = &WAKER_REGISTRY_KEY as *const u8 as *const c_void; ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, waker_key); })?; @@ -461,7 +461,7 @@ impl Lua { mlua_expect!( (|state| { push_gc_userdata(state, Arc::clone(&extra))?; - protect_lua!(main_state, 1, 0, state => { + protect_lua!(main_state, 1, 0, fn(state) { let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void; ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key); }) @@ -555,7 +555,7 @@ impl Lua { let loaded = unsafe { let _sg = StackGuard::new(self.state); check_stack(self.state, 2)?; - protect_lua!(self.state, 0, 1, state => { + protect_lua!(self.state, 0, 1, fn(state) { ffi::luaL_getsubtable(state, ffi::LUA_REGISTRYINDEX, cstr!("_LOADED")); })?; Table(self.pop_ref()) @@ -781,7 +781,7 @@ impl Lua { let state = self.main_state.unwrap_or(self.state); unsafe { check_stack(state, 3)?; - protect_lua!(state, 0, 0, state => ffi::lua_gc(state, ffi::LUA_GCCOLLECT, 0)) + protect_lua!(state, 0, 0, fn(state) ffi::lua_gc(state, ffi::LUA_GCCOLLECT, 0)) } } @@ -977,7 +977,7 @@ impl Lua { unsafe { let _sg = StackGuard::new(self.state); check_stack(self.state, 2)?; - protect_lua!(self.state, 0, 1, state => ffi::lua_newtable(state))?; + protect_lua!(self.state, 0, 1, fn(state) ffi::lua_newtable(state))?; Ok(Table(self.pop_ref())) } } @@ -1012,7 +1012,7 @@ impl Lua { for (k, v) in iter { self.push_value(k.to_lua(self)?)?; self.push_value(v.to_lua(self)?)?; - protect_lua!(self.state, 3, 1, state => ffi::lua_rawset(state, -3))?; + protect_lua!(self.state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?; } Ok(Table(self.pop_ref())) @@ -1928,7 +1928,7 @@ impl Lua { let lua = self.clone(); let func = mem::transmute(func); push_gc_userdata(self.state, CallbackUpvalue { lua, func })?; - protect_lua!(self.state, 1, 1, state => { + protect_lua!(self.state, 1, 1, fn(state) { ffi::lua_pushcclosure(state, call_callback, 1); })?; @@ -1980,7 +1980,7 @@ impl Lua { let fut = ((*upvalue).func)(lua, args); let lua = lua.clone(); push_gc_userdata(state, AsyncPollUpvalue { lua, fut })?; - protect_lua!(state, 1, 1, state => { + protect_lua!(state, 1, 1, fn(state) { ffi::lua_pushcclosure(state, poll_future, 1); })?; @@ -2046,7 +2046,7 @@ impl Lua { let lua = self.clone(); let func = mem::transmute(func); push_gc_userdata(self.state, AsyncCallbackUpvalue { lua, func })?; - protect_lua!(self.state, 1, 1, state => { + protect_lua!(self.state, 1, 1, fn(state) { ffi::lua_pushcclosure(state, call_callback, 1); })?; diff --git a/src/macros.rs b/src/macros.rs index 043164b..eb49c15 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -100,7 +100,7 @@ macro_rules! protect_lua { crate::util::protect_lua_closure($state, $nargs, $nresults, $f) }; - ($state:expr, $nargs:expr, $nresults:expr, $state_inner:ident => $code:expr) => {{ + ($state:expr, $nargs:expr, $nresults:expr, fn($state_inner:ident) $code:expr) => {{ unsafe extern "C" fn do_call($state_inner: *mut ffi::lua_State) -> ::std::os::raw::c_int { $code; $nresults diff --git a/src/serde/mod.rs b/src/serde/mod.rs index c80fecd..1e37c05 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -243,7 +243,7 @@ impl<'lua> LuaSerdeExt<'lua> for Lua { // Uses 2 stack spaces and calls checkstack. pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) -> Result<()> { check_stack(state, 2)?; - protect_lua!(state, 0, 0, state => { + protect_lua!(state, 0, 0, fn(state) { ffi::lua_createtable(state, 0, 1); ffi::lua_pushstring(state, cstr!("__metatable")); diff --git a/src/serde/ser.rs b/src/serde/ser.rs index f872282..e7cc738 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -322,7 +322,7 @@ impl<'lua> ser::SerializeSeq for SerializeVec<'lua> { lua.push_ref(&self.table.0); lua.push_value(value)?; - protect_lua!(lua.state, 2, 0, state => { + protect_lua!(lua.state, 2, 0, fn(state) { let len = ffi::lua_rawlen(state, -2) as Integer; ffi::lua_rawseti(state, -2, len + 1); }) diff --git a/src/table.rs b/src/table.rs index 5ac7ddb..b684060 100644 --- a/src/table.rs +++ b/src/table.rs @@ -67,7 +67,7 @@ impl<'lua> Table<'lua> { lua.push_ref(&self.0); lua.push_value(key)?; lua.push_value(value)?; - protect_lua!(lua.state, 3, 0, state => ffi::lua_settable(state, -3)) + protect_lua!(lua.state, 3, 0, fn(state) ffi::lua_settable(state, -3)) } } @@ -105,7 +105,7 @@ impl<'lua> Table<'lua> { lua.push_ref(&self.0); lua.push_value(key)?; - protect_lua!(lua.state, 2, 1, state => ffi::lua_gettable(state, -2))?; + protect_lua!(lua.state, 2, 1, fn(state) ffi::lua_gettable(state, -2))?; lua.pop_value() }; @@ -123,7 +123,7 @@ impl<'lua> Table<'lua> { lua.push_ref(&self.0); lua.push_value(key)?; - protect_lua!(lua.state, 2, 1, state => ffi::lua_gettable(state, -2))?; + protect_lua!(lua.state, 2, 1, fn(state) ffi::lua_gettable(state, -2))?; Ok(ffi::lua_isnil(lua.state, -1) == 0) } } @@ -197,7 +197,7 @@ impl<'lua> Table<'lua> { lua.push_ref(&self.0); lua.push_value(key)?; lua.push_value(value)?; - protect_lua!(lua.state, 3, 0, state => ffi::lua_rawset(state, -3)) + protect_lua!(lua.state, 3, 0, fn(state) ffi::lua_rawset(state, -3)) } } diff --git a/src/util.rs b/src/util.rs index 6c35504..f883f29 100644 --- a/src/util.rs +++ b/src/util.rs @@ -452,7 +452,7 @@ pub unsafe fn init_userdata_metatable( ffi::lua_pushnil(state); } } - protect_lua!(state, 3, 1, state => { + protect_lua!(state, 3, 1, fn(state) { ffi::lua_pushcclosure(state, meta_index_impl, 3); })?; } @@ -468,7 +468,7 @@ pub unsafe fn init_userdata_metatable( match newindex_type { ffi::LUA_TNIL | ffi::LUA_TTABLE | ffi::LUA_TFUNCTION => { ffi::lua_pushvalue(state, field_setters); - protect_lua!(state, 2, 1, state => { + protect_lua!(state, 2, 1, fn(state) { ffi::lua_pushcclosure(state, meta_newindex_impl, 2); })?; } @@ -853,7 +853,7 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<()> { } ffi::lua_pop(state, 1); - protect_lua!(state, 1, 0, state => { + protect_lua!(state, 1, 0, fn(state) { let destructed_mt_key = &DESTRUCTED_USERDATA_METATABLE as *const u8 as *const c_void; ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, destructed_mt_key); })?; @@ -861,7 +861,7 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<()> { // Create error print buffer init_gc_metatable::(state, None)?; push_gc_userdata(state, String::new())?; - protect_lua!(state, 1, 0, state => { + protect_lua!(state, 1, 0, fn(state) { let err_buf_key = &ERROR_PRINT_BUFFER_KEY as *const u8 as *const c_void; ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, err_buf_key); })?;