summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml1
-rw-r--r--src/conversion.rs19
-rw-r--r--src/lib.rs1
-rw-r--r--tests/tests.rs4
4 files changed, 22 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 561ab4c..47da1c9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -34,6 +34,7 @@ system-lua = ["pkg-config"]
[dependencies]
libc = { version = "0.2" }
failure = { version = "0.1.2" }
+num-traits = { version = "0.2" }
compiletest_rs = { version = "0.3", optional = true }
[build-dependencies]
diff --git a/src/conversion.rs b/src/conversion.rs
index b66a9f0..a28b93a 100644
--- a/src/conversion.rs
+++ b/src/conversion.rs
@@ -2,13 +2,15 @@ use std::collections::{BTreeMap, HashMap};
use std::hash::{BuildHasher, Hash};
use std::string::String as StdString;
+use num_traits::cast;
+
use error::{Error, Result};
use function::Function;
use lua::Lua;
use string::String;
use table::Table;
use thread::Thread;
-use types::{Integer, LightUserData, Number};
+use types::{LightUserData, Number};
use userdata::{AnyUserData, UserData};
use value::{FromLua, Nil, ToLua, Value};
@@ -207,13 +209,22 @@ macro_rules! lua_convert_int {
($x:ty) => {
impl<'lua> ToLua<'lua> for $x {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
- Ok(Value::Integer(self as Integer))
+ cast(self)
+ .ok_or_else(|| Error::ToLuaConversionError {
+ from: stringify!($x),
+ to: "integer",
+ message: Some("integer out of range".to_owned()),
+ }).map(Value::Integer)
}
}
impl<'lua> FromLua<'lua> for $x {
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
- Ok(lua.coerce_integer(value)? as $x)
+ cast(lua.coerce_integer(value)?).ok_or_else(|| Error::FromLuaConversionError {
+ from: "integer",
+ to: stringify!($x),
+ message: Some("integer out of range".to_owned()),
+ })
}
}
};
@@ -227,6 +238,8 @@ lua_convert_int!(i32);
lua_convert_int!(u32);
lua_convert_int!(i64);
lua_convert_int!(u64);
+lua_convert_int!(i128);
+lua_convert_int!(u128);
lua_convert_int!(isize);
lua_convert_int!(usize);
diff --git a/src/lib.rs b/src/lib.rs
index b974897..21e8749 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,6 +42,7 @@
extern crate failure;
extern crate libc;
+extern crate num_traits;
mod error;
mod ffi;
diff --git a/tests/tests.rs b/tests/tests.rs
index c31a4e6..8383afe 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -373,6 +373,10 @@ fn test_num_conversion() {
lua.exec::<()>("a = math.huge", None).unwrap();
assert!(globals.get::<_, i64>("n").is_err());
+
+ assert!(lua.eval::<u64>("-1", None).is_err());
+ assert!(lua.eval::<i64>("-1", None).is_ok());
+ assert!(lua.pack(1u128 << 64).is_err());
}
#[test]