diff --git a/src/error.rs b/src/error.rs new file mode 100755 index 0000000..117df41 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; + +use mlua_sys as lua; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("Syntax error: {0}")] + Syntax(String), + /// An error returned from lua + #[error("Lua runtime error: {0}")] + Runtime(String), + #[error("Error when running a __gc metamethod")] + GcFailure(String), + /// Ran into a not enough memory error when trying to grow the lua stack. + #[error("Failed to allocate memory")] + MemoryAlloc, + #[error("Ran into a nill value on the stack")] + Nil, + #[error("Unexpected type, expected {0} but got {1}")] + UnexpectedType(String, String), + #[error("bad argument #{arg_index}{} to `{}` ({error})", + .arg_name.clone().map(|a| format!(" (name: {})", a)).unwrap_or("".to_string()), + .func.clone().unwrap_or("Unknown".to_string()) + )] + BadArgument { + func: Option, + arg_index: i32, + arg_name: Option, + /// the error that describes what was wrong for this argument + #[source] + error: Arc + }, + #[error("Incorrect number of arguments, expected {arg_expected}, got {arg_count}")] + IncorrectArgCount { + arg_expected: i32, + arg_count: i32, + }, + #[error("There is already a registry entry with the key {0}")] + RegistryConflict(String), + #[error("Userdata types did not match")] + UserdataMismatch, + #[error("Missing meta table for userdata")] + MissingMetatable, + #[error("An error occurred when attempting to convert from a ValueVec at value index {value_idx}, cause: {error}")] + ValueVecError { + value_idx: i32, + #[source] + error: Arc, + }, +} + +impl Error { + pub fn runtime(msg: &str) -> Self { + Self::Runtime(msg.to_string()) + } + + pub fn unexpected_type(expected: &str, got: &str) -> Self { + Self::UnexpectedType(expected.to_string(), got.to_string()) + } + + /// Throw the error in lua. + /// + /// This method never returns + pub unsafe fn throw_lua(self, lua: *mut lua::lua_State) -> ! { + let msg = format!("{}\0", self); + let msg_c = msg.as_ptr() as *const i8; + lua::luaL_error(lua, msg_c); + unreachable!(); + } +} + +/// A result for use with lua functions +pub type Result = core::result::Result; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2f91304..b170659 100755 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,9 @@ use util::*; pub mod chunk; use chunk::*; +pub mod error; +use error::*; + fn main() -> Result<()> { let lua = State::new(); lua.expose_libraries(&[StdLibrary::Debug, StdLibrary::Package]); @@ -198,76 +201,6 @@ impl<'a> PushToLuaStack<'a> for LuaRef<'a> { } } -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("Syntax error: {0}")] - Syntax(String), - /// An error returned from lua - #[error("Lua runtime error: {0}")] - Runtime(String), - #[error("Error when running a __gc metamethod")] - GcFailure(String), - /// Ran into a not enough memory error when trying to grow the lua stack. - #[error("Failed to allocate memory")] - MemoryAlloc, - #[error("Ran into a nill value on the stack")] - Nil, - #[error("Unexpected type, expected {0} but got {1}")] - UnexpectedType(String, String), - #[error("bad argument #{arg_index}{} to `{}` ({error})", - .arg_name.clone().map(|a| format!(" (name: {})", a)).unwrap_or("".to_string()), - .func.clone().unwrap_or("Unknown".to_string()) - )] - BadArgument { - func: Option, - arg_index: i32, - arg_name: Option, - /// the error that describes what was wrong for this argument - #[source] - error: Arc - }, - #[error("Incorrect number of arguments, expected {arg_expected}, got {arg_count}")] - IncorrectArgCount { - arg_expected: i32, - arg_count: i32, - }, - #[error("There is already a registry entry with the key {0}")] - RegistryConflict(String), - #[error("Userdata types did not match")] - UserdataMismatch, - #[error("Missing meta table for userdata")] - MissingMetatable, - #[error("An error occurred when attempting to convert from a ValueVec at value index {value_idx}, cause: {error}")] - ValueVecError { - value_idx: i32, - #[source] - error: Arc, - }, -} - -impl Error { - pub fn runtime(msg: &str) -> Self { - Self::Runtime(msg.to_string()) - } - - pub fn unexpected_type(expected: &str, got: &str) -> Self { - Self::UnexpectedType(expected.to_string(), got.to_string()) - } - - /// Throw the error in lua. - /// - /// This method never returns - pub unsafe fn throw_lua(self, lua: *mut lua::lua_State) -> ! { - let msg = format!("{}\0", self); - let msg_c = msg.as_ptr() as *const i8; - lua::luaL_error(lua, msg_c); - unreachable!(); - } -} - -/// A result for use with lua functions -type Result = core::result::Result; - pub trait PushToLuaStack<'a> { unsafe fn push_to_lua_stack(&self, state: &'a State) -> Result<()>; } diff --git a/src/userdata.rs b/src/userdata.rs index 340e5f2..c71fbe0 100755 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -1,12 +1,9 @@ -use std::{borrow::Borrow, cell::{Ref, RefCell, RefMut}, collections::HashMap, ffi::CStr, marker::PhantomData, ops::{Deref, DerefMut}}; +use std::{cell::{Ref, RefCell, RefMut}, collections::HashMap, ffi::CStr, marker::PhantomData, ops::DerefMut}; use crate::{ensure_type, AsLua, FromLua, FromLuaStack, FromLuaVec, LuaRef, PushToLuaStack, StackGuard, State, Value, ValueVec}; use mlua_sys as lua; -//pub type FieldSetter = fn(lua: &State, this: &T); -//pub type FieldGetter = fn(lua: &State, this: &T, val: &U); - /// An enum representing all Lua MetaMethods /// https://gist.github.com/oatmealine/655c9e64599d0f0dd47687c1186de99f pub enum MetaMethod { @@ -92,14 +89,6 @@ impl<'a> PushToLuaStack<'a> for MetaMethod { } } -pub trait FieldSetter { - fn set_field(&self, val: Value); -} - -pub trait FieldGetter { - fn get_field(&self) -> Value; -} - type UserdataFn<'a> = Box) -> crate::Result>>; pub struct UserdataBuilder<'a, T> {