Create a "frankenstein" instance with a single memory. This separate instance is necessary because Wasm objects in Wasmtime must be attached to instances (versus the store, e.g.) and some objects exist outside: a host-provided memory import, shared memory.
(
store: &mut StoreOpaque,
limiter: Option<&mut StoreResourceLimiter<'_>>,
memory_ty: &MemoryType,
preallocation: Option<&SharedMemory>,
)
| 25 | /// attached to instances (versus the store, e.g.) and some objects exist |
| 26 | /// outside: a host-provided memory import, shared memory. |
| 27 | pub async fn create_memory( |
| 28 | store: &mut StoreOpaque, |
| 29 | limiter: Option<&mut StoreResourceLimiter<'_>>, |
| 30 | memory_ty: &MemoryType, |
| 31 | preallocation: Option<&SharedMemory>, |
| 32 | ) -> Result<InstanceId> { |
| 33 | let mut module = Module::new(StaticModuleIndex::from_u32(0)); |
| 34 | |
| 35 | // Create a memory, though it will never be used for constructing a memory |
| 36 | // with an allocator: instead the memories are either preallocated (i.e., |
| 37 | // shared memory) or allocated manually below. |
| 38 | let memory_id = module.memories.push(*memory_ty.wasmtime_memory())?; |
| 39 | |
| 40 | // Since we have only associated a single memory with the "frankenstein" |
| 41 | // instance, it will be exported at index 0. |
| 42 | debug_assert_eq!(memory_id.as_u32(), 0); |
| 43 | let name = module.strings.insert("")?; |
| 44 | module |
| 45 | .exports |
| 46 | .insert(name, EntityIndex::Memory(memory_id))?; |
| 47 | let info = ModuleRuntimeInfo::bare(try_new::<Arc<_>>(module)?)?; |
| 48 | |
| 49 | // We create an instance in the on-demand allocator when creating handles |
| 50 | // associated with external objects. The configured instance allocator |
| 51 | // should only be used when creating module instances as we don't want host |
| 52 | // objects to count towards instance limits. |
| 53 | let allocator = SingleMemoryInstance { |
| 54 | preallocation, |
| 55 | ondemand: OnDemandInstanceAllocator::default(), |
| 56 | }; |
| 57 | unsafe { |
| 58 | store |
| 59 | .allocate_instance( |
| 60 | limiter, |
| 61 | AllocateInstanceKind::Dummy { |
| 62 | allocator: &allocator, |
| 63 | }, |
| 64 | &info, |
| 65 | Default::default(), |
| 66 | ) |
| 67 | .await |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | struct LinearMemoryProxy { |
| 72 | mem: Box<dyn LinearMemory>, |
no test coverage detected