1 use crate::ExnType;
2 use crate::TagType;
3 use crate::prelude::*;
4 use crate::runtime::vm::{self, Imports, ModuleRuntimeInfo, OnDemandInstanceAllocator};
5 use crate::store::{AllocateInstanceKind, InstanceId, StoreOpaque};
6 use alloc::sync::Arc;
7 use wasmtime_environ::EngineOrModuleTypeIndex;
8 use wasmtime_environ::StaticModuleIndex;
9 use wasmtime_environ::Tag;
10 use wasmtime_environ::{EntityIndex, Module, TypeTrace};
11
create_tag(store: &mut StoreOpaque, ty: &TagType) -> Result<InstanceId>12 pub 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 let name = module.strings.insert("")?;
32 module.exports.insert(name, EntityIndex::Tag(tag_id))?;
33
34 let imports = Imports::default();
35
36 unsafe {
37 let allocator =
38 OnDemandInstanceAllocator::new(store.engine().config().mem_creator.clone(), 0, false);
39 let module = try_new::<Arc<_>>(module)?;
40
41 // Note that `assert_ready` should be valid here because this module
42 // doesn't allocate tables or memories meaning it shouldn't need a
43 // resource limiter so `None` is passed. As a result no `await` points
44 // should ever be hit.
45 vm::assert_ready(store.allocate_instance(
46 None,
47 AllocateInstanceKind::Dummy {
48 allocator: &allocator,
49 },
50 &ModuleRuntimeInfo::bare_with_registered_type(module, Some(func_ty))?,
51 imports,
52 ))
53 }
54 }
55