From 50a07d1b5db353a291afc421b3d8b0ba7e905813 Mon Sep 17 00:00:00 2001 From: Srlion Date: Tue, 21 Sep 2021 19:35:28 +0200 Subject: [PATCH] Add luaL_newstate --- gmod/src/lua/import.rs | 4 ++++ gmod/src/lua/lua_state.rs | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/gmod/src/lua/import.rs b/gmod/src/lua/import.rs index d2de566..afff2a7 100644 --- a/gmod/src/lua/import.rs +++ b/gmod/src/lua/import.rs @@ -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"), diff --git a/gmod/src/lua/lua_state.rs b/gmod/src/lua/lua_state.rs index 93ec317..d9342ef 100644 --- a/gmod/src/lua/lua_state.rs +++ b/gmod/src/lua/lua_state.rs @@ -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 { + 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.