diff --git a/Cargo.lock b/Cargo.lock index 9bece02..42c560d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,12 +492,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "critical-section" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" - [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -1251,10 +1245,6 @@ name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -dependencies = [ - "critical-section", - "portable-atomic", -] [[package]] name = "openssl-probe" @@ -1388,12 +1378,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" -[[package]] -name = "portable-atomic" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" - [[package]] name = "proc-macro-error" version = "1.0.4" diff --git a/anda-config/Cargo.toml b/anda-config/Cargo.toml index 63bfe36..2e57ff6 100644 --- a/anda-config/Cargo.toml +++ b/anda-config/Cargo.toml @@ -21,7 +21,7 @@ num-traits = "0.2.15" regex = "~1" walkdir = "2.3.2" ignore = "0.4" -once_cell = { version = "1.16", default-features = false, features = ["critical-section"] } +once_cell = { version = "1.16", default-features = false, features = ["std"] } parking_lot = "0.12.1" [dev-dependencies] diff --git a/andax/src/fns/io.rs b/andax/src/fns/io.rs index b878133..46ef9da 100644 --- a/andax/src/fns/io.rs +++ b/andax/src/fns/io.rs @@ -140,9 +140,11 @@ pub mod ar { #[cfg(test)] mod test { use super::*; + #[test] fn shells() -> Result<(), Box> { - let (en, sc) = crate::run::gen_en(); - en.run(r#" + let (en, _) = crate::run::gen_en(); + en.run( + r#" let a = sh("echo hai > test"); let b = sh(["echo", "hai"]); let c = sh(["rm", "-rf", "test"]); @@ -161,7 +163,8 @@ mod test { if d.sh_stdout().is_empty() { throw "why is out empty?"; } - "#)?; + "#, + )?; Ok(()) } -} \ No newline at end of file +} diff --git a/andax/src/run.rs b/andax/src/run.rs index 76f1b92..8b9966b 100644 --- a/andax/src/run.rs +++ b/andax/src/run.rs @@ -80,7 +80,7 @@ fn module_resolver() -> ModuleResolversCollection { resolv } -pub(crate) fn gen_en() -> (Engine, Scope<'static>) { +pub fn gen_en() -> (Engine, Scope<'static>) { let mut sc = Scope::new(); sc.push("USER_AGENT", f::tsunagu::USER_AGENT); sc.push("IS_WIN32", cfg!(windows)); @@ -111,59 +111,62 @@ lazy_static! { static ref WORD_REGEX: Regex = Regex::new(r"[A-Za-z_][A-Za-z0-9_]*").unwrap(); } -#[instrument(name = "traceback")] -pub fn _tb(proj: &str, scr: &Path, nanitozo: TbErr, pos: Position, rhai_fn: &str, fn_src: &str) { - if let Some((line, col)) = _gpos(pos) { - // Print code - let f = std::fs::File::open(scr); - let scr = scr.display(); - macro_rules! die { - ($var:expr, $msg:expr) => {{ - if let Err(e) = $var { - error!($msg, e); - return error!("{proj}: {scr} (no position data)\n{nanitozo}"); - } - $var.unwrap() - }}; - } - let f = die!(f, "{proj}: Cannot open `{scr}`: {}"); - let sl = std::io::BufReader::new(f).lines().nth(line - 1); - let sl = die!(sl, "{proj}: Non-existence exception at {scr}:{line}:{col}"); - // replace tabs to avoid wrong position when print - let sl = die!(sl, "{proj}: Cannot read line: {}").replace('\t', " "); - let m = WORD_REGEX.find_at(sl.as_str(), col - 1).map_or(1, |x| { - let r = x.range(); - if r.start == col - 1 { - r.len() - } else { - 1 +// proj: project name, scr: script path, nntz (nanitozo): just give me the error +// pos: error position, rhai_fn: function that caused the issue, fn_src: idk… +#[instrument] +pub fn traceback(proj: &str, scr: &Path, nntz: TbErr, pos: Position, rhai_fn: &str, fn_src: &str) { + let Some((line, col)) = _gpos(pos) else { + return error!("{proj}: {scr:?} (no position data)\n{nntz}"); + }; + let f = std::fs::File::open(scr); + let scr = scr.display(); + macro_rules! die { + ($var:expr, $msg:expr) => {{ + if let Err(e) = $var { + error!($msg, e); + return error!("{proj}: {scr} (no position data)\n{nntz}"); } - }); - let ln = line.to_string().len(); - let lns = " ".repeat(ln); - let l = "─".repeat(ln); - let r = "─".repeat(sl.len() + 2); - let mut code = format!( - "─{l}─┬{r}\n {lns} │ {scr}:{line}:{col}\n─{l}─┼{r}\n {line} │ {sl}\n {lns} │ {}{}", - " ".repeat(col - 1), - "🭶".repeat(m) - ); - if !rhai_fn.is_empty() { - code += &format!("\n {lns} └─═ When invoking: {rhai_fn}()"); - } - if !fn_src.is_empty() { - code += &format!("\n {lns} └─═ Function source: {fn_src}"); - } - code += &format!("\n {lns} └─═ {nanitozo}"); - code += &hint(&sl, &lns, &nanitozo, rhai_fn).unwrap_or_default(); - // slow but works! - let c = code.matches('└').count(); - if c > 0 { - code = code.replacen('└', "├", c - 1); + $var.unwrap() + }}; + } + let f = die!(f, "{proj}: Cannot open `{scr}`: {}"); + let Some(sl) = std::io::BufReader::new(f).lines().nth(line - 1) else { + error!("{proj}: Non-existence exception at {scr}:{line}:{col}"); + return error!("{proj}: {scr} (no position data)\n{nntz}"); + }; + // replace tabs to avoid wrong position when print + let sl = die!(sl, "{proj}: Cannot read line: {}").replace('\t', " "); + let m = WORD_REGEX.find_at(sl.as_str(), col - 1).map_or(1, |x| { + let r = x.range(); + if r.start == col - 1 { + r.len() + } else { + 1 } - return error!("Script Exception —— {proj}\n{code}"); + }); // number of underline chars + let ln = line.to_string().len(); // length of the string of the line number + let lns = " ".repeat(ln); // spaces for padding the left hand side line number place + let l = "─".repeat(ln); // padding for the top of line number display + let r = "─".repeat(sl.len() + 2); // right hand side padding + let mut code = format!( + "─{l}─┬{r}\n {lns} │ {scr}:{line}:{col}\n─{l}─┼{r}\n {line} │ {sl}\n {lns} │ {}{}", + " ".repeat(col - 1), // padding at left of underline + "🭶".repeat(m) // underline the word + ); + if !rhai_fn.is_empty() { + code += &format!("\n {lns} └─═ When invoking: {rhai_fn}()"); + } + if !fn_src.is_empty() { + code += &format!("\n {lns} └─═ Function source: {fn_src}"); + } + code += &format!("\n {lns} └─═ {nntz}"); + code += &hint(&sl, &lns, &nntz, rhai_fn).unwrap_or_default(); + // slow but works! + let c = code.matches('└').count(); + if c > 0 { + code = code.replacen('└', "├", c - 1); } - error!("{proj}: {scr:?} (no position data)\n{nanitozo}"); + return error!("Script Exception —— {proj}\n{code}"); } /// Handles an exception thrown while executing an AndaX script. @@ -172,10 +175,17 @@ pub fn errhdl(name: &str, scr: &Path, err: EvalAltResult) { if let EvalAltResult::ErrorRuntime(ref run_err, pos) = err { match run_err.clone().try_cast::() { Some(AErr::RustReport(rhai_fn, fn_src, others)) => { - return _tb(name, scr, Report(others), pos, rhai_fn.as_str(), fn_src.as_str()); + return traceback( + name, + scr, + Report(others), + pos, + rhai_fn.as_str(), + fn_src.as_str(), + ); } Some(AErr::RustError(rhai_fn, fn_src, others)) => { - return _tb(name, scr, Arb(others), pos, rhai_fn.as_str(), fn_src.as_str()); + return traceback(name, scr, Arb(others), pos, rhai_fn.as_str(), fn_src.as_str()); } Some(AErr::Exit(b)) => { if b { @@ -190,7 +200,7 @@ pub fn errhdl(name: &str, scr: &Path, err: EvalAltResult) { } trace!("Rhai moment: {err:#?}"); let pos = err.position(); - _tb(name, scr, Rhai(err), pos, "", ""); + traceback(name, scr, Rhai(err), pos, "", ""); } /// Executes an AndaX script.