Skip to content

Commit

Permalink
Make removal of JS bit array functions possible when they're not used
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Feb 11, 2025
1 parent 8b5c1f7 commit ad8f1c0
Show file tree
Hide file tree
Showing 34 changed files with 362 additions and 283 deletions.
17 changes: 16 additions & 1 deletion compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,19 @@ impl<'a> Generator<'a> {

if self.tracker.bit_array_literal_used {
self.register_prelude_usage(&mut imports, "toBitArray", None);
};
}

if self.tracker.bit_array_slice_used {
self.register_prelude_usage(&mut imports, "bitArraySlice", None);
}

if self.tracker.bit_array_slice_to_float_used {
self.register_prelude_usage(&mut imports, "bitArraySliceToFloat", None);
}

if self.tracker.bit_array_slice_to_int_used {
self.register_prelude_usage(&mut imports, "bitArraySliceToInt", None);
}

if self.tracker.sized_integer_segment_used {
self.register_prelude_usage(&mut imports, "sizedInt", None);
Expand Down Expand Up @@ -786,6 +798,9 @@ pub(crate) struct UsageTracker {
pub float_division_used: bool,
pub object_equality_used: bool,
pub bit_array_literal_used: bool,
pub bit_array_slice_used: bool,
pub bit_array_slice_to_float_used: bool,
pub bit_array_slice_to_int_used: bool,
pub sized_integer_segment_used: bool,
pub string_bit_array_segment_used: bool,
pub codepoint_bit_array_segment_used: bool,
Expand Down
13 changes: 9 additions & 4 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,14 @@ impl<'module> Generator<'module> {
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
TypedExpr::Int { value: size, .. } => {
Ok(docvec![value, ".slice(0, ", size, ")"])
self.tracker.bit_array_slice_used = true;
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
}

TypedExpr::Var { name, .. } => Ok(docvec![value, ".slice(0, ", name, ")"]),
TypedExpr::Var { name, .. } => {
self.tracker.bit_array_slice_used = true;
Ok(docvec!["bitArraySlice(", value, ", 0, ", name, ")"])
}

_ => Err(Error::Unsupported {
feature: "This bit array segment option".into(),
Expand Down Expand Up @@ -1327,7 +1331,7 @@ pub(crate) fn guard_constant_expression<'a>(
Constant::Var { name, .. } => Ok(assignments
.iter()
.find(|assignment| assignment.name == name)
.map(|assignment| assignment.subject.clone().append(assignment.path.clone()))
.map(|assignment| assignment.subject.clone())
.unwrap_or_else(|| maybe_escape_identifier_doc(name))),

expression => constant_expression(Context::Function, tracker, expression),
Expand Down Expand Up @@ -1546,7 +1550,8 @@ fn bit_array<'a>(
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
Constant::Int { value: size, .. } => {
Ok(docvec![value, ".slice(0, ", size, ")"])
tracker.bit_array_slice_used = true;
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
}

_ => Err(Error::Unsupported {
Expand Down
Loading

0 comments on commit ad8f1c0

Please sign in to comment.