From 6acd3b94fb69ab8690bca97f97eeef7dcaa8e8b6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 18 Jul 2024 14:19:50 +0800 Subject: [PATCH] chore: add fail reason to assert (#2192) --- cli/src/api/templates/load-wasi-template.ts | 4 +-- crates/napi/src/error.rs | 6 +++-- crates/napi/src/tokio_runtime.rs | 29 ++++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cli/src/api/templates/load-wasi-template.ts b/cli/src/api/templates/load-wasi-template.ts index 0f62305b..ad62b29f 100644 --- a/cli/src/api/templates/load-wasi-template.ts +++ b/cli/src/api/templates/load-wasi-template.ts @@ -23,7 +23,7 @@ const __wasi = new __WASI({ })` const workerFsHandler = fs - ? `worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))\n` + ? ` worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))\n` : '' return `import { @@ -58,7 +58,7 @@ const { const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), { type: 'module', }) - ${workerFsHandler} +${workerFsHandler} return worker }, overwriteImports(importObject) { diff --git a/crates/napi/src/error.rs b/crates/napi/src/error.rs index e48945c9..a9e9e18c 100644 --- a/crates/napi/src/error.rs +++ b/crates/napi/src/error.rs @@ -291,14 +291,16 @@ macro_rules! impl_object_methods { let mut is_pending_exception = false; assert_eq!( unsafe { $crate::sys::napi_is_exception_pending(env, &mut is_pending_exception) }, - $crate::sys::Status::napi_ok + $crate::sys::Status::napi_ok, + "Check exception status failed" ); let js_error = match is_pending_exception { true => { let mut error_result = std::ptr::null_mut(); assert_eq!( unsafe { $crate::sys::napi_get_and_clear_last_exception(env, &mut error_result) }, - $crate::sys::Status::napi_ok + $crate::sys::Status::napi_ok, + "Get and clear last exception failed" ); error_result } diff --git a/crates/napi/src/tokio_runtime.rs b/crates/napi/src/tokio_runtime.rs index 038addbe..b382adfe 100644 --- a/crates/napi/src/tokio_runtime.rs +++ b/crates/napi/src/tokio_runtime.rs @@ -85,14 +85,22 @@ pub fn spawn(fut: F) -> tokio::task::JoinHandle where F: 'static + Send + Future, { - RT.read().unwrap().as_ref().unwrap().spawn(fut) + RT.read() + .unwrap() + .as_ref() + .expect("Tokio runtime is not created") + .spawn(fut) } /// Runs a future to completion /// This is blocking, meaning that it pauses other execution until the future is complete, /// only use it when it is absolutely necessary, in other places use async functions instead. pub fn block_on(fut: F) -> F::Output { - RT.read().unwrap().as_ref().unwrap().block_on(fut) + RT.read() + .unwrap() + .as_ref() + .expect("Tokio runtime is not created") + .block_on(fut) } /// spawn_blocking on the current Tokio runtime. @@ -101,7 +109,11 @@ where F: FnOnce() -> R + Send + 'static, R: Send + 'static, { - RT.read().unwrap().as_ref().unwrap().spawn_blocking(func) + RT.read() + .unwrap() + .as_ref() + .expect("Tokio runtime is not created") + .spawn_blocking(func) } // This function's signature must be kept in sync with the one in lib.rs, otherwise napi @@ -109,10 +121,15 @@ where /// If the feature `tokio_rt` has been enabled this will enter the runtime context and /// then call the provided closure. Otherwise it will just call the provided closure. -#[inline] pub fn within_runtime_if_available T, T>(f: F) -> T { - let _rt_guard = RT.read().unwrap().as_ref().unwrap().enter(); - f() + let rt_lock = RT.read().unwrap(); + let rt_guard = rt_lock + .as_ref() + .expect("Tokio runtime is not created") + .enter(); + let ret = f(); + drop(rt_guard); + ret } struct SendableResolver<