allow module environment to parse name section

This commit is contained in:
data-pup
2019-06-28 19:29:53 +00:00
committed by Benjamin Bouvier
parent 3293ca6b69
commit ac2ca6116b
5 changed files with 119 additions and 16 deletions

View File

@@ -12,7 +12,7 @@ use crate::translation_utils::{
tabletype_to_type, type_to_type, FuncIndex, Global, GlobalIndex, GlobalInit, Memory,
MemoryIndex, SignatureIndex, Table, TableElementType, TableIndex,
};
use crate::wasm_unsupported;
use crate::{wasm_unsupported, HashMap};
use core::convert::TryFrom;
use cranelift_codegen::ir::{self, AbiParam, Signature};
use cranelift_entity::EntityRef;
@@ -21,8 +21,8 @@ use wasmparser::{
self, CodeSectionReader, Data, DataKind, DataSectionReader, Element, ElementKind,
ElementSectionReader, Export, ExportSectionReader, ExternalKind, FuncType,
FunctionSectionReader, GlobalSectionReader, GlobalType, ImportSectionEntryType,
ImportSectionReader, MemorySectionReader, MemoryType, Operator, TableSectionReader,
TypeSectionReader,
ImportSectionReader, MemorySectionReader, MemoryType, NameSectionReader, Naming, NamingReader,
Operator, TableSectionReader, TypeSectionReader,
};
/// Parses the Type section of the wasm module.
@@ -351,3 +351,47 @@ pub fn parse_data_section<'data>(
Ok(())
}
/// Parses the Name section of the wasm module.
pub fn parse_name_section<'data>(
mut names: NameSectionReader<'data>,
environ: &mut dyn ModuleEnvironment<'data>,
) -> WasmResult<()> {
while let Ok(subsection) = names.read() {
match subsection {
wasmparser::Name::Function(function_subsection) => {
if let Some(function_names) = function_subsection
.get_map()
.ok()
.and_then(parse_function_name_subsection)
{
for (index, name) in function_names {
environ.declare_func_name(index, name)?;
}
}
return Ok(());
}
wasmparser::Name::Local(_) | wasmparser::Name::Module(_) => {}
};
}
Ok(())
}
fn parse_function_name_subsection<'data>(
mut naming_reader: NamingReader<'data>,
) -> Option<HashMap<FuncIndex, &str>> {
let mut function_names = HashMap::new();
for _ in 0..naming_reader.get_count() {
let Naming { index, name } = naming_reader.read().ok()?;
if function_names
.insert(FuncIndex::from_u32(index), name)
.is_some()
{
// If the function index has been previously seen, then we
// break out of the loop and early return `None`, because these
// should be unique.
return None;
}
}
return Some(function_names);
}