pub fn new(
        module: Rc<Module>,
        global_exports: Rc<RefCell<HashMap<String, Option<Export>>>>,
        finished_functions: BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>,
        imports: Imports,
        data_initializers: &[DataInitializer<'_>],
        vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
        dbg_jit_registration: Option<Rc<GdbJitImageRegistration>>,
        host_state: Box<dyn Any>,
    ) -> Result<Self, InstantiationError> {
        let mut tables = create_tables(&module);
        let mut memories = create_memories(&module)?;

        let vmctx_tables = tables
            .values_mut()
            .map(Table::vmtable)
            .collect::<PrimaryMap<DefinedTableIndex, _>>()
            .into_boxed_slice();

        let vmctx_memories = memories
            .values_mut()
            .map(LinearMemory::vmmemory)
            .collect::<PrimaryMap<DefinedMemoryIndex, _>>()
            .into_boxed_slice();

        let vmctx_globals = create_globals(&module);

        let offsets = VMOffsets::new(mem::size_of::<*const u8>() as u8, &module);

        let mut instance_mmap = Mmap::with_at_least(
            mem::size_of::<Instance>()
                .checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap())
                .unwrap(),
        )
        .map_err(InstantiationError::Resource)?;

        let instance = {
            #[allow(clippy::cast_ptr_alignment)]
            let instance_ptr = instance_mmap.as_mut_ptr() as *mut Instance;
            let instance = Instance {
                refcount: 1,
                dependencies: imports.dependencies,
                mmap: instance_mmap,
                module,
                global_exports,
                offsets,
                memories,
                tables,
                finished_functions,
                dbg_jit_registration,
                host_state,
                signal_handler: Box::new(signal_handler_none) as Box<SignalHandler>,
                vmctx: VMContext {},
            };
            unsafe {
                ptr::write(instance_ptr, instance);
                &mut *instance_ptr
            }
        };

        unsafe {
            ptr::copy(
                vmshared_signatures.values().as_slice().as_ptr(),
                instance.signature_ids_ptr() as *mut VMSharedSignatureIndex,
                vmshared_signatures.len(),
            );
            ptr::copy(
                imports.functions.values().as_slice().as_ptr(),
                instance.imported_functions_ptr() as *mut VMFunctionImport,
                imports.functions.len(),
            );
            ptr::copy(
                imports.tables.values().as_slice().as_ptr(),
                instance.imported_tables_ptr() as *mut VMTableImport,
                imports.tables.len(),
            );
            ptr::copy(
                imports.memories.values().as_slice().as_ptr(),
                instance.imported_memories_ptr() as *mut VMMemoryImport,
                imports.memories.len(),
            );
            ptr::copy(
                imports.globals.values().as_slice().as_ptr(),
                instance.imported_globals_ptr() as *mut VMGlobalImport,
                imports.globals.len(),
            );
            ptr::copy(
                vmctx_tables.values().as_slice().as_ptr(),
                instance.tables_ptr() as *mut VMTableDefinition,
                vmctx_tables.len(),
            );
            ptr::copy(
                vmctx_memories.values().as_slice().as_ptr(),
                instance.memories_ptr() as *mut VMMemoryDefinition,
                vmctx_memories.len(),
            );
            ptr::copy(
                vmctx_globals.values().as_slice().as_ptr(),
                instance.globals_ptr() as *mut VMGlobalDefinition,
                vmctx_globals.len(),
            );
            ptr::write(
                instance.builtin_functions_ptr() as *mut VMBuiltinFunctionsArray,
                VMBuiltinFunctionsArray::initialized(),
            );
        }

        // Check initializer bounds before initializing anything.
        check_table_init_bounds(instance)?;
        check_memory_init_bounds(instance, data_initializers)?;

        // Apply the initializers.
        initialize_tables(instance)?;
        initialize_memories(instance, data_initializers)?;
        initialize_globals(instance);

        // Collect the exports for the global export map.
        for (field, decl) in &instance.module.exports {
            use std::collections::hash_map::Entry::*;
            let cell: &RefCell<HashMap<String, Option<Export>>> = instance.global_exports.borrow();
            let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
            match map.entry(field.to_string()) {
                Vacant(entry) => {
                    entry.insert(Some(lookup_by_declaration(
                        &instance.module,
                        &mut instance.vmctx,
                        &instance.offsets,
                        &instance.finished_functions,
                        &decl,
                    )));
                }
                Occupied(ref mut entry) => *entry.get_mut() = None,
            }
        }

        // Ensure that our signal handlers are ready for action.
        // TODO: Move these calls out of `InstanceHandle`.
        wasmtime_init_eager();
        wasmtime_init_finish(instance.vmctx_mut());

        // The WebAssembly spec specifies that the start function is
        // invoked automatically at instantiation time.
        instance.invoke_start_function()?;

        Ok(Self { instance })
    }
