add flag to change exit behavior to RuntimeError

This commit is contained in:
Mark 2024-10-14 00:30:57 +02:00
parent 8e07f240cc
commit 8868746e17
3 changed files with 20 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "mers_lib"
version = "0.9.11"
version = "0.9.12"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "library to use the mers language in other projects"

View File

@ -14,7 +14,7 @@ use crate::{
use super::{
gen::{
function::{func, func_end},
function::{func, func_err},
IntR, OneOrNone,
},
Config,
@ -60,8 +60,15 @@ impl Config {
// )
.add_var(
"exit",
func_end(|code: IntR<INT_MIN, INT_MAX>, _| {
std::process::exit(code.0.try_into().unwrap_or(255));
func_err(|code: IntR<INT_MIN, INT_MAX>, i| {
if i.global
.allow_process_exit_via_exit
.load(std::sync::atomic::Ordering::Relaxed)
{
std::process::exit(code.0.try_into().unwrap_or(255));
} else {
format!("Program exited with status code {}", code.0).into()
}
}),
)
.add_var(

View File

@ -2,7 +2,7 @@ use std::{
collections::HashMap,
fmt::Debug,
io::{Read, Write},
sync::{Arc, Mutex, RwLock},
sync::{atomic::AtomicBool, Arc, Mutex, RwLock},
time::Instant,
};
@ -135,6 +135,7 @@ pub struct RunLocalGlobalInfo {
pub object_fields_rev: Arc<Mutex<Vec<String>>>,
pub stdin: Arc<Mutex<Option<Box<dyn Read + Send + Sync>>>>,
pub stdout: Arc<Mutex<Option<(Box<dyn Write + Send + Sync>, Box<dyn Write + Send + Sync>)>>>,
pub allow_process_exit_via_exit: Arc<AtomicBool>,
}
#[derive(Debug)]
#[allow(unused)]
@ -144,6 +145,7 @@ struct RunLocalGlobalInfoDebug<'a> {
pub object_fields_rev: &'a Arc<Mutex<Vec<String>>>,
pub stdin: bool,
pub stdout: bool,
pub allow_process_exit_via_exit: bool,
}
impl Debug for RunLocalGlobalInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -155,7 +157,10 @@ impl Debug for RunLocalGlobalInfo {
object_fields: &self.object_fields,
object_fields_rev: &self.object_fields_rev,
stdin: self.stdin.lock().unwrap().is_some(),
stdout: self.stdout.lock().unwrap().is_some()
stdout: self.stdout.lock().unwrap().is_some(),
allow_process_exit_via_exit: self
.allow_process_exit_via_exit
.load(std::sync::atomic::Ordering::Relaxed),
}
)
}
@ -168,6 +173,7 @@ impl RunLocalGlobalInfo {
object_fields_rev: Default::default(),
stdin: Arc::new(Mutex::new(None)),
stdout: Arc::new(Mutex::new(None)),
allow_process_exit_via_exit: Arc::new(AtomicBool::new(true)),
}
}
}
@ -247,6 +253,7 @@ impl info::Local for RunLocal {
object_fields_rev: Default::default(),
stdin: Default::default(),
stdout: Default::default(),
allow_process_exit_via_exit: Arc::new(AtomicBool::new(false)),
}
}
fn init_var(&mut self, id: Self::VariableIdentifier, value: Self::VariableData) {