Merge pull request #2 from Srlion/master

Add luaL_newstate
This commit is contained in:
William 2021-09-21 18:55:22 +01:00 committed by GitHub
commit 3ae5aa4652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -75,6 +75,8 @@ lazy_static::lazy_static! {
} }
pub(crate) struct LuaShared { 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_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_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>, 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 { Self {
lual_newstate: find_symbol!("luaL_newstate"),
lual_openlibs: find_symbol!("luaL_openlibs"),
lua_pushlightuserdata: find_symbol!("lua_pushlightuserdata"), lua_pushlightuserdata: find_symbol!("lua_pushlightuserdata"),
lual_checktype: find_symbol!("luaL_checktype"), lual_checktype: find_symbol!("luaL_checktype"),
lual_loadfile: find_symbol!("luaL_loadfile"), 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); pub struct LuaState(pub *mut std::ffi::c_void);
unsafe impl Send for LuaState {} unsafe impl Send for LuaState {}
impl 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. /// 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. /// **WARNING:** This will CHANGE the type of the value at the given index to a string.