Skip to content

Commit 8d864e2

Browse files
committed
Reduce stack operations when creating userdata
1 parent b0140cd commit 8d864e2

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/state/raw.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,11 @@ impl RawLua {
911911
let _sg = StackGuard::new(state);
912912
check_stack(state, 3)?;
913913

914-
// We push metatable first to ensure having correct metatable with `__gc` method
915-
ffi::lua_pushnil(state);
916-
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, get_metatable_id()?);
914+
// We generate metatable first to make sure it *always* available when userdata pushed
915+
let mt_id = get_metatable_id()?;
917916
let protect = !self.unlikely_memory_error();
918917
crate::util::push_userdata(state, data, protect)?;
919-
ffi::lua_replace(state, -3);
918+
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, mt_id);
920919
ffi::lua_setmetatable(state, -2);
921920

922921
// Set empty environment for Lua 5.1

0 commit comments

Comments
 (0)