Module fill context

This commit is contained in:
Sergey Pepyakin
2017-12-01 16:38:12 +03:00
parent 39f33cc32f
commit 859d985599
3 changed files with 58 additions and 22 deletions

View File

@ -4,8 +4,9 @@ mod module;
mod func;
use std::fmt;
use elements::{Module, ResizableLimits, MemoryType, TableType};
use elements::{Module, ResizableLimits, MemoryType, TableType, GlobalType, External};
use common::stack;
use self::module::ModuleContext;
pub struct Error(String);
@ -22,8 +23,6 @@ impl From<stack::Error> for Error {
}
pub fn validate_module(module: &Module) -> Result<(), Error> {
// TODO: Functions
if let Some(table_section) = module.table_section() {
table_section
.entries()
@ -43,6 +42,43 @@ pub fn validate_module(module: &Module) -> Result<(), Error> {
Ok(())
}
fn prepare_context(module: &Module) -> ModuleContext {
// Copy types from module as is.
let types = module
.type_section()
.map(|ts| ts.types().into_iter().cloned().collect())
.unwrap_or_default();
// Fill elements with imported values.
let mut func_type_indexes = Vec::new();
let mut tables = Vec::new();
let mut memories = Vec::new();
let mut globals = Vec::new();
for import_entry in module
.import_section()
.map(|i| i.entries())
.unwrap_or_default()
{
match import_entry.external() {
&External::Function(idx) => func_type_indexes.push(idx),
&External::Table(ref table) => tables.push(table.clone()),
&External::Memory(ref memory) => memories.push(memory.clone()),
&External::Global(ref global) => globals.push(global.clone()),
}
}
// Concatenate elements with defined in the module.
ModuleContext {
types,
tables,
memories,
globals,
func_type_indexes,
}
}
impl ResizableLimits {
fn validate(&self) -> Result<(), Error> {
if let Some(maximum) = self.maximum() {