Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code error message #1633

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading