Change API names, add unset function

This commit is contained in:
kyren 2017-12-16 18:05:53 -05:00
parent ad23fe83e0
commit e6d84a4bb3
3 changed files with 27 additions and 12 deletions

View File

@ -45,8 +45,6 @@ There are currently a few notable missing pieces of this API:
* "Context" or "Sandboxing" support. There should be the ability to set the
`_ENV` upvalue of a loaded chunk to a table other than `_G`, so that you can
have different environments for different loaded chunks.
* More fleshed out Lua API, there is some missing nice to have functionality
not exposed like storing values in the registry.
* Benchmarks, and quantifying performance differences with what you would
might write in C.

View File

@ -404,15 +404,19 @@ impl Lua {
T::from_lua_multi(value, self)
}
/// Set a value in the Lua registry based on a string key.
/// Set a value in the Lua registry based on a string name.
///
/// This value will be available to rust from all `Lua` instances which share the same main
/// state.
pub fn set_registry<'lua, T: ToLua<'lua>>(&'lua self, registry_key: &str, t: T) -> Result<()> {
pub fn set_named_registry_value<'lua, T: ToLua<'lua>>(
&'lua self,
name: &str,
t: T,
) -> Result<()> {
unsafe {
stack_err_guard(self.state, 0, || {
check_stack(self.state, 5);
push_string(self.state, registry_key)?;
push_string(self.state, name)?;
self.push_value(self.state, t.to_lua(self)?);
protect_lua_call(self.state, 2, 0, |state| {
ffi::lua_settable(state, ffi::LUA_REGISTRYINDEX);
@ -421,15 +425,15 @@ impl Lua {
}
}
/// Get a value from the Lua registry based on a string key.
/// Get a value from the Lua registry based on a string name.
///
/// Any Lua instance which shares the underlying main state may call `get_registry` to get a
/// value previously set by `set_registry`.
pub fn get_registry<'lua, T: FromLua<'lua>>(&'lua self, registry_key: &str) -> Result<T> {
pub fn named_registry_value<'lua, T: FromLua<'lua>>(&'lua self, name: &str) -> Result<T> {
unsafe {
stack_err_guard(self.state, 0, || {
check_stack(self.state, 4);
push_string(self.state, registry_key)?;
push_string(self.state, name)?;
protect_lua_call(self.state, 1, 1, |state| {
ffi::lua_gettable(state, ffi::LUA_REGISTRYINDEX)
})?;
@ -438,6 +442,13 @@ impl Lua {
}
}
/// Clears a named value in the Lua registry.
///
/// Equivalent to calling `Lua::set_named_registry_value` with a value of Nil.
pub fn unset_named_registry_value<'lua>(&'lua self, name: &str) -> Result<()> {
self.set_named_registry_value(name, Nil)
}
// Uses 1 stack space, does not call checkstack
pub(crate) unsafe fn push_value(&self, state: *mut ffi::lua_State, value: Value) {
match value {

View File

@ -2,7 +2,7 @@ use std::fmt;
use std::error;
use std::panic::catch_unwind;
use {Error, ExternalError, Function, Lua, Result, Table, Value, Variadic};
use {Error, ExternalError, Function, Lua, Nil, Result, Table, Value, Variadic};
#[test]
fn test_load() {
@ -500,16 +500,22 @@ fn test_gc_error() {
}
#[test]
fn test_registry() {
fn test_named_registry_value() {
let lua = Lua::new();
lua.set_registry::<i32>("test", 42).unwrap();
lua.set_named_registry_value::<i32>("test", 42).unwrap();
let f = lua.create_function(move |lua, ()| {
assert_eq!(lua.get_registry::<i32>("test")?, 42);
assert_eq!(lua.named_registry_value::<i32>("test")?, 42);
Ok(())
}).unwrap();
f.call::<_, ()>(()).unwrap();
lua.unset_named_registry_value("test").unwrap();
match lua.named_registry_value("test").unwrap() {
Nil => {}
val => panic!("registry value was not Nil, was {:?}", val),
};
}
// TODO: Need to use compiletest-rs or similar to make sure these don't compile.