wasmtime/runtime/trampoline/
tag.rs

1use crate::ExnType;
2use crate::TagType;
3use crate::prelude::*;
4use crate::runtime::vm::{self, Imports, ModuleRuntimeInfo, OnDemandInstanceAllocator};
5use crate::store::{AllocateInstanceKind, InstanceId, StoreOpaque};
6use alloc::sync::Arc;
7use wasmtime_environ::EngineOrModuleTypeIndex;
8use wasmtime_environ::StaticModuleIndex;
9use wasmtime_environ::Tag;
10use wasmtime_environ::{EntityIndex, Module, TypeTrace};
11
12pub fn create_tag(store: &mut StoreOpaque, ty: &TagType) -> Result<InstanceId> {
13    let mut module = Module::new(StaticModuleIndex::from_u32(0));
14    let func_ty = ty.ty().clone().into_registered_type();
15    let exn_ty = ExnType::from_tag_type(ty)?.registered_type().clone();
16
17    debug_assert!(
18        func_ty.is_canonicalized_for_runtime_usage(),
19        "should be canonicalized for runtime usage: {func_ty:?}",
20    );
21    debug_assert!(
22        exn_ty.is_canonicalized_for_runtime_usage(),
23        "should be canonicalized for runtime usage: {exn_ty:?}",
24    );
25
26    let tag_id = module.tags.push(Tag {
27        signature: EngineOrModuleTypeIndex::Engine(func_ty.index()),
28        exception: EngineOrModuleTypeIndex::Engine(exn_ty.index()),
29    });
30
31    module
32        .exports
33        .insert(String::new(), EntityIndex::Tag(tag_id));
34
35    let imports = Imports::default();
36
37    unsafe {
38        let allocator =
39            OnDemandInstanceAllocator::new(store.engine().config().mem_creator.clone(), 0, false);
40        let module = Arc::new(module);
41
42        // Note that `assert_ready` should be valid here because this module
43        // doesn't allocate tables or memories meaning it shouldn't need a
44        // resource limiter so `None` is passed. As a result no `await` points
45        // should ever be hit.
46        vm::assert_ready(store.allocate_instance(
47            None,
48            AllocateInstanceKind::Dummy {
49                allocator: &allocator,
50            },
51            &ModuleRuntimeInfo::bare_with_registered_type(module, Some(func_ty)),
52            imports,
53        ))
54    }
55}