Skip to content

Commit

Permalink
refactor: code error message
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Sep 9, 2024
1 parent d5f4a27 commit 92b1320
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
12 changes: 10 additions & 2 deletions kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl LibRunner {
}

thread_local! {
static KCL_RUNTIME_PANIC_RECORD: RefCell<RuntimePanicRecord> = RefCell::new(RuntimePanicRecord::default())
pub static KCL_RUNTIME_PANIC_RECORD: RefCell<RuntimePanicRecord> = RefCell::new(RuntimePanicRecord::default())
}

pub struct FastRunner {
Expand Down Expand Up @@ -491,7 +491,7 @@ impl FastRunner {
} else if let Some(s) = info.payload().downcast_ref::<String>() {
(*s).clone()
} else {
"".to_string()
"unknown runtime error".to_string()
};
if let Some(location) = info.location() {
record.rust_file = location.file().to_string();
Expand Down
4 changes: 3 additions & 1 deletion kclvm/runtime/src/_kcl_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>() {
Expand Down
22 changes: 20 additions & 2 deletions kclvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
let api = API::default();
let args = &FormatCodeArgs {
Expand All @@ -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
}
}

Expand Down

0 comments on commit 92b1320

Please sign in to comment.