chore: add fail reason to assert (#2192)

This commit is contained in:
LongYinan 2024-07-18 14:19:50 +08:00 committed by GitHub
parent ba345be957
commit 6acd3b94fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 10 deletions

View File

@ -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) {

View File

@ -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
}

View File

@ -85,14 +85,22 @@ pub fn spawn<F>(fut: F) -> tokio::task::JoinHandle<F::Output>
where
F: 'static + Send + Future<Output = ()>,
{
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<F: Future>(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<F: FnOnce() -> 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<