A bit of code cleanup
This commit is contained in:
parent
eebb93a9a6
commit
6c9798eb5b
|
@ -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<String>,
|
||||||
|
arg_index: i32,
|
||||||
|
arg_name: Option<String>,
|
||||||
|
/// the error that describes what was wrong for this argument
|
||||||
|
#[source]
|
||||||
|
error: Arc<Error>
|
||||||
|
},
|
||||||
|
#[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<Error>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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<T> = core::result::Result<T, Error>;
|
73
src/main.rs
73
src/main.rs
|
@ -26,6 +26,9 @@ use util::*;
|
||||||
pub mod chunk;
|
pub mod chunk;
|
||||||
use chunk::*;
|
use chunk::*;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
use error::*;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let lua = State::new();
|
let lua = State::new();
|
||||||
lua.expose_libraries(&[StdLibrary::Debug, StdLibrary::Package]);
|
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<String>,
|
|
||||||
arg_index: i32,
|
|
||||||
arg_name: Option<String>,
|
|
||||||
/// the error that describes what was wrong for this argument
|
|
||||||
#[source]
|
|
||||||
error: Arc<Error>
|
|
||||||
},
|
|
||||||
#[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<Error>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
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<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
pub trait PushToLuaStack<'a> {
|
pub trait PushToLuaStack<'a> {
|
||||||
unsafe fn push_to_lua_stack(&self, state: &'a State) -> Result<()>;
|
unsafe fn push_to_lua_stack(&self, state: &'a State) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 crate::{ensure_type, AsLua, FromLua, FromLuaStack, FromLuaVec, LuaRef, PushToLuaStack, StackGuard, State, Value, ValueVec};
|
||||||
|
|
||||||
use mlua_sys as lua;
|
use mlua_sys as lua;
|
||||||
|
|
||||||
//pub type FieldSetter<T> = fn(lua: &State, this: &T);
|
|
||||||
//pub type FieldGetter<T, U> = fn(lua: &State, this: &T, val: &U);
|
|
||||||
|
|
||||||
/// An enum representing all Lua MetaMethods
|
/// An enum representing all Lua MetaMethods
|
||||||
/// https://gist.github.com/oatmealine/655c9e64599d0f0dd47687c1186de99f
|
/// https://gist.github.com/oatmealine/655c9e64599d0f0dd47687c1186de99f
|
||||||
pub enum MetaMethod {
|
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<dyn Fn(&'a State, ValueVec<'a>) -> crate::Result<Value<'a>>>;
|
type UserdataFn<'a> = Box<dyn Fn(&'a State, ValueVec<'a>) -> crate::Result<Value<'a>>>;
|
||||||
|
|
||||||
pub struct UserdataBuilder<'a, T> {
|
pub struct UserdataBuilder<'a, T> {
|
||||||
|
|
Loading…
Reference in New Issue