Skip to content

Commit 255e24e

Browse files
committed
Make removal of JS bit array functions possible when they're not used
1 parent e60bc99 commit 255e24e

File tree

34 files changed

+362
-283
lines changed

34 files changed

+362
-283
lines changed

compiler-core/src/javascript.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,19 @@ impl<'a> Generator<'a> {
154154

155155
if self.tracker.bit_array_literal_used {
156156
self.register_prelude_usage(&mut imports, "toBitArray", None);
157-
};
157+
}
158+
159+
if self.tracker.bit_array_slice_used {
160+
self.register_prelude_usage(&mut imports, "bitArraySlice", None);
161+
}
162+
163+
if self.tracker.bit_array_slice_to_float_used {
164+
self.register_prelude_usage(&mut imports, "bitArraySliceToFloat", None);
165+
}
166+
167+
if self.tracker.bit_array_slice_to_int_used {
168+
self.register_prelude_usage(&mut imports, "bitArraySliceToInt", None);
169+
}
158170

159171
if self.tracker.sized_integer_segment_used {
160172
self.register_prelude_usage(&mut imports, "sizedInt", None);
@@ -786,6 +798,9 @@ pub(crate) struct UsageTracker {
786798
pub float_division_used: bool,
787799
pub object_equality_used: bool,
788800
pub bit_array_literal_used: bool,
801+
pub bit_array_slice_used: bool,
802+
pub bit_array_slice_to_float_used: bool,
803+
pub bit_array_slice_to_int_used: bool,
789804
pub sized_integer_segment_used: bool,
790805
pub string_bit_array_segment_used: bool,
791806
pub codepoint_bit_array_segment_used: bool,

compiler-core/src/javascript/expression.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,14 @@ impl<'module> Generator<'module> {
304304
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
305305
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
306306
TypedExpr::Int { value: size, .. } => {
307-
Ok(docvec![value, ".slice(0, ", size, ")"])
307+
self.tracker.bit_array_slice_used = true;
308+
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
308309
}
309310

310-
TypedExpr::Var { name, .. } => Ok(docvec![value, ".slice(0, ", name, ")"]),
311+
TypedExpr::Var { name, .. } => {
312+
self.tracker.bit_array_slice_used = true;
313+
Ok(docvec!["bitArraySlice(", value, ", 0, ", name, ")"])
314+
}
311315

312316
_ => Err(Error::Unsupported {
313317
feature: "This bit array segment option".into(),
@@ -1321,7 +1325,7 @@ pub(crate) fn guard_constant_expression<'a>(
13211325
Constant::Var { name, .. } => Ok(assignments
13221326
.iter()
13231327
.find(|assignment| assignment.name == name)
1324-
.map(|assignment| assignment.subject.clone().append(assignment.path.clone()))
1328+
.map(|assignment| assignment.subject.clone())
13251329
.unwrap_or_else(|| maybe_escape_identifier_doc(name))),
13261330

13271331
expression => constant_expression(Context::Function, tracker, expression),
@@ -1540,7 +1544,8 @@ fn bit_array<'a>(
15401544
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
15411545
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
15421546
Constant::Int { value: size, .. } => {
1543-
Ok(docvec![value, ".slice(0, ", size, ")"])
1547+
tracker.bit_array_slice_used = true;
1548+
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
15441549
}
15451550

15461551
_ => Err(Error::Unsupported {

0 commit comments

Comments
 (0)