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 c84a86a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
19 changes: 17 additions & 2 deletions kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
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
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 c84a86a

Please sign in to comment.