@@ -9,7 +9,8 @@ use std::mem;
99use std:: ops:: Range ;
1010use wasm_encoder:: { ComponentSection as _, ComponentSectionId , Encode , Section } ;
1111use wasmparser:: {
12- ComponentNameSectionReader , NameSectionReader , Parser , Payload :: * , ProducersSectionReader ,
12+ ComponentNameSectionReader , KnownCustom , NameSectionReader , Parser , Payload :: * ,
13+ ProducersSectionReader ,
1314} ;
1415
1516/// A representation of a WebAssembly producers section.
@@ -50,9 +51,11 @@ impl Producers {
5051 match payload {
5152 ModuleSection { .. } | ComponentSection { .. } => depth += 1 ,
5253 End { .. } => depth -= 1 ,
53- CustomSection ( c) if c. name ( ) == "producers" && depth == 0 => {
54- let producers = Self :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
55- return Ok ( Some ( producers) ) ;
54+ CustomSection ( c) if depth == 0 => {
55+ if let KnownCustom :: Producers ( _) = c. as_known ( ) {
56+ let producers = Self :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
57+ return Ok ( Some ( producers) ) ;
58+ }
5659 }
5760 _ => { }
5861 }
@@ -290,57 +293,60 @@ fn rewrite_wasm(
290293 _ => { }
291294 }
292295
293- // Process the wasm sections:
294- match payload {
295- // Only rewrite the outermost producers section:
296- CustomSection ( c) if c. name ( ) == "producers" && stack. len ( ) == 0 => {
297- producers_found = true ;
298- let mut producers = Producers :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
299- // Add to the section according to the command line flags:
300- producers. merge ( & add_producers) ;
301- // Encode into output:
302- producers. section ( ) . append_to ( & mut output) ;
303- }
304-
305- CustomSection ( c) if c. name ( ) == "name" && stack. len ( ) == 0 => {
306- names_found = true ;
307- let mut names = ModuleNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
308- names. merge ( & ModuleNames :: from_name ( add_name) ) ;
309-
310- names. section ( ) ?. as_custom ( ) . append_to ( & mut output) ;
311- }
312-
313- CustomSection ( c) if c. name ( ) == "component-name" && stack. len ( ) == 0 => {
314- names_found = true ;
315- let mut names = ComponentNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
316- names. merge ( & ComponentNames :: from_name ( add_name) ) ;
317- names. section ( ) ?. as_custom ( ) . append_to ( & mut output) ;
318- }
319-
320- CustomSection ( c) if c. name ( ) == "registry-metadata" && stack. len ( ) == 0 => {
321- // Pass section through if a new registry metadata isn't provided, otherwise ignore and overwrite with new
322- if add_registry_metadata. is_none ( ) {
323- let registry: RegistryMetadata = RegistryMetadata :: from_bytes ( & c. data ( ) , 0 ) ?;
324-
325- let registry_metadata = wasm_encoder:: CustomSection {
326- name : Cow :: Borrowed ( "registry-metadata" ) ,
327- data : Cow :: Owned ( serde_json:: to_vec ( & registry) ?) ,
328- } ;
329- registry_metadata. append_to ( & mut output) ;
330- }
331- }
296+ // Only rewrite the outermost custom sections
297+ if let CustomSection ( c) = & payload {
298+ if stack. len ( ) == 0 {
299+ match c. as_known ( ) {
300+ KnownCustom :: Producers ( _) => {
301+ producers_found = true ;
302+ let mut producers = Producers :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
303+ // Add to the section according to the command line flags:
304+ producers. merge ( & add_producers) ;
305+ // Encode into output:
306+ producers. section ( ) . append_to ( & mut output) ;
307+ continue ;
308+ }
309+ KnownCustom :: Name ( _) => {
310+ names_found = true ;
311+ let mut names = ModuleNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
312+ names. merge ( & ModuleNames :: from_name ( add_name) ) ;
332313
333- // All other sections get passed through unmodified:
334- _ => {
335- if let Some ( ( id, range) ) = payload. as_section ( ) {
336- wasm_encoder:: RawSection {
337- id,
338- data : & input[ range] ,
314+ names. section ( ) ?. as_custom ( ) . append_to ( & mut output) ;
315+ continue ;
316+ }
317+ KnownCustom :: ComponentName ( _) => {
318+ names_found = true ;
319+ let mut names = ComponentNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
320+ names. merge ( & ComponentNames :: from_name ( add_name) ) ;
321+ names. section ( ) ?. as_custom ( ) . append_to ( & mut output) ;
322+ continue ;
339323 }
340- . append_to ( & mut output) ;
324+ KnownCustom :: Unknown if c. name ( ) == "registry-metadata" => {
325+ // Pass section through if a new registry metadata isn't provided, otherwise ignore and overwrite with new
326+ if add_registry_metadata. is_none ( ) {
327+ let registry: RegistryMetadata =
328+ RegistryMetadata :: from_bytes ( & c. data ( ) , 0 ) ?;
329+
330+ let registry_metadata = wasm_encoder:: CustomSection {
331+ name : Cow :: Borrowed ( "registry-metadata" ) ,
332+ data : Cow :: Owned ( serde_json:: to_vec ( & registry) ?) ,
333+ } ;
334+ registry_metadata. append_to ( & mut output) ;
335+ continue ;
336+ }
337+ }
338+ _ => { }
341339 }
342340 }
343341 }
342+ // All other sections get passed through unmodified:
343+ if let Some ( ( id, range) ) = payload. as_section ( ) {
344+ wasm_encoder:: RawSection {
345+ id,
346+ data : & input[ range] ,
347+ }
348+ . append_to ( & mut output) ;
349+ }
344350 }
345351 if !names_found && add_name. is_some ( ) {
346352 if output. starts_with ( & wasm_encoder:: Component :: HEADER ) {
@@ -434,39 +440,42 @@ impl Metadata {
434440 metadata. last_mut ( ) . unwrap ( ) . push_child ( finished) ;
435441 }
436442 }
437- CustomSection ( c) if c. name ( ) == "name" => {
438- let names = ModuleNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
439- if let Some ( name) = names. get_name ( ) {
443+ CustomSection ( c) => match c. as_known ( ) {
444+ KnownCustom :: Name ( _) => {
445+ let names = ModuleNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
446+ if let Some ( name) = names. get_name ( ) {
447+ metadata
448+ . last_mut ( )
449+ . expect ( "non-empty metadata stack" )
450+ . set_name ( & name) ;
451+ }
452+ }
453+ KnownCustom :: ComponentName ( _) => {
454+ let names = ComponentNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
455+ if let Some ( name) = names. get_name ( ) {
456+ metadata
457+ . last_mut ( )
458+ . expect ( "non-empty metadata stack" )
459+ . set_name ( name) ;
460+ }
461+ }
462+ KnownCustom :: Producers ( _) => {
463+ let producers = Producers :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
440464 metadata
441465 . last_mut ( )
442466 . expect ( "non-empty metadata stack" )
443- . set_name ( & name ) ;
467+ . set_producers ( producers ) ;
444468 }
445- }
446- CustomSection ( c) if c. name ( ) == "component-name" => {
447- let names = ComponentNames :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
448- if let Some ( name) = names. get_name ( ) {
469+ KnownCustom :: Unknown if c. name ( ) == "registry-metadata" => {
470+ let registry: RegistryMetadata =
471+ RegistryMetadata :: from_bytes ( & c. data ( ) , 0 ) ?;
449472 metadata
450473 . last_mut ( )
451474 . expect ( "non-empty metadata stack" )
452- . set_name ( name ) ;
475+ . set_registry_metadata ( registry ) ;
453476 }
454- }
455- CustomSection ( c) if c. name ( ) == "producers" => {
456- let producers = Producers :: from_bytes ( c. data ( ) , c. data_offset ( ) ) ?;
457- metadata
458- . last_mut ( )
459- . expect ( "non-empty metadata stack" )
460- . set_producers ( producers) ;
461- }
462- CustomSection ( c) if c. name ( ) == "registry-metadata" => {
463- let registry: RegistryMetadata = RegistryMetadata :: from_bytes ( & c. data ( ) , 0 ) ?;
464- metadata
465- . last_mut ( )
466- . expect ( "non-empty metadata stack" )
467- . set_registry_metadata ( registry) ;
468- }
469-
477+ _ => { }
478+ } ,
470479 _ => { }
471480 }
472481 }
0 commit comments