Move lua 5.1 support under new "lua51" feature

This commit is contained in:
Alex Orlenko 2019-11-04 16:31:19 +00:00
parent 6874c2e004
commit ae677b0918
12 changed files with 63 additions and 55 deletions

View file

@ -21,6 +21,7 @@ members = [
[features]
default = ["lua53"]
lua53 = []
lua51 = []
luajit = []
[dependencies]

View file

@ -107,8 +107,14 @@ fn main() {
// Find lua via pkg-config
#[cfg(all(feature = "lua53", feature = "luajit"))]
panic!("Cannot enable lua53 and luajit simultaneously");
#[cfg(not(any(feature = "lua53", feature = "lua51", feature = "luajit")))]
panic!("You must enable one of the features: lua53, lua51, luajit");
#[cfg(all(feature = "lua53", any(feature = "lua51", feature = "luajit")))]
panic!("You can enable only one of the features: lua53, lua51, luajit");
#[cfg(all(feature = "lua51", feature = "luajit"))]
panic!("You can enable only one of the features: lua53, lua51, luajit");
#[cfg(feature = "lua53")]
{
@ -126,7 +132,7 @@ fn main() {
};
}
#[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
#[cfg(feature = "lua51")]
{
let mut lua = pkg_config::Config::new()
.range_version((Bound::Included("5.1"), Bound::Excluded("5.2")))

View file

@ -31,7 +31,7 @@ use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State};
#[cfg(feature = "lua53")]
pub use super::glue::LUAL_NUMSIZES;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use super::compat53::{
luaL_checkversion, luaL_getmetafield, luaL_getsubtable, luaL_len, luaL_loadbufferx,
luaL_newmetatable, luaL_requiref, luaL_setfuncs, luaL_setmetatable, luaL_testudata,
@ -63,7 +63,7 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "luaL_getmetafield"]
pub fn luaL_getmetafield_51(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
@ -89,7 +89,7 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "luaL_newmetatable"]
pub fn luaL_newmetatable_51(L: *mut lua_State, tname: *const c_char) -> c_int;
@ -127,7 +127,7 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn luaL_loadfilex(L: *mut lua_State, filename: *const c_char, mode: *const c_char)
-> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn luaL_loadfile(L: *mut lua_State, filename: *const c_char) -> c_int;
}
@ -146,7 +146,7 @@ extern "C" {
name: *const c_char,
mode: *const c_char,
) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn luaL_loadbuffer(
L: *mut lua_State,
buff: *const c_char,

View file

@ -33,7 +33,7 @@ use super::luaconf;
pub use super::glue::{LUA_RELEASE, LUA_VERSION, LUA_VERSION_NUM};
pub use super::glue::LUA_REGISTRYINDEX;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use super::glue::{LUA_ENVIRONINDEX, LUA_GLOBALSINDEX};
pub const LUA_SIGNATURE: &'static [u8] = b"\x1bLua";
@ -41,7 +41,7 @@ pub const LUA_SIGNATURE: &'static [u8] = b"\x1bLua";
// option for multiple returns in 'lua_pcall' and 'lua_call'
pub const LUA_MULTRET: c_int = -1;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use super::compat53::{
lua_absindex, lua_compare, lua_copy, lua_getextraspace, lua_getfield, lua_geti, lua_gettable,
lua_getuservalue, lua_isinteger, lua_len, lua_pushglobaltable, lua_pushlstring, lua_pushstring,
@ -63,7 +63,7 @@ pub const LUA_ERRSYNTAX: c_int = 3;
pub const LUA_ERRMEM: c_int = 4;
#[cfg(feature = "lua53")]
pub const LUA_ERRGCMM: c_int = 5;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub const LUA_ERRERR: c_int = 5;
#[cfg(feature = "lua53")]
pub const LUA_ERRERR: c_int = 6;
@ -150,11 +150,11 @@ extern "C" {
pub fn lua_gettop(L: *mut lua_State) -> c_int;
pub fn lua_settop(L: *mut lua_State, idx: c_int);
pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_remove(L: *mut lua_State, idx: c_int);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_insert(L: *mut lua_State, idx: c_int);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_replace(L: *mut lua_State, idx: c_int);
#[cfg(feature = "lua53")]
pub fn lua_rotate(L: *mut lua_State, idx: c_int, n: c_int);
@ -174,17 +174,17 @@ extern "C" {
pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number;
#[cfg(feature = "lua53")]
pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_tointeger(L: *mut lua_State, idx: c_int) -> lua_Integer;
#[cfg(feature = "lua53")]
pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
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;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
#[cfg(feature = "lua53")]
pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
@ -234,10 +234,10 @@ pub const LUA_OPLT: c_int = 1;
pub const LUA_OPLE: c_int = 2;
extern "C" {
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
#[cfg(feature = "lua53")]
pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
@ -251,13 +251,13 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_pushlstring"]
pub fn lua_pushlstring_51(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char;
#[cfg(feature = "lua53")]
pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_pushstring"]
pub fn lua_pushstring_51(L: *mut lua_State, s: *const c_char) -> *const c_char;
@ -277,13 +277,13 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_gettable"]
pub fn lua_gettable_51(L: *mut lua_State, idx: c_int) -> c_int;
#[cfg(feature = "lua53")]
pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_getfield"]
pub fn lua_getfield_51(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
@ -292,13 +292,13 @@ extern "C" {
#[cfg(feature = "lua53")]
pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_rawget"]
pub fn lua_rawget_51(L: *mut lua_State, idx: c_int);
#[cfg(feature = "lua53")]
pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_rawgeti"]
pub fn lua_rawgeti_51(L: *mut lua_State, idx: c_int, n: lua_Integer);
@ -310,7 +310,7 @@ extern "C" {
pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
#[cfg(feature = "lua53")]
pub fn lua_getuservalue(L: *mut lua_State, idx: c_int) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
}
@ -329,7 +329,7 @@ extern "C" {
pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
#[cfg(feature = "lua53")]
pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
}
@ -352,9 +352,9 @@ extern "C" {
ctx: lua_KContext,
k: Option<lua_KFunction>,
) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
pub fn lua_load(
L: *mut lua_State,
@ -393,12 +393,12 @@ extern "C" {
ctx: lua_KContext,
k: Option<lua_KFunction>,
) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
#[cfg(feature = "lua53")]
pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[link_name = "lua_resume"]
pub fn lua_resume_51(L: *mut lua_State, narg: c_int) -> c_int;
@ -530,13 +530,13 @@ pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_ch
lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len())
}
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[inline(always)]
pub unsafe fn lua_setglobal(L: *mut lua_State, var: *const c_char) {
lua_setfield(L, LUA_GLOBALSINDEX, var)
}
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[inline(always)]
pub unsafe fn lua_getglobal(L: *mut lua_State, var: *const c_char) -> c_int {
lua_getfield(L, LUA_GLOBALSINDEX, var)
@ -629,7 +629,7 @@ pub struct lua_Debug {
i_ci: *mut c_void,
}
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
#[repr(C)]
pub struct lua_Debug {
pub event: c_int,

View file

@ -39,7 +39,7 @@ pub use self::lua::{
#[cfg(feature = "lua53")]
pub use self::lua::{lua_KContext, lua_KFunction, lua_Unsigned};
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use self::lua::lua_setfenv;
// C API functions
@ -203,7 +203,7 @@ pub use self::lua::{
LUA_OPUNM, LUA_RIDX_GLOBALS, LUA_RIDX_MAINTHREAD,
};
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use self::lua::{LUA_ENVIRONINDEX, LUA_GLOBALSINDEX};
// constants from lauxlib.h
@ -226,7 +226,7 @@ mod glue {
include!(concat!(env!("OUT_DIR"), "/glue.rs"));
}
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
mod compat53;
mod lauxlib;

View file

@ -16,7 +16,7 @@ use crate::table::Table;
use crate::thread::Thread;
use crate::types::{Callback, Integer, LightUserData, LuaRef, Number, RegistryKey};
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods};
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
use crate::util::set_main_state;
use crate::util::{
assert_stack, callback_error, check_stack, get_main_state, get_userdata, get_wrapped_error,
@ -84,7 +84,7 @@ impl Lua {
ffi::luaL_requiref(state, cstr!("package"), ffi::luaopen_package, 1);
#[cfg(feature = "lua53")]
ffi::lua_pop(state, 9);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
ffi::lua_pop(state, 7);
let mut lua = Lua::init_from_ptr(state);
@ -97,7 +97,7 @@ impl Lua {
pub unsafe fn init_from_ptr(state: *mut ffi::lua_State) -> Lua {
#[cfg(feature = "lua53")]
let main_state = get_main_state(state);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
let main_state = {
set_main_state(state);
state
@ -316,7 +316,7 @@ impl Lua {
self.push_value(env)?;
#[cfg(feature = "lua53")]
ffi::lua_setupvalue(self.state, -2, 1);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
ffi::lua_setfenv(self.state, -2);
}
Ok(Function(self.pop_ref()))
@ -507,7 +507,7 @@ impl Lua {
assert_stack(self.state, 2);
#[cfg(feature = "lua53")]
ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_GLOBALS);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
ffi::lua_pushvalue(self.state, ffi::LUA_GLOBALSINDEX);
Table(self.pop_ref())
}

View file

@ -175,7 +175,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
assert_stack(lua.state, 1);
lua.push_ref(&u.0);
ffi::lua_getuservalue(lua.state, -1);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
{
ffi::lua_pushinteger(lua.state, 1);
ffi::lua_gettable(lua.state, -2);
@ -247,7 +247,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
push_userdata(lua.state, ())?;
#[cfg(feature = "lua53")]
ffi::lua_pushlightuserdata(lua.state, data.as_ptr() as *mut c_void);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
protect_lua_closure(lua.state, 0, 1, |state| {
// Lua 5.1 allows to store only table. Then we will wrap the value.
ffi::lua_createtable(state, 1, 0);

View file

@ -333,7 +333,7 @@ impl<'lua> AnyUserData<'lua> {
/// [`get_user_value`]: #method.get_user_value
pub fn set_user_value<V: ToLua<'lua>>(&self, v: V) -> Result<()> {
let lua = self.0.lua;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
let v = {
// Lua 5.1 allows to store only table. Then we will wrap the value.
let t = lua.create_table()?;
@ -364,7 +364,7 @@ impl<'lua> AnyUserData<'lua> {
ffi::lua_getuservalue(lua.state, -1);
lua.pop_value()
};
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
return crate::Table::from_lua(res, lua)?.get(1);
#[cfg(feature = "lua53")]
V::from_lua(res, lua)

View file

@ -444,7 +444,7 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
}
// Does not call lua_checkstack, uses 2 stack spaces.
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub unsafe fn set_main_state(state: *mut ffi::lua_State) {
ffi::lua_pushlightuserdata(state, &MAIN_THREAD_REGISTRY_KEY as *const u8 as *mut c_void);
ffi::lua_pushthread(state);
@ -455,7 +455,7 @@ pub unsafe fn set_main_state(state: *mut ffi::lua_State) {
pub unsafe fn get_main_state(state: *mut ffi::lua_State) -> *mut ffi::lua_State {
#[cfg(feature = "lua53")]
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_MAINTHREAD);
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
{
ffi::lua_pushlightuserdata(state, &MAIN_THREAD_REGISTRY_KEY as *const u8 as *mut c_void);
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
@ -751,7 +751,7 @@ unsafe fn get_destructed_userdata_metatable(state: *mut ffi::lua_State) {
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
}
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
static MAIN_THREAD_REGISTRY_KEY: u8 = 0;
static ERROR_METATABLE_REGISTRY_KEY: u8 = 0;
static PANIC_METATABLE_REGISTRY_KEY: u8 = 0;

View file

@ -1,3 +1,4 @@
#[cfg(any(feature = "lua53", feature = "lua51"))]
#[test]
fn test_compile_fail() {
let t = trybuild::TestCases::new();

View file

@ -402,7 +402,7 @@ fn test_num_conversion() -> Result<()> {
assert_eq!(lua.load("1.0").eval::<f64>()?, 1.0);
#[cfg(feature = "lua53")]
assert_eq!(lua.load("1.0").eval::<String>()?, "1.0");
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
assert_eq!(lua.load("1.0").eval::<String>()?, "1");
assert_eq!(lua.load("1.5").eval::<i64>()?, 1);
@ -435,7 +435,7 @@ fn test_pcall_xpcall() -> Result<()> {
assert!(lua.load("xpcall(function() end)").exec().is_err());
// Lua 5.3 / LuaJIT compatible version of xpcall
#[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
#[cfg(feature = "lua51")]
lua.load(
r#"
local xpcall_orig = xpcall
@ -482,7 +482,7 @@ fn test_pcall_xpcall() -> Result<()> {
globals.get::<_, std::string::String>("xpcall_error")?,
"testerror"
);
#[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
#[cfg(feature = "lua51")]
assert!(globals
.get::<_, String>("xpcall_error")?
.to_str()?
@ -813,14 +813,14 @@ fn context_thread() -> Result<()> {
#[cfg(feature = "lua53")]
f.call::<_, ()>(lua.current_thread())?;
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
f.call::<_, ()>(Nil)?;
Ok(())
}
#[test]
#[cfg(not(feature = "lua53"))]
#[cfg(any(feature = "lua51", feature = "luajit"))]
fn context_thread_51() -> Result<()> {
let lua = Lua::new();

View file

@ -113,7 +113,7 @@ fn coroutine_from_closure() -> Result<()> {
#[cfg(any(feature = "lua53", feature = "luajit"))]
let thrd: Thread = lua.load("coroutine.create(main)").eval()?;
#[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
#[cfg(feature = "lua51")]
let thrd: Thread = lua
.load("coroutine.create(function(...) return main(unpack(arg)) end)")
.eval()?;