@@ -72,21 +72,27 @@ impl Encodable for InstanceType {
72
72
}
73
73
}
74
74
75
- impl Encodable for ( & mut CoreTypeSection , & mut ComponentTypeSection ) {
75
+ struct EncodableEntityType < ' a > {
76
+ start_type_count : u32 ,
77
+ core_types : & ' a mut CoreTypeSection ,
78
+ component_types : & ' a mut ComponentTypeSection ,
79
+ }
80
+
81
+ impl < ' a > Encodable for EncodableEntityType < ' a > {
76
82
fn type_count ( & self ) -> u32 {
77
- self . 1 . len ( )
83
+ self . start_type_count + self . component_types . len ( )
78
84
}
79
85
80
86
fn core_type_count ( & self ) -> u32 {
81
- self . 0 . len ( )
87
+ self . core_types . len ( )
82
88
}
83
89
84
90
fn ty ( & mut self ) -> ComponentTypeEncoder {
85
- self . 1 . ty ( )
91
+ self . component_types . ty ( )
86
92
}
87
93
88
94
fn core_type ( & mut self ) -> CoreTypeEncoder {
89
- self . 0 . ty ( )
95
+ self . core_types . ty ( )
90
96
}
91
97
}
92
98
@@ -132,12 +138,12 @@ impl<'a> TypeEncoder<'a> {
132
138
let mut types = TypeMap :: new ( ) ;
133
139
134
140
for ( name, url, ty) in imports {
135
- let ty = self . encodable_component_entity_type ( & mut encoded, & mut types, ty) ;
141
+ let ty = self . component_entity_type ( & mut encoded, & mut types, ty) ;
136
142
encoded. import ( name, url, ty) ;
137
143
}
138
144
139
145
for ( name, url, ty) in exports {
140
- let ty = self . encodable_component_entity_type ( & mut encoded, & mut types, ty) ;
146
+ let ty = self . component_entity_type ( & mut encoded, & mut types, ty) ;
141
147
encoded. export ( name, url, ty) ;
142
148
}
143
149
@@ -152,7 +158,7 @@ impl<'a> TypeEncoder<'a> {
152
158
let mut types = TypeMap :: new ( ) ;
153
159
154
160
for ( name, url, ty) in exports {
155
- let ty = self . encodable_component_entity_type ( & mut encoded, & mut types, ty) ;
161
+ let ty = self . component_entity_type ( & mut encoded, & mut types, ty) ;
156
162
encoded. export ( name, url, ty) ;
157
163
}
158
164
@@ -180,17 +186,6 @@ impl<'a> TypeEncoder<'a> {
180
186
encoded
181
187
}
182
188
183
- pub fn component_entity_type (
184
- & self ,
185
- core_types : & mut CoreTypeSection ,
186
- component_types : & mut ComponentTypeSection ,
187
- types : & mut TypeMap < ' a > ,
188
- ty : wasmparser:: types:: ComponentEntityType ,
189
- ) -> ComponentTypeRef {
190
- let mut encodable = ( core_types, component_types) ;
191
- self . encodable_component_entity_type ( & mut encodable, types, ty)
192
- }
193
-
194
189
fn entity_type (
195
190
& self ,
196
191
encodable : & mut ModuleType ,
@@ -239,7 +234,7 @@ impl<'a> TypeEncoder<'a> {
239
234
}
240
235
}
241
236
242
- fn encodable_component_entity_type (
237
+ fn component_entity_type (
243
238
& self ,
244
239
encodable : & mut impl Encodable ,
245
240
types : & mut TypeMap < ' a > ,
@@ -988,14 +983,6 @@ impl<'a> ImportMap<'a> {
988
983
}
989
984
}
990
985
991
- #[ derive( Default ) ]
992
- struct ImportEncodingContext < ' a > {
993
- types : TypeMap < ' a > ,
994
- import_section : ComponentImportSection ,
995
- core_type_section : CoreTypeSection ,
996
- type_section : ComponentTypeSection ,
997
- }
998
-
999
986
/// Used to encode a composition graph as a new WebAssembly component.
1000
987
pub ( crate ) struct CompositionGraphEncoder < ' a > {
1001
988
/// The options for the encoding.
@@ -1063,28 +1050,25 @@ impl<'a> CompositionGraphEncoder<'a> {
1063
1050
1064
1051
fn encode_imports ( & mut self , encoded : & mut Component ) -> Result < ( ) > {
1065
1052
let imports = ImportMap :: new ( !self . options . define_components , self . graph ) ?;
1066
- let mut context = ImportEncodingContext :: default ( ) ;
1067
-
1053
+ let mut type_map = TypeMap :: default ( ) ;
1068
1054
for ( name, entry) in imports. 0 {
1069
1055
match entry {
1070
1056
ImportMapEntry :: Component ( component) => {
1071
- self . encode_component_import ( & mut context , name. as_ref ( ) , "" , component) ;
1057
+ self . encode_component_import ( encoded , name. as_ref ( ) , "" , component) ;
1072
1058
}
1073
1059
ImportMapEntry :: Argument ( arg) => {
1074
1060
let index = match arg. kind {
1075
1061
ArgumentImportKind :: Item ( component, ty) => self . encode_item_import (
1076
- & mut context,
1062
+ encoded,
1063
+ & mut type_map,
1077
1064
name. as_ref ( ) ,
1078
1065
arg. url ,
1079
1066
component,
1080
1067
ty,
1081
1068
) ,
1082
- ArgumentImportKind :: Instance ( exports) => self . encode_instance_import (
1083
- & mut context,
1084
- name. as_ref ( ) ,
1085
- arg. url ,
1086
- exports,
1087
- ) ,
1069
+ ArgumentImportKind :: Instance ( exports) => {
1070
+ self . encode_instance_import ( encoded, name. as_ref ( ) , arg. url , exports)
1071
+ }
1088
1072
} ;
1089
1073
1090
1074
self . imported_args
@@ -1093,35 +1077,18 @@ impl<'a> CompositionGraphEncoder<'a> {
1093
1077
}
1094
1078
}
1095
1079
1096
- if !context. core_type_section . is_empty ( ) {
1097
- encoded. section ( & context. core_type_section ) ;
1098
- }
1099
-
1100
- if !context. type_section . is_empty ( ) {
1101
- encoded. section ( & context. type_section ) ;
1102
- }
1103
-
1104
- if !context. import_section . is_empty ( ) {
1105
- encoded. section ( & context. import_section ) ;
1106
- }
1107
-
1108
1080
Ok ( ( ) )
1109
1081
}
1110
1082
1111
1083
fn encode_component_import (
1112
1084
& mut self ,
1113
- context : & mut ImportEncodingContext < ' a > ,
1085
+ encoded : & mut Component ,
1114
1086
name : & str ,
1115
1087
url : & str ,
1116
1088
component : & ' a crate :: graph:: Component ,
1117
1089
) -> u32 {
1118
- let type_index = self . define_component_type ( & mut context. type_section , component) ;
1119
- let index = self . import (
1120
- & mut context. import_section ,
1121
- name,
1122
- url,
1123
- ComponentTypeRef :: Component ( type_index) ,
1124
- ) ;
1090
+ let type_index = self . define_component_type ( encoded, component) ;
1091
+ let index = self . import ( encoded, name, url, ComponentTypeRef :: Component ( type_index) ) ;
1125
1092
1126
1093
assert ! ( self
1127
1094
. encoded_components
@@ -1133,29 +1100,40 @@ impl<'a> CompositionGraphEncoder<'a> {
1133
1100
1134
1101
fn encode_item_import (
1135
1102
& mut self ,
1136
- context : & mut ImportEncodingContext < ' a > ,
1103
+ encoded : & mut Component ,
1104
+ type_map : & mut TypeMap < ' a > ,
1137
1105
name : & str ,
1138
1106
url : & str ,
1139
1107
component : & ' a crate :: graph:: Component ,
1140
1108
ty : ComponentEntityType ,
1141
1109
) -> u32 {
1142
- let prev_type_count = context. type_section . len ( ) ;
1110
+ let mut core_type_section = CoreTypeSection :: new ( ) ;
1111
+ let mut type_section = ComponentTypeSection :: new ( ) ;
1143
1112
1144
1113
let encoder = TypeEncoder :: new ( & component. types ) ;
1145
- let ty = encoder. component_entity_type (
1146
- & mut context. core_type_section ,
1147
- & mut context. type_section ,
1148
- & mut context. types ,
1149
- ty,
1150
- ) ;
1151
1114
1152
- self . types += context. type_section . len ( ) - prev_type_count;
1153
- self . import ( & mut context. import_section , name, url, ty)
1115
+ let mut encodable = EncodableEntityType {
1116
+ start_type_count : self . types ,
1117
+ core_types : & mut core_type_section,
1118
+ component_types : & mut type_section,
1119
+ } ;
1120
+ let ty = encoder. component_entity_type ( & mut encodable, type_map, ty) ;
1121
+
1122
+ if !core_type_section. is_empty ( ) {
1123
+ encoded. section ( & core_type_section) ;
1124
+ }
1125
+
1126
+ if !type_section. is_empty ( ) {
1127
+ encoded. section ( & type_section) ;
1128
+ self . types += type_section. len ( ) ;
1129
+ }
1130
+
1131
+ self . import ( encoded, name, url, ty)
1154
1132
}
1155
1133
1156
1134
fn encode_instance_import (
1157
1135
& mut self ,
1158
- context : & mut ImportEncodingContext < ' a > ,
1136
+ encoded : & mut Component ,
1159
1137
name : & str ,
1160
1138
url : & str ,
1161
1139
exports : IndexMap < & ' a str , ( & ' a str , & ' a crate :: graph:: Component , ComponentEntityType ) > ,
@@ -1164,20 +1142,17 @@ impl<'a> CompositionGraphEncoder<'a> {
1164
1142
let mut types = TypeMap :: new ( ) ;
1165
1143
for ( name, ( url, component, ty) ) in exports {
1166
1144
let encoder = TypeEncoder :: new ( & component. types ) ;
1167
- let ty = encoder. encodable_component_entity_type ( & mut instance_type, & mut types, ty) ;
1145
+ let ty = encoder. component_entity_type ( & mut instance_type, & mut types, ty) ;
1168
1146
instance_type. export ( name, url, ty) ;
1169
1147
}
1170
1148
1171
1149
let index = self . types ;
1172
- context. type_section . instance ( & instance_type) ;
1150
+ let mut type_section = ComponentTypeSection :: new ( ) ;
1151
+ type_section. instance ( & instance_type) ;
1152
+ encoded. section ( & type_section) ;
1173
1153
self . types += 1 ;
1174
1154
1175
- self . import (
1176
- & mut context. import_section ,
1177
- name,
1178
- url,
1179
- ComponentTypeRef :: Instance ( index) ,
1180
- )
1155
+ self . import ( encoded, name, url, ComponentTypeRef :: Instance ( index) )
1181
1156
}
1182
1157
1183
1158
fn encode_instantiations ( & mut self , encoded : & mut Component ) -> Result < ( ) > {
@@ -1306,11 +1281,14 @@ impl<'a> CompositionGraphEncoder<'a> {
1306
1281
1307
1282
fn define_component_type (
1308
1283
& mut self ,
1309
- type_section : & mut ComponentTypeSection ,
1284
+ encoded : & mut Component ,
1310
1285
component : & crate :: graph:: Component ,
1311
1286
) -> u32 {
1312
- let type_index = self . types ;
1287
+ let mut type_section = ComponentTypeSection :: new ( ) ;
1313
1288
type_section. component ( & component. ty ( ) ) ;
1289
+ encoded. section ( & type_section) ;
1290
+
1291
+ let type_index = self . types ;
1314
1292
self . types += 1 ;
1315
1293
type_index
1316
1294
}
@@ -1398,7 +1376,7 @@ impl<'a> CompositionGraphEncoder<'a> {
1398
1376
1399
1377
fn import (
1400
1378
& mut self ,
1401
- import_section : & mut ComponentImportSection ,
1379
+ encoded : & mut Component ,
1402
1380
name : & str ,
1403
1381
url : & str ,
1404
1382
ty : ComponentTypeRef ,
@@ -1414,7 +1392,9 @@ impl<'a> CompositionGraphEncoder<'a> {
1414
1392
1415
1393
log:: debug!( "importing {desc} with `{name}` (encoded index {count}) in composed component" ) ;
1416
1394
1395
+ let mut import_section = ComponentImportSection :: new ( ) ;
1417
1396
import_section. import ( name, url, ty) ;
1397
+ encoded. section ( & import_section) ;
1418
1398
1419
1399
let index = * count;
1420
1400
* count += 1 ;
0 commit comments