Helper method which is the equivalent of [`Wizer::rewrite`], but for components. This effectively plumbs through all non-module sections as-is and updates module sections with whatever [`Wizer::rewrite`] returns.
(
&self,
component: &mut ComponentContext<'_>,
snapshot: &ComponentSnapshot,
)
| 11 | /// This effectively plumbs through all non-module sections as-is and |
| 12 | /// updates module sections with whatever [`Wizer::rewrite`] returns. |
| 13 | pub(crate) fn rewrite_component( |
| 14 | &self, |
| 15 | component: &mut ComponentContext<'_>, |
| 16 | snapshot: &ComponentSnapshot, |
| 17 | ) -> Vec<u8> { |
| 18 | let mut encoder = wasm_encoder::Component::new(); |
| 19 | let mut reencoder = Reencoder { |
| 20 | funcs: 0, |
| 21 | removed_func: None, |
| 22 | wizer: self, |
| 23 | }; |
| 24 | |
| 25 | let mut module_index = 0; |
| 26 | for section in component.sections.iter_mut() { |
| 27 | match section { |
| 28 | RawSection::Module(module) => { |
| 29 | let snapshot = snapshot |
| 30 | .modules |
| 31 | .iter() |
| 32 | .find(|(i, _)| *i == module_index) |
| 33 | .map(|(_, s)| s); |
| 34 | module_index += 1; |
| 35 | match snapshot { |
| 36 | // This module's snapshot is used for [`Wizer::rewrite`] |
| 37 | // and the results of that are spliced into the |
| 38 | // component. |
| 39 | Some(snapshot) => { |
| 40 | let rewritten_wasm = |
| 41 | self.rewrite(module, snapshot, &FuncRenames::default(), false); |
| 42 | encoder.section(&wasm_encoder::RawSection { |
| 43 | id: wasm_encoder::ComponentSectionId::CoreModule as u8, |
| 44 | data: &rewritten_wasm, |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | // This module wasn't instantiated and has no snapshot, |
| 49 | // plumb it through as-is. |
| 50 | None => { |
| 51 | let mut module_encoder = wasm_encoder::Module::new(); |
| 52 | for section in module.raw_sections() { |
| 53 | module_encoder.section(section); |
| 54 | } |
| 55 | encoder.section(&wasm_encoder::ModuleSection(&module_encoder)); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | RawSection::Raw(s) => { |
| 60 | reencoder.raw_section(&mut encoder, s); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | encoder.finish() |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | struct Reencoder<'a> { |
no test coverage detected