Add luaL_newstate

This commit is contained in:
Srlion 2021-09-21 19:35:28 +02:00
parent 460cb0920f
commit 50a07d1b5d
2 changed files with 14 additions and 0 deletions

View File

@ -75,6 +75,8 @@ lazy_static::lazy_static! {
}
pub(crate) struct LuaShared {
pub lual_newstate: Symbol<'static, unsafe extern "C-unwind" fn() -> LuaState>,
pub lual_openlibs: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState)>,
pub lual_loadfile: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, path: LuaString) -> i32>,
pub lual_loadstring: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, path: LuaString) -> i32>,
pub lual_loadbuffer: Symbol<'static, unsafe extern "C-unwind" fn(state: LuaState, buff: LuaString, sz: LuaSize, name: LuaString) -> i32>,
@ -137,6 +139,8 @@ impl LuaShared {
}
Self {
lual_newstate: find_symbol!("luaL_newstate"),
lual_openlibs: find_symbol!("luaL_openlibs"),
lua_pushlightuserdata: find_symbol!("lua_pushlightuserdata"),
lual_checktype: find_symbol!("luaL_checktype"),
lual_loadfile: find_symbol!("luaL_loadfile"),

View File

@ -9,6 +9,16 @@ use crate::userdata::TaggedUserData;
pub struct LuaState(pub *mut std::ffi::c_void);
unsafe impl Send for LuaState {}
impl LuaState {
pub unsafe fn new() -> Result<Self, LuaError> {
let lua = (LUA_SHARED.lual_newstate)();
(LUA_SHARED.lual_openlibs)(lua);
if lua.is_null() {
Err(LuaError::MemoryAllocationError)
} else {
Ok(lua)
}
}
/// Returns the Lua string as a slice of bytes.
///
/// **WARNING:** This will CHANGE the type of the value at the given index to a string.