Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
Extern::SharedMemory(_) => panic!(
"Shared Memory no implemented for wasm_* types. Please use wasmtime_* types instead"
),
Extern::Tag(_) => todo!(), // FIXME: #10252 C embedder API for exceptions and control tags.
}
}

Expand Down Expand Up @@ -143,6 +144,7 @@ impl From<Extern> for wasmtime_extern_t {
sharedmemory: ManuallyDrop::new(Box::new(sharedmemory)),
},
},
Extern::Tag(_) => todo!(), // FIXME: #10252 C embedder API for exceptions and control tags.
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/c-api/src/types/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl CExternType {
ExternType::Global(f) => CExternType::Global(CGlobalType::new(f)),
ExternType::Memory(f) => CExternType::Memory(CMemoryType::new(f)),
ExternType::Table(f) => CExternType::Table(CTableType::new(f)),
ExternType::Tag(_) => todo!(), // FIXME: #10252 C embedder API for exceptions and control tags.
}
}
}
Expand Down
29 changes: 17 additions & 12 deletions crates/environ/src/compile/module_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{
ConstExpr, ConstOp, DataIndex, DefinedFuncIndex, ElemIndex, EngineOrModuleTypeIndex,
EntityIndex, EntityType, FuncIndex, GlobalIndex, IndexType, InitMemory, MemoryIndex,
ModuleInternedTypeIndex, ModuleTypesBuilder, PrimaryMap, SizeOverflow, StaticMemoryInitializer,
TableIndex, TableInitialValue, Tunables, TypeConvert, TypeIndex, Unsigned, WasmError,
WasmHeapTopType, WasmHeapType, WasmResult, WasmValType, WasmparserTypeConverter,
TableIndex, TableInitialValue, Tag, TagIndex, Tunables, TypeConvert, TypeIndex, Unsigned,
WasmError, WasmHeapTopType, WasmHeapType, WasmResult, WasmValType, WasmparserTypeConverter,
};
use anyhow::{bail, Result};
use cranelift_entity::packed_option::ReservedValue;
Expand Down Expand Up @@ -318,9 +318,13 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
self.result.module.num_imported_tables += 1;
EntityType::Table(self.convert_table_type(&ty)?)
}

// doesn't get past validation
TypeRef::Tag(_) => unreachable!(),
TypeRef::Tag(ty) => {
let index = TypeIndex::from_u32(ty.func_type_idx);
let signature = self.result.module.types[index];
let tag = Tag { signature };
self.result.module.num_imported_tags += 1;
EntityType::Tag(tag)
}
};
self.declare_import(import.module, import.name, ty);
}
Expand Down Expand Up @@ -384,9 +388,12 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
Payload::TagSection(tags) => {
self.validator.tag_section(&tags)?;

// This feature isn't enabled at this time, so we should
// never get here.
unreachable!();
for entry in tags {
let sigindex = entry?.func_type_idx;
let ty = TypeIndex::from_u32(sigindex);
let interned_index = self.result.module.types[ty];
self.result.module.push_tag(interned_index);
}
}

