pub fn try_paged_init(&mut self) {
        // This method only attempts to transform a a `Segmented` memory init
        // into a `Paged` one, no other state.
        if !self.module.memory_initialization.is_segmented() {
            return;
        }

        // Initially all memories start out as all zeros, represented with a
        // lack of entries in the `BTreeMap` here. The map indexes byte offset
        // (which is always wasm-page-aligned) to the contents of the page, with
        // missing entries implicitly as all zeros.
        let mut page_contents = PrimaryMap::with_capacity(self.module.memory_plans.len());
        for _ in 0..self.module.memory_plans.len() {
            page_contents.push(BTreeMap::new());
        }

        // Perform a "dry run" of memory initialization which will fail if we
        // can't switch to paged initialization. When data is written it's
        // transformed into the representation of `page_contents`.
        let mut data = self.data.iter();
        let ok = self.module.memory_initialization.init_memory(
            InitMemory::CompileTime(&self.module),
            &mut |memory, init| {
                let data = data.next().unwrap();
                assert_eq!(data.len(), init.data.len());
                // If an initializer references an imported memory then
                // everything will need to be processed in-order anyway to
                // handle the dynamic limits of the memory specified.
                if self.module.defined_memory_index(memory).is_none() {
                    return false;
                };
                let page_size = u64::from(WASM_PAGE_SIZE);
                let contents = &mut page_contents[memory];
                let mut page_index = init.offset / page_size;
                let mut page_offset = (init.offset % page_size) as usize;
                let mut data = &data[..];

                while !data.is_empty() {
                    // If this page hasn't been seen before, then it starts out
                    // as all zeros.
                    let page = contents
                        .entry(page_index)
                        .or_insert_with(|| vec![0; page_size as usize]);
                    let page = &mut page[page_offset..];

                    let len = std::cmp::min(data.len(), page.len());
                    page[..len].copy_from_slice(&data[..len]);

                    page_index += 1;
                    page_offset = 0;
                    data = &data[len..];
                }

                true
            },
        );

        // If anything failed above or hit an unknown case then bail out
        // entirely since this module cannot use paged initialization.
        if !ok {
            return;
        }

        // If we've gotten this far then we're switching to paged
        // initialization. The contents of the initial wasm memory are
        // specified by `page_contents`, so the job now is to transform data
        // representation of wasm memory back into the representation we use
        // in a `Module`.
        //
        // This is done by clearing `self.data`, the original data segments,
        // since those are now all represented in `page_contents`. Afterwards
        // all the pages are subsequently pushed onto `self.data` and the
        // offsets within `self.data` are recorded in each segment that's part
        // of `Paged`.
        self.data.clear();
        let mut map = PrimaryMap::with_capacity(page_contents.len());
        let mut offset = 0;
        for (memory, pages) in page_contents {
            let mut page_offsets = Vec::with_capacity(pages.len());
            for (page_index, page) in pages {
                let end = offset + (page.len() as u32);
                page_offsets.push(StaticMemoryInitializer {
                    offset: page_index * u64::from(WASM_PAGE_SIZE),
                    data: offset..end,
                });
                offset = end;
                self.data.push(page.into());
            }
            let index = map.push(page_offsets);
            assert_eq!(index, memory);
        }
        self.module.memory_initialization = MemoryInitialization::Paged { map };
    }
/// Attempts to convert segmented memory initialization into paged
/// initialization for the module that this translation represents.
///
/// If this module's memory initialization is not compatible with paged
/// initialization then this won't change anything. Otherwise if it is
/// compatible then the `memory_initialization` field will be updated.
Attempts to convert segmented memory initialization into paged
initialization for the module that this translation represents.

