Skip to content

Commit

Permalink
docs(validation): add more for validation stack
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hartung <[email protected]>
  • Loading branch information
florianhartung committed Sep 10, 2024
1 parent 983935a commit 219d933
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/validation/validation_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ impl ValidationStack {

/// This puts an unspecified element on top of the stack.
/// While the top of the stack is unspecified, arbitrary value types can be popped.
/// To remove this, a new label has to be pushed or an existing one has to be popped.
/// To undo this, a new label has to be pushed or an existing one has to be popped.
///
/// See the documentation for [`ValidationStackEntry::UnspecifiedValTypes`] for more info.
pub(super) fn make_unspecified(&mut self) {
// Pop everything until next label or until the stack is empty.
// This is okay, because these values cannot be accessed during execution ever again.
Expand Down Expand Up @@ -89,8 +91,8 @@ impl ValidationStack {

/// Asserts that the values on top of the stack match those of a value iterator
///
/// The last element of the `val_types` [`Iterator`] is compared to the top-most
/// [`ValidationStackEntry`], the second last `val_types` element to the second top-most
/// The last element of `expected_val_types` is compared to the top-most
/// [`ValidationStackEntry`], the second last `expected_val_types` element to the second top-most
/// [`ValidationStackEntry`] etc.
///
/// Any occurence of the [`ValidationStackEntry::Label`] variant in the stack tail will cause an
Expand Down Expand Up @@ -119,6 +121,8 @@ impl ValidationStack {
}
}
ValidationStackEntry::UnspecifiedValTypes => {
// In case we find an `UnspecifiedValTypes`, we pretend that all expected valtypes are found.
// That's because this entry can expand to every possible combination of valtypes.
return Ok(());
}
}
Expand All @@ -127,6 +131,15 @@ impl ValidationStack {
Ok(())
}

/// Asserts that the valtypes on the stack match the expected valtypes.
///
/// This starts by comparing the top-most valtype with the last element from `expected_val_types` and then continues downwards on the stack.
/// If a label is reached and not all `expected_val_types` have been checked, the assertion fails.
///
/// # Returns
///
/// - `Ok(())` if all expected valtypes were found
/// - `Err(_)` otherwise
pub(super) fn assert_val_types(&self, expected_val_types: &[ValType]) -> Result<()> {
let topmost_label_index = self.find_topmost_label_idx();

Expand Down Expand Up @@ -155,7 +168,7 @@ impl ValidationStack {
Ok(())
}

/// Finds the index of the topmost label that is currently on the stack.
/// A helper to find the index of the top-most label in [`ValidationStack::stack`]
fn find_topmost_label_idx(&self) -> Option<usize> {
self.stack
.iter()
Expand All @@ -165,8 +178,13 @@ impl ValidationStack {
.map(|(idx, _entry)| idx)
}

/// Removes all valtypes until the next lower label.
/// If a label is found and popped, its info is returned.
/// Searches for the top-most label, then pops the label and all entry on top of that label.
/// Only the label's [`LabelInfo`] is returned.
///
/// # Returns
///
/// - `Ok(LabelInfo)` if a label has been found and popped
/// - `None` if no label was found on the stack
fn pop_label_and_above(&mut self) -> Option<LabelInfo> {
/// Delete all the values until the topmost label or until the stack is empty
match self.find_topmost_label_idx() {
Expand Down Expand Up @@ -204,12 +222,11 @@ enum ValidationStackEntry {
/// A label
Label(LabelInfo),

/// Special variant to encode that any possible number of ValTypes could be here
/// Special variant to encode that any possible number of [`ValType`]s could be here
///
/// Caused by `return` and `unreachable, as both can push an arbitrary number of values
/// from/to the stack.
/// Caused by `return` and `unreachable`, as both can push an arbitrary number of values to the stack.
///
/// When this variant is popped onto the stack, all valtypes until the next lower label are deleted.
/// When this variant is pushed onto the stack, all valtypes until the next lower label are deleted.
/// They are not needed anymore because this variant can expand to all of them.
UnspecifiedValTypes,
}
Expand Down

0 comments on commit 219d933

Please sign in to comment.