Skip to content

Commit

Permalink
Revert "Fixed js build"
Browse files Browse the repository at this point in the history
This reverts commit 373ee14.
  • Loading branch information
theduke committed Mar 15, 2023
1 parent ee58d7c commit fb7497b
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions lib/api/src/js/module_info_polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use wasmer_types::{
};

use wasmparser::{
self, BinaryReaderError, Export, ExportSectionReader, ExternalKind, FunctionSectionReader,
GlobalSectionReader, GlobalType as WPGlobalType, ImportSectionReader, MemorySectionReader,
MemoryType as WPMemoryType, NameSectionReader, Parser, Payload, TableSectionReader, TypeRef,
TypeSectionReader,
self, BinaryReaderError, Export, ExportSectionReader, ExternalKind, FuncType as WPFunctionType,
FunctionSectionReader, GlobalSectionReader, GlobalType as WPGlobalType, ImportSectionEntryType,
ImportSectionReader, MemorySectionReader, MemoryType as WPMemoryType, NameSectionReader,
Parser, Payload, TableSectionReader, TypeDef, TypeSectionReader,
};

pub type WasmResult<T> = Result<T, String>;
Expand Down Expand Up @@ -283,17 +283,15 @@ pub fn translate_module<'data>(data: &'data [u8]) -> WasmResult<ModuleInfoPolyfi
parse_export_section(exports, &mut module_info)?;
}

Payload::CustomSection(sectionreader) => {
// We still add the custom section data, but also read it as name section reader
let name = sectionreader.name();
if name == "name" {
parse_name_section(
NameSectionReader::new(sectionreader.data(), sectionreader.data_offset())
.map_err(transform_err)?,
&mut module_info,
)?;
}
}
Payload::CustomSection {
name: "name",
data,
data_offset,
..
} => parse_name_section(
NameSectionReader::new(data, data_offset).map_err(transform_err)?,
&mut module_info,
)?,

_ => {}
}
Expand All @@ -303,15 +301,16 @@ pub fn translate_module<'data>(data: &'data [u8]) -> WasmResult<ModuleInfoPolyfi
}

