Skip to content

Commit

Permalink
Merge remote-tracking branch 'original/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	mlua-sys/src/lua51/lua.rs
#	mlua-sys/src/lua52/lua.rs
#	mlua-sys/src/lua53/lua.rs
#	mlua-sys/src/lua54/lua.rs
  • Loading branch information
bytedream committed Dec 14, 2023
2 parents 83c38e7 + 59974d7 commit e62638e
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ num-traits = { version = "0.2.14" }
rustc-hash = "1.0"
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }
serde = { version = "1.0", optional = true }
erased-serde = { version = "0.3", optional = true }
erased-serde = { version = "0.4", optional = true }
serde-value = { version = "0.7", optional = true }
parking_lot = { version = "0.12", optional = true }

Expand Down
1 change: 1 addition & 0 deletions mlua-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub const SYS_MIN_ALIGN: usize = 8;
target_arch = "sparc64",
target_arch = "riscv64",
target_arch = "wasm64",
target_arch = "loongarch64",
))]
#[doc(hidden)]
pub const SYS_MIN_ALIGN: usize = 16;
Expand Down
6 changes: 0 additions & 6 deletions mlua-sys/src/lua51/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, ar: *mut lua_Debug) {
// lua ported functions
//

#[inline(always)]
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!()
}

#[inline(always)]
pub unsafe fn lua_absindex(L: *mut lua_State, mut idx: c_int) -> c_int {
if idx < 0 && idx > LUA_REGISTRYINDEX {
Expand Down
10 changes: 9 additions & 1 deletion mlua-sys/src/lua51/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,21 @@ extern "C-unwind" {
#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
extern "C-unwind" {
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
}

// lua_error does not return but is declared to return int, and Rust translates
// ! to void which can cause link-time errors if the platform linker is aware
// of return types and requires they match (for example: wasm does this).
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!();
}

//
// Some useful macros (implemented as Rust functions)
//
Expand Down
6 changes: 0 additions & 6 deletions mlua-sys/src/lua52/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ unsafe fn compat53_reverse(L: *mut lua_State, mut a: c_int, mut b: c_int) {
// lua ported functions
//

#[inline(always)]
pub unsafe fn lua_error(L: *mut crate::lua_State) -> ! {
crate::lua_error_(L);
unreachable!()
}

pub unsafe fn lua_rotate(L: *mut lua_State, mut idx: c_int, mut n: c_int) {
idx = lua_absindex(L, idx);
if n > 0 {
Expand Down
17 changes: 10 additions & 7 deletions mlua-sys/src/lua52/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,22 @@ extern "C-unwind" {
// Miscellaneous functions
//
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
}

// lua_error does not return but is declared to return int, and Rust translates
// ! to void which can cause link-time errors if the platform linker is aware
// of return types and requires they match (for example: wasm does this).
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!();
}

//
// Some useful macros (implemented as Rust functions)
//
Expand Down Expand Up @@ -454,12 +462,7 @@ extern "C-unwind" {
pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);

pub fn lua_sethook(
L: *mut lua_State,
func: Option<lua_Hook>,
mask: c_int,
count: c_int,
) -> c_int;
pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
Expand Down
6 changes: 0 additions & 6 deletions mlua-sys/src/lua53/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ use std::os::raw::c_int;

use super::lua::*;

#[inline(always)]
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!()
}

#[inline(always)]
pub unsafe fn lua_resume(
L: *mut lua_State,
Expand Down
10 changes: 9 additions & 1 deletion mlua-sys/src/lua53/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ extern "C-unwind" {
// Miscellaneous functions
//
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
Expand All @@ -324,6 +324,14 @@ extern "C-unwind" {
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
}

// lua_error does not return but is declared to return int, and Rust translates
// ! to void which can cause link-time errors if the platform linker is aware
// of return types and requires they match (for example: wasm does this).
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!();
}

//
// Some useful macros (implemented as Rust functions)
//
Expand Down
15 changes: 0 additions & 15 deletions mlua-sys/src/lua54/compat.rs

This file was deleted.

18 changes: 16 additions & 2 deletions mlua-sys/src/lua54/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,19 @@ extern "C-unwind" {
pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
#[link_name = "lua_rawlen"]
pub fn lua_rawlen_(L: *mut lua_State, idx: c_int) -> lua_Unsigned;
fn lua_rawlen_(L: *mut lua_State, idx: c_int) -> lua_Unsigned;
pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
}

// lua_rawlen's return type changed from size_t to lua_Unsigned int in Lua 5.4.
// This adapts the crate API to the new Lua ABI.
pub unsafe fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize {
lua_rawlen_(L, idx) as usize
}

//
// Comparison and arithmetic functions
//
Expand Down Expand Up @@ -338,7 +344,7 @@ extern "C-unwind" {
// Miscellaneous functions
//
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
Expand All @@ -350,6 +356,14 @@ extern "C-unwind" {
pub fn lua_closeslot(L: *mut lua_State, idx: c_int);
}

// lua_error does not return but is declared to return int, and Rust translates
// ! to void which can cause link-time errors if the platform linker is aware
// of return types and requires they match (for example: wasm does this).
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!();
}

//
// Some useful macros (implemented as Rust functions)
//
Expand Down
2 changes: 0 additions & 2 deletions mlua-sys/src/lua54/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Low level bindings to Lua 5.4.
pub use compat::*;
pub use lauxlib::*;
pub use lua::*;
pub use lualib::*;

pub mod compat;
pub mod lauxlib;
pub mod lua;
pub mod lualib;
7 changes: 3 additions & 4 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use std::slice;

use crate::error::{Error, Result};
use crate::lua::Lua;
use crate::memory::MemoryState;
use crate::table::Table;
use crate::types::{Callback, LuaRef, MaybeSend};
use crate::util::{
assert_stack, check_stack, error_traceback, linenumber_to_usize, pop_error, ptr_to_lossy_str,
ptr_to_str, StackGuard,
assert_stack, check_stack, linenumber_to_usize, pop_error, ptr_to_lossy_str, ptr_to_str,
StackGuard,
};
use crate::value::{FromLuaMulti, IntoLua, IntoLuaMulti, Value};

Expand Down Expand Up @@ -131,7 +130,7 @@ impl<'lua> Function<'lua> {
check_stack(state, 2)?;

// Push error handler
MemoryState::relax_limit_with(state, || ffi::lua_pushcfunction(state, error_traceback));
lua.push_error_traceback();
let stack_start = ffi::lua_gettop(state);
// Push function and the arguments
lua.push_ref(&self.0);
Expand Down
54 changes: 39 additions & 15 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use crate::types::{
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataCell};
use crate::userdata_impl::{UserDataProxy, UserDataRegistry};
use crate::util::{
self, assert_stack, check_stack, get_destructed_userdata_metatable, get_gc_metatable,
get_gc_userdata, get_main_state, get_userdata, init_error_registry, init_gc_metatable,
init_userdata_metatable, pop_error, push_gc_userdata, push_string, push_table, rawset_field,
safe_pcall, safe_xpcall, short_type_name, StackGuard, WrappedFailure,
self, assert_stack, check_stack, error_traceback, get_destructed_userdata_metatable,
get_gc_metatable, get_gc_userdata, get_main_state, get_userdata, init_error_registry,
init_gc_metatable, init_userdata_metatable, pop_error, push_gc_userdata, push_string,
push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name, StackGuard, WrappedFailure,
};
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil, Value};

Expand Down Expand Up @@ -499,6 +499,13 @@ impl Lua {
ptr
};

// Store `error_traceback` function on the ref stack
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
{
ffi::lua_pushcfunction(ref_thread, error_traceback);
assert_eq!(ffi::lua_gettop(ref_thread), ExtraData::ERROR_TRACEBACK_IDX);
}

// Create ExtraData
let extra = Arc::new(UnsafeCell::new(ExtraData {
inner: MaybeUninit::uninit(),
Expand Down Expand Up @@ -2601,6 +2608,16 @@ impl Lua {
LuaRef::new(self, index)
}

#[inline]
pub(crate) unsafe fn push_error_traceback(&self) {
let state = self.state();
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
ffi::lua_xpush(self.ref_thread(), state, ExtraData::ERROR_TRACEBACK_IDX);
// Lua 5.2+ support light C functions that does not require extra allocations
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
ffi::lua_pushcfunction(state, error_traceback);
}

unsafe fn register_userdata_metatable<'lua, T: 'static>(
&'lua self,
mut registry: UserDataRegistry<'lua, T>,
Expand Down Expand Up @@ -2944,19 +2961,23 @@ impl Lua {
let fut = &mut (*upvalue).data;
let mut ctx = Context::from_waker(lua.waker());
match fut.as_mut().poll(&mut ctx) {
Poll::Pending => Ok(0),
Poll::Pending => {
ffi::lua_pushnil(state);
let pending = &ASYNC_POLL_PENDING as *const u8 as *mut c_void;
ffi::lua_pushlightuserdata(state, pending);
Ok(2)
}
Poll::Ready(nresults) => {
let nresults = nresults?;
match nresults {
0..=2 => {
match nresults? {
nresults @ 0..=2 => {
// Fast path for up to 2 results without creating a table
ffi::lua_pushinteger(state, nresults as _);
if nresults > 0 {
ffi::lua_insert(state, -nresults - 1);
}
Ok(nresults + 1)
}
_ => {
nresults => {
let results = MultiValue::from_stack_multi(nresults, lua)?;
ffi::lua_pushinteger(state, nresults as _);
lua.push_value(Value::Table(lua.create_sequence_from(results)?))?;
Expand Down Expand Up @@ -3000,20 +3021,17 @@ impl Lua {

let coroutine = self.globals().get::<_, Table>("coroutine")?;

let env = self.create_table_with_capacity(0, 4)?;
let env = self.create_table_with_capacity(0, 3)?;
env.set("get_poll", get_poll)?;
// Cache `yield` function
env.set("yield", coroutine.get::<_, Function>("yield")?)?;
unsafe {
env.set("unpack", self.create_c_function(unpack)?)?;
}
env.set("pending", {
LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut c_void)
})?;

self.load(
r#"
local poll = get_poll(...)
local pending, yield, unpack = pending, yield, unpack
while true do
local nres, res, res2 = poll()
if nres ~= nil then
Expand All @@ -3027,7 +3045,7 @@ impl Lua {
return unpack(res, nres)
end
end
yield(pending)
yield(res) -- `res` is a "pending" value
end
"#,
)
Expand Down Expand Up @@ -3211,6 +3229,12 @@ impl LuaInner {
}
}

impl ExtraData {
// Index of `error_traceback` function in auxiliary thread stack
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
const ERROR_TRACEBACK_IDX: c_int = 1;
}

struct StateGuard<'a>(&'a LuaInner, *mut ffi::lua_State);

impl<'a> StateGuard<'a> {
Expand Down
4 changes: 2 additions & 2 deletions src/userdata_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'lua, T: 'static> UserDataRegistry<'lua, T> {
MR: Future<Output = Result<R>> + 's,
R: IntoLuaMulti<'lua>,
{
let name = get_function_name::<T>(name);
let name = Arc::new(get_function_name::<T>(name));
let method = Arc::new(method);

Box::new(move |lua, mut args| unsafe {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'lua, T: 'static> UserDataRegistry<'lua, T> {
MR: Future<Output = Result<R>> + 's,
R: IntoLuaMulti<'lua>,
{
let name = get_function_name::<T>(name);
let name = Arc::new(get_function_name::<T>(name));
let method = Arc::new(method);

Box::new(move |lua, mut args| unsafe {
Expand Down

0 comments on commit e62638e

Please sign in to comment.