Merge pull request #9 from HybridEidolon/bytecode-chunks

Make Lua::load load binary chunks when unsafe
This commit is contained in:
Alex Orlenko 2020-07-27 14:19:33 +01:00 committed by GitHub
commit 5c8a5e0a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 2 deletions

View File

@ -658,6 +658,9 @@ impl Lua {
/// similar on the returned builder. Code is not even parsed until one of these methods is /// similar on the returned builder. Code is not even parsed until one of these methods is
/// called. /// called.
/// ///
/// If this `Lua` was created with `unsafe_new`, `load` will automatically detect and load
/// chunks of either text or binary type, as if passing `bt` mode to `luaL_loadbufferx`.
///
/// [`Chunk::exec`]: struct.Chunk.html#method.exec /// [`Chunk::exec`]: struct.Chunk.html#method.exec
pub fn load<'lua, 'a, S>(&'lua self, source: &'a S) -> Chunk<'lua, 'a> pub fn load<'lua, 'a, S>(&'lua self, source: &'a S) -> Chunk<'lua, 'a>
where where
@ -680,6 +683,10 @@ impl Lua {
unsafe { unsafe {
let _sg = StackGuard::new(self.state); let _sg = StackGuard::new(self.state);
assert_stack(self.state, 1); assert_stack(self.state, 1);
let mode_string = match self.safe {
true => cstr!("t"),
false => cstr!("bt"),
};
match if let Some(name) = name { match if let Some(name) = name {
ffi::luaL_loadbufferx( ffi::luaL_loadbufferx(
@ -687,7 +694,7 @@ impl Lua {
source.as_ptr() as *const c_char, source.as_ptr() as *const c_char,
source.len(), source.len(),
name.as_ptr() as *const c_char, name.as_ptr() as *const c_char,
cstr!("t"), mode_string,
) )
} else { } else {
ffi::luaL_loadbufferx( ffi::luaL_loadbufferx(
@ -695,7 +702,7 @@ impl Lua {
source.as_ptr() as *const c_char, source.as_ptr() as *const c_char,
source.len(), source.len(),
ptr::null(), ptr::null(),
cstr!("t"), mode_string,
) )
} { } {
ffi::LUA_OK => { ffi::LUA_OK => {