Skip to content

Commit

Permalink
give arrays the same checks as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdotink committed Dec 16, 2023
1 parent ae24835 commit f321514
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions zap/src/irgen/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ impl Gen {
self.local("len");
self.assign("len".into(), from_expr.clone().len());

if self.ser_checks {
self.check_range(&"len".into(), len);
}

self.write_num("len".into(), NumTy::U16);

self.num_for("i".into(), 1.into(), "len".into());
Expand Down Expand Up @@ -325,6 +329,10 @@ impl Gen {
self.local("len");
self.read_num("len".into(), NumTy::U16);

if self.des_checks {
self.check_range(&"len".into(), len);
}

self.num_for("i".into(), 1.into(), "len".into());

self.des(ty, &into.clone().expr_index("i".into()));
Expand Down Expand Up @@ -480,39 +488,10 @@ impl Gen {
Ty::I16(range) => self.check_range(var, range),
Ty::I32(range) => self.check_range(var, range),

// Checks for strings are done inline in the serializer/deserializer
// Checks for strings and arrays are done inline in the serializer/deserializer
// because we need to check len before deserializing the string.
Ty::Str { .. } => {}

Ty::Arr { len, .. } => {
if len.is_exact() {
self.assert(
Expr::from(var.clone()).len().eq(len.min().unwrap().into()),
format!("Array is not exactly {} elements long!", len.min().unwrap()),
);
} else {
if let Some(min) = len.min() {
self.assert(
Expr::from(var.clone()).len().ge(min.into()),
format!("Array is shorter than minimum length of {}!", min),
);
}

if let Some(max) = len.max() {
if len.max_inclusive() {
self.assert(
Expr::from(var.clone()).len().le(max.into()),
format!("Array is longer than maximum length of {}!", max),
);
} else {
self.assert(
Expr::from(var.clone()).len().lt(max.into()),
format!("Array is longer than maximum length of {}!", max),
);
}
}
}
}
Ty::Arr { .. } => {}

Ty::Instance(Some(class)) => {
self.assert(
Expand Down

0 comments on commit f321514

Please sign in to comment.