1 use crate::TableType;
2 use crate::prelude::*;
3 use crate::runtime::vm::{Imports, ModuleRuntimeInfo, OnDemandInstanceAllocator};
4 use crate::store::{AllocateInstanceKind, InstanceId, StoreOpaque};
5 use alloc::sync::Arc;
6 use wasmtime_environ::{EntityIndex, Module, TypeTrace};
7 
8 pub fn create_table(store: &mut StoreOpaque, table: &TableType) -> Result<InstanceId> {
9     let mut module = Module::new();
10 
11     let wasmtime_table = *table.wasmtime_table();
12 
13     debug_assert!(
14         wasmtime_table.ref_type.is_canonicalized_for_runtime_usage(),
15         "should be canonicalized for runtime usage: {:?}",
16         wasmtime_table.ref_type
17     );
18 
19     let table_id = module.tables.push(wasmtime_table);
20 
21     // TODO: can this `exports.insert` get removed?
22     module
23         .exports
24         .insert(String::new(), EntityIndex::Table(table_id));
25 
26     let imports = Imports::default();
27 
28     unsafe {
29         let allocator =
30             OnDemandInstanceAllocator::new(store.engine().config().mem_creator.clone(), 0, false);
31         let module = Arc::new(module);
32         store.allocate_instance(
33             AllocateInstanceKind::Dummy {
34                 allocator: &allocator,
35             },
36             &ModuleRuntimeInfo::bare_with_registered_type(
37                 module,
38                 table.element().clone().into_registered_type(),
39             ),
40             imports,
41         )
42     }
43 }
44