wasmtime/runtime/trampoline/
table.rs

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