Skip to content

Commit

Permalink
refactor: rename getters macros name
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jul 16, 2024
1 parent caf9b5b commit ec84411
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 134 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions crates/rspack_binding_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
futures = { workspace = true }
heck = { workspace = true }
napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] }
napi-derive = { workspace = true }
once_cell = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_macros = { path = "../rspack_macros" }
rspack_error = { path = "../rspack_error" }
rspack_napi = { path = "../rspack_napi" }
rspack_regex = { path = "../rspack_regex" }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
dashmap = { workspace = true }
futures = { workspace = true }
heck = { workspace = true }
napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] }
napi-derive = { workspace = true }
once_cell = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_macros = { path = "../rspack_macros" }
rspack_napi = { path = "../rspack_napi" }
rspack_regex = { path = "../rspack_regex" }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
213 changes: 97 additions & 116 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::collections::HashMap;

use dashmap::DashMap;
use napi::bindgen_prelude::Reference;
use napi::Env;
use napi_derive::napi;
use rspack_core::{ModuleIdentifier, Stats, StatsChunk, StatsModule, StatsUsedExports};
use rspack_macros::generate_getters;
use rspack_core::{Stats, StatsChunk, StatsModule, StatsUsedExports};
use rspack_macros::getters;
use rspack_napi::napi::bindgen_prelude::Buffer;
use rspack_napi::napi::{
bindgen_prelude::{Result, SharedReference},
Expand Down Expand Up @@ -327,7 +326,7 @@ type JsStatsModuleSource = Either<String, Buffer>;
type JsStatsUsedExports = Either<String, Vec<String>>;

#[napi]
#[generate_getters]
#[getters]
pub struct JsStatsModule {
r#type: String,
module_type: String,
Expand Down Expand Up @@ -438,7 +437,7 @@ impl From<rspack_core::StatsModuleIssuer> for JsStatsModuleIssuer {
}

#[derive(Clone)]
#[napi(object)]
#[napi]
pub struct JsStatsModuleReason {
pub module_identifier: Option<String>,
pub module_name: Option<String>,
Expand Down Expand Up @@ -471,14 +470,14 @@ pub struct JsOriginRecord {
}

#[derive(Clone)]
#[napi(object)]
#[napi]
pub struct JsStatsSize {
pub source_type: String,
pub size: f64,
}

#[napi]
#[generate_getters]
#[getters]
pub struct JsStatsChunk {
r#type: String,
files: Vec<String>,
Expand Down Expand Up @@ -605,22 +604,18 @@ impl From<rspack_core::StatsAssetsByChunkName> for JsStatsAssetsByChunkName {
#[napi]
pub struct JsStats {
inner: SharedReference<JsCompilation, Stats<'static>>,
module_references: DashMap<ModuleIdentifier, Reference<JsStatsModule>>,
}

impl JsStats {
pub fn new(inner: SharedReference<JsCompilation, Stats<'static>>) -> Self {
Self {
inner,
module_references: DashMap::default(),
}
Self { inner }
}
}

#[napi(object)]
#[napi]
pub struct JsStatsGetAssets {
pub assets: Vec<JsStatsAsset>,
pub assets_by_chunk_name: Vec<JsStatsAssetsByChunkName>,
assets: Vec<JsStatsAsset>,
assets_by_chunk_name: Vec<JsStatsAssetsByChunkName>,
}

impl JsStats {
Expand All @@ -629,109 +624,95 @@ impl JsStats {
env: Env,
stats_module: StatsModule,
) -> Result<Reference<JsStatsModule>> {
let mut reference = match self.module_references.entry(stats_module.identifier) {
dashmap::mapref::entry::Entry::Occupied(entry) => entry.get().clone(env),
dashmap::mapref::entry::Entry::Vacant(entry) => {
let source = stats_module
.source
.map(|source| {
source.to_js_compat_source().map(|js_compat_source| {
if js_compat_source.is_raw && js_compat_source.is_buffer {
JsStatsModuleSource::B(js_compat_source.source)
} else {
let s = String::from_utf8_lossy(js_compat_source.source.as_ref()).to_string();
JsStatsModuleSource::A(s)
}
})
})
.transpose()
.map_err(|e| napi::Error::from_reason(e.to_string()))?;

let mut sizes = stats_module
.sizes
.into_iter()
.map(|s| JsStatsSize {
source_type: s.source_type.to_string(),
size: s.size,
})
.collect::<Vec<_>>();
sizes.sort_by(|a, b| a.source_type.cmp(&b.source_type));

let module = JsStatsModule {
r#type: stats_module.r#type.to_string(),
name: stats_module.name,
size: stats_module.size,
sizes,
depth: stats_module.depth.map(|d| d as u32),
chunks: stats_module.chunks,
module_type: stats_module.module_type.as_str().to_string(),
identifier: stats_module.identifier.to_string(),
id: stats_module.id,
dependent: stats_module.dependent,
issuer: stats_module.issuer,
issuer_name: stats_module.issuer_name,
issuer_id: stats_module.issuer_id,
name_for_condition: stats_module.name_for_condition,
issuer_path: stats_module
.issuer_path
.into_iter()
.map(Into::into)
.collect(),
reasons: stats_module
.reasons
.map(|i| i.into_iter().map(Into::into).collect()),
assets: stats_module.assets,
source,
profile: stats_module.profile.map(|p| p.into()),
orphan: stats_module.orphan,
provided_exports: stats_module.provided_exports,
used_exports: stats_module
.used_exports
.map(|used_exports| match used_exports {
StatsUsedExports::Bool(b) => JsStatsUsedExports::A(b.to_string()),
StatsUsedExports::Vec(v) => JsStatsUsedExports::B(v),
StatsUsedExports::Null => JsStatsUsedExports::A("null".to_string()),
}),
optimization_bailout: Some(stats_module.optimization_bailout),
modules: None,
pre_order_index: stats_module.pre_order_index,
post_order_index: stats_module.post_order_index,
built: stats_module.built,
code_generated: stats_module.code_generated,
build_time_executed: stats_module.build_time_executed,
cached: stats_module.cached,
cacheable: stats_module.cacheable,
optional: stats_module.optional,
failed: stats_module.failed,
errors: stats_module.errors,
warnings: stats_module.warnings,
};

let reference = JsStatsModule::into_reference(module, env)?;
entry.insert(reference.clone(env)?);
Ok(reference)
}
};
let source = stats_module
.source
.map(|source| {
source.to_js_compat_source().map(|js_compat_source| {
if js_compat_source.is_raw && js_compat_source.is_buffer {
JsStatsModuleSource::B(js_compat_source.source)
} else {
let s = String::from_utf8_lossy(js_compat_source.source.as_ref()).to_string();
JsStatsModuleSource::A(s)
}
})
})
.transpose()
.map_err(|e| napi::Error::from_reason(e.to_string()))?;

if let Ok(r) = &mut reference {
if stats_module.modules.is_some() && r.modules.is_none() {
let modules: Option<Vec<Reference<JsStatsModule>>> = stats_module
.modules
.map(|modules| -> Result<_> {
let mut res = Vec::with_capacity(modules.len());
for module in modules {
let reference = self.convert_to_js_stats_module_reference(env, module)?;
res.push(reference);
}
Ok(res)
})
.transpose()
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
r.modules = modules;
}
}
let mut sizes = stats_module
.sizes
.into_iter()
.map(|s| JsStatsSize {
source_type: s.source_type.to_string(),
size: s.size,
})
.collect::<Vec<_>>();
sizes.sort_by(|a, b| a.source_type.cmp(&b.source_type));

let modules: Option<Vec<Reference<JsStatsModule>>> = stats_module
.modules
.map(|modules| -> Result<_> {
let mut res = Vec::with_capacity(modules.len());
for module in modules {
let reference = self.convert_to_js_stats_module_reference(env, module)?;
res.push(reference);
}
Ok(res)
})
.transpose()
.map_err(|e| napi::Error::from_reason(e.to_string()))?;

let module = JsStatsModule {
r#type: stats_module.r#type.to_string(),
name: stats_module.name,
size: stats_module.size,
sizes,
depth: stats_module.depth.map(|d| d as u32),
chunks: stats_module.chunks,
module_type: stats_module.module_type.as_str().to_string(),
identifier: stats_module.identifier.to_string(),
id: stats_module.id,
dependent: stats_module.dependent,
issuer: stats_module.issuer,
issuer_name: stats_module.issuer_name,
issuer_id: stats_module.issuer_id,
name_for_condition: stats_module.name_for_condition,
issuer_path: stats_module
.issuer_path
.into_iter()
.map(Into::into)
.collect(),
reasons: stats_module
.reasons
.map(|i| i.into_iter().map(Into::into).collect()),
assets: stats_module.assets,
source,
profile: stats_module.profile.map(|p| p.into()),
orphan: stats_module.orphan,
provided_exports: stats_module.provided_exports,
used_exports: stats_module
.used_exports
.map(|used_exports| match used_exports {
StatsUsedExports::Bool(b) => JsStatsUsedExports::A(b.to_string()),
StatsUsedExports::Vec(v) => JsStatsUsedExports::B(v),
StatsUsedExports::Null => JsStatsUsedExports::A("null".to_string()),
}),
optimization_bailout: Some(stats_module.optimization_bailout),
modules,
pre_order_index: stats_module.pre_order_index,
post_order_index: stats_module.post_order_index,
built: stats_module.built,
code_generated: stats_module.code_generated,
build_time_executed: stats_module.build_time_executed,
cached: stats_module.cached,
cacheable: stats_module.cacheable,
optional: stats_module.optional,
failed: stats_module.failed,
errors: stats_module.errors,
warnings: stats_module.warnings,
};

reference
JsStatsModule::into_reference(module, env)
}

fn convert_to_js_stats_chunk(&self, env: Env, stats: StatsChunk) -> Result<JsStatsChunk> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ pub fn merge_from_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStr
}

#[proc_macro_attribute]
pub fn generate_getters(
pub fn getters(
args: proc_macro::TokenStream,
tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
napi::generate_getters(args, tokens)
napi::getters(args, tokens)
}
2 changes: 1 addition & 1 deletion crates/rspack_macros/src/napi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn rm_raw_prefix(s: String) -> String {
}
}

pub fn generate_getters(
pub fn getters(
_args: proc_macro::TokenStream,
tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
Expand Down

0 comments on commit ec84411

Please sign in to comment.