Skip to content

Commit

Permalink
Change chunk env to use luau's load env parameter (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjcscat authored Aug 25, 2024
1 parent 23d4e25 commit 7bfd327
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 5 additions & 4 deletions mlua-sys/src/luau/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub unsafe fn luaL_loadbufferx(
mut size: usize,
name: *const c_char,
mode: *const c_char,
env: c_int
) -> c_int {
extern "C" {
fn free(p: *mut c_void);
Expand All @@ -345,12 +346,12 @@ pub unsafe fn luaL_loadbufferx(

if chunk_is_text {
let data = luau_compile_(data, size, ptr::null_mut(), &mut size);
let ok = luau_load(L, name, data, size, 0) == 0;
let ok = luau_load(L, name, data, size, env) == 0;
free(data as *mut c_void);
if !ok {
return LUA_ERRSYNTAX;
}
} else if luau_load(L, name, data, size, 0) != 0 {
} else if luau_load(L, name, data, size, env) != 0 {
return LUA_ERRSYNTAX;
}
LUA_OK
Expand All @@ -361,9 +362,9 @@ pub unsafe fn luaL_loadbuffer(
L: *mut lua_State,
data: *const c_char,
size: usize,
name: *const c_char,
name: *const c_char
) -> c_int {
luaL_loadbufferx(L, data, size, name, ptr::null())
luaL_loadbufferx(L, data, size, name, ptr::null(), 0)
}

#[inline(always)]
Expand Down
11 changes: 10 additions & 1 deletion src/state/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,22 @@ impl RawLua {
source.len(),
name.map(|n| n.as_ptr()).unwrap_or_else(ptr::null),
mode_str,
#[cfg(feature="luau")]
match &env {
Some(env) => {
self.push_ref(&env.0);
-1
}
_ => 0
},
) {
ffi::LUA_OK => {
if let Some(env) = env {
#[cfg(not(feature="luau"))]
self.push_ref(&env.0);
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
ffi::lua_setupvalue(state, -2, 1);
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
ffi::lua_setfenv(state, -2);
}

Expand Down

0 comments on commit 7bfd327

Please sign in to comment.