Skip to content

Commit

Permalink
Implement IntoLua for &RegistryKey
Browse files Browse the repository at this point in the history
This would allow just passing registry keys to arguments with fasttrack to push directly into stack.
  • Loading branch information
khvzak committed Jan 20, 2024
1 parent 3c801e7 commit 727f99e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::lua::Lua;
use crate::string::String;
use crate::table::Table;
use crate::thread::Thread;
use crate::types::{LightUserData, MaybeSend};
use crate::types::{LightUserData, MaybeSend, RegistryKey};
use crate::userdata::{AnyUserData, UserData, UserDataRef, UserDataRefMut};
use crate::value::{FromLua, IntoLua, Nil, Value};

Expand Down Expand Up @@ -240,6 +240,26 @@ impl<'lua> FromLua<'lua> for Error {
}
}

impl<'lua> IntoLua<'lua> for &RegistryKey {
#[inline]
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
lua.registry_value(self)
}

unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> {
if !lua.owns_registry_value(self) {
return Err(Error::MismatchedRegistryKey);
}

if self.is_nil() {
ffi::lua_pushnil(lua.state());
} else {
ffi::lua_rawgeti(lua.state(), ffi::LUA_REGISTRYINDEX, self.registry_id as _);
}
Ok(())
}
}

impl<'lua> IntoLua<'lua> for bool {
#[inline]
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
Expand Down
33 changes: 31 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::Arc;
use std::{error, f32, f64, fmt};

use mlua::{
ChunkMode, Error, ExternalError, Function, Lua, LuaOptions, Nil, Result, StdLib, String, Table,
UserData, Value, Variadic,
ChunkMode, Error, ExternalError, Function, IntoLua, Lua, LuaOptions, Nil, Result, StdLib,
String, Table, UserData, Value, Variadic,
};

#[cfg(not(feature = "luau"))]
Expand Down Expand Up @@ -779,6 +779,35 @@ fn test_registry_value() -> Result<()> {
Ok(())
}

#[test]
fn test_registry_value_into_lua() -> Result<()> {
let lua = Lua::new();

let t = lua.create_table()?;
let r = lua.create_registry_value(t)?;
let f = lua.create_function(|_, t: Table| t.raw_set("hello", "world"))?;

f.call(&r)?;
let v = r.into_lua(&lua)?;
let t = v.as_table().unwrap();
assert_eq!(t.get::<_, String>("hello")?, "world");

// Try to set nil registry key
let r_nil = lua.create_registry_value(Value::Nil)?;
t.set("hello", &r_nil)?;
assert_eq!(t.get::<_, Value>("hello")?, Value::Nil);

// Check non-owned registry key
let lua2 = Lua::new();
let r2 = lua2.create_registry_value("abc")?;
assert!(matches!(
f.call::<_, ()>(&r2),
Err(Error::MismatchedRegistryKey)
));

Ok(())
}

#[test]
fn test_drop_registry_value() -> Result<()> {
struct MyUserdata(Arc<()>);
Expand Down

0 comments on commit 727f99e

Please sign in to comment.