From 00cb274bf3e380510abe4173901fc3817d559a33 Mon Sep 17 00:00:00 2001 From: William Venner Date: Sat, 23 Oct 2021 20:40:31 +0100 Subject: [PATCH] Add new_metatable check_function check_table push_registry --- Cargo.lock | 2 +- gmod/Cargo.toml | 2 +- gmod/src/lua/import.rs | 2 ++ gmod/src/lua/lua_state.rs | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fad0a87..b199c48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,7 +109,7 @@ dependencies = [ [[package]] name = "gmod" -version = "6.0.1" +version = "7.0.0" dependencies = [ "cfg_table", "cstr", diff --git a/gmod/Cargo.toml b/gmod/Cargo.toml index b953752..ae76793 100644 --- a/gmod/Cargo.toml +++ b/gmod/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gmod" -version = "6.0.1" +version = "7.0.0" authors = ["William Venner "] edition = "2018" license = "MIT" diff --git a/gmod/src/lua/import.rs b/gmod/src/lua/import.rs index 0d92e0f..cd05a3e 100644 --- a/gmod/src/lua/import.rs +++ b/gmod/src/lua/import.rs @@ -169,6 +169,7 @@ pub struct LuaShared { pub lua_next: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> i32>, pub lua_topointer: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> *const c_void>, pub lua_newuserdata: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, size: usize) -> *mut c_void>, + pub lual_newmetatable: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, name: LuaString) -> i32>, } unsafe impl Sync for LuaShared {} impl LuaShared { @@ -235,6 +236,7 @@ impl LuaShared { lua_next: find_symbol!("lua_next"), lua_topointer: find_symbol!("lua_topointer"), lua_newuserdata: find_symbol!("lua_newuserdata"), + lual_newmetatable: find_symbol!("lual_newmetatable"), } } } diff --git a/gmod/src/lua/lua_state.rs b/gmod/src/lua/lua_state.rs index 64fe4e0..a37f228 100644 --- a/gmod/src/lua/lua_state.rs +++ b/gmod/src/lua/lua_state.rs @@ -218,6 +218,11 @@ impl LuaState { (LUA_SHARED.lua_pushvalue)(*self, LUA_GLOBALSINDEX) } + #[inline(always)] + pub unsafe fn push_registry(&self) { + (LUA_SHARED.lua_pushvalue)(*self, LUA_REGISTRYINDEX) + } + #[inline(always)] pub unsafe fn push_string(&self, data: &str) { (LUA_SHARED.lua_pushlstring)(*self, data.as_ptr() as LuaString, data.len()) @@ -325,6 +330,16 @@ impl LuaState { (LUA_SHARED.lua_getmetatable)(*self, index) } + #[inline(always)] + pub unsafe fn check_table(&self, arg: i32) { + (LUA_SHARED.lual_checktype)(*self, arg, LUA_TTABLE) + } + + #[inline(always)] + pub unsafe fn check_function(&self, arg: i32) { + (LUA_SHARED.lual_checktype)(*self, arg, LUA_TFUNCTION) + } + #[inline(always)] pub unsafe fn check_integer(&self, arg: i32) -> LuaInt { (LUA_SHARED.lual_checkinteger)(*self, arg) @@ -391,6 +406,13 @@ impl LuaState { (LUA_SHARED.lua_touserdata)(*self, index) } + /// Creates a new table in the registry with the given `name` as the key if it doesn't already exist, and pushes it onto the stack. + /// + /// Returns if the metatable was already present in the registry. + pub unsafe fn new_metatable(&self, name: LuaString) -> bool { + (LUA_SHARED.lual_newmetatable)(*self, name) == 0 + } + pub unsafe fn new_userdata(&self, data: T, metatable: Option) -> *mut T { let has_metatable = if std::mem::needs_drop::() { if let Some(metatable) = metatable {