Skip to content
This repository was archived by the owner on Sep 12, 2025. It is now read-only.

Commit b25d2aa

Browse files
committed
Updates to get tests passing with mlua.
This is the first batch.
1 parent 1fcc8be commit b25d2aa

File tree

16 files changed

+64
-54
lines changed

16 files changed

+64
-54
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ categories = [ "api-bindings", "development-tools::ffi" ]
1515
circle-ci = { repository = "mlua-rs/rlua", branch = "master" }
1616

1717
[dependencies]
18-
mlua = { version = "0.9.5" }
18+
mlua = { version = "0.9.5", features = ["macros"] }
1919

2020
[features]
2121
default=["builtin-lua54"]
@@ -34,9 +34,8 @@ system-luajit=["mlua/luajit"]
3434
# Enabled functions from the math module that have been deprecated
3535
#lua-compat-mathlib = []
3636

37-
38-
3937
[dev-dependencies]
4038
rustyline = "13.0"
41-
criterion = "0.5.1"
42-
compiletest_rs = { version = "0.10.2", features = ["stable"] }
39+
#criterion = "0.5.1"
40+
#compiletest_rs = { version = "0.10.2", features = ["stable"] }
41+
bstr = "1.9.0"

examples/custom_lua_path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Lua, Result};
1+
use rlua::{Lua, Result, RluaCompat};
22

33
fn main() -> Result<()> {
44
let lua = Lua::new();
@@ -11,7 +11,7 @@ fn main() -> Result<()> {
1111
// require a module located in the newly added directory
1212
lua_ctx.load("require'new_module'").exec()?;
1313

14-
Ok(())
14+
Ok::<_, rlua::Error>(())
1515
})?;
1616

1717
Ok(())

examples/guided_tour.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::f32;
22
use std::iter::FromIterator;
33

4-
use rlua::{Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic};
4+
use rlua::{Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic, RluaCompat};
55

66
fn main() -> Result<()> {
77
// You can create a new Lua state with `Lua::new()`. This loads the default Lua std library
@@ -22,7 +22,7 @@ fn main() -> Result<()> {
2222
globals.set("string_var", "hello")?;
2323
globals.set("int_var", 42)?;
2424

25-
Ok(())
25+
Ok::<_, rlua::Error>(())
2626
})?;
2727

