summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2022-02-14 20:51:24 +0000
committerAlex Orlenko <zxteam@protonmail.com>2022-02-14 20:51:24 +0000
commit6190427f373b24a12ab527704fab56561da11316 (patch)
treea7ec0653c8e7c92ee7c6f367ee3e1a9cf37952a9 /src
parent9a5a341e44eb28f6f78d013b1ff9162f79eff163 (diff)
downloadmlua-6190427f373b24a12ab527704fab56561da11316.zip
Add Lua::replace_registry_value
Diffstat (limited to 'src')
-rw-r--r--src/lua.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/lua.rs b/src/lua.rs
index 0138de7..fcef729 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -1777,6 +1777,33 @@ impl Lua {
Ok(())
}
+ /// Replaces a value in the Lua registry by its `RegistryKey`.
+ ///
+ /// See [`create_registry_value`] for more details.
+ ///
+ /// [`create_registry_value`]: #method.create_registry_value
+ pub fn replace_registry_value<'lua, T: ToLua<'lua>>(
+ &'lua self,
+ key: &RegistryKey,
+ t: T,
+ ) -> Result<()> {
+ let t = t.to_lua(self)?;
+ unsafe {
+ let _sg = StackGuard::new(self.state);
+ check_stack(self.state, 4)?;
+
+ self.push_value(t)?;
+ // It must be safe to replace the value without triggering memory error
+ ffi::lua_rawseti(
+ self.state,
+ ffi::LUA_REGISTRYINDEX,
+ key.registry_id as Integer,
+ );
+
+ Ok(())
+ }
+ }
+
/// Returns true if the given `RegistryKey` was created by a `Lua` which shares the underlying
/// main state with this `Lua` instance.
///