2020-05-16 10:13:38 -04:00
|
|
|
#![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",
|
|
|
|
allow(unused_attributes)
|
|
|
|
)]
|
|
|
|
extern "system" {}
|
|
|
|
|
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;
|
2020-05-05 21:32:05 -04:00
|
|
|
use std::sync::Arc;
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
use std::{error, f32, f64, fmt};
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-10-14 17:21:30 -04:00
|
|
|
use mlua::{
|
2020-05-10 11:56:19 -04:00
|
|
|
Error, ExternalError, Function, Lua, Nil, Result, StdLib, String, Table, UserData, Value,
|
|
|
|
Variadic,
|
2019-10-14 17:21:30 -04:00
|
|
|
};
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2020-05-10 11:56:19 -04:00
|
|
|
#[test]
|
|
|
|
fn test_safety() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
assert!(lua.load(r#"require "debug""#).exec().is_err());
|
|
|
|
match lua.load_from_std_lib(StdLib::DEBUG) {
|
|
|
|
Err(Error::SafetyError(_)) => {}
|
|
|
|
Err(e) => panic!("expected SafetyError, got {:?}", e),
|
|
|
|
Ok(_) => panic!("expected SafetyError, got no error"),
|
|
|
|
}
|
|
|
|
drop(lua);
|
|
|
|
|
|
|
|
let lua = unsafe { Lua::unsafe_new() };
|
|
|
|
assert!(lua.load(r#"require "debug""#).exec().is_ok());
|
|
|
|
drop(lua);
|
|
|
|
|
|
|
|
match Lua::new_with(StdLib::DEBUG) {
|
|
|
|
Err(Error::SafetyError(_)) => {}
|
|
|
|
Err(e) => panic!("expected SafetyError, got {:?}", e),
|
|
|
|
Ok(_) => panic!("expected SafetyError, got new Lua state"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
|
|
|
match lua.load(r#"package.loadlib()"#).exec() {
|
|
|
|
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
|
|
|
Error::SafetyError(_) => {}
|
|
|
|
e => panic!("expected SafetyError cause, got {:?}", e),
|
|
|
|
},
|
|
|
|
Err(e) => panic!("expected CallbackError, got {:?}", e),
|
|
|
|
Ok(_) => panic!("expected CallbackError, got no error"),
|
|
|
|
};
|
|
|
|
match lua.load(r#"require "fake_ffi""#).exec() {
|
|
|
|
Err(Error::RuntimeError(msg)) => assert!(msg.contains("can't load C modules in safe mode")),
|
|
|
|
Err(e) => panic!("expected RuntimeError, got {:?}", e),
|
|
|
|
Ok(_) => panic!("expected RuntimeError, got no error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:38:58 -04:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_load() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
let func = lua.load("return 1+2").into_function()?;
|
|
|
|
let result: i32 = func.call(())?;
|
2017-06-21 20:38:58 -04:00
|
|
|
assert_eq!(result, 3);
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert!(lua.load("§$%§&$%&").exec().is_err());
|
2017-06-21 20:38:58 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2017-10-29 17:53:30 -04:00
|
|
|
}
|
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_exec() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
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();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
res = 'foo'..'bar'
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
|
|
|
assert_eq!(globals.get::<_, String>("res")?, "foobar");
|
2017-06-11 01:12:25 -04:00
|
|
|
|
2018-09-24 22:13:42 -04:00
|
|
|
let module: Table = lua
|
2019-09-28 10:23:17 -04:00
|
|
|
.load(
|
2018-08-05 09:51:39 -04:00
|
|
|
r#"
|
2017-06-11 01:12:25 -04:00
|
|
|
local module = {}
|
|
|
|
|
|
|
|
function module.func()
|
|
|
|
return "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
return module
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.eval()?;
|
|
|
|
assert!(module.contains_key("func")?);
|
2017-06-15 10:26:39 -04:00
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
module.get::<_, Function>("func")?.call::<_, String>(())?,
|
2017-06-15 10:26:39 -04:00
|
|
|
"hello"
|
|
|
|
);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_eval() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
assert_eq!(lua.load("1 + 1").eval::<i32>()?, 2);
|
|
|
|
assert_eq!(lua.load("false == false").eval::<bool>()?, true);
|
|
|
|
assert_eq!(lua.load("return 1 + 2").eval::<i32>()?, 3);
|
|
|
|
match lua.load("if true then").eval::<()>() {
|
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
|
|
|
}
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_lua_multi() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2020-01-06 18:59:50 -05:00
|
|
|
function concat(arg1, arg2)
|
|
|
|
return arg1 .. arg2
|
|
|
|
end
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2020-01-06 18:59:50 -05:00
|
|
|
function mreturn()
|
|
|
|
return 1, 2, 3, 4, 5, 6
|
|
|
|
end
|
2019-09-28 10:23:17 -04:00
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let globals = lua.globals();
|
|
|
|
let concat = globals.get::<_, Function>("concat")?;
|
|
|
|
let mreturn = globals.get::<_, Function>("mreturn")?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(concat.call::<_, String>(("foo", "bar"))?, "foobar");
|
|
|
|
let (a, b) = mreturn.call::<_, (u64, u64)>(())?;
|
2017-05-21 19:50:59 -04:00
|
|
|
assert_eq!((a, b), (1, 2));
|
2019-09-28 10:23:17 -04:00
|
|
|
let (a, b, v) = mreturn.call::<_, (u64, u64, Variadic<u64>)>(())?;
|
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]);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_coercion() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
int = 123
|
|
|
|
str = "123"
|
|
|
|
num = 123.0
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let globals = lua.globals();
|
|
|
|
assert_eq!(globals.get::<_, String>("int")?, "123");
|
|
|
|
assert_eq!(globals.get::<_, i32>("str")?, 123);
|
|
|
|
assert_eq!(globals.get::<_, i32>("num")?, 123);
|
|
|
|
|
|
|
|
Ok(())
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_error() -> Result<()> {
|
2017-05-21 19:50:59 -04:00
|
|
|
#[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"
|
|
|
|
}
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
fn cause(&self) -> Option<&dyn error::Error> {
|
2017-05-21 19:50:59 -04:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
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();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
function no_error()
|
|
|
|
end
|
|
|
|
|
|
|
|
function lua_error()
|
|
|
|
error("this is a lua error")
|
|
|
|
end
|
|
|
|
|
|
|
|
function rust_error()
|
|
|
|
rust_error_function()
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
function test_pcall()
|
|
|
|
local testvar = 0
|
|
|
|
|
|
|
|
pcall(function(arg)
|
|
|
|
testvar = testvar + arg
|
|
|
|
error("should be ignored")
|
|
|
|
end, 3)
|
|
|
|
|
|
|
|
local function handler(err)
|
2019-11-29 08:26:30 -05:00
|
|
|
if string.match(_VERSION, ' 5%.1$') or string.match(_VERSION, ' 5%.2$') then
|
|
|
|
-- Special case for Lua 5.1/5.2
|
2019-10-14 17:21:30 -04:00
|
|
|
local caps = string.match(err, ': (%d+)$')
|
|
|
|
if caps then
|
|
|
|
err = caps
|
|
|
|
end
|
|
|
|
end
|
2019-09-28 10:23:17 -04:00
|
|
|
testvar = testvar + err
|
|
|
|
return "should be ignored"
|
2017-05-21 19:50:59 -04:00
|
|
|
end
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
local status, res = xpcall(function()
|
|
|
|
error(5)
|
|
|
|
end, handler)
|
|
|
|
assert(not status)
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
if testvar ~= 8 then
|
|
|
|
error("testvar had the wrong value, pcall / xpcall misbehaving "..testvar)
|
2017-06-25 02:40:09 -04:00
|
|
|
end
|
2019-09-28 10:23:17 -04:00
|
|
|
end
|
2017-06-25 02:40:09 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
function understand_recursion()
|
|
|
|
understand_recursion()
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let rust_error_function =
|
|
|
|
lua.create_function(|_, ()| -> Result<()> { Err(TestError.to_lua_err()) })?;
|
|
|
|
globals.set("rust_error_function", rust_error_function)?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let no_error = globals.get::<_, Function>("no_error")?;
|
|
|
|
let lua_error = globals.get::<_, Function>("lua_error")?;
|
|
|
|
let rust_error = globals.get::<_, Function>("rust_error")?;
|
|
|
|
let return_error = globals.get::<_, Function>("return_error")?;
|
|
|
|
let return_string_error = globals.get::<_, Function>("return_string_error")?;
|
|
|
|
let test_pcall = globals.get::<_, Function>("test_pcall")?;
|
|
|
|
let understand_recursion = globals.get::<_, Function>("understand_recursion")?;
|
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(_)) => {}
|
2019-10-16 05:56:44 -04:00
|
|
|
Err(e) => panic!("error is not RuntimeError kind, got {:?}", e),
|
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 { .. }) => {}
|
2019-10-16 05:56:44 -04:00
|
|
|
Err(e) => panic!("error is not CallbackError kind, got {:?}", e),
|
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
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
match lua
|
|
|
|
.load("if youre happy and you know it syntax error")
|
|
|
|
.exec()
|
|
|
|
{
|
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"),
|
|
|
|
}
|
2019-09-28 10:23:17 -04:00
|
|
|
match lua.load("function i_will_finish_what_i()").exec() {
|
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
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
test_pcall.call::<_, ()>(())?;
|
2017-05-21 19:50:59 -04:00
|
|
|
|
|
|
|
assert!(understand_recursion.call::<_, ()>(()).is_err());
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
match catch_unwind(|| -> Result<()> {
|
2019-10-14 17:21:30 -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();
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
function rust_panic()
|
|
|
|
local _, err = pcall(function () rust_panic_function() end)
|
|
|
|
if err ~= nil then
|
|
|
|
error(err)
|
2017-05-21 19:50:59 -04:00
|
|
|
end
|
2019-09-28 10:23:17 -04:00
|
|
|
end
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()?;
|
|
|
|
let rust_panic_function =
|
|
|
|
lua.create_function(|_, ()| -> Result<()> { panic!("test_panic") })?;
|
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::<_, ()>(())
|
|
|
|
}) {
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(Ok(_)) => panic!("no panic was detected"),
|
2017-05-21 19:50:59 -04:00
|
|
|
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<()> {
|
2019-10-14 17:21:30 -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();
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-06-05 00:03:39 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
function rust_panic()
|
|
|
|
local _, err = pcall(function () rust_panic_function() end)
|
|
|
|
if err ~= nil then
|
|
|
|
error(tostring(err))
|
2017-05-21 19:50:59 -04:00
|
|
|
end
|
2019-09-28 10:23:17 -04:00
|
|
|
end
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()?;
|
|
|
|
let rust_panic_function =
|
|
|
|
lua.create_function(|_, ()| -> Result<()> { panic!("test_panic") })?;
|
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::<_, ()>(())
|
|
|
|
}) {
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(Ok(_)) => panic!("no error was detected"),
|
|
|
|
Ok(Err(Error::RuntimeError(_))) => {}
|
|
|
|
Ok(Err(e)) => panic!("unexpected error during panic test {:?}", e),
|
|
|
|
Err(_) => panic!("panic was detected"),
|
2017-05-21 19:50:59 -04:00
|
|
|
};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
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]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_result_conversions() -> Result<()> {
|
2019-10-14 17:21:30 -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-13 19:17:41 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let err = lua.create_function(|_, ()| {
|
|
|
|
Ok(Err::<String, _>(
|
|
|
|
"only through failure can we succeed".to_lua_err(),
|
|
|
|
))
|
|
|
|
})?;
|
|
|
|
let ok = lua.create_function(|_, ()| Ok(Ok::<_, Error>("!".to_owned())))?;
|
|
|
|
|
|
|
|
globals.set("err", err)?;
|
|
|
|
globals.set("ok", ok)?;
|
|
|
|
|
|
|
|
lua.load(
|
2017-06-13 19:17:41 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
local r, e = err()
|
|
|
|
assert(r == nil)
|
|
|
|
assert(tostring(e):find("only through failure can we succeed") ~= nil)
|
2017-06-23 19:41:08 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
local r, e = ok()
|
|
|
|
assert(r == "!")
|
|
|
|
assert(e == nil)
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
|
|
|
|
|
|
|
Ok(())
|
2017-06-13 19:17:41 -04:00
|
|
|
}
|
2017-06-21 17:53:52 -04:00
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_num_conversion() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2017-06-21 17:53:52 -04:00
|
|
|
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_integer(Value::String(lua.create_string("1")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
Some(1)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_integer(Value::String(lua.create_string("1.0")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
Some(1)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_integer(Value::String(lua.create_string("1.5")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
None
|
|
|
|
);
|
2017-06-23 00:51:16 -04:00
|
|
|
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_number(Value::String(lua.create_string("1")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
Some(1.0)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_number(Value::String(lua.create_string("1.0")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
Some(1.0)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.coerce_number(Value::String(lua.create_string("1.5")?))?,
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
Some(1.5)
|
|
|
|
);
|
2017-06-23 00:51:16 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(lua.load("1.0").eval::<i64>()?, 1);
|
|
|
|
assert_eq!(lua.load("1.0").eval::<f64>()?, 1.0);
|
2020-05-08 07:42:40 -04:00
|
|
|
#[cfg(any(feature = "lua54", feature = "lua53"))]
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(lua.load("1.0").eval::<String>()?, "1.0");
|
2019-11-29 08:26:30 -05:00
|
|
|
#[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))]
|
2019-10-14 17:21:30 -04:00
|
|
|
assert_eq!(lua.load("1.0").eval::<String>()?, "1");
|
2017-06-23 00:51:16 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(lua.load("1.5").eval::<i64>()?, 1);
|
|
|
|
assert_eq!(lua.load("1.5").eval::<f64>()?, 1.5);
|
|
|
|
assert_eq!(lua.load("1.5").eval::<String>()?, "1.5");
|
2018-09-24 22:14:50 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert!(lua.load("-1").eval::<u64>().is_err());
|
|
|
|
assert_eq!(lua.load("-1").eval::<i64>()?, -1);
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert!(lua.unpack::<u64>(lua.pack(1u128 << 64)?).is_err());
|
|
|
|
assert!(lua.load("math.huge").eval::<i64>().is_err());
|
Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:
1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)
The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.
The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).
The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5. I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.
I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 20:41:07 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(lua.unpack::<f64>(lua.pack(f32::MAX)?)?, f32::MAX as f64);
|
|
|
|
assert!(lua.unpack::<f32>(lua.pack(f64::MAX)?).is_err());
|
2018-09-26 21:13:25 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(lua.unpack::<i128>(lua.pack(1i128 << 64)?)?, 1i128 << 64);
|
|
|
|
|
|
|
|
Ok(())
|
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]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_pcall_xpcall() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
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
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert!(lua.load("pcall()").exec().is_err());
|
|
|
|
assert!(lua.load("xpcall()").exec().is_err());
|
|
|
|
assert!(lua.load("xpcall(function() end)").exec().is_err());
|
2017-08-02 10:42:18 -04:00
|
|
|
|
2019-11-29 08:26:30 -05:00
|
|
|
// Lua 5.3/5.2 / LuaJIT compatible version of xpcall
|
2019-11-04 11:31:19 -05:00
|
|
|
#[cfg(feature = "lua51")]
|
2019-10-14 17:21:30 -04:00
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
local xpcall_orig = xpcall
|
|
|
|
function xpcall(f, err, ...)
|
|
|
|
return xpcall_orig(function() return f(unpack(arg)) end, err)
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()?;
|
|
|
|
|
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
|
2019-09-28 10:23:17 -04:00
|
|
|
.load("pcall(function(p) return p end, 'foo')")
|
|
|
|
.eval::<(bool, String)>()?;
|
2018-03-12 16:00:11 -04:00
|
|
|
assert!(r);
|
|
|
|
assert_eq!(e, "foo");
|
|
|
|
|
2018-08-05 09:51:39 -04:00
|
|
|
let (r, e) = lua
|
2019-09-28 10:23:17 -04:00
|
|
|
.load("xpcall(function(p) return p end, print, 'foo')")
|
|
|
|
.eval::<(bool, String)>()?;
|
2018-03-12 16:00:11 -04:00
|
|
|
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
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-08-02 10:42:18 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
pcall_error = nil
|
|
|
|
pcall_status, pcall_error = pcall(error, "testerror")
|
2017-08-02 10:42:18 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
xpcall_error = nil
|
|
|
|
xpcall_status, _ = xpcall(error, function(err) xpcall_error = err end, "testerror")
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2017-08-02 10:42:18 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(globals.get::<_, bool>("pcall_status")?, false);
|
|
|
|
assert_eq!(globals.get::<_, String>("pcall_error")?, "testerror");
|
2017-08-02 10:42:18 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
assert_eq!(globals.get::<_, bool>("xpcall_statusr")?, false);
|
2020-05-08 07:42:40 -04:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "lua54",
|
|
|
|
feature = "lua53",
|
|
|
|
feature = "lua52",
|
|
|
|
feature = "luajit"
|
|
|
|
))]
|
2019-10-16 05:56:44 -04:00
|
|
|
assert_eq!(
|
|
|
|
globals.get::<_, std::string::String>("xpcall_error")?,
|
|
|
|
"testerror"
|
|
|
|
);
|
2019-11-04 11:31:19 -05:00
|
|
|
#[cfg(feature = "lua51")]
|
2019-10-14 17:21:30 -04:00
|
|
|
assert!(globals
|
|
|
|
.get::<_, String>("xpcall_error")?
|
|
|
|
.to_str()?
|
|
|
|
.ends_with(": testerror"));
|
2017-08-02 15:56:16 -04:00
|
|
|
|
|
|
|
// Make sure that weird xpcall error recursion at least doesn't cause unsafety or panics.
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2017-10-25 14:51:10 -04:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
function xpcall_recursion()
|
|
|
|
xpcall(error, function(err) error(err) end, "testerror")
|
|
|
|
end
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2017-08-02 15:56:16 -04:00
|
|
|
let _ = globals
|
2019-09-28 10:23:17 -04:00
|
|
|
.get::<_, Function>("xpcall_recursion")?
|
2017-08-02 15:56:16 -04:00
|
|
|
.call::<_, ()>(());
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
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]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_recursive_mut_callback_error() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2017-10-14 18:26:09 -04:00
|
|
|
|
|
|
|
let mut v = Some(Box::new(123));
|
2019-09-28 10:23:17 -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);
|
|
|
|
}
|
2018-08-05 09:51:39 -04:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
lua.globals().set("f", f)?;
|
|
|
|
match lua.globals().get::<_, Function>("f")?.call::<_, ()>(false) {
|
2017-12-04 02:46:57 -05:00
|
|
|
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),
|
|
|
|
};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2017-10-14 18:26:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:13:52 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_set_metatable_nil() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
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
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
|
|
|
Ok(())
|
2017-11-07 23:13:52 -05:00
|
|
|
}
|
|
|
|
|
2017-12-04 00:35:13 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_named_registry_value() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2017-12-04 00:35:13 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.set_named_registry_value::<_, i32>("test", 42)?;
|
|
|
|
let f = lua.create_function(move |lua, ()| {
|
|
|
|
assert_eq!(lua.named_registry_value::<_, i32>("test")?, 42);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2017-12-16 17:44:13 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
f.call::<_, ()>(())?;
|
2017-12-16 17:44:13 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.unset_named_registry_value("test")?;
|
|
|
|
match lua.named_registry_value("test")? {
|
2017-12-16 18:05:53 -05:00
|
|
|
Nil => {}
|
|
|
|
val => panic!("registry value was not Nil, was {:?}", val),
|
|
|
|
};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2017-12-16 17:44:13 -05:00
|
|
|
}
|
|
|
|
|
2017-12-17 00:19:59 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_registry_value() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
let mut r = Some(lua.create_registry_value::<i32>(42)?);
|
|
|
|
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(())
|
|
|
|
})?;
|
2017-12-17 00:19:59 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
f.call::<_, ()>(())?;
|
2017-12-17 00:19:59 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2017-12-17 00:19:59 -05:00
|
|
|
}
|
|
|
|
|
2018-02-06 03:33:19 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_drop_registry_value() -> Result<()> {
|
2020-05-05 21:32:05 -04:00
|
|
|
struct MyUserdata(Arc<()>);
|
2018-02-06 03:33:19 -05:00
|
|
|
|
|
|
|
impl UserData for MyUserdata {}
|
|
|
|
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2020-05-05 21:32:05 -04:00
|
|
|
let rc = Arc::new(());
|
2018-02-06 03:33:19 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let r = lua.create_registry_value(MyUserdata(rc.clone()))?;
|
2020-05-05 21:32:05 -04:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 2);
|
2018-02-06 03:33:19 -05:00
|
|
|
|
|
|
|
drop(r);
|
|
|
|
lua.expire_registry_values();
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(r#"collectgarbage("collect")"#).exec()?;
|
2018-02-06 03:33:19 -05:00
|
|
|
|
2020-05-05 21:32:05 -04:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 1);
|
2018-01-26 19:27:41 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2018-02-06 00:54:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_lua_registry_ownership() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua1 = Lua::new();
|
|
|
|
let lua2 = Lua::new();
|
2018-02-06 00:54:04 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let r1 = lua1.create_registry_value("hello")?;
|
|
|
|
let r2 = lua2.create_registry_value("hello")?;
|
2018-02-06 00:54:04 -05:00
|
|
|
|
|
|
|
assert!(lua1.owns_registry_value(&r1));
|
|
|
|
assert!(!lua2.owns_registry_value(&r1));
|
|
|
|
assert!(lua2.owns_registry_value(&r2));
|
|
|
|
assert!(!lua1.owns_registry_value(&r2));
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-01-26 19:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn test_mismatched_registry_key() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua1 = Lua::new();
|
|
|
|
let lua2 = Lua::new();
|
2018-01-26 19:27:41 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let r = lua1.create_registry_value("hello")?;
|
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),
|
|
|
|
};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-01-26 19:27:41 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 23:35:29 -05:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn too_many_returns() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
let f = lua.create_function(|_, ()| Ok(Variadic::from_iter(1..1000000)))?;
|
2018-02-09 23:35:29 -05:00
|
|
|
assert!(f.call::<_, Vec<u32>>(()).is_err());
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2018-02-09 23:35:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn too_many_arguments() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load("function test(...) end").exec()?;
|
2018-02-09 23:35:29 -05:00
|
|
|
let args = Variadic::from_iter(1..1000000);
|
2019-09-27 12:38:24 -04:00
|
|
|
assert!(lua
|
|
|
|
.globals()
|
2019-09-28 10:23:17 -04:00
|
|
|
.get::<_, Function>("test")?
|
2019-09-27 12:38:24 -04:00
|
|
|
.call::<_, ()>(args)
|
|
|
|
.is_err());
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2018-02-09 23:35:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-16 05:56:44 -04:00
|
|
|
#[cfg(not(feature = "luajit"))]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn too_many_recursions() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2018-08-05 09:51:39 -04:00
|
|
|
let f = lua
|
2019-09-28 10:23:17 -04:00
|
|
|
.create_function(move |lua, ()| lua.globals().get::<_, Function>("f")?.call::<_, ()>(()))?;
|
|
|
|
lua.globals().set("f", f)?;
|
2018-02-09 23:35:29 -05:00
|
|
|
|
2019-09-27 12:38:24 -04:00
|
|
|
assert!(lua
|
|
|
|
.globals()
|
2019-09-28 10:23:17 -04:00
|
|
|
.get::<_, Function>("f")?
|
2019-09-27 12:38:24 -04:00
|
|
|
.call::<_, ()>(())
|
|
|
|
.is_err());
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:35:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn too_many_binds() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2018-02-09 23:35:29 -05:00
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.load(
|
2018-02-09 23:35:29 -05:00
|
|
|
r#"
|
2019-09-28 10:23:17 -04:00
|
|
|
function f(...)
|
|
|
|
end
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.exec()?;
|
2018-02-09 23:35:29 -05:00
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
let concat = globals.get::<_, Function>("f")?;
|
2018-02-09 23:35:29 -05:00
|
|
|
assert!(concat.bind(Variadic::from_iter(1..1000000)).is_err());
|
2019-09-27 12:38:24 -04:00
|
|
|
assert!(concat
|
|
|
|
.call::<_, ()>(Variadic::from_iter(1..1000000))
|
|
|
|
.is_err());
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-09 23:35:29 -05:00
|
|
|
}
|
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]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn large_args() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
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
|
|
|
let globals = lua.globals();
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
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)
|
|
|
|
})?,
|
|
|
|
)?;
|
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
|
|
|
|
2018-09-24 22:13:42 -04:00
|
|
|
let f: Function = lua
|
2019-09-28 10:23:17 -04:00
|
|
|
.load(
|
2018-08-05 09:51:39 -04:00
|
|
|
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
|
|
|
|
"#,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
2019-09-28 10:23:17 -04:00
|
|
|
.eval()?;
|
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!(
|
2019-09-28 10:23:17 -04:00
|
|
|
f.call::<_, usize>((0..100).collect::<Variadic<usize>>())?,
|
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
|
|
|
4950
|
|
|
|
);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
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
|
|
|
}
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2018-03-12 16:14:52 -04:00
|
|
|
#[test]
|
2019-09-28 10:23:17 -04:00
|
|
|
fn large_args_ref() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
let f = lua.create_function(|_, args: Variadic<String>| {
|
|
|
|
for i in 0..args.len() {
|
|
|
|
assert_eq!(args[i], i.to_string());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
f.call::<_, ()>((0..100).map(|i| i.to_string()).collect::<Variadic<_>>())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chunk_env() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
let assert: Function = lua.globals().get("assert")?;
|
|
|
|
|
|
|
|
let env1 = lua.create_table()?;
|
|
|
|
env1.set("assert", assert.clone())?;
|
|
|
|
|
|
|
|
let env2 = lua.create_table()?;
|
|
|
|
env2.set("assert", assert)?;
|
|
|
|
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
test_var = 1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.set_environment(env1.clone())?
|
|
|
|
.exec()?;
|
|
|
|
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
assert(test_var == nil)
|
|
|
|
test_var = 2
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.set_environment(env2.clone())?
|
|
|
|
.exec()?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
lua.load("test_var").set_environment(env1)?.eval::<i32>()?,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
lua.load("test_var").set_environment(env2)?.eval::<i32>()?,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn context_thread() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2018-09-24 22:13:42 -04:00
|
|
|
let f = lua
|
2019-09-28 10:23:17 -04:00
|
|
|
.load(
|
|
|
|
r#"
|
|
|
|
local thread = ...
|
|
|
|
assert(coroutine.running() == thread)
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.into_function()?;
|
|
|
|
|
2020-05-08 07:42:40 -04:00
|
|
|
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
|
2019-09-28 10:23:17 -04:00
|
|
|
f.call::<_, ()>(lua.current_thread())?;
|
2018-03-12 16:00:11 -04:00
|
|
|
|
2019-11-04 11:31:19 -05:00
|
|
|
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
2019-10-14 17:21:30 -04:00
|
|
|
f.call::<_, ()>(Nil)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-11-04 11:31:19 -05:00
|
|
|
#[cfg(any(feature = "lua51", feature = "luajit"))]
|
2019-10-14 17:21:30 -04:00
|
|
|
fn context_thread_51() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let thread = lua.create_thread(
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
function (thread)
|
|
|
|
assert(coroutine.running() == thread)
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.eval()?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
thread.resume::<_, ()>(thread.clone())?;
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
Ok(())
|
2018-03-12 16:00:11 -04:00
|
|
|
}
|