linera_wasmer_compiler/translator/
module.rs1use super::environ::ModuleEnvironment;
7use super::error::from_binaryreadererror_wasmerror;
8use super::sections::{
9 parse_data_section, parse_element_section, parse_export_section, parse_function_section,
10 parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
11 parse_start_section, parse_table_section, parse_type_section,
12};
13use super::state::ModuleTranslationState;
14use wasmer_types::WasmResult;
15use wasmparser::{NameSectionReader, Parser, Payload};
16
17pub fn translate_module<'data>(
20 data: &'data [u8],
21 environ: &mut ModuleEnvironment<'data>,
22) -> WasmResult<ModuleTranslationState> {
23 let mut module_translation_state = ModuleTranslationState::new();
24
25 for payload in Parser::new(0).parse_all(data) {
26 match payload.map_err(from_binaryreadererror_wasmerror)? {
27 Payload::Version { .. } | Payload::End { .. } => {}
28
29 Payload::TypeSection(types) => {
30 parse_type_section(types, &mut module_translation_state, environ)?;
31 }
32
33 Payload::ImportSection(imports) => {
34 parse_import_section(imports, environ)?;
35 }
36
37 Payload::FunctionSection(functions) => {
38 parse_function_section(functions, environ)?;
39 }
40
41 Payload::TableSection(tables) => {
42 parse_table_section(tables, environ)?;
43 }
44
45 Payload::MemorySection(memories) => {
46 parse_memory_section(memories, environ)?;
47 }
48
49 Payload::GlobalSection(globals) => {
50 parse_global_section(globals, environ)?;
51 }
52
53 Payload::ExportSection(exports) => {
54 parse_export_section(exports, environ)?;
55 }
56
57 Payload::StartSection { func, .. } => {
58 parse_start_section(func, environ)?;
59 }
60
61 Payload::ElementSection(elements) => {
62 parse_element_section(elements, environ)?;
63 }
64
65 Payload::CodeSectionStart { .. } => {}
66 Payload::CodeSectionEntry(code) => {
67 let mut code = code.get_binary_reader();
68 let size = code.bytes_remaining();
69 let offset = code.original_position();
70 environ.define_function_body(
71 &module_translation_state,
72 code.read_bytes(size)
73 .map_err(from_binaryreadererror_wasmerror)?,
74 offset,
75 )?;
76 }
77
78 Payload::DataSection(data) => {
79 parse_data_section(data, environ)?;
80 }
81
82 Payload::DataCountSection { count, .. } => {
83 environ.reserve_passive_data(count)?;
84 }
85
86 Payload::InstanceSection(_)
87 | Payload::ComponentSection { .. }
88 | Payload::CoreTypeSection(_)
89 | Payload::ComponentTypeSection(_)
90 | Payload::ComponentInstanceSection(_)
91 | Payload::ComponentAliasSection(_)
92 | Payload::ComponentCanonicalSection(_)
93 | Payload::ComponentStartSection { .. }
94 | Payload::ComponentImportSection(_)
95 | Payload::ComponentExportSection(_)
96 | Payload::ModuleSection { .. } => {
97 unimplemented!("module linking not implemented. It will only be implemented if/when browsers support it")
98 }
99
100 Payload::TagSection(_) => {
101 unimplemented!("exception handling not implemented yet")
102 }
103
104 Payload::CustomSection(sectionreader) => {
105 let name = sectionreader.name();
107 environ.custom_section(name, sectionreader.data())?;
108 if name == "name" {
109 parse_name_section(
110 NameSectionReader::new(sectionreader.data(), sectionreader.data_offset()),
111 environ,
112 )?;
113 }
114 }
115
116 Payload::UnknownSection { .. } => unreachable!(),
117 }
118 }
119
120 Ok(module_translation_state)
121}