diff --git a/mers_lib/Cargo.toml b/mers_lib/Cargo.toml index 70fe203..ece12c9 100755 --- a/mers_lib/Cargo.toml +++ b/mers_lib/Cargo.toml @@ -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" diff --git a/mers_lib/src/program/configs/with_stdio.rs b/mers_lib/src/program/configs/with_stdio.rs index 5010a75..213a9be 100755 --- a/mers_lib/src/program/configs/with_stdio.rs +++ b/mers_lib/src/program/configs/with_stdio.rs @@ -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, _| { - std::process::exit(code.0.try_into().unwrap_or(255)); + func_err(|code: IntR, 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( diff --git a/mers_lib/src/program/run/mod.rs b/mers_lib/src/program/run/mod.rs index 10832c6..0a6a149 100755 --- a/mers_lib/src/program/run/mod.rs +++ b/mers_lib/src/program/run/mod.rs @@ -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>>, pub stdin: Arc>>>, pub stdout: Arc, Box)>>>, + pub allow_process_exit_via_exit: Arc, } #[derive(Debug)] #[allow(unused)] @@ -144,6 +145,7 @@ struct RunLocalGlobalInfoDebug<'a> { pub object_fields_rev: &'a Arc>>, 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) {