summaryrefslogtreecommitdiff
path: root/src/serde
diff options
context:
space:
mode:
Diffstat (limited to 'src/serde')
-rw-r--r--src/serde/mod.rs18
-rw-r--r--src/serde/ser.rs8
2 files changed, 15 insertions, 11 deletions
diff --git a/src/serde/mod.rs b/src/serde/mod.rs
index d9253d0..27ce492 100644
--- a/src/serde/mod.rs
+++ b/src/serde/mod.rs
@@ -10,7 +10,7 @@ use crate::ffi;
use crate::lua::Lua;
use crate::table::Table;
use crate::types::LightUserData;
-use crate::util::{assert_stack, check_stack, StackGuard};
+use crate::util::{assert_stack, check_stack, protect_lua, StackGuard};
use crate::value::Value;
/// Trait for serializing/deserializing Lua values using Serde.
@@ -200,15 +200,17 @@ impl<'lua> LuaSerdeExt<'lua> for Lua {
// Uses 6 stack spaces and calls checkstack.
pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) -> Result<()> {
- check_stack(state, 6)?;
+ check_stack(state, 3)?;
+ protect_lua(state, 0, 0, |state| {
+ ffi::lua_createtable(state, 0, 1);
- ffi::safe::lua_createtable(state, 0, 1)?;
+ ffi::lua_pushstring(state, cstr!("__metatable"));
+ ffi::lua_pushboolean(state, 0);
+ ffi::lua_rawset(state, -3);
- ffi::lua_pushboolean(state, 0);
- ffi::safe::lua_rawsetfield(state, -2, "__metatable")?;
-
- let array_metatable_key = &ARRAY_METATABLE_REGISTRY_KEY as *const u8 as *const c_void;
- ffi::safe::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, array_metatable_key)
+ let array_metatable_key = &ARRAY_METATABLE_REGISTRY_KEY as *const u8 as *const c_void;
+ ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, array_metatable_key);
+ })
}
pub(crate) unsafe fn push_array_metatable(state: *mut ffi::lua_State) {
diff --git a/src/serde/ser.rs b/src/serde/ser.rs
index 4b774cb..4c6423c 100644
--- a/src/serde/ser.rs
+++ b/src/serde/ser.rs
@@ -9,7 +9,7 @@ use crate::lua::Lua;
use crate::string::String;
use crate::table::Table;
use crate::types::Integer;
-use crate::util::{check_stack, StackGuard};
+use crate::util::{check_stack, protect_lua, StackGuard};
use crate::value::{ToLua, Value};
/// A struct for serializing Rust values into Lua values.
@@ -318,12 +318,14 @@ impl<'lua> ser::SerializeSeq for SerializeVec<'lua> {
let value = lua.to_value_with(value, self.options)?;
unsafe {
let _sg = StackGuard::new(lua.state);
- check_stack(lua.state, 6)?;
+ check_stack(lua.state, 5)?;
lua.push_ref(&self.table.0);
lua.push_value(value)?;
let len = ffi::lua_rawlen(lua.state, -2) as Integer;
- ffi::safe::lua_rawseti(lua.state, -2, len + 1)
+ protect_lua(lua.state, 2, 0, |state| {
+ ffi::lua_rawseti(state, -2, len + 1);
+ })
}
}