Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unaligned bit arrays on the JavaScript target #3946

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
path to a directory containing CA certificates to install Hex packages.
([winstxnhdw](https://github.com/winstxnhdw))

- On the JavaScript target, bit array expressions and patterns no longer need to
be byte aligned, and the `bits` segment type is now supported in patterns.
([Richard Viney](https://github.com/richard-viney))

### Language server

- The language server now offers a code action to convert the first step of a
Expand Down
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
69 changes: 45 additions & 24 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ impl<'module> Generator<'module> {
if segment.type_ == crate::type_::int() {
match (details.size_value, segment.value.as_ref()) {
(Some(size_value), TypedExpr::Int { int_value, .. })
if size_value <= SAFE_INT_SEGMENT_MAX_SIZE.into() =>
if size_value <= SAFE_INT_SEGMENT_MAX_SIZE.into()
&& (&size_value % BigInt::from(8) == BigInt::ZERO) =>
{
let bytes = bit_array_segment_int_value_to_bytes(
int_value.clone(),
Expand Down Expand Up @@ -296,7 +297,28 @@ impl<'module> Generator<'module> {
}

// Bit arrays
[Opt::Bytes { .. } | Opt::Bits { .. }] => Ok(docvec![value, ".buffer"]),
[Opt::Bits { .. }] => Ok(value),

// Bit arrays with explicit size. The explicit size slices the bit array to the
// specified size. A runtime exception is thrown if the size exceeds the number
// of bits in the bit array.
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
TypedExpr::Int { value: size, .. } => {
self.tracker.bit_array_slice_used = true;
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
}

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(),
location: segment.location,
}),
},

// Anything else
_ => Err(Error::Unsupported {
Expand Down Expand Up @@ -349,15 +371,6 @@ impl<'module> Generator<'module> {
_ => None,
};

if let Some(size_value) = size_value.as_ref() {
if *size_value > BigInt::ZERO && size_value % 8 != BigInt::ZERO {
return Err(Error::Unsupported {
feature: "Non byte aligned array".into(),
location: segment.location,
});
}
}

(
size_value,
self.not_in_tail_position(|gen| gen.wrap_expression(size))?,
Expand Down Expand Up @@ -1318,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 @@ -1473,7 +1486,8 @@ fn bit_array<'a>(
if segment.type_ == crate::type_::int() {
match (details.size_value, segment.value.as_ref()) {
(Some(size_value), Constant::Int { int_value, .. })
if size_value <= SAFE_INT_SEGMENT_MAX_SIZE.into() =>
if size_value <= SAFE_INT_SEGMENT_MAX_SIZE.into()
&& (&size_value % BigInt::from(8) == BigInt::ZERO) =>
{
let bytes = bit_array_segment_int_value_to_bytes(
int_value.clone(),
Expand Down Expand Up @@ -1527,8 +1541,24 @@ fn bit_array<'a>(
Ok(docvec!["codepointBits(", value, ")"])
}

// Bit strings
[Opt::Bits { .. }] => Ok(docvec![value, ".buffer"]),
// Bit arrays
[Opt::Bits { .. }] => Ok(value),

// Bit arrays with explicit size. The explicit size slices the bit array to the
// specified size. A runtime exception is thrown if the size exceeds the number
// of bits in the bit array.
[Opt::Bits { .. }, Opt::Size { value: size, .. }]
| [Opt::Size { value: size, .. }, Opt::Bits { .. }] => match &**size {
Constant::Int { value: size, .. } => {
tracker.bit_array_slice_used = true;
Ok(docvec!["bitArraySlice(", value, ", 0, ", size, ")"])
}

_ => Err(Error::Unsupported {
feature: "This bit array segment option".into(),
location: segment.location,
}),
},

// Anything else
_ => Err(Error::Unsupported {
Expand Down Expand Up @@ -1591,15 +1621,6 @@ fn sized_bit_array_segment_details<'a>(
_ => None,
};

if let Some(size_value) = size_value.as_ref() {
if *size_value > BigInt::ZERO && size_value % 8 != BigInt::ZERO {
return Err(Error::Unsupported {
feature: "Non byte aligned array".into(),
location: segment.location,
});
}
}

(size_value, constant_expr_fun(tracker, size)?)
}
_ => {
Expand Down
Loading
Loading