/// Create a new `InstanceHandle` pointing at a new `Instance`.
Create a new `InstanceHandle` pointing at a new `Instance`.
Create a new ` InstanceHandle ` pointing at a new ` Instance ` .
pub fn new(
        module: Rc<Module>,
        global_exports: Rc<RefCell<HashMap<String, Option<Export>>>>,
        finished_functions: BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>,
        imports: Imports,
        data_initializers: &[DataInitializer<'_>],
        vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
        dbg_jit_registration: Option<Rc<GdbJitImageRegistration>>,
        host_state: Box<dyn Any>,
    ) -> Result<Self, InstantiationError> {
        let mut tables = create_tables(&module);
        let mut memories = create_memories(&module)?;
        let vmctx_tables = tables
            .values_mut()
            .map(Table::vmtable)
            .collect::<PrimaryMap<DefinedTableIndex, _>>()
            .into_boxed_slice();
        let vmctx_memories = memories
            .values_mut()
            .map(LinearMemory::vmmemory)
            .collect::<PrimaryMap<DefinedMemoryIndex, _>>()
            .into_boxed_slice();
        let vmctx_globals = create_globals(&module);
        let offsets = VMOffsets::new(mem::size_of::<*const u8>() as u8, &module);
        let mut instance_mmap = Mmap::with_at_least(
            mem::size_of::<Instance>()
                .checked_add(usize::try_from(offsets.size_of_vmctx()).unwrap())
                .unwrap(),
        )
        .map_err(InstantiationError::Resource)?;
        let instance = {
            #[allow(clippy::cast_ptr_alignment)]
            let instance_ptr = instance_mmap.as_mut_ptr() as *mut Instance;
            let instance = Instance {
                refcount: 1,
                dependencies: imports.dependencies,
                mmap: instance_mmap,
                module,
                global_exports,
                offsets,
                memories,
                tables,
                finished_functions,
                dbg_jit_registration,
                host_state,
                signal_handler: Box::new(signal_handler_none) as Box<SignalHandler>,
                vmctx: VMContext {},
            };
            unsafe {
                ptr::write(instance_ptr, instance);
                &mut *instance_ptr
            }
        };
        unsafe {
            ptr::copy(
                vmshared_signatures.values().as_slice().as_ptr(),
                instance.signature_ids_ptr() as *mut VMSharedSignatureIndex,
                vmshared_signatures.len(),
            );
            ptr::copy(
                imports.functions.values().as_slice().as_ptr(),
                instance.imported_functions_ptr() as *mut VMFunctionImport,
                imports.functions.len(),
            );
            ptr::copy(
                imports.tables.values().as_slice().as_ptr(),
                instance.imported_tables_ptr() as *mut VMTableImport,
                imports.tables.len(),
            );
            ptr::copy(
                imports.memories.values().as_slice().as_ptr(),
                instance.imported_memories_ptr() as *mut VMMemoryImport,
                imports.memories.len(),
            );
            ptr::copy(
                imports.globals.values().as_slice().as_ptr(),
                instance.imported_globals_ptr() as *mut VMGlobalImport,
                imports.globals.len(),
            );
            ptr::copy(
                vmctx_tables.values().as_slice().as_ptr(),
                instance.tables_ptr() as *mut VMTableDefinition,
                vmctx_tables.len(),
            );
            ptr::copy(
                vmctx_memories.values().as_slice().as_ptr(),
                instance.memories_ptr() as *mut VMMemoryDefinition,
                vmctx_memories.len(),
            );
            ptr::copy(
                vmctx_globals.values().as_slice().as_ptr(),
                instance.globals_ptr() as *mut VMGlobalDefinition,
                vmctx_globals.len(),
            );
            ptr::write(
                instance.builtin_functions_ptr() as *mut VMBuiltinFunctionsArray,
                VMBuiltinFunctionsArray::initialized(),
            );
        }
        // Check initializer bounds before initializing anything.
        check_table_init_bounds(instance)?;
        check_memory_init_bounds(instance, data_initializers)?;
        // Apply the initializers.
        initialize_tables(instance)?;
        initialize_memories(instance, data_initializers)?;
        initialize_globals(instance);
        // Collect the exports for the global export map.
        for (field, decl) in &instance.module.exports {
            use std::collections::hash_map::Entry::*;
            let cell: &RefCell<HashMap<String, Option<Export>>> = instance.global_exports.borrow();
            let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
            match map.entry(field.to_string()) {
                Vacant(entry) => {
                    entry.insert(Some(lookup_by_declaration(
                        &instance.module,
                        &mut instance.vmctx,
                        &instance.offsets,
                        &instance.finished_functions,
                        &decl,
                    )));
                }
                Occupied(ref mut entry) => *entry.get_mut() = None,
            }
        }
        // Ensure that our signal handlers are ready for action.
        // TODO: Move these calls out of `InstanceHandle`.
        wasmtime_init_eager();
        wasmtime_init_finish(instance.vmctx_mut());
        // The WebAssembly spec specifies that the start function is
        // invoked automatically at instantiation time.
        instance.invoke_start_function()?;
        Ok(Self { instance })
    }
pub fn new ( module : Rc < Module > , global_exports : Rc < RefCell < HashMap < String , Option < Export > > > > , finished_functions : BoxedSlice < DefinedFuncIndex , * const VMFunctionBody > , imports : Imports , data_initializers : & [ DataInitializer < ' _ > ] , vmshared_signatures : BoxedSlice < SignatureIndex , VMSharedSignatureIndex > , dbg_jit_registration : Option < Rc < GdbJitImageRegistration > > , host_state : Box < dyn Any > , ) -> Result < Self , InstantiationError > { let mut tables = create_tables ( & module ) ; let mut memories = create_memories ( & module ) ? ; let vmctx_tables = tables . values_mut ( ) . map ( Table :: vmtable ) . collect :: < PrimaryMap < DefinedTableIndex , _ > > ( ) . into_boxed_slice ( ) ; let vmctx_memories = memories . values_mut ( ) . map ( LinearMemory :: vmmemory ) . collect :: < PrimaryMap < DefinedMemoryIndex , _ > > ( ) . into_boxed_slice ( ) ; let vmctx_globals = create_globals ( & module ) ; let offsets = VMOffsets :: new ( mem :: size_of :: < * const u8 > ( ) as u8 , & module ) ; let mut instance_mmap = Mmap :: with_at_least ( mem :: size_of :: < Instance > ( ) . checked_add ( usize :: try_from ( offsets . size_of_vmctx ( ) ) . unwrap ( ) ) . unwrap ( ) , ) . map_err ( InstantiationError :: Resource ) ? ; let instance = { # [ allow ( clippy cast_ptr_alignment ) ] let instance_ptr = instance_mmap . as_mut_ptr ( ) as * mut Instance ; let instance = Instance { refcount : 1 , dependencies : imports . dependencies , mmap : instance_mmap , module , global_exports , offsets , memories , tables , finished_functions , dbg_jit_registration , host_state , signal_handler : Box :: new ( signal_handler_none ) as Box < SignalHandler > , vmctx : VMContext { } , } ; unsafe { ptr :: write ( instance_ptr , instance ) ; & mut * instance_ptr } } ; unsafe { ptr :: copy ( vmshared_signatures . values ( ) . as_slice ( ) . as_ptr ( ) , instance . signature_ids_ptr ( ) as * mut VMSharedSignatureIndex , vmshared_signatures . len ( ) , ) ; ptr :: copy ( imports . functions . values ( ) . as_slice ( ) . as_ptr ( ) , instance . imported_functions_ptr ( ) as * mut VMFunctionImport , imports . functions . len ( ) , ) ; ptr :: copy ( imports . tables . values ( ) . as_slice ( ) . as_ptr ( ) , instance . imported_tables_ptr ( ) as * mut VMTableImport , imports . tables . len ( ) , ) ; ptr :: copy ( imports . memories . values ( ) . as_slice ( ) . as_ptr ( ) , instance . imported_memories_ptr ( ) as * mut VMMemoryImport , imports . memories . len ( ) , ) ; ptr :: copy ( imports . globals . values ( ) . as_slice ( ) . as_ptr ( ) , instance . imported_globals_ptr ( ) as * mut VMGlobalImport , imports . globals . len ( ) , ) ; ptr :: copy ( vmctx_tables . values ( ) . as_slice ( ) . as_ptr ( ) , instance . tables_ptr ( ) as * mut VMTableDefinition , vmctx_tables . len ( ) , ) ; ptr :: copy ( vmctx_memories . values ( ) . as_slice ( ) . as_ptr ( ) , instance . memories_ptr ( ) as * mut VMMemoryDefinition , vmctx_memories . len ( ) , ) ; ptr :: copy ( vmctx_globals . values ( ) . as_slice ( ) . as_ptr ( ) , instance . globals_ptr ( ) as * mut VMGlobalDefinition , vmctx_globals . len ( ) , ) ; ptr :: write ( instance . builtin_functions_ptr ( ) as * mut VMBuiltinFunctionsArray , VMBuiltinFunctionsArray :: initialized ( ) , ) ; } // Check initializer bounds before initializing anything. check_table_init_bounds ( instance ) ? ; check_memory_init_bounds ( instance , data_initializers ) ? ; // Apply the initializers. initialize_tables ( instance ) ? ; initialize_memories ( instance , data_initializers ) ? ; initialize_globals ( instance ) ; // Collect the exports for the global export map. for ( field , decl ) in & instance . module . exports { use std :: collections :: hash_map :: Entry :: * ; let cell : & RefCell < HashMap < String , Option < Export > > > = instance . global_exports . borrow ( ) ; let map : & mut HashMap < String , Option < Export > > = & mut cell . borrow_mut ( ) ; match map . entry ( field . to_string ( ) ) { Vacant ( entry ) => { entry . insert ( Some ( lookup_by_declaration ( & instance . module , & mut instance . vmctx , & instance . offsets , & instance . finished_functions , & decl , ) ) ) ; } Occupied ( ref mut entry ) => * entry . get_mut ( ) = None , } } // Ensure that our signal handlers are ready for action. // TODO: Move these calls out of `InstanceHandle`. wasmtime_init_eager ( ) ; wasmtime_init_finish ( instance . vmctx_mut ( ) ) ; // The WebAssembly spec specifies that the start function is // invoked automatically at instantiation time. instance . invoke_start_function ( ) ? ; Ok ( Self { instance } ) }
Create a new `InstanceHandle` pointing at a new `Instance`.
Create a new ` InstanceHandle ` pointing at a new ` Instance ` .
module
Rc<Module>
global_exports
Rc<RefCell<HashMap<String, Option<Export>>>>
finished_functions
BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>
imports
Imports
data_initializers
&[DataInitializer<'_>]
vmshared_signatures
BoxedSlice<SignatureIndex, VMSharedSignatureIndex>
dbg_jit_registration
Option<Rc<GdbJitImageRegistration>>
host_state
Box<dyn Any>
module
Rc<Module>
global_exports
Rc<RefCell<HashMap<String, Option<Export>>>>
finished_functions
BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>
imports
Imports
data_initializers
&[DataInitializer<'_>]
vmshared_signatures
BoxedSlice<SignatureIndex, VMSharedSignatureIndex>
dbg_jit_registration
Option<Rc<GdbJitImageRegistration>>
host_state
Box<dyn Any>