@@ -333,6 +333,9 @@ pub struct Module {
333333 /// Number of imported or aliased globals in the module.
334334 pub num_imported_globals : usize ,
335335
336+ /// Number of imported or aliased tags in the module.
337+ pub num_imported_tags : usize ,
338+
336339 /// Number of functions that "escape" from this module may need to have a
337340 /// `VMFuncRef` constructed for them.
338341 ///
@@ -354,6 +357,9 @@ pub struct Module {
354357
355358 /// WebAssembly global initializers for locally-defined globals.
356359 pub global_initializers : PrimaryMap < DefinedGlobalIndex , ConstExpr > ,
360+
361+ /// WebAssembly exception and control tags.
362+ pub tags : PrimaryMap < TagIndex , Tag > ,
357363}
358364
359365/// Initialization routines for creating an instance, encompassing imports,
@@ -500,6 +506,29 @@ impl Module {
500506 index. index ( ) < self . num_imported_globals
501507 }
502508
509+ /// Test whether the given tag index is for an imported tag.
510+ #[ inline]
511+ pub fn is_imported_tag ( & self , index : TagIndex ) -> bool {
512+ index. index ( ) < self . num_imported_tags
513+ }
514+
515+ /// Convert a `DefinedTagIndex` into a `TagIndex`.
516+ #[ inline]
517+ pub fn tag_index ( & self , defined_tag : DefinedTagIndex ) -> TagIndex {
518+ TagIndex :: new ( self . num_imported_tags + defined_tag. index ( ) )
519+ }
520+
521+ /// Convert a `TagIndex` into a `DefinedTagIndex`. Returns None if the
522+ /// index is an imported tag.
523+ #[ inline]
524+ pub fn defined_tag_index ( & self , tag : TagIndex ) -> Option < DefinedTagIndex > {
525+ if tag. index ( ) < self . num_imported_tags {
526+ None
527+ } else {
528+ Some ( DefinedTagIndex :: new ( tag. index ( ) - self . num_imported_tags ) )
529+ }
530+ }
531+
503532 /// Returns an iterator of all the imports in this module, along with their
504533 /// module name, field name, and type that's being imported.
505534 pub fn imports ( & self ) -> impl ExactSizeIterator < Item = ( & str , & str , EntityType ) > {
@@ -517,9 +546,16 @@ impl Module {
517546 EntityIndex :: Table ( i) => EntityType :: Table ( self . tables [ i] ) ,
518547 EntityIndex :: Memory ( i) => EntityType :: Memory ( self . memories [ i] ) ,
519548 EntityIndex :: Function ( i) => EntityType :: Function ( self . functions [ i] . signature ) ,
549+ EntityIndex :: Tag ( i) => EntityType :: Tag ( self . tags [ i] ) ,
520550 }
521551 }
522552
553+ /// Appends a new tag to this module with the given type information.
554+ pub fn push_tag ( & mut self , signature : impl Into < EngineOrModuleTypeIndex > ) -> TagIndex {
555+ let signature = signature. into ( ) ;
556+ self . tags . push ( Tag { signature } )
557+ }
558+
523559 /// Appends a new function to this module with the given type information,
524560 /// used for functions that either don't escape or aren't certain whether
525561 /// they escape yet.
@@ -548,6 +584,12 @@ impl Module {
548584 pub fn num_defined_memories ( & self ) -> usize {
549585 self . memories . len ( ) - self . num_imported_memories
550586 }
587+
588+ /// Returns the number of tags defined by this module itself: all tags
589+ /// minus imported tags.
590+ pub fn num_defined_tags ( & self ) -> usize {
591+ self . tags . len ( ) - self . num_imported_tags
592+ }
551593}
552594
553595impl TypeTrace for Module {
@@ -572,12 +614,14 @@ impl TypeTrace for Module {
572614 num_imported_tables : _,
573615 num_imported_memories : _,
574616 num_imported_globals : _,
617+ num_imported_tags : _,
575618 num_escaped_funcs : _,
576619 functions,
577620 tables,
578621 memories : _,
579622 globals,
580623 global_initializers : _,
624+ tags,
581625 } = self ;
582626
583627 for t in types. values ( ) . copied ( ) {
@@ -592,6 +636,9 @@ impl TypeTrace for Module {
592636 for g in globals. values ( ) {
593637 g. trace ( func) ?;
594638 }
639+ for t in tags. values ( ) {
640+ t. trace ( func) ?;
641+ }
595642 Ok ( ( ) )
596643 }
597644
@@ -616,12 +663,14 @@ impl TypeTrace for Module {
616663 num_imported_tables : _,
617664 num_imported_memories : _,
618665 num_imported_globals : _,
666+ num_imported_tags : _,
619667 num_escaped_funcs : _,
620668 functions,
621669 tables,
622670 memories : _,
623671 globals,
624672 global_initializers : _,
673+ tags,
625674 } = self ;
626675
627676 for t in types. values_mut ( ) {
@@ -636,6 +685,9 @@ impl TypeTrace for Module {
636685 for g in globals. values_mut ( ) {
637686 g. trace_mut ( func) ?;
638687 }
688+ for t in tags. values_mut ( ) {
689+ t. trace_mut ( func) ?;
690+ }
639691 Ok ( ( ) )
640692 }
641693}
0 commit comments