2018-09-16 20:15:51 -04:00
|
|
|
extern crate failure;
|
|
|
|
extern crate rlua;
|
2018-02-09 23:52:05 -05:00
|
|
|
|
2018-02-09 23:35:29 -05:00
|
|
|
use std::iter::FromIterator;
|
2018-08-05 09:51:39 -04:00
|
|
|
use std::panic::catch_unwind;
|
2018-02-06 20:23:16 -05:00
|
|
|
use std::sync::Arc;
|
2018-08-05 09:51:39 -04:00
|
|
|
use std::{error, fmt};
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2018-09-16 20:15:51 -04:00
|
|
|
use failure::err_msg;
|
|
|
|
use rlua::{
|
|
|
|
Error, ExternalError, Function, Lua, Nil, Result, String, Table, UserData, Value, Variadic,
|
|
|
|
};
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-06-21 20:38:58 -04:00
|
|
|
#[test]
|
|
|
|
fn test_load() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let func = lua.load("return 1+2", None).unwrap();
|
|
|
|
let result: i32 = func.call(()).unwrap();
|
|
|
|
assert_eq!(result, 3);
|
|
|
|
|
|
|
|
assert!(lua.load("§$%§&$%&", None).is_err());
|
|
|
|
}
|
|
|
|
|
2017-10-29 17:53:30 -04:00
|
|
|
#[test]
|
2017-12-03 23:01:03 -05:00
|
|
|
fn test_debug() {
|
|
|
|
let lua = unsafe { Lua::new_with_debug() };
|
2017-10-29 17:53:30 -04:00
|
|
|
match lua.eval("debug", None).unwrap() {
|
2017-12-02 18:56:14 -05:00
|
|
|
Value::Table(_) => {}
|
|
|
|
val => panic!("Expected table for debug library, got {:#?}", val),
|
2017-10-29 17:53:30 -04:00
|
|
|
}
|
|
|
|
let traceback_output = lua.eval::<String>("debug.traceback()", None).unwrap();
|
2017-12-02 18:56:14 -05:00
|
|
|
assert_eq!(
|
2018-03-12 16:00:11 -04:00
|
|
|
traceback_output.to_str().unwrap().split("\n").next(),
|
2017-12-02 18:56:14 -05:00
|
|
|
"stack traceback:".into()
|
|
|
|
);
|
2017-10-29 17:53:30 -04:00
|
|
|
}
|
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
#[test]
|
2017-06-21 18:13:49 -04:00
|
|
|
fn test_exec() {
|
2017-05-21 19:50:59 -04:00
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
res = 'foo'..'bar'
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
2017-06-11 01:12:25 -04:00
|
|
|
).unwrap();
|
|
|
|
assert_eq!(globals.get::<_, String>("res").unwrap(), "foobar");
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let module: Table =
|
|
|
|
lua.exec(
|
|
|
|
r#"
|
2017-06-11 01:12:25 -04:00
|
|
|
local module = {}
|
|
|
|
|
|
|
|
function module.func()
|
|
|
|
return "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
return module
|
|
|
|
"#,
|
2018-08-05 09:51:39 -04:00
|
|
|
None,
|
|
|
|
).unwrap();
|
2017-06-18 08:39:18 -04:00
|
|
|
assert!(module.contains_key("func").unwrap());
|
2017-06-15 10:26:39 -04:00
|
|
|
assert_eq!(
|
|
|
|
module
|
2017-07-18 16:21:12 -04:00
|
|
|
.get::<_, Function>("func")
|
2017-06-15 10:26:39 -04:00
|
|
|
.unwrap()
|
|
|
|
.call::<_, String>(())
|
|
|
|
.unwrap(),
|
|
|
|
"hello"
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eval() {
|
|
|
|
let lua = Lua::new();
|
2017-07-16 16:53:32 -04:00
|
|
|
assert_eq!(lua.eval::<i32>("1 + 1", None).unwrap(), 2);
|
|
|
|
assert_eq!(lua.eval::<bool>("false == false", None).unwrap(), true);
|
|
|
|
assert_eq!(lua.eval::<i32>("return 1 + 2", None).unwrap(), 3);
|
|
|
|
match lua.eval::<()>("if true then", None) {
|
2017-10-23 16:42:20 -04:00
|
|
|
Err(Error::SyntaxError {
|
|
|
|
incomplete_input: true,
|
|
|
|
..
|
|
|
|
}) => {}
|
|
|
|
r => panic!(
|
|
|
|
"expected SyntaxError with incomplete_input=true, got {:?}",
|
|
|
|
r
|
|
|
|
),
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lua_multi() {
|
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
function concat(arg1, arg2)
|
|
|
|
return arg1 .. arg2
|
|
|
|
end
|
|
|
|
|
|
|
|
function mreturn()
|
|
|
|
return 1, 2, 3, 4, 5, 6
|
|
|
|
end
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
2017-06-11 01:12:25 -04:00
|
|
|
).unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
let concat = globals.get::<_, Function>("concat").unwrap();
|
|
|
|
let mreturn = globals.get::<_, Function>("mreturn").unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
assert_eq!(concat.call::<_, String>(("foo", "bar")).unwrap(), "foobar");
|
|
|
|
let (a, b) = mreturn.call::<_, (u64, u64)>(()).unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
assert_eq!((a, b), (1, 2));
|
2017-08-01 13:55:08 -04:00
|
|
|
let (a, b, v) = mreturn.call::<_, (u64, u64, Variadic<u64>)>(()).unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
assert_eq!((a, b), (1, 2));
|
2017-08-01 13:55:08 -04:00
|
|
|
assert_eq!(v[..], [3, 4, 5, 6]);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_coercion() {
|
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
int = 123
|
|
|
|
str = "123"
|
|
|
|
num = 123.0
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
2017-06-11 01:12:25 -04:00
|
|
|
).unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-06-11 01:12:25 -04:00
|
|
|
assert_eq!(globals.get::<_, String>("int").unwrap(), "123");
|
|
|
|
assert_eq!(globals.get::<_, i32>("str").unwrap(), 123);
|
|
|
|
assert_eq!(globals.get::<_, i32>("num").unwrap(), 123);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error() {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TestError;
|
|
|
|
|
|
|
|
impl fmt::Display for TestError {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2017-05-21 19:50:59 -04:00
|
|
|
write!(fmt, "test error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl error::Error for TestError {
|
2017-05-21 19:50:59 -04:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"test error"
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
2017-05-21 19:50:59 -04:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
function no_error()
|
|
|
|
end
|
|
|
|
|
|
|
|
function lua_error()
|
|
|
|
error("this is a lua error")
|
|
|
|
end
|
|
|
|
|
|
|
|
function rust_error()
|
|
|
|
rust_error_function()
|
|
|
|
end
|
|
|
|
|
2017-06-25 02:40:09 -04:00
|
|
|
function return_error()
|
|
|
|
local status, res = pcall(rust_error_function)
|
|
|
|
assert(not status)
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
function return_string_error()
|
|
|
|
return "this should be converted to an error"
|
|
|
|
end
|
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
function test_pcall()
|
|
|
|
local testvar = 0
|
|
|
|
|
|
|
|
pcall(function(arg)
|
|
|
|
testvar = testvar + arg
|
|
|
|
error("should be ignored")
|
|
|
|
end, 3)
|
|
|
|
|
|
|
|
local function handler(err)
|
|
|
|
testvar = testvar + err
|
|
|
|
return "should be ignored"
|
|
|
|
end
|
|
|
|
|
2017-06-25 02:40:09 -04:00
|
|
|
local status, res = xpcall(function()
|
2017-05-21 19:50:59 -04:00
|
|
|
error(5)
|
|
|
|
end, handler)
|
2017-06-25 02:40:09 -04:00
|
|
|
assert(not status)
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
if testvar ~= 8 then
|
|
|
|
error("testvar had the wrong value, pcall / xpcall misbehaving "..testvar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function understand_recursion()
|
|
|
|
understand_recursion()
|
|
|
|
end
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
2017-06-11 01:12:25 -04:00
|
|
|
).unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let rust_error_function = lua
|
|
|
|
.create_function(|_, ()| -> Result<()> { Err(TestError.to_lua_err()) })
|
|
|
|
.unwrap();
|
2017-06-11 01:12:25 -04:00
|
|
|
globals
|
|
|
|
.set("rust_error_function", rust_error_function)
|
|
|
|
.unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
let no_error = globals.get::<_, Function>("no_error").unwrap();
|
|
|
|
let lua_error = globals.get::<_, Function>("lua_error").unwrap();
|
|
|
|
let rust_error = globals.get::<_, Function>("rust_error").unwrap();
|
|
|
|
let return_error = globals.get::<_, Function>("return_error").unwrap();
|
2017-07-24 10:40:00 -04:00
|
|
|
let return_string_error = globals.get::<_, Function>("return_string_error").unwrap();
|
2017-07-18 16:21:12 -04:00
|
|
|
let test_pcall = globals.get::<_, Function>("test_pcall").unwrap();
|
2017-07-24 10:40:00 -04:00
|
|
|
let understand_recursion = globals.get::<_, Function>("understand_recursion").unwrap();
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
assert!(no_error.call::<_, ()>(()).is_ok());
|
|
|
|
match lua_error.call::<_, ()>(()) {
|
2017-07-18 16:21:12 -04:00
|
|
|
Err(Error::RuntimeError(_)) => {}
|
2017-06-24 20:57:04 -04:00
|
|
|
Err(_) => panic!("error is not RuntimeError kind"),
|
2017-06-17 23:50:40 -04:00
|
|
|
_ => panic!("error not returned"),
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
match rust_error.call::<_, ()>(()) {
|
2017-08-01 13:46:05 -04:00
|
|
|
Err(Error::CallbackError { .. }) => {}
|
2017-05-22 14:24:19 -04:00
|
|
|
Err(_) => panic!("error is not CallbackError kind"),
|
2017-06-17 23:50:40 -04:00
|
|
|
_ => panic!("error not returned"),
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
2017-06-25 02:40:09 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
match return_error.call::<_, Value>(()) {
|
|
|
|
Ok(Value::Error(_)) => {}
|
|
|
|
_ => panic!("Value::Error not returned"),
|
2017-06-25 02:40:09 -04:00
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
assert!(return_string_error.call::<_, Error>(()).is_ok());
|
2017-06-25 02:40:09 -04:00
|
|
|
|
2017-07-16 16:53:32 -04:00
|
|
|
match lua.eval::<()>("if youre happy and you know it syntax error", None) {
|
2017-10-23 16:42:20 -04:00
|
|
|
Err(Error::SyntaxError {
|
|
|
|
incomplete_input: false,
|
|
|
|
..
|
|
|
|
}) => {}
|
2017-06-24 20:57:04 -04:00
|
|
|
Err(_) => panic!("error is not LuaSyntaxError::Syntax kind"),
|
|
|
|
_ => panic!("error not returned"),
|
|
|
|
}
|
2017-07-16 16:53:32 -04:00
|
|
|
match lua.eval::<()>("function i_will_finish_what_i()", None) {
|
2017-10-23 16:42:20 -04:00
|
|
|
Err(Error::SyntaxError {
|
|
|
|
incomplete_input: true,
|
|
|
|
..
|
|
|
|
}) => {}
|
2017-06-24 20:57:04 -04:00
|
|
|
Err(_) => panic!("error is not LuaSyntaxError::IncompleteStatement kind"),
|
|
|
|
_ => panic!("error not returned"),
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
test_pcall.call::<_, ()>(()).unwrap();
|
|
|
|
|
|
|
|
assert!(understand_recursion.call::<_, ()>(()).is_err());
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
match catch_unwind(|| -> Result<()> {
|
2017-05-21 19:50:59 -04:00
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
|
|
|
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
function rust_panic()
|
|
|
|
pcall(function () rust_panic_function() end)
|
|
|
|
end
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
|
|
|
)?;
|
2018-08-05 09:51:39 -04:00
|
|
|
let rust_panic_function = lua
|
|
|
|
.create_function(|_, ()| -> Result<()> { panic!("test_panic") })
|
|
|
|
.unwrap();
|
2017-06-11 01:12:25 -04:00
|
|
|
globals.set("rust_panic_function", rust_panic_function)?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
let rust_panic = globals.get::<_, Function>("rust_panic")?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
rust_panic.call::<_, ()>(())
|
|
|
|
}) {
|
|
|
|
Ok(Ok(_)) => panic!("no panic was detected, pcall caught it!"),
|
|
|
|
Ok(Err(e)) => panic!("error during panic test {:?}", e),
|
2018-03-12 16:00:11 -04:00
|
|
|
Err(p) => assert!(*p.downcast::<&str>().unwrap() == "test_panic"),
|
2017-05-21 19:50:59 -04:00
|
|
|
};
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
match catch_unwind(|| -> Result<()> {
|
2017-05-21 19:50:59 -04:00
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
|
|
|
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2017-05-21 19:50:59 -04:00
|
|
|
function rust_panic()
|
|
|
|
xpcall(function() rust_panic_function() end, function() end)
|
|
|
|
end
|
|
|
|
"#,
|
2017-06-05 00:03:39 -04:00
|
|
|
None,
|
|
|
|
)?;
|
2018-08-05 09:51:39 -04:00
|
|
|
let rust_panic_function = lua
|
|
|
|
.create_function(|_, ()| -> Result<()> { panic!("test_panic") })
|
|
|
|
.unwrap();
|
2017-06-11 01:12:25 -04:00
|
|
|
globals.set("rust_panic_function", rust_panic_function)?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
let rust_panic = globals.get::<_, Function>("rust_panic")?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
rust_panic.call::<_, ()>(())
|
|
|
|
}) {
|
|
|
|
Ok(Ok(_)) => panic!("no panic was detected, xpcall caught it!"),
|
|
|
|
Ok(Err(e)) => panic!("error during panic test {:?}", e),
|
2018-03-12 16:00:11 -04:00
|
|
|
Err(p) => assert!(*p.downcast::<&str>().unwrap() == "test_panic"),
|
2017-05-21 19:50:59 -04:00
|
|
|
};
|
|
|
|
}
|
2017-05-24 23:13:58 -04:00
|
|
|
|
2017-06-13 19:17:41 -04:00
|
|
|
#[test]
|
|
|
|
fn test_result_conversions() {
|
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-13 19:17:41 -04:00
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let err =
|
|
|
|
lua.create_function(|_, ()| {
|
|
|
|
Ok(Err::<String, _>(
|
2018-09-16 20:15:51 -04:00
|
|
|
err_msg("only through failure can we succeed").to_lua_err(),
|
2018-08-05 09:51:39 -04:00
|
|
|
))
|
|
|
|
}).unwrap();
|
|
|
|
let ok = lua
|
|
|
|
.create_function(|_, ()| Ok(Ok::<_, Error>("!".to_owned())))
|
2017-12-04 00:57:39 -05:00
|
|
|
.unwrap();
|
2017-06-13 19:17:41 -04:00
|
|
|
|
|
|
|
globals.set("err", err).unwrap();
|
|
|
|
globals.set("ok", ok).unwrap();
|
|
|
|
|
2017-06-18 08:31:38 -04:00
|
|
|
lua.exec::<()>(
|
2017-06-13 19:17:41 -04:00
|
|
|
r#"
|
2017-06-23 19:41:08 -04:00
|
|
|
local r, e = err()
|
|
|
|
assert(r == nil)
|
|
|
|
assert(tostring(e) == "only through failure can we succeed")
|
|
|
|
|
|
|
|
local r, e = ok()
|
|
|
|
assert(r == "!")
|
|
|
|
assert(e == nil)
|
2017-06-13 19:17:41 -04:00
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
|
|
|
}
|
2017-06-21 17:53:52 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_num_conversion() {
|
|
|
|
let lua = Lua::new();
|
Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious. I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.
I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.
One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit. I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.
Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic. I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable. For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic. There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue. When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.
I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.
This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 16:52:32 -04:00
|
|
|
let globals = lua.globals();
|
2017-06-21 17:53:52 -04:00
|
|
|
|
2017-06-23 00:51:16 -04:00
|
|
|
globals.set("n", "1.0").unwrap();
|
|
|
|
assert_eq!(globals.get::<_, i64>("n").unwrap(), 1);
|
|
|
|
assert_eq!(globals.get::<_, f64>("n").unwrap(), 1.0);
|
|
|
|
assert_eq!(globals.get::<_, String>("n").unwrap(), "1.0");
|
|
|
|
|
|
|
|
globals.set("n", "1.5").unwrap();
|
|
|
|
assert!(globals.get::<_, i64>("n").is_err());
|
|
|
|
assert_eq!(globals.get::<_, f64>("n").unwrap(), 1.5);
|
|
|
|
assert_eq!(globals.get::<_, String>("n").unwrap(), "1.5");
|
|
|
|
|
|
|
|
globals.set("n", 1.5).unwrap();
|
|
|
|
assert!(globals.get::<_, i64>("n").is_err());
|
|
|
|
assert_eq!(globals.get::<_, f64>("n").unwrap(), 1.5);
|
|
|
|
assert_eq!(globals.get::<_, String>("n").unwrap(), "1.5");
|
|
|
|
|
|
|
|
lua.exec::<()>("a = math.huge", None).unwrap();
|
|
|
|
assert!(globals.get::<_, i64>("n").is_err());
|
2017-06-21 17:53:52 -04:00
|
|
|
}
|
2017-07-23 01:00:33 -04:00
|
|
|
|
2017-08-02 10:42:18 -04:00
|
|
|
#[test]
|
|
|
|
fn test_pcall_xpcall() {
|
|
|
|
let lua = Lua::new();
|
2017-10-25 14:51:10 -04:00
|
|
|
let globals = lua.globals();
|
|
|
|
|
2017-08-02 10:42:18 -04:00
|
|
|
// make sure that we handle not enough arguments
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2017-08-02 10:42:18 -04:00
|
|
|
assert!(lua.exec::<()>("pcall()", None).is_err());
|
|
|
|
assert!(lua.exec::<()>("xpcall()", None).is_err());
|
|
|
|
assert!(lua.exec::<()>("xpcall(function() end)", None).is_err());
|
|
|
|
|
2017-10-25 14:51:10 -04:00
|
|
|
// Make sure that the return values from are correct on success
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let (r, e) = lua
|
|
|
|
.eval::<(bool, String)>("pcall(function(p) return p end, 'foo')", None)
|
2018-03-12 16:00:11 -04:00
|
|
|
.unwrap();
|
|
|
|
assert!(r);
|
|
|
|
assert_eq!(e, "foo");
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let (r, e) = lua
|
|
|
|
.eval::<(bool, String)>("xpcall(function(p) return p end, print, 'foo')", None)
|
2018-03-12 16:00:11 -04:00
|
|
|
.unwrap();
|
|
|
|
assert!(r);
|
|
|
|
assert_eq!(e, "foo");
|
2017-10-25 14:51:10 -04:00
|
|
|
|
|
|
|
// Make sure that the return values are correct on errors, and that error handling works
|
|
|
|
|
2017-08-02 10:42:18 -04:00
|
|
|
lua.exec::<()>(
|
|
|
|
r#"
|
|
|
|
pcall_error = nil
|
2017-10-25 14:51:10 -04:00
|
|
|
pcall_status, pcall_error = pcall(error, "testerror")
|
2017-08-02 10:42:18 -04:00
|
|
|
|
|
|
|
xpcall_error = nil
|
2017-10-25 14:51:10 -04:00
|
|
|
xpcall_status, _ = xpcall(error, function(err) xpcall_error = err end, "testerror")
|
2017-08-02 10:42:18 -04:00
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
|
|
|
|
2017-10-25 14:51:10 -04:00
|
|
|
assert_eq!(globals.get::<_, bool>("pcall_status").unwrap(), false);
|
2017-08-02 10:42:18 -04:00
|
|
|
assert_eq!(
|
2017-08-02 15:56:16 -04:00
|
|
|
globals.get::<_, String>("pcall_error").unwrap(),
|
2017-08-02 10:42:18 -04:00
|
|
|
"testerror"
|
|
|
|
);
|
|
|
|
|
2017-10-25 14:51:10 -04:00
|
|
|
assert_eq!(globals.get::<_, bool>("xpcall_statusr").unwrap(), false);
|
2017-08-02 10:42:18 -04:00
|
|
|
assert_eq!(
|
2017-08-02 15:56:16 -04:00
|
|
|
globals.get::<_, String>("xpcall_error").unwrap(),
|
2017-08-02 10:42:18 -04:00
|
|
|
"testerror"
|
|
|
|
);
|
2017-08-02 15:56:16 -04:00
|
|
|
|
|
|
|
// Make sure that weird xpcall error recursion at least doesn't cause unsafety or panics.
|
2017-10-25 14:51:10 -04:00
|
|
|
lua.exec::<()>(
|
|
|
|
r#"
|
|
|
|
function xpcall_recursion()
|
|
|
|
xpcall(error, function(err) error(err) end, "testerror")
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
2017-08-02 15:56:16 -04:00
|
|
|
let _ = globals
|
|
|
|
.get::<_, Function>("xpcall_recursion")
|
|
|
|
.unwrap()
|
|
|
|
.call::<_, ()>(());
|
2017-08-02 10:42:18 -04:00
|
|
|
}
|
2017-08-01 12:03:05 -04:00
|
|
|
|
2017-10-14 18:26:09 -04:00
|
|
|
#[test]
|
2018-02-09 23:35:29 -05:00
|
|
|
fn test_recursive_mut_callback_error() {
|
2017-10-14 18:26:09 -04:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let mut v = Some(Box::new(123));
|
2018-08-05 09:51:39 -04:00
|
|
|
let f = lua
|
|
|
|
.create_function_mut::<_, (), _>(move |lua, mutate: bool| {
|
|
|
|
if mutate {
|
|
|
|
v = None;
|
|
|
|
} else {
|
|
|
|
// Produce a mutable reference
|
|
|
|
let r = v.as_mut().unwrap();
|
|
|
|
// Whoops, this will recurse into the function and produce another mutable reference!
|
|
|
|
lua.globals().get::<_, Function>("f")?.call::<_, ()>(true)?;
|
|
|
|
println!("Should not get here, mutable aliasing has occurred!");
|
|
|
|
println!("value at {:p}", r as *mut _);
|
|
|
|
println!("value is {}", r);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
2017-10-14 18:26:09 -04:00
|
|
|
lua.globals().set("f", f).unwrap();
|
2018-08-05 09:51:39 -04:00
|
|
|
match lua
|
|
|
|
.globals()
|
2017-12-04 02:46:57 -05:00
|
|
|
.get::<_, Function>("f")
|
|
|
|
.unwrap()
|
|
|
|
.call::<_, ()>(false)
|
|
|
|
{
|
|
|
|
Err(Error::CallbackError { ref cause, .. }) => match *cause.as_ref() {
|
|
|
|
Error::CallbackError { ref cause, .. } => match *cause.as_ref() {
|
2018-02-09 23:35:29 -05:00
|
|
|
Error::RecursiveMutCallback { .. } => {}
|
2017-12-04 02:46:57 -05:00
|
|
|
ref other => panic!("incorrect result: {:?}", other),
|
|
|
|
},
|
|
|
|
ref other => panic!("incorrect result: {:?}", other),
|
|
|
|
},
|
|
|
|
other => panic!("incorrect result: {:?}", other),
|
|
|
|
};
|
2017-10-14 18:26:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:13:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_set_metatable_nil() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
lua.exec::<()>(
|
2017-12-02 18:56:14 -05:00
|
|
|
r#"
|
2017-11-07 23:13:52 -05:00
|
|
|
a = {}
|
|
|
|
setmetatable(a, nil)
|
2017-12-02 18:56:14 -05:00
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
2017-11-07 23:13:52 -05:00
|
|
|
}
|
|
|
|
|
2017-12-04 00:35:13 -05:00
|
|
|
#[test]
|
|
|
|
fn test_gc_error() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
match lua.exec::<()>(
|
|
|
|
r#"
|
|
|
|
val = nil
|
|
|
|
table = {}
|
|
|
|
setmetatable(table, {
|
|
|
|
__gc = function()
|
|
|
|
error("gcwascalled")
|
|
|
|
end
|
|
|
|
})
|
|
|
|
table = nil
|
|
|
|
collectgarbage("collect")
|
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
) {
|
|
|
|
Err(Error::GarbageCollectorError(_)) => {}
|
2017-12-04 01:47:04 -05:00
|
|
|
Err(e) => panic!("__gc error did not result in correct error, instead: {}", e),
|
|
|
|
Ok(()) => panic!("__gc error did not result in error"),
|
2017-12-04 00:35:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:44:13 -05:00
|
|
|
#[test]
|
2017-12-16 18:05:53 -05:00
|
|
|
fn test_named_registry_value() {
|
2017-12-16 17:44:13 -05:00
|
|
|
let lua = Lua::new();
|
|
|
|
|
2017-12-16 18:05:53 -05:00
|
|
|
lua.set_named_registry_value::<i32>("test", 42).unwrap();
|
2018-08-05 09:51:39 -04:00
|
|
|
let f =
|
|
|
|
lua.create_function(move |lua, ()| {
|
|
|
|
assert_eq!(lua.named_registry_value::<i32>("test")?, 42);
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
2017-12-16 17:44:13 -05:00
|
|
|
|
|
|
|
f.call::<_, ()>(()).unwrap();
|
2017-12-16 18:05:53 -05:00
|
|
|
|
|
|
|
lua.unset_named_registry_value("test").unwrap();
|
|
|
|
match lua.named_registry_value("test").unwrap() {
|
|
|
|
Nil => {}
|
|
|
|
val => panic!("registry value was not Nil, was {:?}", val),
|
|
|
|
};
|
2017-12-16 17:44:13 -05:00
|
|
|
}
|
|
|
|
|
2017-12-17 00:19:59 -05:00
|
|
|
#[test]
|
|
|
|
fn test_registry_value() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let mut r = Some(lua.create_registry_value::<i32>(42).unwrap());
|
2018-08-05 09:51:39 -04:00
|
|
|
let f =
|
|
|
|
lua.create_function_mut(move |lua, ()| {
|
|
|
|
if let Some(r) = r.take() {
|
|
|
|
assert_eq!(lua.registry_value::<i32>(&r)?, 42);
|
|
|
|
lua.remove_registry_value(r).unwrap();
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
2017-12-17 00:19:59 -05:00
|
|
|
|
|
|
|
f.call::<_, ()>(()).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:33:19 -05:00
|
|
|
#[test]
|
|
|
|
fn test_drop_registry_value() {
|
2018-02-06 20:23:16 -05:00
|
|
|
struct MyUserdata(Arc<()>);
|
2018-02-06 03:33:19 -05:00
|
|
|
|
|
|
|
impl UserData for MyUserdata {}
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
2018-02-06 20:23:16 -05:00
|
|
|
let rc = Arc::new(());
|
2018-02-06 03:33:19 -05:00
|
|
|
|
|
|
|
let r = lua.create_registry_value(MyUserdata(rc.clone())).unwrap();
|
2018-02-06 20:23:16 -05:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 2);
|
2018-02-06 03:33:19 -05:00
|
|
|
|
|
|
|
drop(r);
|
|
|
|
lua.expire_registry_values();
|
|
|
|
|
|
|
|
lua.exec::<()>(r#"collectgarbage("collect")"#, None)
|
|
|
|
.unwrap();
|
|
|
|
|
2018-02-06 20:23:16 -05:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 1);
|
2018-02-06 03:33:19 -05:00
|
|
|
}
|
|
|
|
|
2018-01-26 19:27:41 -05:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_mismatched_lua_ref() {
|
|
|
|
let lua1 = Lua::new();
|
|
|
|
let lua2 = Lua::new();
|
|
|
|
|
|
|
|
let s = lua1.create_string("hello").unwrap();
|
|
|
|
let f = lua2.create_function(|_, _: String| Ok(())).unwrap();
|
|
|
|
|
|
|
|
f.call::<_, ()>(s).unwrap();
|
2018-02-06 00:54:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lua_registry_ownership() {
|
|
|
|
let lua1 = Lua::new();
|
|
|
|
let lua2 = Lua::new();
|
|
|
|
|
|
|
|
let r1 = lua1.create_registry_value("hello").unwrap();
|
|
|
|
let r2 = lua2.create_registry_value("hello").unwrap();
|
|
|
|
|
|
|
|
assert!(lua1.owns_registry_value(&r1));
|
|
|
|
assert!(!lua2.owns_registry_value(&r1));
|
|
|
|
assert!(lua2.owns_registry_value(&r2));
|
|
|
|
assert!(!lua1.owns_registry_value(&r2));
|
2018-01-26 19:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mismatched_registry_key() {
|
|
|
|
let lua1 = Lua::new();
|
|
|
|
let lua2 = Lua::new();
|
|
|
|
|
|
|
|
let r = lua1.create_registry_value("hello").unwrap();
|
2018-02-06 10:51:39 -05:00
|
|
|
match lua2.remove_registry_value(r) {
|
|
|
|
Err(Error::MismatchedRegistryKey) => {}
|
|
|
|
r => panic!("wrong result type for mismatched registry key, {:?}", r),
|
|
|
|
};
|
2018-01-26 19:27:41 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 23:35:29 -05:00
|
|
|
#[test]
|
|
|
|
fn too_many_returns() {
|
|
|
|
let lua = Lua::new();
|
2018-08-05 09:51:39 -04:00
|
|
|
let f = lua
|
|
|
|
.create_function(|_, ()| Ok(Variadic::from_iter(1..1000000)))
|
2018-02-09 23:35:29 -05:00
|
|
|
.unwrap();
|
|
|
|
assert!(f.call::<_, Vec<u32>>(()).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn too_many_arguments() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
lua.exec::<()>("function test(...) end", None).unwrap();
|
|
|
|
let args = Variadic::from_iter(1..1000000);
|
|
|
|
assert!(
|
|
|
|
lua.globals()
|
|
|
|
.get::<_, Function>("test")
|
|
|
|
.unwrap()
|
|
|
|
.call::<_, ()>(args)
|
|
|
|
.is_err()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn too_many_recursions() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let f = lua
|
|
|
|
.create_function(move |lua, ()| lua.globals().get::<_, Function>("f")?.call::<_, ()>(()))
|
|
|
|
.unwrap();
|
2018-02-09 23:35:29 -05:00
|
|
|
lua.globals().set("f", f).unwrap();
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
lua.globals()
|
|
|
|
.get::<_, Function>("f")
|
|
|
|
.unwrap()
|
|
|
|
.call::<_, ()>(())
|
|
|
|
.is_err()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn too_many_binds() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let globals = lua.globals();
|
|
|
|
lua.exec::<()>(
|
|
|
|
r#"
|
|
|
|
function f(...)
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
let concat = globals.get::<_, Function>("f").unwrap();
|
|
|
|
assert!(concat.bind(Variadic::from_iter(1..1000000)).is_err());
|
|
|
|
assert!(
|
|
|
|
concat
|
|
|
|
.call::<_, ()>(Variadic::from_iter(1..1000000))
|
|
|
|
.is_err()
|
|
|
|
);
|
|
|
|
}
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn large_args() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let globals = lua.globals();
|
|
|
|
|
|
|
|
globals
|
|
|
|
.set(
|
|
|
|
"c",
|
|
|
|
lua.create_function(|_, args: Variadic<usize>| {
|
|
|
|
let mut s = 0;
|
|
|
|
for i in 0..args.len() {
|
|
|
|
s += i;
|
|
|
|
assert_eq!(i, args[i]);
|
|
|
|
}
|
|
|
|
Ok(s)
|
|
|
|
}).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let f: Function =
|
|
|
|
lua.eval(
|
|
|
|
r#"
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
return function(...)
|
|
|
|
return c(...)
|
|
|
|
end
|
|
|
|
"#,
|
2018-08-05 09:51:39 -04:00
|
|
|
None,
|
|
|
|
).unwrap();
|
A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.
There are some small improvements here and there, but most of the benefits of
this change are from a few big changes. The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases. Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.
The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time. There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance. This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement. On the plus side,
the rules for LuaRef types are easier to state now.
There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:
create table time: [314.13 ns 315.71 ns 317.44 ns]
change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10 time: [2.9731 us 2.9816 us 2.9901 us]
change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
Performance has improved.
create string table 10 time: [5.6904 us 5.7164 us 5.7411 us]
change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
Performance has improved.
call add function 3 10 time: [5.1134 us 5.1222 us 5.1320 us]
change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
Performance has improved.
call callback add 2 10 time: [5.4408 us 5.4480 us 5.4560 us]
change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
Performance has improved.
call callback append 10 time: [9.8243 us 9.8410 us 9.8586 us]
change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
Performance has improved.
create registry 10 time: [3.7005 us 3.7089 us 3.7174 us]
change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
Performance has improved.
I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
f.call::<_, usize>((0..100).collect::<Variadic<usize>>())
|
|
|
|
.unwrap(),
|
|
|
|
4950
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2018-03-12 16:14:52 -04:00
|
|
|
#[test]
|
2018-03-12 16:00:11 -04:00
|
|
|
fn large_args_ref() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let f =
|
|
|
|
lua.create_function(|_, args: Variadic<String>| {
|
|
|
|
for i in 0..args.len() {
|
|
|
|
assert_eq!(args[i], i.to_string());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
2018-03-12 16:00:11 -04:00
|
|
|
|
|
|
|
f.call::<_, ()>((0..100).map(|i| i.to_string()).collect::<Variadic<_>>())
|
|
|
|
.unwrap();
|
|
|
|
}
|