If this module's memory initialization is not compatible with paged
initialization then this won't change anything. Otherwise if it is
compatible then the `memory_initialization` field will be updated.
Attempts to convert segmented memory initialization into paged initialization for the module that this translation represents . If this module ' s memory initialization is not compatible with paged initialization then this won ' t change anything . Otherwise if it is compatible then the ` memory_initialization ` field will be updated .
pub fn try_paged_init(&mut self) {
        // This method only attempts to transform a a `Segmented` memory init
        // into a `Paged` one, no other state.
        if !self.module.memory_initialization.is_segmented() {
            return;
        }
        // Initially all memories start out as all zeros, represented with a
        // lack of entries in the `BTreeMap` here. The map indexes byte offset
        // (which is always wasm-page-aligned) to the contents of the page, with
        // missing entries implicitly as all zeros.
        let mut page_contents = PrimaryMap::with_capacity(self.module.memory_plans.len());
        for _ in 0..self.module.memory_plans.len() {
            page_contents.push(BTreeMap::new());
        }
        // Perform a "dry run" of memory initialization which will fail if we
        // can't switch to paged initialization. When data is written it's
        // transformed into the representation of `page_contents`.
        let mut data = self.data.iter();
        let ok = self.module.memory_initialization.init_memory(
            InitMemory::CompileTime(&self.module),
            &mut |memory, init| {
                let data = data.next().unwrap();
                assert_eq!(data.len(), init.data.len());
                // If an initializer references an imported memory then
                // everything will need to be processed in-order anyway to
                // handle the dynamic limits of the memory specified.
                if self.module.defined_memory_index(memory).is_none() {
                    return false;
                };
                let page_size = u64::from(WASM_PAGE_SIZE);
                let contents = &mut page_contents[memory];
                let mut page_index = init.offset / page_size;
                let mut page_offset = (init.offset % page_size) as usize;
                let mut data = &data[..];
                while !data.is_empty() {
                    // If this page hasn't been seen before, then it starts out
                    // as all zeros.
                    let page = contents
                        .entry(page_index)
                        .or_insert_with(|| vec![0; page_size as usize]);
                    let page = &mut page[page_offset..];
                    let len = std::cmp::min(data.len(), page.len());
                    page[..len].copy_from_slice(&data[..len]);
                    page_index += 1;
                    page_offset = 0;
                    data = &data[len..];
                }
                true
            },
        );
        // If anything failed above or hit an unknown case then bail out
        // entirely since this module cannot use paged initialization.
        if !ok {
            return;
        }
        // If we've gotten this far then we're switching to paged
        // initialization. The contents of the initial wasm memory are
        // specified by `page_contents`, so the job now is to transform data
        // representation of wasm memory back into the representation we use
        // in a `Module`.
        //
        // This is done by clearing `self.data`, the original data segments,
        // since those are now all represented in `page_contents`. Afterwards
        // all the pages are subsequently pushed onto `self.data` and the
        // offsets within `self.data` are recorded in each segment that's part
        // of `Paged`.
        self.data.clear();
        let mut map = PrimaryMap::with_capacity(page_contents.len());
        let mut offset = 0;
        for (memory, pages) in page_contents {
            let mut page_offsets = Vec::with_capacity(pages.len());
            for (page_index, page) in pages {
                let end = offset + (page.len() as u32);
                page_offsets.push(StaticMemoryInitializer {
                    offset: page_index * u64::from(WASM_PAGE_SIZE),
                    data: offset..end,
                });
                offset = end;
                self.data.push(page.into());
            }
            let index = map.push(page_offsets);
            assert_eq!(index, memory);
        }
        self.module.memory_initialization = MemoryInitialization::Paged { map };
    }
pub fn try_paged_init ( & mut self ) { // This method only attempts to transform a a `Segmented` memory init // into a `Paged` one, no other state. if ! self . module . memory_initialization . is_segmented ( ) { return ; } // Initially all memories start out as all zeros, represented with a // lack of entries in the `BTreeMap` here. The map indexes byte offset // (which is always wasm-page-aligned) to the contents of the page, with // missing entries implicitly as all zeros. let mut page_contents = PrimaryMap :: with_capacity ( self . module . memory_plans . len ( ) ) ; for _ in 0 .. self . module . memory_plans . len ( ) { page_contents . push ( BTreeMap :: new ( ) ) ; } // Perform a "dry run" of memory initialization which will fail if we // can't switch to paged initialization. When data is written it's // transformed into the representation of `page_contents`. let mut data = self . data . iter ( ) ; let ok = self . module . memory_initialization . init_memory ( InitMemory :: CompileTime ( & self . module ) , & mut | memory , init | { let data = data . next ( ) . unwrap ( ) ; assert_eq ! ( data len ( ) init data len ( ) ) ; // If an initializer references an imported memory then // everything will need to be processed in-order anyway to // handle the dynamic limits of the memory specified. if self . module . defined_memory_index ( memory ) . is_none ( ) { return false ; } ; let page_size = u64 :: from ( WASM_PAGE_SIZE ) ; let contents = & mut page_contents [ memory ] ; let mut page_index = init . offset / page_size ; let mut page_offset = ( init . offset % page_size ) as usize ; let mut data = & data [ .. ] ; while ! data . is_empty ( ) { // If this page hasn't been seen before, then it starts out // as all zeros. let page = contents . entry ( page_index ) . or_insert_with ( | | vec ! [ 0 page_size as usize ] ) ; let page = & mut page [ page_offset .. ] ; let len = std :: cmp :: min ( data . len ( ) , page . len ( ) ) ; page [ .. len ] . copy_from_slice ( & data [ .. len ] ) ; page_index += 1 ; page_offset = 0 ; data = & data [ len .. ] ; } true } , ) ; // If anything failed above or hit an unknown case then bail out // entirely since this module cannot use paged initialization. if ! ok { return ; } // If we've gotten this far then we're switching to paged // initialization. The contents of the initial wasm memory are // specified by `page_contents`, so the job now is to transform data // representation of wasm memory back into the representation we use // in a `Module`. // // This is done by clearing `self.data`, the original data segments, // since those are now all represented in `page_contents`. Afterwards // all the pages are subsequently pushed onto `self.data` and the // offsets within `self.data` are recorded in each segment that's part // of `Paged`. self . data . clear ( ) ; let mut map = PrimaryMap :: with_capacity ( page_contents . len ( ) ) ; let mut offset = 0 ; for ( memory , pages ) in page_contents { let mut page_offsets = Vec :: with_capacity ( pages . len ( ) ) ; for ( page_index , page ) in pages { let end = offset + ( page . len ( ) as u32 ) ; page_offsets . push ( StaticMemoryInitializer { offset : page_index * u64 :: from ( WASM_PAGE_SIZE ) , data : offset .. end , } ) ; offset = end ; self . data . push ( page . into ( ) ) ; } let index = map . push ( page_offsets ) ; assert_eq ! ( index memory ) ; } self . module . memory_initialization = MemoryInitialization :: Paged { map } ; }
Attempts to convert segmented memory initialization into paged
initialization for the module that this translation represents.
Attempts to convert segmented memory initialization into paged initialization for the module that this translation represents .
&mut self
&mut self