Add table validation.

This commit is contained in:
Sergey Pepyakin
2017-12-01 09:05:33 +03:00
parent e46690d64a
commit 28199fdac8

View File

@ -1,4 +1,4 @@
use elements::{Module, ResizableLimits, MemoryType};
use elements::{Module, ResizableLimits, MemoryType, TableType};
pub struct Error(pub String);
@ -11,6 +11,14 @@ pub fn validate_module(module: &Module) -> Result<(), Error> {
.collect::<Result<_, _>>()?
}
if let Some(table_section) = module.table_section() {
table_section
.entries()
.iter()
.map(TableType::validate)
.collect::<Result<_, _>>()?
}
Ok(())
}
@ -35,6 +43,12 @@ impl MemoryType {
}
}
impl TableType {
fn validate(&self) -> Result<(), Error> {
self.limits().validate()
}
}
#[cfg(test)]
mod tests {
use super::validate_module;
@ -69,7 +83,7 @@ mod tests {
.build();
assert!(validate_module(&m).is_ok());
// mod is always valid without max.
// mem is always valid without max.
let m = module()
.memory()
.with_min(10)
@ -77,4 +91,33 @@ mod tests {
.build();
assert!(validate_module(&m).is_ok());
}
#[test]
fn table_limits() {
// min > max
let m = module()
.table()
.with_min(10)
.with_max(Some(9))
.build()
.build();
assert!(validate_module(&m).is_err());
// min = max
let m = module()
.table()
.with_min(10)
.with_max(Some(10))
.build()
.build();
assert!(validate_module(&m).is_ok());
// table is always valid without max.
let m = module()
.table()
.with_min(10)
.build()
.build();
assert!(validate_module(&m).is_ok());
}
}