2828
lua.context(|lua_ctx| {
@@ -35,7 +35,7 @@ fn main() -> Result<()> {
3535
assert_eq!(globals.get::<_, String>("string_var")?, "hello");
3636
assert_eq!(globals.get::<_, i64>("int_var")?, 42);
3737

38-
Ok(())
38+
Ok::<_, rlua::Error>(())
3939
})?;
4040

4141
lua.context(|lua_ctx| {
@@ -52,7 +52,7 @@ fn main() -> Result<()> {
5252
global = 'foo'..'bar'
5353
"#,
5454
)
55-
.set_name("example code")?
55+
.set_name("example code")
5656
.exec()?;
5757
assert_eq!(globals.get::<_, String>("global")?, "foobar");
5858

@@ -167,7 +167,7 @@ fn main() -> Result<()> {
167167
// Here's a worked example that shows many of the features of this API
168168
// together
169169

170-
#[derive(Copy, Clone)]
170+
#[derive(Copy, Clone, mlua::FromLua)]
171171
struct Vec2(f32, f32);
172172

173173
impl UserData for Vec2 {

examples/repl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This example shows a simple read-evaluate-print-loop (REPL).
22
3-
use rlua::{Error, Lua, MultiValue};
3+
use rlua::{Error, Lua, MultiValue, RluaCompat};
44

55
fn main() {
66
Lua::new().context(|lua| {

src/lib.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ pub use mlua::*;
33
pub mod prelude {
44
pub use mlua::prelude::*;
55
pub use super::ToLua;
6+
pub use super::RluaCompat;
67
}
78

89
pub type Context<'lua> = &'lua Lua;
910

11+
pub trait RluaCompat {
12+
fn context<R, F>(&self, f: F) -> R
13+
where F: FnOnce(&Lua) -> R;
14+
}
15+
16+
impl RluaCompat for Lua {
17+
fn context<R, F>(&self, f: F) -> R
18+
where F: FnOnce(&Lua) -> R {
19+
f(self)
20+
}
21+
}
22+
1023
pub use mlua::IntoLua as ToLua;
11-
/*
12-
pub trait ToLua<'lua> {
13-
fn to_lua(self, context: Context<'lua>) -> mlua::Result<Value<'lua>>;
24+
25+
pub trait ToLuaCompat<'lua> {
26+
fn to_lua(self, context: &'lua Lua) -> mlua::Result<Value<'lua>>;
1427
}
1528

16-
impl<'lua, T: IntoLua<'lua>> ToLua<'lua> for T {
17-
fn to_lua(self, context: Context<'lua>) -> mlua::Result<Value<'lua>> {
29+
impl<'lua, T:IntoLua<'lua>> ToLuaCompat<'lua> for T {
30+
fn to_lua(self, context: &'lua Lua) -> mlua::Result<Value<'lua>> {
1831
self.into_lua(context)
1932
}
2033
}
21-
*/
22-

tests/byte_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bstr::{BStr, BString};
2-
use rlua::Lua;
2+
use rlua::{Lua, RluaCompat};
33

44
// Lua 5.1 doesn't have hex escapes
55
#[cfg(not(rlua_lua51))]

tests/conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Integer, Lua, Result, String, Table, ToLua, Value};
1+
use rlua::{Integer, Lua, Result, String, Table, Value, RluaCompat, ToLuaCompat};
22

33
fn valid_float(verify: Result<Value>, expected: f64) {
44
let verify_unwrap = verify.unwrap();

tests/fromtolua.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::Lua;
1+
use rlua::{Lua, RluaCompat};
22

33
#[test]
44
fn test_to_array() {

tests/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rlua::{Function, Lua, String};
1+
use rlua::{Function, Lua, String, RluaCompat};
22

33
#[test]
44
fn test_function() {

tests/hooks.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::cell::RefCell;
22
use std::ops::Deref;
3-
use std::str;
43
use std::sync::{Arc, Mutex};
54

6-
use rlua::{Error, HookTriggers, Lua, Value};
5+
use rlua::{Error, HookTriggers, Lua, Value, RluaCompat};
76

87
#[cfg(not(rlua_luajit))] // LuaJIT gives different results
98
#[test]
@@ -52,8 +51,8 @@ fn function_calls() {
5251
move |_lua, debug| {
5352
let names = debug.names();
5453
let source = debug.source();
55-
let name = names.name.map(|s| str::from_utf8(s).unwrap().to_owned());
56-
let what = source.what.map(|s| str::from_utf8(s).unwrap().to_owned());
54+
let name = names.name.map(|s| s.to_string());
55+
let what = source.what;
5756
hook_output.lock().unwrap().push((name, what));
5857
Ok(())
5958
},
@@ -73,8 +72,8 @@ fn function_calls() {
7372
assert_eq!(
7473
*output,
7574
vec![
76-
(None, Some("main".to_string())),
77-
(Some("len".to_string()), Some("C".to_string()))
75+
(None, "main"),
76+
(Some("len".to_string()), "C")
7877
]
7978
);
8079
#[cfg(rlua_luajit)]
@@ -119,16 +118,18 @@ fn error_within_hook() {
119118
#[test]
120119
fn limit_execution_instructions() {
121120
let lua = Lua::new();
122-
let mut max_instructions = 10000;
121+
let max_instructions = Arc::new(Mutex::new(10000));
122+
let max_int = Arc::clone(&max_instructions);
123123

124124
lua.set_hook(
125125
HookTriggers {
126126
every_nth_instruction: Some(30),
127127
..Default::default()
128128
},
129129
move |_lua, _debug| {
130-
max_instructions -= 30;
131-
if max_instructions < 0 {
130+
let mut max_instructions = max_int.lock().unwrap();
131+
(*max_instructions) -= 30;
132+
if (*max_instructions) < 0 {
132133
Err(Error::RuntimeError("time's up".to_string()))
133134
} else {
134135
Ok(())

0 commit comments

Comments
 (0)