2
2
//! to deal with each part of it.
3
3
use crate :: environ:: { ModuleEnvironment , WasmResult } ;
4
4
use crate :: sections_translator:: {
5
- parse_code_section , parse_data_section, parse_element_section, parse_export_section,
6
- parse_function_section , parse_global_section, parse_import_section, parse_memory_section,
7
- parse_name_section , parse_start_section, parse_table_section, parse_type_section,
5
+ parse_data_section, parse_element_section, parse_export_section, parse_function_section ,
6
+ parse_global_section, parse_import_section, parse_memory_section, parse_name_section ,
7
+ parse_start_section, parse_table_section, parse_type_section,
8
8
} ;
9
9
use crate :: state:: ModuleTranslationState ;
10
10
use cranelift_codegen:: timing;
11
- use wasmparser:: { CustomSectionContent , ModuleReader , SectionContent } ;
11
+ use wasmparser:: { NameSectionReader , Parser , Payload } ;
12
12
13
13
/// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cranelift IR
14
14
/// [`Function`](cranelift_codegen::ir::Function).
@@ -17,80 +17,85 @@ pub fn translate_module<'data>(
17
17
environ : & mut dyn ModuleEnvironment < ' data > ,
18
18
) -> WasmResult < ModuleTranslationState > {
19
19
let _tt = timing:: wasm_translate_module ( ) ;
20
- let mut reader = ModuleReader :: new ( data) ?;
21
20
let mut module_translation_state = ModuleTranslationState :: new ( ) ;
22
21
23
- while !reader. eof ( ) {
24
- let section = reader. read ( ) ?;
25
- match section. content ( ) ? {
26
- SectionContent :: Type ( types) => {
22
+ for payload in Parser :: new ( 0 ) . parse_all ( data) {
23
+ match payload? {
24
+ Payload :: Version { .. } | Payload :: End => { }
25
+
26
+ Payload :: TypeSection ( types) => {
27
27
parse_type_section ( types, & mut module_translation_state, environ) ?;
28
28
}
29
29
30
- SectionContent :: Import ( imports) => {
30
+ Payload :: ImportSection ( imports) => {
31
31
parse_import_section ( imports, environ) ?;
32
32
}
33
33
34
- SectionContent :: Function ( functions) => {
34
+ Payload :: FunctionSection ( functions) => {
35
35
parse_function_section ( functions, environ) ?;
36
36
}
37
37
38
- SectionContent :: Table ( tables) => {
38
+ Payload :: TableSection ( tables) => {
39
39
parse_table_section ( tables, environ) ?;
40
40
}
41
41
42
- SectionContent :: Memory ( memories) => {
42
+ Payload :: MemorySection ( memories) => {
43
43
parse_memory_section ( memories, environ) ?;
44
44
}
45
45
46
- SectionContent :: Global ( globals) => {
46
+ Payload :: GlobalSection ( globals) => {
47
47
parse_global_section ( globals, environ) ?;
48
48
}
49
49
50
- SectionContent :: Export ( exports) => {
50
+ Payload :: ExportSection ( exports) => {
51
51
parse_export_section ( exports, environ) ?;
52
52
}
53
53
54
- SectionContent :: Start ( start ) => {
55
- parse_start_section ( start , environ) ?;
54
+ Payload :: StartSection { func , .. } => {
55
+ parse_start_section ( func , environ) ?;
56
56
}
57
57
58
- SectionContent :: Element ( elements) => {
58
+ Payload :: ElementSection ( elements) => {
59
59
parse_element_section ( elements, environ) ?;
60
60
}
61
61
62
- SectionContent :: Code ( code) => {
63
- parse_code_section ( code, & module_translation_state, environ) ?;
62
+ Payload :: CodeSectionStart { .. } => { }
63
+ Payload :: CodeSectionEntry ( code) => {
64
+ let mut code = code. get_binary_reader ( ) ;
65
+ let size = code. bytes_remaining ( ) ;
66
+ let offset = code. original_position ( ) ;
67
+ environ. define_function_body (
68
+ & module_translation_state,
69
+ code. read_bytes ( size) ?,
70
+ offset,
71
+ ) ?;
64
72
}
65
73
66
- SectionContent :: Data ( data) => {
74
+ Payload :: DataSection ( data) => {
67
75
parse_data_section ( data, environ) ?;
68
76
}
69
77
70
- SectionContent :: DataCount ( count) => {
78
+ Payload :: DataCountSection { count, .. } => {
71
79
environ. reserve_passive_data ( count) ?;
72
80
}
73
81
74
- SectionContent :: Module ( _)
75
- | SectionContent :: ModuleCode ( _)
76
- | SectionContent :: Instance ( _)
77
- | SectionContent :: Alias ( _) => unimplemented ! ( "module linking not implemented yet" ) ,
78
-
79
- SectionContent :: Custom {
80
- name,
81
- binary,
82
- content,
83
- } => match content {
84
- Some ( CustomSectionContent :: Name ( names) ) => {
85
- parse_name_section ( names, environ) ?;
86
- }
87
- _ => {
88
- let mut reader = binary. clone ( ) ;
89
- let len = reader. bytes_remaining ( ) ;
90
- let payload = reader. read_bytes ( len) ?;
91
- environ. custom_section ( name, payload) ?;
92
- }
93
- } ,
82
+ Payload :: ModuleSection ( _)
83
+ | Payload :: InstanceSection ( _)
84
+ | Payload :: AliasSection ( _)
85
+ | Payload :: ModuleCodeSectionStart { .. }
86
+ | Payload :: ModuleCodeSectionEntry { .. } => {
87
+ unimplemented ! ( "module linking not implemented yet" )
88
+ }
89
+
90
+ Payload :: CustomSection {
91
+ name : "name" ,
92
+ data,
93
+ data_offset,
94
+ } => parse_name_section ( NameSectionReader :: new ( data, data_offset) ?, environ) ?,
95
+
96
+ Payload :: CustomSection { name, data, .. } => environ. custom_section ( name, data) ?,
97
+
98
+ Payload :: UnknownSection { .. } => unreachable ! ( ) ,
94
99
}
95
100
}
96
101
0 commit comments