Add LuaJIT support

This commit is contained in:
Alex Orlenko 2019-10-16 10:56:44 +01:00
parent 29aa25a48b
commit 6f42a6cca9
12 changed files with 141 additions and 11 deletions

View File

@ -3,7 +3,7 @@ name = "mlua"
version = "0.1.0" version = "0.1.0"
authors = ["Aleksandr Orlenko <zxteam@pm.me>", "kyren <catherine@chucklefish.org>"] authors = ["Aleksandr Orlenko <zxteam@pm.me>", "kyren <catherine@chucklefish.org>"]
edition = "2018" 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" repository = "https://github.com/khvzak/mlua"
documentation = "https://docs.rs/mlua" documentation = "https://docs.rs/mlua"
readme = "README.md" readme = "README.md"
@ -21,6 +21,7 @@ members = [
[features] [features]
default = ["lua53"] default = ["lua53"]
lua53 = [] lua53 = []
luajit = []
[dependencies] [dependencies]
num-traits = { version = "0.2.6" } num-traits = { version = "0.2.6" }

View File

@ -107,6 +107,9 @@ fn main() {
// Find lua via pkg-config // Find lua via pkg-config
#[cfg(all(feature = "lua53", feature = "luajit"))]
panic!("Cannot enable lua53 and luajit simultaneously");
#[cfg(feature = "lua53")] #[cfg(feature = "lua53")]
{ {
let mut lua = pkg_config::Config::new() 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() let mut lua = pkg_config::Config::new()
.range_version((Bound::Included("5.1"), Bound::Excluded("5.2"))) .range_version((Bound::Included("5.1"), Bound::Excluded("5.2")))
@ -138,4 +141,16 @@ fn main() {
Err(err) => panic!(err), 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),
};
}
} }

View File

@ -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 bstr::{BStr, BString};
use mlua::{Lua, Result}; use mlua::{Lua, Result};

View File

@ -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}; use mlua::{Function, Lua, Result, String};
#[test] #[test]

View File

@ -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 std::sync::Arc;
use mlua::{Lua, Result, UserData}; use mlua::{Lua, Result, UserData};

View File

@ -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::cell::Cell;
use std::rc::Rc; use std::rc::Rc;

View File

@ -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 std::borrow::Cow;
use mlua::{Lua, Result, String}; use mlua::{Lua, Result, String};

View File

@ -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}; use mlua::{Lua, Nil, Result, Table, Value};
#[test] #[test]

View File

@ -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::iter::FromIterator;
use std::panic::catch_unwind; use std::panic::catch_unwind;
use std::sync::Arc; use std::sync::Arc;
@ -226,12 +237,12 @@ fn test_error() -> Result<()> {
assert!(no_error.call::<_, ()>(()).is_ok()); assert!(no_error.call::<_, ()>(()).is_ok());
match lua_error.call::<_, ()>(()) { match lua_error.call::<_, ()>(()) {
Err(Error::RuntimeError(_)) => {} Err(Error::RuntimeError(_)) => {}
Err(_) => panic!("error is not RuntimeError kind"), Err(e) => panic!("error is not RuntimeError kind, got {:?}", e),
_ => panic!("error not returned"), _ => panic!("error not returned"),
} }
match rust_error.call::<_, ()>(()) { match rust_error.call::<_, ()>(()) {
Err(Error::CallbackError { .. }) => {} Err(Error::CallbackError { .. }) => {}
Err(_) => panic!("error is not CallbackError kind"), Err(e) => panic!("error is not CallbackError kind, got {:?}", e),
_ => panic!("error not returned"), _ => panic!("error not returned"),
} }
@ -423,8 +434,8 @@ fn test_pcall_xpcall() -> Result<()> {
assert!(lua.load("xpcall()").exec().is_err()); assert!(lua.load("xpcall()").exec().is_err());
assert!(lua.load("xpcall(function() end)").exec().is_err()); assert!(lua.load("xpcall(function() end)").exec().is_err());
// Lua5.3 compatible version of xpcall // Lua 5.3 / LuaJIT compatible version of xpcall
#[cfg(not(feature = "lua53"))] #[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
lua.load( lua.load(
r#" r#"
local xpcall_orig = xpcall 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::<_, String>("pcall_error")?, "testerror");
assert_eq!(globals.get::<_, bool>("xpcall_statusr")?, false); assert_eq!(globals.get::<_, bool>("xpcall_statusr")?, false);
#[cfg(feature = "lua53")] #[cfg(any(feature = "lua53", feature = "luajit"))]
assert_eq!(globals.get::<_, String>("xpcall_error")?, "testerror"); assert_eq!(
#[cfg(not(feature = "lua53"))] globals.get::<_, std::string::String>("xpcall_error")?,
"testerror"
);
#[cfg(all(not(feature = "lua53"), not(feature = "luajit")))]
assert!(globals assert!(globals
.get::<_, String>("xpcall_error")? .get::<_, String>("xpcall_error")?
.to_str()? .to_str()?
@ -653,6 +667,7 @@ fn too_many_arguments() -> Result<()> {
} }
#[test] #[test]
#[cfg(not(feature = "luajit"))]
fn too_many_recursions() -> Result<()> { fn too_many_recursions() -> Result<()> {
let lua = Lua::new(); let lua = Lua::new();
let f = lua let f = lua

View File

@ -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 std::panic::catch_unwind;
use mlua::{Error, Function, Lua, Result, Thread, ThreadStatus}; use mlua::{Error, Function, Lua, Result, Thread, ThreadStatus};
@ -100,9 +111,9 @@ fn coroutine_from_closure() -> Result<()> {
let thrd_main = lua.create_function(|_, ()| Ok(()))?; let thrd_main = lua.create_function(|_, ()| Ok(()))?;
lua.globals().set("main", thrd_main)?; lua.globals().set("main", thrd_main)?;
#[cfg(feature = "lua53")] #[cfg(any(feature = "lua53", feature = "luajit"))]
let thrd: Thread = lua.load("coroutine.create(main)").eval()?; 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 let thrd: Thread = lua
.load("coroutine.create(function(...) return main(unpack(arg)) end)") .load("coroutine.create(function(...) return main(unpack(arg)) end)")
.eval()?; .eval()?;

View File

@ -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 std::os::raw::c_void;
use mlua::{Function, LightUserData, Lua, Result}; use mlua::{Function, LightUserData, Lua, Result};

View File

@ -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 std::sync::Arc;
use mlua::{ use mlua::{