From ac0c973a45f2408416c171b46e40340ae21776ff Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 9 Sep 2024 18:34:10 +0800 Subject: [PATCH] refactor: code error message Signed-off-by: peefy --- kclvm/error/src/lib.rs | 12 ++++++++++-- kclvm/runner/src/runner.rs | 4 ++-- kclvm/runtime/src/_kcl_run.rs | 4 +++- kclvm/src/lib.rs | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/kclvm/error/src/lib.rs b/kclvm/error/src/lib.rs index 5b419a3f2..a307a4d80 100644 --- a/kclvm/error/src/lib.rs +++ b/kclvm/error/src/lib.rs @@ -554,11 +554,19 @@ impl SessionDiagnostic for Diagnostic { diag.append_component(Box::new(format!("{dl}\n"))); } None => { - diag.append_component(Box::new(format!("{}\n", msg.message))); + diag.append_component(Box::new(format!( + "{}: {}\n", + msg.range.0.info(), + msg.message + ))); } }; } - Err(_) => diag.append_component(Box::new(format!("{}\n", msg.message))), + Err(_) => diag.append_component(Box::new(format!( + "{}: {}\n", + msg.range.0.info(), + msg.message + ))), }; if let Some(note) = &msg.note { diag.append_component(Box::new(Label::Note)); diff --git a/kclvm/runner/src/runner.rs b/kclvm/runner/src/runner.rs index 85f9be358..806ec56d0 100644 --- a/kclvm/runner/src/runner.rs +++ b/kclvm/runner/src/runner.rs @@ -460,7 +460,7 @@ impl LibRunner { } thread_local! { - static KCL_RUNTIME_PANIC_RECORD: RefCell = RefCell::new(RuntimePanicRecord::default()) + pub static KCL_RUNTIME_PANIC_RECORD: RefCell = RefCell::new(RuntimePanicRecord::default()) } pub struct FastRunner { @@ -491,7 +491,7 @@ impl FastRunner { } else if let Some(s) = info.payload().downcast_ref::() { (*s).clone() } else { - "".to_string() + "unknown runtime error".to_string() }; if let Some(location) = info.location() { record.rust_file = location.file().to_string(); diff --git a/kclvm/runtime/src/_kcl_run.rs b/kclvm/runtime/src/_kcl_run.rs index a9ab2cbb0..efe0df0c1 100644 --- a/kclvm/runtime/src/_kcl_run.rs +++ b/kclvm/runtime/src/_kcl_run.rs @@ -44,6 +44,9 @@ type kclvm_float_t = f64; pub struct RuntimePanicRecord { pub kcl_panic_info: bool, pub message: String, + pub kcl_file: String, + pub kcl_line: i32, + pub kcl_column: i32, pub rust_file: String, pub rust_line: i32, pub rust_col: i32, @@ -114,7 +117,6 @@ pub unsafe extern "C" fn _kcl_run( KCL_RUNTIME_PANIC_RECORD.with(|record| { let mut record = record.borrow_mut(); record.kcl_panic_info = true; - record.message = if let Some(s) = info.payload().downcast_ref::<&str>() { s.to_string() } else if let Some(s) = info.payload().downcast_ref::<&String>() { diff --git a/kclvm/src/lib.rs b/kclvm/src/lib.rs index 70d23b148..7c9acb316 100644 --- a/kclvm/src/lib.rs +++ b/kclvm/src/lib.rs @@ -161,6 +161,24 @@ pub unsafe extern "C" fn kcl_fmt(src_ptr: *const c_char) -> *const c_char { } } +/// Exposes a normal kcl runtime error function to the WASM host. +#[no_mangle] +pub extern "C" fn kcl_runtime_err(buffer: *mut u8, length: usize) -> isize { + KCL_RUNTIME_PANIC_RECORD.with(|e| { + let message = &e.borrow().message; + if !message.is_empty() { + let bytes = message.as_bytes(); + let copy_len = std::cmp::min(bytes.len(), length); + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), buffer, copy_len); + } + copy_len as isize + } else { + 0 + } + }) +} + fn intern_fmt(src: &str) -> Result { let api = API::default(); let args = &FormatCodeArgs {