Change syntax of `protect_lua` macro
This commit is contained in:
parent
a74b637ed4
commit
5b1483bd56
|
@ -198,7 +198,7 @@ impl<'lua> Function<'lua> {
|
||||||
for arg in args {
|
for arg in args {
|
||||||
lua.push_value(arg)?;
|
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));
|
ffi::lua_pushcclosure(state, bind_call_impl, ffi::lua_gettop(state));
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
18
src/lua.rs
18
src/lua.rs
|
@ -414,7 +414,7 @@ impl Lua {
|
||||||
|
|
||||||
// Create empty Waker slot
|
// Create empty Waker slot
|
||||||
push_gc_userdata::<Option<Waker>>(state, None)?;
|
push_gc_userdata::<Option<Waker>>(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;
|
let waker_key = &WAKER_REGISTRY_KEY as *const u8 as *const c_void;
|
||||||
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, waker_key);
|
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, waker_key);
|
||||||
})?;
|
})?;
|
||||||
|
@ -461,7 +461,7 @@ impl Lua {
|
||||||
mlua_expect!(
|
mlua_expect!(
|
||||||
(|state| {
|
(|state| {
|
||||||
push_gc_userdata(state, Arc::clone(&extra))?;
|
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;
|
let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
|
||||||
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);
|
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);
|
||||||
})
|
})
|
||||||
|
@ -555,7 +555,7 @@ impl Lua {
|
||||||
let loaded = unsafe {
|
let loaded = unsafe {
|
||||||
let _sg = StackGuard::new(self.state);
|
let _sg = StackGuard::new(self.state);
|
||||||
check_stack(self.state, 2)?;
|
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"));
|
ffi::luaL_getsubtable(state, ffi::LUA_REGISTRYINDEX, cstr!("_LOADED"));
|
||||||
})?;
|
})?;
|
||||||
Table(self.pop_ref())
|
Table(self.pop_ref())
|
||||||
|
@ -781,7 +781,7 @@ impl Lua {
|
||||||
let state = self.main_state.unwrap_or(self.state);
|
let state = self.main_state.unwrap_or(self.state);
|
||||||
unsafe {
|
unsafe {
|
||||||
check_stack(state, 3)?;
|
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 {
|
unsafe {
|
||||||
let _sg = StackGuard::new(self.state);
|
let _sg = StackGuard::new(self.state);
|
||||||
check_stack(self.state, 2)?;
|
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()))
|
Ok(Table(self.pop_ref()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1012,7 +1012,7 @@ impl Lua {
|
||||||
for (k, v) in iter {
|
for (k, v) in iter {
|
||||||
self.push_value(k.to_lua(self)?)?;
|
self.push_value(k.to_lua(self)?)?;
|
||||||
self.push_value(v.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()))
|
Ok(Table(self.pop_ref()))
|
||||||
|
@ -1928,7 +1928,7 @@ impl Lua {
|
||||||
let lua = self.clone();
|
let lua = self.clone();
|
||||||
let func = mem::transmute(func);
|
let func = mem::transmute(func);
|
||||||
push_gc_userdata(self.state, CallbackUpvalue { lua, 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);
|
ffi::lua_pushcclosure(state, call_callback, 1);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -1980,7 +1980,7 @@ impl Lua {
|
||||||
let fut = ((*upvalue).func)(lua, args);
|
let fut = ((*upvalue).func)(lua, args);
|
||||||
let lua = lua.clone();
|
let lua = lua.clone();
|
||||||
push_gc_userdata(state, AsyncPollUpvalue { lua, fut })?;
|
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);
|
ffi::lua_pushcclosure(state, poll_future, 1);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -2046,7 +2046,7 @@ impl Lua {
|
||||||
let lua = self.clone();
|
let lua = self.clone();
|
||||||
let func = mem::transmute(func);
|
let func = mem::transmute(func);
|
||||||
push_gc_userdata(self.state, AsyncCallbackUpvalue { lua, 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);
|
ffi::lua_pushcclosure(state, call_callback, 1);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ macro_rules! protect_lua {
|
||||||
crate::util::protect_lua_closure($state, $nargs, $nresults, $f)
|
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 {
|
unsafe extern "C" fn do_call($state_inner: *mut ffi::lua_State) -> ::std::os::raw::c_int {
|
||||||
$code;
|
$code;
|
||||||
$nresults
|
$nresults
|
||||||
|
|
|
@ -243,7 +243,7 @@ impl<'lua> LuaSerdeExt<'lua> for Lua {
|
||||||
// Uses 2 stack spaces and calls checkstack.
|
// Uses 2 stack spaces and calls checkstack.
|
||||||
pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) -> Result<()> {
|
pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) -> Result<()> {
|
||||||
check_stack(state, 2)?;
|
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_createtable(state, 0, 1);
|
||||||
|
|
||||||
ffi::lua_pushstring(state, cstr!("__metatable"));
|
ffi::lua_pushstring(state, cstr!("__metatable"));
|
||||||
|
|
|
@ -322,7 +322,7 @@ impl<'lua> ser::SerializeSeq for SerializeVec<'lua> {
|
||||||
|
|
||||||
lua.push_ref(&self.table.0);
|
lua.push_ref(&self.table.0);
|
||||||
lua.push_value(value)?;
|
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;
|
let len = ffi::lua_rawlen(state, -2) as Integer;
|
||||||
ffi::lua_rawseti(state, -2, len + 1);
|
ffi::lua_rawseti(state, -2, len + 1);
|
||||||
})
|
})
|
||||||
|
|
|
@ -67,7 +67,7 @@ impl<'lua> Table<'lua> {
|
||||||
lua.push_ref(&self.0);
|
lua.push_ref(&self.0);
|
||||||
lua.push_value(key)?;
|
lua.push_value(key)?;
|
||||||
lua.push_value(value)?;
|
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_ref(&self.0);
|
||||||
lua.push_value(key)?;
|
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()
|
lua.pop_value()
|
||||||
};
|
};
|
||||||
|
@ -123,7 +123,7 @@ impl<'lua> Table<'lua> {
|
||||||
|
|
||||||
lua.push_ref(&self.0);
|
lua.push_ref(&self.0);
|
||||||
lua.push_value(key)?;
|
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)
|
Ok(ffi::lua_isnil(lua.state, -1) == 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,7 @@ impl<'lua> Table<'lua> {
|
||||||
lua.push_ref(&self.0);
|
lua.push_ref(&self.0);
|
||||||
lua.push_value(key)?;
|
lua.push_value(key)?;
|
||||||
lua.push_value(value)?;
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -452,7 +452,7 @@ pub unsafe fn init_userdata_metatable<T>(
|
||||||
ffi::lua_pushnil(state);
|
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);
|
ffi::lua_pushcclosure(state, meta_index_impl, 3);
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
@ -468,7 +468,7 @@ pub unsafe fn init_userdata_metatable<T>(
|
||||||
match newindex_type {
|
match newindex_type {
|
||||||
ffi::LUA_TNIL | ffi::LUA_TTABLE | ffi::LUA_TFUNCTION => {
|
ffi::LUA_TNIL | ffi::LUA_TTABLE | ffi::LUA_TFUNCTION => {
|
||||||
ffi::lua_pushvalue(state, field_setters);
|
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);
|
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);
|
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;
|
let destructed_mt_key = &DESTRUCTED_USERDATA_METATABLE as *const u8 as *const c_void;
|
||||||
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, destructed_mt_key);
|
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
|
// Create error print buffer
|
||||||
init_gc_metatable::<String>(state, None)?;
|
init_gc_metatable::<String>(state, None)?;
|
||||||
push_gc_userdata(state, String::new())?;
|
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;
|
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);
|
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, err_buf_key);
|
||||||
})?;
|
})?;
|
||||||
|
|
Loading…
Reference in New Issue