Payload::GlobalSection(globals) => {
Expand Down Expand Up @@ -424,9 +431,7 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
ExternalKind::Table => EntityIndex::Table(TableIndex::from_u32(index)),
ExternalKind::Memory => EntityIndex::Memory(MemoryIndex::from_u32(index)),
ExternalKind::Global => EntityIndex::Global(GlobalIndex::from_u32(index)),

// this never gets past validation
ExternalKind::Tag => unreachable!(),
ExternalKind::Tag => EntityIndex::Tag(TagIndex::from_u32(index)),
};
self.result
.module
Expand Down Expand Up @@ -770,7 +775,7 @@ and for re-adding support for interface types you can see this issue:
EntityType::Table(ty) => EntityIndex::Table(self.result.module.tables.push(ty)),
EntityType::Memory(ty) => EntityIndex::Memory(self.result.module.memories.push(ty)),
EntityType::Global(ty) => EntityIndex::Global(self.result.module.globals.push(ty)),
EntityType::Tag(_) => unimplemented!(),
EntityType::Tag(ty) => EntityIndex::Tag(self.result.module.tags.push(ty)),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/environ/src/component/translate/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ impl<'a> Inliner<'a> {
EntityIndex::Table(i) => frame.tables[i].clone().into(),
EntityIndex::Global(i) => frame.globals[i].clone().into(),
EntityIndex::Memory(i) => frame.memories[i].clone().into(),
EntityIndex::Tag(_) => todo!(), // FIXME: #10252 support for tags in the component model
},
}
}
Expand Down
52 changes: 52 additions & 0 deletions crates/environ/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ pub struct Module {
/// Number of imported or aliased globals in the module.
pub num_imported_globals: usize,

/// Number of imported or aliased tags in the module.
pub num_imported_tags: usize,

/// Number of functions that "escape" from this module may need to have a
/// `VMFuncRef` constructed for them.
///
Expand All @@ -354,6 +357,9 @@ pub struct Module {

/// WebAssembly global initializers for locally-defined globals.
pub global_initializers: PrimaryMap<DefinedGlobalIndex, ConstExpr>,

/// WebAssembly exception and control tags.
pub tags: PrimaryMap<TagIndex, Tag>,
}

/// Initialization routines for creating an instance, encompassing imports,
Expand Down Expand Up @@ -500,6 +506,29 @@ impl Module {
index.index() < self.num_imported_globals
}

/// Test whether the given tag index is for an imported tag.
#[inline]
pub fn is_imported_tag(&self, index: TagIndex) -> bool {
index.index() < self.num_imported_tags
}

/// Convert a `DefinedTagIndex` into a `TagIndex`.
#[inline]
pub fn tag_index(&self, defined_tag: DefinedTagIndex) -> TagIndex {
TagIndex::new(self.num_imported_tags + defined_tag.index())
}

/// Convert a `TagIndex` into a `DefinedTagIndex`. Returns None if the
/// index is an imported tag.
#[inline]
pub fn defined_tag_index(&self, tag: TagIndex) -> Option<DefinedTagIndex> {
if tag.index() < self.num_imported_tags {
None
} else {
Some(DefinedTagIndex::new(tag.index() - self.num_imported_tags))
}
}

/// Returns an iterator of all the imports in this module, along with their
/// module name, field name, and type that's being imported.
pub fn imports(&self) -> impl ExactSizeIterator<Item = (&str, &str, EntityType)> {
Expand All @@ -517,9 +546,16 @@ impl Module {
EntityIndex::Table(i) => EntityType::Table(self.tables[i]),
EntityIndex::Memory(i) => EntityType::Memory(self.memories[i]),
EntityIndex::Function(i) => EntityType::Function(self.functions[i].signature),
EntityIndex::Tag(i) => EntityType::Tag(self.tags[i]),
}
}

/// Appends a new tag to this module with the given type information.
pub fn push_tag(&mut self, signature: impl Into<EngineOrModuleTypeIndex>) -> TagIndex {
let signature = signature.into();
self.tags.push(Tag { signature })
}

/// Appends a new function to this module with the given type information,
/// used for functions that either don't escape or aren't certain whether
/// they escape yet.
Expand Down Expand Up @@ -548,6 +584,12 @@ impl Module {
pub fn num_defined_memories(&self) -> usize {
self.memories.len() - self.num_imported_memories
}

/// Returns the number of tags defined by this module itself: all tags
/// minus imported tags.
pub fn num_defined_tags(&self) -> usize {
self.tags.len() - self.num_imported_tags
}
}

impl TypeTrace for Module {
Expand All @@ -572,12 +614,14 @@ impl TypeTrace for Module {
num_imported_tables: _,
num_imported_memories: _,
num_imported_globals: _,
num_imported_tags: _,
num_escaped_funcs: _,
functions,
tables,
memories: _,
globals,
global_initializers: _,
tags,
} = self;

for t in types.values().copied() {
Expand All @@ -592,6 +636,9 @@ impl TypeTrace for Module {
for g in globals.values() {
g.trace(func)?;
}
for t in tags.values() {
t.trace(func)?;
}
Ok(())
}

Expand All @@ -616,12 +663,14 @@ impl TypeTrace for Module {
num_imported_tables: _,
num_imported_memories: _,
num_imported_globals: _,
num_imported_tags: _,
num_escaped_funcs: _,
functions,
tables,
memories: _,
globals,
global_initializers: _,
tags,
} = self;

for t in types.values_mut() {
Expand All @@ -636,6 +685,9 @@ impl TypeTrace for Module {
for g in globals.values_mut() {
g.trace_mut(func)?;
}
for t in tags.values_mut() {
t.trace_mut(func)?;
}
Ok(())
}
}
Expand Down
47 changes: 34 additions & 13 deletions crates/environ/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,11 @@ entity_impl!(DataIndex);
pub struct ElemIndex(u32);
entity_impl!(ElemIndex);

/// Index type of a defined tag inside the WebAssembly module.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DefinedTagIndex(u32);
entity_impl!(DefinedTagIndex);

/// Index type of an event inside the WebAssembly module.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct TagIndex(u32);
Expand All @@ -1339,6 +1344,8 @@ pub enum EntityIndex {
Memory(MemoryIndex),
/// Global index.
Global(GlobalIndex),
/// Tag index.
Tag(TagIndex),
}

impl From<FuncIndex> for EntityIndex {
Expand All @@ -1365,6 +1372,12 @@ impl From<GlobalIndex> for EntityIndex {
}
}

impl From<TagIndex> for EntityIndex {
fn from(idx: TagIndex) -> EntityIndex {
EntityIndex::Tag(idx)
}
}

/// A type of an item in a wasm module where an item is typically something that
/// can be exported.
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -1373,7 +1386,7 @@ pub enum EntityType {
Global(Global),
/// A linear memory with the specified limits
Memory(Memory),
/// An event definition.
/// An exception and control tag definition.
Tag(Tag),
/// A table with the specified element type and limits
Table(Table),
Expand All @@ -1391,7 +1404,8 @@ impl TypeTrace for EntityType {
Self::Global(g) => g.trace(func),
Self::Table(t) => t.trace(func),
Self::Function(idx) => func(*idx),
Self::Memory(_) | Self::Tag(_) => Ok(()),
Self::Memory(_) => Ok(()),
Self::Tag(t) => t.trace(func),
}
}

Expand All @@ -1403,7 +1417,8 @@ impl TypeTrace for EntityType {
Self::Global(g) => g.trace_mut(func),
Self::Table(t) => t.trace_mut(func),
Self::Function(idx) => func(idx),
Self::Memory(_) | Self::Tag(_) => Ok(()),
Self::Memory(_) => Ok(()),
Self::Tag(t) => t.trace_mut(func),
}
}
}
Expand Down Expand Up @@ -1933,20 +1948,26 @@ impl From<wasmparser::MemoryType> for Memory {
}
}

/// WebAssembly event.
/// WebAssembly exception and control tag.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Tag {
/// The event signature type.
pub ty: TypeIndex,
/// The tag signature type.
pub signature: EngineOrModuleTypeIndex,
}

impl From<wasmparser::TagType> for Tag {
fn from(ty: wasmparser::TagType) -> Tag {
match ty.kind {
wasmparser::TagKind::Exception => Tag {
ty: TypeIndex::from_u32(ty.func_type_idx),
},
}
impl TypeTrace for Tag {
fn trace<F, E>(&self, func: &mut F) -> Result<(), E>
where
F: FnMut(EngineOrModuleTypeIndex) -> Result<(), E>,
{
func(self.signature)
}

fn trace_mut<F, E>(&mut self, func: &mut F) -> Result<(), E>
where
F: FnMut(&mut EngineOrModuleTypeIndex) -> Result<(), E>,
{
func(&mut self.signature)
}
}

Expand Down
Loading