improve panic function, add stacktrace

This commit is contained in:
Mark
2024-06-19 12:35:23 +02:00
parent 4770e3f939
commit cd21c2171e
35 changed files with 367 additions and 232 deletions

View File

@@ -16,7 +16,8 @@ pub struct Function {
pub info: Arc<Info>,
pub info_check: Arc<Mutex<CheckInfo>>,
pub out: Arc<dyn Fn(&Type, &mut CheckInfo) -> Result<Type, CheckError> + Send + Sync>,
pub run: Arc<dyn Fn(Data, &mut crate::program::run::Info) -> Data + Send + Sync>,
pub run:
Arc<dyn Fn(Data, &mut crate::program::run::Info) -> Result<Data, CheckError> + Send + Sync>,
pub inner_statements: Option<(
Arc<Box<dyn crate::program::run::MersStatement>>,
Arc<Box<dyn crate::program::run::MersStatement>>,
@@ -25,7 +26,7 @@ pub struct Function {
impl Function {
pub fn new(
out: impl Fn(&Type) -> Result<Type, CheckError> + Send + Sync + 'static,
run: impl Fn(Data) -> Data + Send + Sync + 'static,
run: impl Fn(Data) -> Result<Data, CheckError> + Send + Sync + 'static,
) -> Self {
Self {
info: Arc::new(crate::info::Info::neverused()),
@@ -56,7 +57,7 @@ impl Function {
drop(lock);
(self.out)(arg, &mut info)
}
pub fn run(&self, arg: Data) -> Data {
pub fn run(&self, arg: Data) -> Result<Data, CheckError> {
(self.run)(arg, &mut self.info.as_ref().clone())
}
pub fn get_as_type(&self) -> FunctionT {
@@ -72,10 +73,19 @@ impl Function {
}
impl MersData for Function {
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Data>>> {
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Result<Data, CheckError>>>> {
let s = Clone::clone(self);
Some(Box::new(std::iter::from_fn(move || {
s.run(Data::empty_tuple()).one_tuple_content()
match s.run(Data::empty_tuple()) {
Err(e) => Some(Err(e)),
Ok(v) => {
if let Some(v) = v.one_tuple_content() {
Some(Ok(v))
} else {
None
}
}
}
})))
}
fn is_eq(&self, _other: &dyn MersData) -> bool {

View File

@@ -4,6 +4,8 @@ use std::{
sync::{atomic::AtomicUsize, Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
use crate::errors::CheckError;
pub mod bool;
pub mod byte;
pub mod float;
@@ -17,12 +19,12 @@ pub mod tuple;
pub mod defs;
pub trait MersData: Any + Debug + Display + Send + Sync {
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Data>>> {
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Result<Data, CheckError>>>> {
None
}
/// By default, uses `iterable` to get an iterator and `nth` to retrieve the nth element.
/// Should have a custom implementation for better performance on most types
fn get(&self, i: usize) -> Option<Data> {
fn get(&self, i: usize) -> Option<Result<Data, CheckError>> {
self.iterable()?.nth(i)
}
/// If self and other are different types (`other.as_any().downcast_ref::<Self>().is_none()`),

View File

@@ -1,5 +1,7 @@
use std::{any::Any, fmt::Display, sync::Arc};
use crate::errors::CheckError;
use super::{Data, MersData, MersType, Type};
#[derive(Debug, Clone)]
@@ -22,8 +24,8 @@ impl MersData for Tuple {
false
}
}
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Data>>> {
Some(Box::new(self.0.clone().into_iter()))
fn iterable(&self) -> Option<Box<dyn Iterator<Item = Result<Data, CheckError>>>> {
Some(Box::new(self.0.clone().into_iter().map(Ok)))
}
fn clone(&self) -> Box<dyn MersData> {
Box::new(Clone::clone(self))