diff --git a/Cargo.toml b/Cargo.toml index 75b3223..f33dcdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "mlua" version = "0.1.0" authors = ["Aleksandr Orlenko ", "kyren "] edition = "2018" -description = "High level bindings to Lua 5.3/5.1 for writing modules" +description = "High level bindings to Lua 5.1 / LuaJIT / Lua 5.3" repository = "https://github.com/khvzak/mlua" documentation = "https://docs.rs/mlua" readme = "README.md" @@ -21,6 +21,7 @@ members = [ [features] default = ["lua53"] lua53 = [] +luajit = [] [dependencies] num-traits = { version = "0.2.6" } diff --git a/build.rs b/build.rs index a675114..4f91483 100644 --- a/build.rs +++ b/build.rs @@ -107,6 +107,9 @@ fn main() { // Find lua via pkg-config + #[cfg(all(feature = "lua53", feature = "luajit"))] + panic!("Cannot enable lua53 and luajit simultaneously"); + #[cfg(feature = "lua53")] { let mut lua = pkg_config::Config::new() @@ -123,7 +126,7 @@ fn main() { }; } - #[cfg(not(feature = "lua53"))] + #[cfg(all(not(feature = "lua53"), not(feature = "luajit")))] { let mut lua = pkg_config::Config::new() .range_version((Bound::Included("5.1"), Bound::Excluded("5.2"))) @@ -138,4 +141,16 @@ fn main() { Err(err) => panic!(err), }; } + + #[cfg(feature = "luajit")] + { + let lua = pkg_config::Config::new() + .range_version((Bound::Included("2.0.5"), Bound::Excluded("2.1.0"))) + .probe("luajit"); + + match lua { + Ok(lua) => build_glue(&lua.include_paths), + Err(err) => panic!(err), + }; + } } diff --git a/tests/byte_string.rs b/tests/byte_string.rs index 3b05ee5..ecafd50 100644 --- a/tests/byte_string.rs +++ b/tests/byte_string.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use bstr::{BStr, BString}; use mlua::{Lua, Result}; diff --git a/tests/function.rs b/tests/function.rs index cc1c9ab..3744866 100644 --- a/tests/function.rs +++ b/tests/function.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use mlua::{Function, Lua, Result, String}; #[test] diff --git a/tests/memory.rs b/tests/memory.rs index a06ac5f..e3f8063 100644 --- a/tests/memory.rs +++ b/tests/memory.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::sync::Arc; use mlua::{Lua, Result, UserData}; diff --git a/tests/scope.rs b/tests/scope.rs index 54840d6..22f28de 100644 --- a/tests/scope.rs +++ b/tests/scope.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::cell::Cell; use std::rc::Rc; diff --git a/tests/string.rs b/tests/string.rs index abe8aaa..ce61f3c 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::borrow::Cow; use mlua::{Lua, Result, String}; diff --git a/tests/table.rs b/tests/table.rs index 1fa20cc..14ccb87 100644 --- a/tests/table.rs +++ b/tests/table.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use mlua::{Lua, Nil, Result, Table, Value}; #[test] diff --git a/tests/tests.rs b/tests/tests.rs index 1c850c8..d4c4cb6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::iter::FromIterator; use std::panic::catch_unwind; use std::sync::Arc; @@ -226,12 +237,12 @@ fn test_error() -> Result<()> { assert!(no_error.call::<_, ()>(()).is_ok()); match lua_error.call::<_, ()>(()) { Err(Error::RuntimeError(_)) => {} - Err(_) => panic!("error is not RuntimeError kind"), + Err(e) => panic!("error is not RuntimeError kind, got {:?}", e), _ => panic!("error not returned"), } match rust_error.call::<_, ()>(()) { Err(Error::CallbackError { .. }) => {} - Err(_) => panic!("error is not CallbackError kind"), + Err(e) => panic!("error is not CallbackError kind, got {:?}", e), _ => panic!("error not returned"), } @@ -423,8 +434,8 @@ fn test_pcall_xpcall() -> Result<()> { assert!(lua.load("xpcall()").exec().is_err()); assert!(lua.load("xpcall(function() end)").exec().is_err()); - // Lua5.3 compatible version of xpcall - #[cfg(not(feature = "lua53"))] + // Lua 5.3 / LuaJIT compatible version of xpcall + #[cfg(all(not(feature = "lua53"), not(feature = "luajit")))] lua.load( r#" local xpcall_orig = xpcall @@ -466,9 +477,12 @@ fn test_pcall_xpcall() -> Result<()> { assert_eq!(globals.get::<_, String>("pcall_error")?, "testerror"); assert_eq!(globals.get::<_, bool>("xpcall_statusr")?, false); - #[cfg(feature = "lua53")] - assert_eq!(globals.get::<_, String>("xpcall_error")?, "testerror"); - #[cfg(not(feature = "lua53"))] + #[cfg(any(feature = "lua53", feature = "luajit"))] + assert_eq!( + globals.get::<_, std::string::String>("xpcall_error")?, + "testerror" + ); + #[cfg(all(not(feature = "lua53"), not(feature = "luajit")))] assert!(globals .get::<_, String>("xpcall_error")? .to_str()? @@ -653,6 +667,7 @@ fn too_many_arguments() -> Result<()> { } #[test] +#[cfg(not(feature = "luajit"))] fn too_many_recursions() -> Result<()> { let lua = Lua::new(); let f = lua diff --git a/tests/thread.rs b/tests/thread.rs index 211e4eb..cd09002 100644 --- a/tests/thread.rs +++ b/tests/thread.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::panic::catch_unwind; use mlua::{Error, Function, Lua, Result, Thread, ThreadStatus}; @@ -100,9 +111,9 @@ fn coroutine_from_closure() -> Result<()> { let thrd_main = lua.create_function(|_, ()| Ok(()))?; lua.globals().set("main", thrd_main)?; - #[cfg(feature = "lua53")] + #[cfg(any(feature = "lua53", feature = "luajit"))] let thrd: Thread = lua.load("coroutine.create(main)").eval()?; - #[cfg(not(feature = "lua53"))] + #[cfg(all(not(feature = "lua53"), not(feature = "luajit")))] let thrd: Thread = lua .load("coroutine.create(function(...) return main(unpack(arg)) end)") .eval()?; diff --git a/tests/types.rs b/tests/types.rs index 72f9484..c5c6c21 100644 --- a/tests/types.rs +++ b/tests/types.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::os::raw::c_void; use mlua::{Function, LightUserData, Lua, Result}; diff --git a/tests/userdata.rs b/tests/userdata.rs index d521b35..1664468 100644 --- a/tests/userdata.rs +++ b/tests/userdata.rs @@ -1,3 +1,14 @@ +#![cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + feature(link_args) +)] + +#[cfg_attr( + all(feature = "luajit", target_os = "macos", target_arch = "x86_64"), + link_args = "-pagezero_size 10000 -image_base 100000000" +)] +extern "system" {} + use std::sync::Arc; use mlua::{