/// Helper function translating wasmparser types to Wasm Type.
pub fn wptype_to_type(ty: wasmparser::ValType) -> WasmResult<Type> {
pub fn wptype_to_type(ty: wasmparser::Type) -> WasmResult<Type> {
match ty {
wasmparser::ValType::I32 => Ok(Type::I32),
wasmparser::ValType::I64 => Ok(Type::I64),
wasmparser::ValType::F32 => Ok(Type::F32),
wasmparser::ValType::F64 => Ok(Type::F64),
wasmparser::ValType::V128 => Ok(Type::V128),
wasmparser::ValType::ExternRef => Ok(Type::ExternRef),
wasmparser::ValType::FuncRef => Ok(Type::FuncRef),
wasmparser::Type::I32 => Ok(Type::I32),
wasmparser::Type::I64 => Ok(Type::I64),
wasmparser::Type::F32 => Ok(Type::F32),
wasmparser::Type::F64 => Ok(Type::F64),
wasmparser::Type::V128 => Ok(Type::V128),
wasmparser::Type::ExternRef => Ok(Type::ExternRef),
wasmparser::Type::FuncRef => Ok(Type::FuncRef),
ty => Err(format!("wptype_to_type: wasmparser type {:?}", ty)),
}
}

Expand All @@ -324,9 +323,7 @@ pub fn parse_type_section(
module_info.reserve_signatures(count)?;

for entry in types {
if let Ok(wasmparser::Type::Func(functype)) = entry {
let params = functype.params();
let returns = functype.results();
if let Ok(TypeDef::Func(WPFunctionType { params, returns })) = entry {
let sig_params: Vec<Type> = params
.iter()
.map(|ty| {
Expand Down Expand Up @@ -361,20 +358,23 @@ pub fn parse_import_section<'data>(
for entry in imports {
let import = entry.map_err(transform_err)?;
let module_name = import.module;
let field_name = import.name;
let field_name = import.field;

match import.ty {
TypeRef::Func(sig) => {
ImportSectionEntryType::Function(sig) => {
module_info.declare_func_import(
SignatureIndex::from_u32(sig),
module_name,
field_name,
field_name.unwrap_or_default(),
)?;
}
TypeRef::Tag(_) => {
ImportSectionEntryType::Module(_) | ImportSectionEntryType::Instance(_) => {
unimplemented!("module linking not implemented yet")
}
ImportSectionEntryType::Tag(_) => {
unimplemented!("exception handling not implemented yet")
}
TypeRef::Memory(WPMemoryType {
ImportSectionEntryType::Memory(WPMemoryType {
shared,
memory64,
initial,
Expand All @@ -390,28 +390,28 @@ pub fn parse_import_section<'data>(
shared,
},
module_name,
field_name,
field_name.unwrap_or_default(),
)?;
}
TypeRef::Global(ref ty) => {
ImportSectionEntryType::Global(ref ty) => {
module_info.declare_global_import(
GlobalType {
ty: wptype_to_type(ty.content_type).unwrap(),
mutability: ty.mutable.into(),
},
module_name,
field_name,
field_name.unwrap_or_default(),
)?;
}
TypeRef::Table(ref tab) => {
ImportSectionEntryType::Table(ref tab) => {
module_info.declare_table_import(
TableType {
ty: wptype_to_type(tab.element_type).unwrap(),
minimum: tab.initial,
maximum: tab.maximum,
},
module_name,
field_name,
field_name.unwrap_or_default(),
)?;
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ pub fn parse_export_section<'data>(

for entry in exports {
let Export {
name,
field,
ref kind,
index,
} = entry.map_err(transform_err)?;
Expand All @@ -522,17 +522,20 @@ pub fn parse_export_section<'data>(
// becomes a concern here.
let index = index as usize;
match *kind {
ExternalKind::Func => {
module_info.declare_func_export(FunctionIndex::new(index), name)?
ExternalKind::Function => {
module_info.declare_func_export(FunctionIndex::new(index), field)?
}
ExternalKind::Table => {
module_info.declare_table_export(TableIndex::new(index), name)?
module_info.declare_table_export(TableIndex::new(index), field)?
}
ExternalKind::Memory => {
module_info.declare_memory_export(MemoryIndex::new(index), name)?
module_info.declare_memory_export(MemoryIndex::new(index), field)?
}
ExternalKind::Global => {
module_info.declare_global_export(GlobalIndex::new(index), name)?
module_info.declare_global_export(GlobalIndex::new(index), field)?
}
ExternalKind::Type | ExternalKind::Module | ExternalKind::Instance => {
unimplemented!("module linking not implemented yet")
}
ExternalKind::Tag => {
unimplemented!("exception handling not implemented yet")
Expand All @@ -556,20 +559,20 @@ pub fn parse_name_section<'data>(
while let Ok(subsection) = names.read() {
match subsection {
wasmparser::Name::Function(_function_subsection) => {
//for naming in function_subsection.into_iter().flatten() {
// if naming.index != std::u32::MAX {
// environ.declare_function_name(
// FunctionIndex::from_u32(naming.index),
// naming.name,
// )?;
// }
//}
// if let Some(function_names) = function_subsection
// .get_map()
// .ok()
// .and_then(parse_function_name_subsection)
// {
// for (index, name) in function_names {
// module_info.declare_function_name(index, name)?;
// }
// }
}
wasmparser::Name::Module {
name,
name_range: _,
} => {
module_info.declare_module_name(name)?;
wasmparser::Name::Module(module) => {
if let Ok(name) = module.get_name() {
module_info.declare_module_name(name)?;
}
}
wasmparser::Name::Local(_) => {}
wasmparser::Name::Label(_)
Expand Down

0 comments on commit fb7497b

Please sign in to comment.