summaryrefslogtreecommitdiff
path: root/src/lua.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua.rs')
-rw-r--r--src/lua.rs46
1 files changed, 22 insertions, 24 deletions
diff --git a/src/lua.rs b/src/lua.rs
index b81e442..a0c094e 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -28,7 +28,7 @@ use crate::userdata::{
use crate::util::{
self, assert_stack, callback_error, check_stack, get_destructed_userdata_metatable,
get_gc_metatable_for, get_gc_userdata, get_main_state, get_userdata, get_wrapped_error,
- init_error_registry, init_gc_metatable_for, init_userdata_metatable, pop_error, protect_lua,
+ init_error_registry, init_gc_metatable_for, init_userdata_metatable, pop_error,
push_gc_userdata, push_string, push_table, push_userdata, push_wrapped_error, rawset_field,
safe_pcall, safe_xpcall, StackGuard, WrappedError, WrappedPanic,
};
@@ -408,8 +408,8 @@ impl Lua {
// Create empty Waker slot
push_gc_userdata::<Option<Waker>>(state, None)?;
- let waker_key = &WAKER_REGISTRY_KEY as *const u8 as *const c_void;
- protect_lua(state, 1, 0, |state| {
+ protect_lua!(state, 1, 0, state => {
+ let waker_key = &WAKER_REGISTRY_KEY as *const u8 as *const c_void;
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, waker_key);
})?;
}
@@ -420,7 +420,7 @@ impl Lua {
// Create ref stack thread and place it in the registry to prevent it from being garbage
// collected.
- let ref_thread = protect_lua(state, 0, 0, |state| {
+ let ref_thread = protect_lua!(state, 0, 0, |state| {
let thread = ffi::lua_newthread(state);
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX);
thread
@@ -453,9 +453,9 @@ impl Lua {
push_gc_userdata(main_state, Arc::downgrade(&extra)),
"Error while storing extra data",
);
- let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
mlua_expect!(
- protect_lua(main_state, 1, 0, |state| {
+ protect_lua!(main_state, 1, 0, state => {
+ let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key)
}),
"Error while storing extra data",
@@ -535,7 +535,7 @@ impl Lua {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 3)?;
- protect_lua(self.state, 0, 1, |state| {
+ protect_lua!(self.state, 0, 1, state => {
ffi::luaL_getsubtable(state, ffi::LUA_REGISTRYINDEX, cstr!("_LOADED"));
})?;
let loaded = Table(self.pop_ref());
@@ -735,9 +735,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, state => ffi::lua_gc(state, ffi::LUA_GCCOLLECT, 0))
}
}
@@ -756,7 +754,7 @@ impl Lua {
let state = self.main_state.unwrap_or(self.state);
unsafe {
check_stack(state, 3)?;
- protect_lua(state, 0, 0, |state| {
+ protect_lua!(state, 0, 0, |state| {
ffi::lua_gc(state, ffi::LUA_GCSTEP, kbytes) != 0
})
}
@@ -933,7 +931,7 @@ impl Lua {
unsafe {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 3)?;
- push_table(self.state, 0, 0)?;
+ protect_lua!(self.state, 0, 1, state => ffi::lua_newtable(state))?;
Ok(Table(self.pop_ref()))
}
}
@@ -968,7 +966,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, state => ffi::lua_rawset(state, -3))?;
}
Ok(Table(self.pop_ref()))
@@ -990,7 +988,7 @@ impl Lua {
push_table(self.state, lower_bound as c_int, 0)?;
for (i, v) in iter.enumerate() {
self.push_value(v.to_lua(self)?)?;
- protect_lua(self.state, 2, 1, |state| {
+ protect_lua!(self.state, 2, 1, |state| {
ffi::lua_rawseti(state, -2, (i + 1) as Integer);
})?;
}
@@ -1087,7 +1085,7 @@ impl Lua {
pub unsafe fn create_c_function(&self, func: ffi::lua_CFunction) -> Result<Function> {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 3)?;
- protect_lua(self.state, 0, 1, |state| {
+ protect_lua!(self.state, 0, 1, |state| {
ffi::lua_pushcfunction(state, func);
})?;
Ok(Function(self.pop_ref()))
@@ -1163,7 +1161,7 @@ impl Lua {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 3)?;
- let thread_state = protect_lua(self.state, 0, 1, |state| ffi::lua_newthread(state))?;
+ let thread_state = protect_lua!(self.state, 0, 1, |state| ffi::lua_newthread(state))?;
self.push_ref(&func.0);
ffi::lua_xmove(self.state, thread_state, 1);
@@ -1278,7 +1276,7 @@ impl Lua {
check_stack(self.state, 4)?;
self.push_value(v)?;
- let res = protect_lua(self.state, 1, 1, |state| {
+ let res = protect_lua!(self.state, 1, 1, |state| {
ffi::lua_tolstring(state, -1, ptr::null_mut())
})?;
if !res.is_null() {
@@ -1432,7 +1430,7 @@ impl Lua {
check_stack(self.state, 4)?;
self.push_value(t)?;
- let registry_id = protect_lua(self.state, 1, 0, |state| {
+ let registry_id = protect_lua!(self.state, 1, 0, |state| {
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
})?;
@@ -1708,7 +1706,7 @@ impl Lua {
rawset_field(self.state, -2, k.validate()?.name())?;
}
// Add special `__mlua_type_id` field
- let type_id_ptr = protect_lua(self.state, 0, 1, |state| {
+ let type_id_ptr = protect_lua!(self.state, 0, 1, |state| {
ffi::lua_newuserdata(state, mem::size_of::<TypeId>()) as *mut TypeId
})?;
ptr::write(type_id_ptr, type_id);
@@ -1774,7 +1772,7 @@ impl Lua {
let ptr = ffi::lua_topointer(self.state, -1);
ffi::lua_pushvalue(self.state, -1);
- let id = protect_lua(self.state, 1, 0, |state| {
+ let id = protect_lua!(self.state, 1, 0, |state| {
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
})?;
@@ -1881,7 +1879,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, state => {
ffi::lua_pushcclosure(state, call_callback, 1);
})?;
@@ -1933,7 +1931,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, state => {
ffi::lua_pushcclosure(state, poll_future, 1);
})?;
@@ -1999,7 +1997,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, state => {
ffi::lua_pushcclosure(state, call_callback, 1);
})?;
@@ -2476,7 +2474,7 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result<
glb: c_int,
) -> Result<()> {
let modname = mlua_expect!(CString::new(modname.as_ref()), "modname contains nil bytes");
- protect_lua(state, 0, 1, |state| {
+ protect_lua!(state, 0, 1, |state| {
ffi::luaL_requiref(state, modname.as_ptr() as *const c_char, openf, glb)
})
}