1- use crate :: { parse_err, Error , Result } ;
2- use std:: result:: Result as StdResult ;
1+ use crate :: {
2+ compiler:: { self , ModuleCode } ,
3+ parse_err,
4+ parser:: Parser ,
5+ Error , Result ,
6+ } ;
7+ use std:: sync:: Arc ;
8+
9+ #[ derive( Debug ) ]
10+ pub struct Component {
11+ pub ( crate ) parsed : ParsedComponent ,
12+ }
13+
14+ impl Component {
15+ pub fn new ( bytes : & [ u8 ] ) -> Result < Self > {
16+ let parsed = Parser :: new ( bytes) . parse ( ) ?. try_as_component ( ) ?;
17+
18+ Ok ( Self { parsed } )
19+ }
20+ }
21+
22+ /// A parsed and compiled WASM module ready to be instantiated
23+ #[ derive( Debug ) ]
24+ pub struct Module {
25+ pub ( crate ) code : Arc < ModuleCode > ,
26+
27+ pub ( crate ) functions : Vec < Function > ,
28+ pub ( crate ) tables : Vec < TableDef > ,
29+ pub ( crate ) mems : Vec < MemoryType > ,
30+ pub ( crate ) element_segments : Vec < ElementSegment > ,
31+ pub ( crate ) globals : Vec < Global > ,
32+ pub ( crate ) data_segments : Vec < DataSegment > ,
33+ pub ( crate ) start : Option < u32 > ,
34+ pub ( crate ) import_declarations : Vec < ImportDeclaration > ,
35+ pub ( crate ) exports : Vec < Export > ,
36+ pub ( crate ) tags : Vec < Tag > ,
37+ }
38+
39+ impl Module {
40+ pub fn new ( bytes : & [ u8 ] ) -> Result < Self > {
41+ let module = Parser :: new ( bytes) . parse ( ) ?. try_as_module ( ) ?;
42+
43+ let code = compiler:: compile ( & module) ;
44+
45+ Ok ( Self {
46+ code : Arc :: new ( code) ,
47+
48+ functions : module. functions ,
49+ tables : module. tables ,
50+ mems : module. mems ,
51+ element_segments : module. element_segments ,
52+ globals : module. globals ,
53+ data_segments : module. data_segments ,
54+ start : module. start ,
55+ import_declarations : module. import_declarations ,
56+ exports : module. exports ,
57+ tags : module. tags ,
58+ } )
59+ }
60+
61+ pub fn from_parsed ( parsed : ParsedModule ) -> Result < Self > {
62+ let code = compiler:: compile ( & parsed) ;
63+ Ok ( Self {
64+ code : Arc :: new ( code) ,
65+ functions : parsed. functions ,
66+ tables : parsed. tables ,
67+ mems : parsed. mems ,
68+ element_segments : parsed. element_segments ,
69+ globals : parsed. globals ,
70+ data_segments : parsed. data_segments ,
71+ start : parsed. start ,
72+ import_declarations : parsed. import_declarations ,
73+ exports : parsed. exports ,
74+ tags : parsed. tags ,
75+ } )
76+ }
77+
78+ pub fn import_declarations ( & self ) -> & [ ImportDeclaration ] {
79+ & self . import_declarations
80+ }
81+
82+ pub fn types ( & self ) -> & [ SubType ] {
83+ & self . code . types
84+ }
85+ }
386
487// todo: should we box ParsedModule?
588#[ allow( clippy:: large_enum_variant) ]
@@ -9,19 +92,38 @@ pub enum Parsed {
992 Component ( ParsedComponent ) ,
1093}
1194
95+ impl Parsed {
96+ pub fn try_as_module ( self ) -> Result < ParsedModule > {
97+ let out = match self {
98+ Self :: Module ( m) => m,
99+ Self :: Component ( _) => parse_err ! ( "expected module" ) ,
100+ } ;
101+
102+ Ok ( out)
103+ }
104+
105+ pub fn try_as_component ( self ) -> Result < ParsedComponent > {
106+ let out = match self {
107+ Self :: Module ( _) => parse_err ! ( "expected component" ) ,
108+ Self :: Component ( c) => c,
109+ } ;
110+
111+ Ok ( out)
112+ }
113+ }
114+
12115#[ derive( Debug , Clone ) ]
13116pub struct ParsedComponent {
14117 pub sections : Vec < ComponentSection > ,
15118}
16119
17- #[ allow( clippy:: large_enum_variant) ]
18120#[ derive( Debug , Clone ) ]
19121pub enum ComponentSection {
20- CoreModule ( ParsedModule ) ,
122+ CoreModule ( Box < ParsedModule > ) ,
21123 CoreInstance ( Vec < CoreInstance > ) ,
22124 CoreType ( Vec < CoreType > ) ,
23125 Component ( ParsedComponent ) ,
24- Instance ( Vec < ComponentInstance > ) ,
126+ Instance ( Vec < ParsedComponentInstance > ) ,
25127 Alias ( Vec < Alias > ) ,
26128 ComponentType ( Vec < ComponentTypeDef > ) ,
27129 Canonical ( Vec < CanonicalDef > ) ,
@@ -283,7 +385,7 @@ pub enum Alias {
283385}
284386
285387#[ derive( Debug , Clone ) ]
286- pub enum ComponentInstance {
388+ pub enum ParsedComponentInstance {
287389 Instantiate {
288390 component_idx : u32 ,
289391 args : Vec < ComponentInstantiateArg > ,
@@ -445,115 +547,6 @@ pub struct ComponentExportDecl {
445547 pub desc : ExternDesc ,
446548}
447549
448- #[ derive( Debug , Clone , PartialEq ) ]
449- pub enum ComponentValue {
450- Bool ( bool ) ,
451- S8 ( i8 ) ,
452- U8 ( u8 ) ,
453- S16 ( i16 ) ,
454- U16 ( u16 ) ,
455- S32 ( i32 ) ,
456- U32 ( u32 ) ,
457- S64 ( i64 ) ,
458- U64 ( u64 ) ,
459- F32 ( f32 ) ,
460- F64 ( f64 ) ,
461- Char ( char ) ,
462- String ( String ) ,
463- List ( Vec < Self > ) ,
464- Record ( Vec < ( String , Self ) > ) ,
465- Tuple ( Vec < Self > ) ,
466- Variant ( String , Option < Box < Self > > ) ,
467- Enum ( String ) ,
468- Option ( Option < Box < Self > > ) ,
469- Result ( StdResult < Option < Box < Self > > , Option < Box < Self > > > ) ,
470- Flags ( Vec < String > ) ,
471- }
472-
473- impl From < bool > for ComponentValue {
474- fn from ( v : bool ) -> Self {
475- Self :: Bool ( v)
476- }
477- }
478-
479- impl From < i8 > for ComponentValue {
480- fn from ( v : i8 ) -> Self {
481- Self :: S8 ( v)
482- }
483- }
484-
485- impl From < u8 > for ComponentValue {
486- fn from ( v : u8 ) -> Self {
487- Self :: U8 ( v)
488- }
489- }
490-
491- impl From < i16 > for ComponentValue {
492- fn from ( v : i16 ) -> Self {
493- Self :: S16 ( v)
494- }
495- }
496-
497- impl From < u16 > for ComponentValue {
498- fn from ( v : u16 ) -> Self {
499- Self :: U16 ( v)
500- }
501- }
502-
503- impl From < i32 > for ComponentValue {
504- fn from ( v : i32 ) -> Self {
505- Self :: S32 ( v)
506- }
507- }
508-
509- impl From < u32 > for ComponentValue {
510- fn from ( v : u32 ) -> Self {
511- Self :: U32 ( v)
512- }
513- }
514-
515- impl From < i64 > for ComponentValue {
516- fn from ( v : i64 ) -> Self {
517- Self :: S64 ( v)
518- }
519- }
520-
521- impl From < u64 > for ComponentValue {
522- fn from ( v : u64 ) -> Self {
523- Self :: U64 ( v)
524- }
525- }
526-
527- impl From < f32 > for ComponentValue {
528- fn from ( v : f32 ) -> Self {
529- Self :: F32 ( v)
530- }
531- }
532-
533- impl From < f64 > for ComponentValue {
534- fn from ( v : f64 ) -> Self {
535- Self :: F64 ( v)
536- }
537- }
538-
539- impl From < char > for ComponentValue {
540- fn from ( v : char ) -> Self {
541- Self :: Char ( v)
542- }
543- }
544-
545- impl From < String > for ComponentValue {
546- fn from ( v : String ) -> Self {
547- Self :: String ( v)
548- }
549- }
550-
551- impl From < & str > for ComponentValue {
552- fn from ( v : & str ) -> Self {
553- Self :: String ( v. to_string ( ) )
554- }
555- }
556-
557550#[ derive( Debug , Default , Clone ) ]
558551pub struct ParsedModule {
559552 pub types : Vec < SubType > ,
0 commit comments