Merge pull request #1 from Srlion/master

Add load_buffer to load bytecode
This commit is contained in:
William 2021-09-20 19:16:12 +01:00 committed by GitHub
commit ac09fe59ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -75,6 +75,7 @@ lazy_static::lazy_static! {
pub(crate) struct LuaShared { pub(crate) struct LuaShared {
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 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_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)>,
@ -134,6 +135,7 @@ impl LuaShared {
Self { Self {
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"),
lua_getfield: find_symbol!("lua_getfield"), lua_getfield: find_symbol!("lua_getfield"),
lua_pushvalue: find_symbol!("lua_pushvalue"), lua_pushvalue: find_symbol!("lua_pushvalue"),
lua_pushboolean: find_symbol!("lua_pushboolean"), lua_pushboolean: find_symbol!("lua_pushboolean"),

View File

@ -147,6 +147,15 @@ impl LuaState {
} }
} }
pub unsafe fn load_buffer(&self, buff: &[u8], name: LuaString) -> Result<(), LuaError> {
let lua_error_code = (LUA_SHARED.lual_loadbuffer)(*self, buff.as_ptr() as LuaString, buff.len(), name);
if lua_error_code == 0 {
Ok(())
} else {
Err(LuaError::from_lua_state(*self, lua_error_code))
}
}
pub unsafe fn load_file(&self, path: LuaString) -> Result<(), LuaError> { pub unsafe fn load_file(&self, path: LuaString) -> Result<(), LuaError> {
let lua_error_code = (LUA_SHARED.lual_loadfile)(*self, path); let lua_error_code = (LUA_SHARED.lual_loadfile)(*self, path);
if lua_error_code == 0 { if lua_error_code == 0 {