Skip to content

Commit e0c593b

Browse files
committed
fix
1 parent 8e369fc commit e0c593b

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

crates/node_binding/src/plugins/js_loader/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{collections::HashMap, ptr::NonNull, sync::Arc};
22

3+
use derive_more::Debug;
34
use napi::bindgen_prelude::*;
45
use napi_derive::napi;
56
use rspack_core::{LoaderContext, Module, RunnerContext};
@@ -81,33 +82,41 @@ impl From<LoaderState> for JsLoaderState {
8182
}
8283

8384
#[napi(object)]
85+
#[derive(Debug)]
8486
pub struct JsLoaderContext {
87+
#[debug(skip)]
8588
#[napi(ts_type = "Readonly<JsResourceData>")]
8689
pub resource_data: JsResourceData,
8790
/// Will be deprecated. Use module.module_identifier instead
8891
#[napi(js_name = "_moduleIdentifier", ts_type = "Readonly<string>")]
8992
pub module_identifier: String,
9093
#[napi(js_name = "_module", ts_type = "Module")]
94+
#[debug(skip)]
9195
pub module: ModuleObject,
9296
#[napi(ts_type = "Readonly<boolean>")]
9397
pub hot: bool,
9498

9599
/// Content maybe empty in pitching stage
100+
#[debug(skip)]
96101
pub content: Either<Null, Buffer>,
97102
#[napi(ts_type = "any")]
103+
#[debug(skip)]
98104
pub additional_data: Option<ThreadsafeJsValueRef<Unknown<'static>>>,
99105
#[napi(js_name = "__internal__parseMeta")]
100106
pub parse_meta: HashMap<String, String>,
107+
#[debug(skip)]
101108
pub source_map: Option<Buffer>,
102109
pub cacheable: bool,
103110
pub file_dependencies: Vec<String>,
104111
pub context_dependencies: Vec<String>,
105112
pub missing_dependencies: Vec<String>,
106113
pub build_dependencies: Vec<String>,
107114

115+
#[debug(skip)]
108116
pub loader_items: Vec<JsLoaderItem>,
109117
pub loader_index: i32,
110118
#[napi(ts_type = "Readonly<JsLoaderState>")]
119+
#[debug(skip)]
111120
pub loader_state: JsLoaderState,
112121
#[napi(js_name = "__internal__error")]
113122
pub error: Option<JsRspackError>,

crates/node_binding/src/plugins/js_loader/mod.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{RspackResultToNapiResultExt, COMPILER_REFERENCES};
2424
pub type JsLoaderRunner = ThreadsafeFunction<
2525
(
2626
JsLoaderContext,
27-
tokio::sync::oneshot::Sender<napi::Result<JsLoaderContext>>,
27+
tokio::sync::oneshot::Sender<JsLoaderContext>,
2828
),
2929
(),
3030
FnArgs<(JsLoaderContext, napi_value)>,
@@ -33,44 +33,38 @@ pub type JsLoaderRunner = ThreadsafeFunction<
3333
0,
3434
>;
3535

36-
unsafe extern "C" fn raw_done<Value>(
36+
unsafe extern "C" fn raw_done(
3737
env: sys::napi_env,
38-
cbinfo: sys::napi_callback_info,
39-
) -> sys::napi_value
40-
where
41-
Value: FromNapiValue,
42-
{
43-
handle_done_callback::<Value>(env, cbinfo)
44-
.unwrap_or_else(|err| throw_error(env, err, "Error in done"))
38+
cb_info: sys::napi_callback_info,
39+
) -> sys::napi_value {
40+
handle_done_callback(env, cb_info).unwrap_or_else(|err| throw_error(env, err, "Error in done"))
4541
}
4642

4743
#[inline(always)]
48-
fn handle_done_callback<Value>(
44+
fn handle_done_callback(
4945
env: sys::napi_env,
50-
cbinfo: sys::napi_callback_info,
51-
) -> napi::Result<sys::napi_value>
52-
where
53-
Value: FromNapiValue,
54-
{
46+
cb_info: sys::napi_callback_info,
47+
) -> napi::Result<sys::napi_value> {
5548
let mut callback_values = [ptr::null_mut()];
56-
let mut rust_cb = ptr::null_mut();
49+
let mut data = ptr::null_mut();
5750
check_status!(
5851
unsafe {
5952
sys::napi_get_cb_info(
6053
env,
61-
cbinfo,
54+
cb_info,
6255
&mut 1,
6356
callback_values.as_mut_ptr(),
6457
ptr::null_mut(),
65-
&mut rust_cb,
58+
&mut data,
6659
)
6760
},
68-
"Get callback info from finally callback failed"
61+
"Get callback info from loader runner callback failed"
6962
)?;
7063

71-
let tx: Box<tokio::sync::oneshot::Sender<Value>> = unsafe { Box::from_raw(rust_cb.cast()) };
64+
let tx: Box<tokio::sync::oneshot::Sender<JsLoaderContext>> =
65+
unsafe { Box::from_raw(data.cast()) };
7266

73-
let value: Value = unsafe { FromNapiValue::from_napi_value(env, callback_values[0]) }?;
67+
let value = unsafe { FromNapiValue::from_napi_value(env, callback_values[0]) }?;
7468
let _ = tx.send(value);
7569

7670
Ok(ptr::null_mut())
@@ -146,19 +140,21 @@ extern "C" fn napi_js_callback(
146140
.build_callback(
147141
|ctx: ThreadsafeCallContext<(
148142
JsLoaderContext,
149-
tokio::sync::oneshot::Sender<napi::Result<JsLoaderContext>>,
143+
tokio::sync::oneshot::Sender<JsLoaderContext>,
150144
)>| {
151-
let (context, sender): (_, _) = ctx.value;
145+
let context = ctx.value.0;
146+
let sender = ctx.value.1;
152147
let mut done = ptr::null_mut();
153148
const DONE: &[u8; 5] = b"done\0";
149+
let data = Box::into_raw(Box::new(sender)).cast();
154150
check_status!(
155151
unsafe {
156152
sys::napi_create_function(
157153
ctx.env.raw(),
158154
DONE.as_ptr().cast(),
159155
4,
160-
Some(raw_done::<JsLoaderContext>),
161-
Box::into_raw(Box::new(sender)).cast(),
156+
Some(raw_done),
157+
data,
162158
&mut done,
163159
)
164160
},

crates/node_binding/src/plugins/js_loader/scheduler.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ pub(crate) async fn loader_yield(
3535
let read_guard = self.runner.read().await;
3636
match &*read_guard {
3737
Some(runner) => {
38-
let (tx, rx) = tokio::sync::oneshot::channel::<napi::Result<JsLoaderContext>>();
38+
let (tx, rx) = tokio::sync::oneshot::channel::<JsLoaderContext>();
3939
runner
4040
.call_async((loader_context.try_into()?, tx))
4141
.await
4242
.into_diagnostic()?;
43-
let new_cx = rx.await.into_diagnostic()?.into_diagnostic()?;
43+
let new_cx = rx.await.into_diagnostic()?;
4444
drop(read_guard);
4545

4646
merge_loader_context(loader_context, new_cx)?;
@@ -62,15 +62,15 @@ pub(crate) async fn loader_yield(
6262
};
6363

6464
let read_guard = self.runner.read().await;
65-
let (tx, rx) = tokio::sync::oneshot::channel::<napi::Result<JsLoaderContext>>();
65+
let (tx, rx) = tokio::sync::oneshot::channel::<JsLoaderContext>();
6666
#[allow(clippy::unwrap_used)]
6767
read_guard
6868
.as_ref()
6969
.unwrap()
7070
.call_async((loader_context.try_into()?, tx))
7171
.await
7272
.into_diagnostic()?;
73-
let new_cx = rx.await.into_diagnostic()?.into_diagnostic()?;
73+
let new_cx = rx.await.into_diagnostic()?;
7474
drop(read_guard);
7575

7676
merge_loader_context(loader_context, new_cx)?;
@@ -131,7 +131,6 @@ pub(crate) fn merge_loader_context(
131131
} else {
132132
rspack_core::Content::from(Into::<Vec<u8>>::into(c))
133133
};
134-
135134
Some(content)
136135
}
137136
};

packages/rspack/src/loader-runner/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function runLoaders(
256256
context: JsLoaderContext,
257257
callback: (loaderContext: JsLoaderContext) => void
258258
): void {
259-
runLoadersAsync(compiler, context).then(ctx => callback(ctx));
259+
runLoadersAsync(compiler, context).then(callback);
260260
}
261261

262262
async function runLoadersAsync(

0 commit comments

Comments
 (0)