add some lua bindings

This commit is contained in:
William Venner 2021-09-21 18:13:19 +01:00
parent 59e396858c
commit d47174601a
2 changed files with 17 additions and 0 deletions

View File

@ -1,3 +1,5 @@
use std::ffi::c_void;
use libloading::{Library, Symbol}; use libloading::{Library, Symbol};
macro_rules! find_library { macro_rules! find_library {
@ -78,6 +80,7 @@ pub(crate) struct LuaShared {
pub lual_loadbuffer: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, buff: LuaString, sz: LuaSize, name: LuaString) -> i32>, pub lual_loadbuffer: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, buff: LuaString, sz: LuaSize, name: LuaString) -> i32>,
pub lua_getfield: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32, k: LuaString)>, pub lua_getfield: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32, k: LuaString)>,
pub lua_pushvalue: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32)>, pub lua_pushvalue: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32)>,
pub lua_pushlightuserdata: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, data: *mut c_void)>,
pub lua_pushboolean: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, bool: i32)>, pub lua_pushboolean: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, bool: i32)>,
pub lua_tolstring: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32, out_size: *mut LuaSize) -> LuaString>, pub lua_tolstring: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32, out_size: *mut LuaSize) -> LuaString>,
pub lua_pcall: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, nargs: i32, nresults: i32, errfunc: i32) -> i32>, pub lua_pcall: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, nargs: i32, nresults: i32, errfunc: i32) -> i32>,
@ -99,6 +102,7 @@ pub(crate) struct LuaShared {
pub lual_checkinteger: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, arg: i32) -> LuaInt>, pub lual_checkinteger: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, arg: i32) -> LuaInt>,
pub lual_checklstring: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, arg: i32, out_size: *mut LuaSize) -> LuaString>, pub lual_checklstring: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, arg: i32, out_size: *mut LuaSize) -> LuaString>,
pub lua_toboolean: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> i32>, pub lua_toboolean: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> i32>,
pub lual_checktype: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32, r#type: i32)>,
pub lua_setmetatable: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> i32>, pub lua_setmetatable: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, index: i32) -> i32>,
pub lua_pushinteger: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, int: LuaInt)>, pub lua_pushinteger: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, int: LuaInt)>,
pub lua_pushnumber: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, int: LuaNumber)>, pub lua_pushnumber: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, int: LuaNumber)>,
@ -133,6 +137,8 @@ impl LuaShared {
} }
Self { Self {
lua_pushlightuserdata: find_symbol!("lua_pushlightuserdata"),
lual_checktype: find_symbol!("luaL_checktype"),
lual_loadfile: find_symbol!("luaL_loadfile"), lual_loadfile: find_symbol!("luaL_loadfile"),
lual_loadstring: find_symbol!("luaL_loadstring"), lual_loadstring: find_symbol!("luaL_loadstring"),
lual_loadbuffer: find_symbol!("luaL_loadbuffer"), lual_loadbuffer: find_symbol!("luaL_loadbuffer"),

View File

@ -110,6 +110,11 @@ impl LuaState {
(LUA_SHARED.lua_pushvalue)(*self, index) (LUA_SHARED.lua_pushvalue)(*self, index)
} }
#[inline]
pub unsafe fn push_lightuserdata(&self, data: *mut c_void) {
(LUA_SHARED.lua_pushlightuserdata)(*self, data)
}
#[inline] #[inline]
pub unsafe fn get_field(&self, index: i32, k: LuaString) { pub unsafe fn get_field(&self, index: i32, k: LuaString) {
(LUA_SHARED.lua_getfield)(*self, index, k) (LUA_SHARED.lua_getfield)(*self, index, k)
@ -320,6 +325,12 @@ impl LuaState {
(LUA_SHARED.lual_checknumber)(*self, arg) (LUA_SHARED.lual_checknumber)(*self, arg)
} }
#[inline]
pub unsafe fn check_boolean(&self, arg: i32) -> bool {
(LUA_SHARED.lual_checktype)(*self, arg, LUA_TBOOLEAN);
(LUA_SHARED.lua_toboolean)(*self, arg) == 1
}
#[inline] #[inline]
pub unsafe fn to_integer(&self, index: i32) -> LuaInt { pub unsafe fn to_integer(&self, index: i32) -> LuaInt {
(LUA_SHARED.lua_tointeger)(*self, index) (LUA_SHARED.lua_tointeger)(*self, index)