diff --git a/kclvm/error/src/lib.rs b/kclvm/error/src/lib.rs index 5b419a3f2..facd304db 100644 --- a/kclvm/error/src/lib.rs +++ b/kclvm/error/src/lib.rs @@ -554,11 +554,26 @@ impl SessionDiagnostic for Diagnostic { diag.append_component(Box::new(format!("{dl}\n"))); } None => { - diag.append_component(Box::new(format!("{}\n", msg.message))); + let info = msg.range.0.info(); + if !info.is_empty() { + diag.append_component(Box::new(format!( + "{}: {}\n", + info, msg.message + ))); + } else { + diag.append_component(Box::new(format!("{}\n", msg.message))); + } } }; } - Err(_) => diag.append_component(Box::new(format!("{}\n", msg.message))), + Err(_) => { + let info = msg.range.0.info(); + if !info.is_empty() { + diag.append_component(Box::new(format!("{}: {}\n", info, msg.message))); + } else { + diag.append_component(Box::new(format!("{}\n", 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/src/lib.rs b/kclvm/src/lib.rs index 70d23b148..15bd00125 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 unsafe 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 { @@ -182,12 +200,12 @@ pub unsafe extern "C" fn kcl_malloc(size: usize) -> *mut u8 { if layout.size() > 0 { let ptr = alloc(layout); if !ptr.is_null() { - return ptr; + ptr } else { std::alloc::handle_alloc_error(layout); } } else { - return align as *mut u8; + align as *mut u8 } }