Add new_metatable check_function check_table push_registry

This commit is contained in:
William Venner 2021-10-23 20:40:31 +01:00
parent 10efbfdd16
commit 00cb274bf3
4 changed files with 26 additions and 2 deletions

2
Cargo.lock generated
View File

@ -109,7 +109,7 @@ dependencies = [
[[package]]
name = "gmod"
version = "6.0.1"
version = "7.0.0"
dependencies = [
"cfg_table",
"cstr",

View File

@ -1,6 +1,6 @@
[package]
name = "gmod"
version = "6.0.1"
version = "7.0.0"
authors = ["William Venner <william@venner.io>"]
edition = "2018"
license = "MIT"

View File

@ -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"),
}
}
}

View File

@ -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<T: Sized>(&self, data: T, metatable: Option<i32>) -> *mut T {
let has_metatable = if std::mem::needs_drop::<T>() {
if let Some(metatable) = metatable {