Skip to content

Commit

Permalink
feat: make wasm target panic hook set only once
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Sep 10, 2024
1 parent d16b969 commit 6085941
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
35 changes: 34 additions & 1 deletion kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use kclvm_runtime::kclvm_plugin_init;
#[cfg(feature = "llvm")]
use kclvm_runtime::FFIRunOptions;
use kclvm_runtime::{Context, PanicInfo, RuntimePanicRecord};
#[cfg(target_arch = "wasm32")]
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::os::raw::c_char;
Expand Down Expand Up @@ -460,9 +462,33 @@ impl LibRunner {
}

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

#[cfg(target_arch = "wasm32")]
static ONCE_PANIC_HOOK: Lazy<()> = Lazy::new(|| {
std::panic::set_hook(Box::new(|info: &std::panic::PanicInfo| {
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>() {
(*s).clone()
} else if let Some(s) = info.payload().downcast_ref::<String>() {
(*s).clone()
} else {
"unknown runtime error".to_string()
};
if let Some(location) = info.location() {
record.rust_file = location.file().to_string();
record.rust_line = location.line() as i32;
record.rust_col = location.column() as i32;
}
})
}));
});

pub struct FastRunner {
opts: RunnerOptions,
}
Expand All @@ -479,7 +505,13 @@ impl FastRunner {
pub fn run(&self, program: &ast::Program, args: &ExecProgramArgs) -> Result<ExecProgramResult> {
let ctx = Rc::new(RefCell::new(args_to_ctx(program, args)));
let evaluator = Evaluator::new_with_runtime_ctx(program, ctx.clone());
#[cfg(target_arch = "wasm32")]
// Ensure the panic hook is set (this will only happen once) for the WASM target,
// because it is single threaded.
Lazy::force(&ONCE_PANIC_HOOK);
#[cfg(not(target_arch = "wasm32"))]
let prev_hook = std::panic::take_hook();
#[cfg(not(target_arch = "wasm32"))]
std::panic::set_hook(Box::new(|info: &std::panic::PanicInfo| {
KCL_RUNTIME_PANIC_RECORD.with(|record| {
let mut record = record.borrow_mut();
Expand Down Expand Up @@ -514,6 +546,7 @@ impl FastRunner {
}
evaluator.run()
});
#[cfg(not(target_arch = "wasm32"))]
std::panic::set_hook(prev_hook);
KCL_RUNTIME_PANIC_RECORD.with(|record| {
let record = record.borrow();
Expand Down
17 changes: 16 additions & 1 deletion kclvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use kclvm_runner::exec_program;
use kclvm_runner::runner::*;
pub use kclvm_runtime::*;
use std::alloc::{alloc, dealloc, Layout};
use std::ffi::c_char;
use std::ffi::c_int;
use std::ffi::{c_char, c_void};
use std::ffi::{CStr, CString};
use std::mem;
use std::process::ExitCode;
Expand Down Expand Up @@ -223,3 +223,18 @@ pub unsafe extern "C" fn kcl_free(ptr: *mut u8, size: usize) {
let layout = Layout::from_size_align_unchecked(size, align);
dealloc(ptr, layout);
}

#[no_mangle]
pub fn kcl_alloc(size: usize) -> *mut c_void {
let mut buf = Vec::with_capacity(size);
let ptr = buf.as_mut_ptr();
mem::forget(buf);
ptr as *mut c_void
}

#[no_mangle]
pub fn kcl_dealloc(ptr: *mut c_void, size: usize) {
unsafe {
let _buf = Vec::from_raw_parts(ptr, 0, size);
}
}

0 comments on commit 6085941

Please sign in to comment.