1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
// Copyright 2024 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
    metadata, new_core_section_cache, AstCustomization, IndexSpace, Section, SectionCache,
    SectionIndex, SectionType, Sections,
};
use mappable_rc::Mrc;
use std::fmt::{Debug, Formatter};

#[cfg(feature = "parser")]
pub mod parser;
#[cfg(feature = "writer")]
pub mod writer;

pub type DataIdx = u32;
pub type ElemIdx = u32;
pub type ExportIdx = u32;
pub type FuncIdx = u32;
pub type GlobalIdx = u32;
pub type LabelIdx = u32;
pub type LocalIdx = u32;
pub type MemIdx = u32;
pub type TableIdx = u32;
pub type TypeIdx = u32;

/// Trait to be implemented by custom Custom node types in order to provide information
/// for the metadata feature [Module::get_metadata].
pub trait RetainsCustomSection {
    fn name(&self) -> &str;
    fn data(&self) -> &[u8];
}

/// The core section nodes
///
/// See [Section] for more information.
#[derive(Debug, Clone, PartialEq)]
pub enum CoreSection<Ast: AstCustomization> {
    Type(FuncType),
    Func(FuncTypeRef),
    Code(FuncCode<Ast::Expr>),
    Table(Table),
    Mem(Mem),
    Global(Global),
    Elem(Elem<Ast::Expr>),
    Data(Ast::Data),
    DataCount(DataCount),
    Start(Start),
    Export(Export),
    Import(Import),
    Custom(Ast::Custom),
}

#[allow(unused)]
impl<Ast: AstCustomization> CoreSection<Ast> {
    pub fn as_type(&self) -> &FuncType {
        match self {
            CoreSection::Type(ty) => ty,
            _ => panic!("Expected type section, got {}", self.type_name()),
        }
    }

    pub fn as_func(&self) -> &FuncTypeRef {
        match self {
            CoreSection::Func(func) => func,
            _ => panic!("Expected func section, got {}", self.type_name()),
        }
    }

    pub fn as_code(&self) -> &FuncCode<Ast::Expr> {
        match self {
            CoreSection::Code(code) => code,
            _ => panic!("Expected code section, got {}", self.type_name()),
        }
    }

    pub fn as_table(&self) -> &Table {
        match self {
            CoreSection::Table(table) => table,
            _ => panic!("Expected table section, got {}", self.type_name()),
        }
    }

    pub fn as_mem(&self) -> &Mem {
        match self {
            CoreSection::Mem(mem) => mem,
            _ => panic!("Expected mem section, got {}", self.type_name()),
        }
    }

    pub fn as_global(&self) -> &Global {
        match self {
            CoreSection::Global(global) => global,
            _ => panic!("Expected global section, got {}", self.type_name()),
        }
    }

    pub fn as_elem(&self) -> &Elem<Ast::Expr> {
        match self {
            CoreSection::Elem(elem) => elem,
            _ => panic!("Expected elem section, got {}", self.type_name()),
        }
    }

    pub fn as_data(&self) -> &Ast::Data {
        match self {
            CoreSection::Data(data) => data,
            _ => panic!("Expected data section, got {}", self.type_name()),
        }
    }

    pub fn as_data_count(&self) -> &DataCount {
        match self {
            CoreSection::DataCount(data_count) => data_count,
            _ => panic!("Expected data count section, got {}", self.type_name()),
        }
    }

    pub fn as_start(&self) -> &Start {
        match self {
            CoreSection::Start(start) => start,
            _ => panic!("Expected start section, got {}", self.type_name()),
        }
    }

    pub fn as_export(&self) -> &Export {
        match self {
            CoreSection::Export(export) => export,
            _ => panic!("Expected export section, got {}", self.type_name()),
        }
    }

    pub fn as_import(&self) -> &Import {
        match self {
            CoreSection::Import(import) => import,
            _ => panic!("Expected import section, got {}", self.type_name()),
        }
    }

    pub fn as_custom(&self) -> &Ast::Custom {
        match self {
            CoreSection::Custom(custom) => custom,
            _ => panic!("Expected custom section, got {}", self.type_name()),
        }
    }

    pub fn type_name(&self) -> &'static str {
        match self {
            CoreSection::Type(_) => "type",
            CoreSection::Func(_) => "func",
            CoreSection::Code(_) => "code",
            CoreSection::Table(_) => "table",
            CoreSection::Mem(_) => "mem",
            CoreSection::Global(_) => "global",
            CoreSection::Elem(_) => "elem",
            CoreSection::Data(_) => "data",
            CoreSection::DataCount(_) => "data count",
            CoreSection::Start(_) => "start",
            CoreSection::Export(_) => "export",
            CoreSection::Import(_) => "import",
            CoreSection::Custom(_) => "custom",
        }
    }
}

impl<Ast: AstCustomization> Section<CoreIndexSpace, CoreSectionType> for CoreSection<Ast> {
    fn index_space(&self) -> CoreIndexSpace {
        match self {
            CoreSection::Type(inner) => inner.index_space(),
            CoreSection::Func(inner) => inner.index_space(),
            CoreSection::Code(inner) => inner.index_space(),
            CoreSection::Table(inner) => inner.index_space(),
            CoreSection::Mem(inner) => inner.index_space(),
            CoreSection::Global(inner) => inner.index_space(),
            CoreSection::Elem(inner) => inner.index_space(),
            CoreSection::Data(inner) => inner.index_space(),
            CoreSection::DataCount(inner) => inner.index_space(),
            CoreSection::Start(inner) => inner.index_space(),
            CoreSection::Export(inner) => inner.index_space(),
            CoreSection::Import(inner) => inner.index_space(),
            CoreSection::Custom(inner) => inner.index_space(),
        }
    }

    fn section_type(&self) -> CoreSectionType {
        match self {
            CoreSection::Type(inner) => inner.section_type(),
            CoreSection::Func(inner) => inner.section_type(),
            CoreSection::Code(inner) => inner.section_type(),
            CoreSection::Table(inner) => inner.section_type(),
            CoreSection::Mem(inner) => inner.section_type(),
            CoreSection::Global(inner) => inner.section_type(),
            CoreSection::Elem(inner) => inner.section_type(),
            CoreSection::Data(inner) => inner.section_type(),
            CoreSection::DataCount(inner) => inner.section_type(),
            CoreSection::Start(inner) => inner.section_type(),
            CoreSection::Export(inner) => inner.section_type(),
            CoreSection::Import(inner) => inner.section_type(),
            CoreSection::Custom(inner) => inner.section_type(),
        }
    }
}

/// The core section types
///
/// See [SectionType] for more information.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum CoreSectionType {
    Type,
    Func,
    Code,
    Table,
    Mem,
    Global,
    Elem,
    Data,
    DataCount,
    Start,
    Export,
    Import,
    Custom,
}

impl SectionType for CoreSectionType {
    fn allow_grouping(&self) -> bool {
        match self {
            CoreSectionType::Type => true,
            CoreSectionType::Func => true,
            CoreSectionType::Code => true,
            CoreSectionType::Table => true,
            CoreSectionType::Mem => true,
            CoreSectionType::Global => true,
            CoreSectionType::Elem => true,
            CoreSectionType::Data => true,
            CoreSectionType::DataCount => false,
            CoreSectionType::Start => false,
            CoreSectionType::Export => true,
            CoreSectionType::Import => true,
            CoreSectionType::Custom => false,
        }
    }
}

/// The core index space
///
/// See [IndexSpace] for more information.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum CoreIndexSpace {
    Type,
    Func,
    Table,
    Mem,
    Global,
    Elem,
    Data,
    Local,
    Label,
    Code,
    Export,
    Start,
    Custom,
}

impl IndexSpace for CoreIndexSpace {
    type Index = u32;
}

/// Number types classify numeric values.
///
/// The types i32 and i64 classify 32 and 64 bit integers, respectively. Integers are not inherently signed or unsigned,
/// their interpretation is determined by individual operations.
///
/// The types f32 and f64 classify 32 and 64 bit floating-point data, respectively. They correspond to the respective
/// binary floating-point representations, also known as single and double precision, as defined by the IEEE 754
/// standard (Section 3.3).
///
/// Number types are transparent, meaning that their bit patterns can be observed. Values of number type can be stored
/// in memories.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NumType {
    I32,
    I64,
    F32,
    F64,
}

/// Vector types classify vectors of numeric values processed by vector instructions (also known as SIMD instructions,
/// single instruction multiple data).
///
/// The type v128 corresponds to a 128 bit vector of packed integer or floating-point data. The packed data can be
/// interpreted as signed or unsigned integers, single or double precision floating-point values, or a single 128 bit
/// type. The interpretation is determined by individual operations.
///
/// Vector types, like number types are transparent, meaning that their bit patterns can be observed. Values of vector
/// type can be stored in memories.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VecType {
    V128,
}

/// Reference types classify first-class references to objects in the runtime store.
///
/// The type funcref denotes the infinite union of all references to functions, regardless of their function types.
///
/// The type externref denotes the infinite union of all references to objects owned by the embedder and that can be
/// passed into WebAssembly under this type.
///
/// Reference types are opaque, meaning that neither their size nor their bit pattern can be observed. Values of
/// reference type can be stored in tables.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RefType {
    FuncRef,
    ExternRef,
}

/// Value types classify the individual values that WebAssembly code can compute with and the values that a variable
/// accepts. They are either number types, vector types, or reference types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValType {
    Num(NumType),
    Vec(VecType),
    Ref(RefType),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NumOrVecType {
    Num(NumType),
    Vec(VecType),
}

/// Result types classify the result of executing instructions or functions, which is a sequence of values, written with
/// brackets.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResultType {
    pub values: Vec<ValType>,
}

/// Function types classify the signature of functions, mapping a vector of parameters to a vector of results. They are
/// also used to classify the inputs and outputs of instructions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FuncType {
    pub input: ResultType,
    pub output: ResultType,
}

impl Section<CoreIndexSpace, CoreSectionType> for FuncType {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Type
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Type
    }
}

/// Limits classify the size range of resizeable storage associated with memory types and table types.
///
/// If no maximum is given, the respective storage can grow to any size.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Limits {
    pub min: u64,
    pub max: Option<u64>,
}

/// Memory types classify linear memories and their size range.
///
/// The limits constrain the minimum and optionally the maximum size of a memory. The limits are given in units of page
/// size.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemType {
    pub limits: Limits,
}

/// Table types classify tables over elements of reference type within a size range.
///
/// Like memories, tables are constrained by limits for their minimum and optionally maximum size. The limits are given
/// in numbers of entries.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableType {
    pub limits: Limits,
    pub elements: RefType,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mut {
    Const,
    Var,
}

/// Global types classify global variables, which hold a value and can either be mutable or immutable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GlobalType {
    pub mutability: Mut,
    pub val_type: ValType,
}

/// External types classify imports and external values with their respective types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExternType {
    Func(FuncType),
    Table(TableType),
    Mem(MemType),
    Global(GlobalType),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FuncTypeRef {
    pub type_idx: TypeIdx,
}

impl Section<CoreIndexSpace, CoreSectionType> for FuncTypeRef {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Func
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Func
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FuncCode<Expr> {
    pub locals: Vec<ValType>,
    pub body: Expr,
}

impl<Expr: Debug + Clone + PartialEq> Section<CoreIndexSpace, CoreSectionType> for FuncCode<Expr> {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Func
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Code
    }
}

/// The funcs component of a module defines a vector of functions with the following structure.
///
/// Functions are referenced through function indices, starting with the smallest index not referencing a function
/// import.
///
/// `typ` is the type of a function declares its signature by reference to a type defined in the module. The parameters of the
/// function are referenced through 0-based local indices in the function’s body; they are mutable.
///
/// The `locals` declare a vector of mutable local variables and their types. These variables are referenced through
/// local indices in the function’s body. The index of the first local is the smallest index not referencing a
/// parameter.
///
/// The `body` is an instruction sequence that upon termination must produce a stack matching the function type’s result
/// type.
/// /
#[derive(Debug, Clone, PartialEq)]
pub struct Func<Expr: 'static> {
    pub type_idx: TypeIdx,
    code: Mrc<FuncCode<Expr>>,
}

impl<Expr: 'static> Func<Expr> {
    pub fn locals(&self) -> Mrc<Vec<ValType>> {
        Mrc::map(self.code.clone(), |code| &code.locals)
    }

    pub fn body(&self) -> Mrc<Expr> {
        Mrc::map(self.code.clone(), |code| &code.body)
    }
}

/// The tables component of a module defines a vector of tables described by their table type:
///
/// A table is a vector of opaque values of a particular reference type. The size in the limits of the table type
/// specifies the initial size of that table, while its max, if present, restricts the size to which it can grow later.
///
/// Tables can be initialized through element segments.
///
/// Tables are referenced through table indices, starting with the smallest index not referencing a table import. Most
/// constructs implicitly reference table index 0.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Table {
    pub table_type: TableType,
}

impl Section<CoreIndexSpace, CoreSectionType> for Table {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Table
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Table
    }
}

/// The mems component of a module defines a vector of linear memories (or memories for short) as described by their
/// memory type:
///
/// A memory is a vector of raw uninterpreted bytes. The size in the limits of the memory type specifies the initial
/// size of that memory, while its max, if present, restricts the size to which it can grow later. Both are in units of
/// page size.
///
/// Memories can be initialized through data segments.
///
/// Memories are referenced through memory indices, starting with the smallest index not referencing a memory import.
/// Most constructs implicitly reference memory index 0.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Mem {
    pub mem_type: MemType,
}

impl Section<CoreIndexSpace, CoreSectionType> for Mem {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Mem
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Mem
    }
}

/// The globals component of a module defines a vector of global variables (or globals for short):
///
/// Each global stores a single value of the given global type. Its type also specifies whether a global is immutable or
/// mutable. Moreover, each global is initialized with an value given by a constant initializer expression.
///
/// Globals are referenced through global indices, starting with the smallest index not referencing a global import.
#[derive(Debug, Clone, PartialEq)]
pub struct Global {
    pub global_type: GlobalType,
    pub init: Expr,
}

impl Section<CoreIndexSpace, CoreSectionType> for Global {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Global
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Global
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ElemMode {
    Passive,
    Active { table_idx: TableIdx, offset: Expr },
    Declarative,
}

/// The initial contents of a table is uninitialized. Element segments can be used to initialize a subrange of a table
/// from a static vector of elements.
///
/// The elems component of a module defines a vector of element segments. Each element segment defines a reference type
/// and a corresponding list of constant element expressions.
///
/// Element segments have a mode that identifies them as either passive, active, or declarative. A passive element
/// segment’s elements can be copied to a table using the table.init instruction. An active element segment copies its
/// elements into a table during instantiation, as specified by a table index and a constant expression defining an
/// offset into that table. A declarative element segment is not available at runtime but merely serves to
/// forward-declare references that are formed in code with instructions like ref.func.
///
/// Element segments are referenced through element indices.
///
#[derive(Debug, Clone, PartialEq)]
pub struct Elem<Expr> {
    pub ref_type: RefType,
    pub init: Vec<Expr>,
    pub mode: ElemMode,
}

impl<Expr: Debug + Clone + PartialEq> Section<CoreIndexSpace, CoreSectionType> for Elem<Expr> {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Elem
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Elem
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum DataMode<Expr> {
    Passive,
    Active { memory: MemIdx, offset: Expr },
}

/// The initial contents of a memory are zero bytes. Data segments can be used to initialize a range of memory from a
/// static vector of bytes.
///
/// The datas component of a module defines a vector of data segments.
///
/// Like element segments, data segments have a mode that identifies them as either passive or active. A passive data
/// segment’s contents can be copied into a memory using the memory.init instruction. An active data segment copies its
/// contents into a memory during instantiation, as specified by a memory index and a constant expression defining an
/// offset into that memory.
///
/// Data segments are referenced through data indices.
///
#[derive(Debug, Clone, PartialEq)]
pub struct Data<Expr: Clone> {
    init: Vec<u8>,
    mode: DataMode<Expr>,
}

impl<Expr: Debug + Clone + PartialEq> Section<CoreIndexSpace, CoreSectionType> for Data<Expr> {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Data
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Data
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataCount {
    pub count: u32,
}

impl Section<CoreIndexSpace, CoreSectionType> for DataCount {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Data
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::DataCount
    }
}

/// The start component of a module declares the function index of a start function that is automatically invoked when
/// the module is instantiated, after tables and memories have been initialized.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Start {
    pub func: FuncIdx,
}

impl Section<CoreIndexSpace, CoreSectionType> for Start {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Start
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Start
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExportDesc {
    Func(FuncIdx),
    Table(TableIdx),
    Mem(MemIdx),
    Global(GlobalIdx),
}

/// The exports component of a module defines a set of exports that become accessible to the host environment once the
/// module has been instantiated.
///
/// Each export is labeled by a unique name. Exportable definitions are functions, tables, memories, and globals, which
/// are referenced through a respective descriptor.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Export {
    pub name: String,
    pub desc: ExportDesc,
}

impl Section<CoreIndexSpace, CoreSectionType> for Export {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Export
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Export
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeRef {
    Func(TypeIdx),
    Table(TableType),
    Mem(MemType),
    Global(GlobalType),
}

/// The imports component of a module defines a set of imports that are required for instantiation.
///
/// Each import is labeled by a two-level name space, consisting of a module name and a name for an entity within that
/// module. Importable definitions are functions, tables, memories, and globals. Each import is specified by a
/// descriptor with a respective type that a definition provided during instantiation is required to match.
///
/// Every import defines an index in the respective index space. In each index space, the indices of imports go before
/// the first index of any definition contained in the module itself.
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Import {
    pub module: String,
    pub name: String,
    pub desc: TypeRef,
}

impl Section<CoreIndexSpace, CoreSectionType> for Import {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Func
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Import
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Custom {
    pub name: String,
    pub data: Vec<u8>,
}

impl RetainsCustomSection for Custom {
    fn name(&self) -> &str {
        &self.name
    }

    fn data(&self) -> &[u8] {
        &self.data
    }
}

impl Section<CoreIndexSpace, CoreSectionType> for Custom {
    fn index_space(&self) -> CoreIndexSpace {
        CoreIndexSpace::Custom
    }

    fn section_type(&self) -> CoreSectionType {
        CoreSectionType::Custom
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Expr {
    pub instrs: Vec<Instr>,
}

pub trait ExprSource: IntoIterator<Item = Result<Instr, String>> {
    fn unparsed(self) -> Result<Vec<u8>, String>;
}

pub trait RetainsInstructions {
    fn instructions(&self) -> &[Instr];
}

pub trait TryFromExprSource {
    fn try_from<S: ExprSource>(value: S) -> Result<Self, String>
    where
        Self: Sized;
}

impl TryFromExprSource for Expr {
    fn try_from<S: ExprSource>(value: S) -> Result<Self, String> {
        let instrs = value.into_iter().collect::<Result<Vec<Instr>, String>>()?;
        Ok(Self { instrs })
    }
}

impl RetainsInstructions for Expr {
    fn instructions(&self) -> &[Instr] {
        &self.instrs
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IntWidth {
    I32,
    I64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FloatWidth {
    F32,
    F64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Signedness {
    Signed,
    Unsigned,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IShape {
    I8x16,
    I16x8,
    I32x4,
    I64x2,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FShape {
    F32x4,
    F64x2,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Shape {
    Int(IShape),
    Float(FShape),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Half {
    Low,
    High,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemArg {
    pub align: u8,
    pub offset: u32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VectorLoadShape {
    WW8,
    WW16,
    WW32,
    WW64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockType {
    None,
    Index(TypeIdx),
    Value(ValType),
}

pub type LaneIdx = u8;

#[derive(Debug, Clone, PartialEq)]
pub enum Instr {
    // NumericInstr
    I32Const(i32),
    I64Const(i64),
    F32Const(f32),
    F64Const(f64),

    // ITestOp
    IEqz(IntWidth),

    // IRelOp
    IEq(IntWidth),
    INe(IntWidth),
    ILt(IntWidth, Signedness),
    IGt(IntWidth, Signedness),
    ILe(IntWidth, Signedness),
    IGe(IntWidth, Signedness),

    // FRelOp
    FEq(FloatWidth),
    FNe(FloatWidth),
    FLt(FloatWidth),
    FGt(FloatWidth),
    FLe(FloatWidth),
    FGe(FloatWidth),

    // IUnOp
    IClz(IntWidth),
    ICtz(IntWidth),
    IPopCnt(IntWidth),

    // IBinOp
    IAdd(IntWidth),
    ISub(IntWidth),
    IMul(IntWidth),
    IDiv(IntWidth, Signedness),
    IRem(IntWidth, Signedness),
    IAnd(IntWidth),
    IOr(IntWidth),
    IXor(IntWidth),
    IShl(IntWidth),
    IShr(IntWidth, Signedness),
    IRotL(IntWidth),
    IRotR(IntWidth),

    // FUnOp
    FAbs(FloatWidth),
    FNeg(FloatWidth),
    FCeil(FloatWidth),
    FFloor(FloatWidth),
    FTrunc(FloatWidth),
    FNearest(FloatWidth),

    // FBinOp
    FSqrt(FloatWidth),
    FAdd(FloatWidth),
    FSub(FloatWidth),
    FMul(FloatWidth),
    FDiv(FloatWidth),
    FMin(FloatWidth),
    FMax(FloatWidth),
    FCopySign(FloatWidth),

    I32WrapI64,

    ITruncF(IntWidth, FloatWidth, Signedness),

    I64ExtendI32(Signedness),
    I64Extend32S,
    IExtend8S(IntWidth),
    IExtend16S(IntWidth),

    FConvertI(FloatWidth, IntWidth, Signedness),

    F32DemoteF64,
    F64PromoteF32,

    IReinterpretF(IntWidth),
    FReinterpretI(FloatWidth),

    ITruncSatF(IntWidth, FloatWidth, Signedness),

    // VectorInstr
    V128Const(i128),

    // VVUnOp
    V128Not,

    // VVBinOp
    V128And,
    V128AndNot,
    V128Or,
    V128XOr,

    // VVTernOp
    V128BitSelect,

    // VVTestOp
    V128AnyTrue,

    VI8x16Shuffle([LaneIdx; 16]),

    VI18x16Swizzle,
    VSplat(Shape),
    VI8x16ExtractLane(Signedness, LaneIdx),
    VI16x8ExtractLane(Signedness, LaneIdx),
    VI32x4ExtractLane(LaneIdx),
    VI64x2ExtractLane(LaneIdx),
    VFExtractLane(FShape, LaneIdx),
    VReplaceLane(Shape, LaneIdx),

    // VIRelOp
    VIEq(IShape),
    VINe(IShape),
    VILt(IShape, Signedness),
    VIGt(IShape, Signedness),
    VILe(IShape, Signedness),
    VIGe(IShape, Signedness),
    VI64x2Lt,
    VI64x2Gt,
    VI64x2Le,
    VI64x2Ge,

    // VFRelOp
    VFEq(FShape),
    VFNe(FShape),
    VFLt(FShape),
    VFGt(FShape),
    VFLe(FShape),
    VFGe(FShape),

    // VIUnOp
    VIAbs(IShape),
    VINeg(IShape),

    VI8x16PopCnt,
    VI16x8Q15MulrSat,
    VI32x4DotI16x8,

    // VFUnOp
    VFAbs(FShape),
    VFNeg(FShape),
    VFSqrt(FShape),
    VFCeil(FShape),
    VFFloor(FShape),
    VFTrunc(FShape),
    VFNearest(FShape),

    // VITestOp
    VIAllTrue(IShape),

    VIBitMask(IShape),

    VI8x16NarrowI16x8(Signedness),
    VI16x8NarrowI32x4(Signedness),

    VI16x8ExtendI8x16(Half, Signedness),
    VI32x4ExtendI16x8(Half, Signedness),
    VI64x2ExtendI32x4(Half, Signedness),

    // VIShiftOp
    VIShl(IShape),
    VIShr(IShape, Signedness),

    // VIBinOp
    VIAdd(IShape),
    VISub(IShape),

    // VIMinMaxOp
    VIMin(IShape, Signedness),
    VIMax(IShape, Signedness),

    // VISatBinOp
    VIAddSat(IShape, Signedness),
    VISubSat(IShape, Signedness),

    VIMul(IShape),
    VIAvgr(IShape),
    VIExtMul(IShape, Half, Signedness),
    VIExtAddPairwise(IShape, Signedness),

    // VFBinOp
    VFAdd(FShape),
    VFSub(FShape),
    VFMul(FShape),
    VFDiv(FShape),
    VFMin(FShape),
    VFMax(FShape),
    VFPMin(FShape),
    VFPMax(FShape),

    VI32x4TruncSatF32x4(Signedness),
    VI32x4TruncSatF64x2Zero(Signedness),
    VI32x4ConvertI32x4(Signedness),
    VF32x4DemoteF64x2Zero,
    VF64x2ConvertLowI32x4(Signedness),
    VF64x2PromoteLowI32x4,

    // ReferenceInstr
    RefNull(RefType),
    RefIsNull,
    RefFunc(FuncIdx),

    // ParametricInstr
    Drop,
    Select(Option<Vec<ValType>>),

    // VariableInstr
    LocalGet(LocalIdx),
    LocalSet(LocalIdx),
    LocalTee(LocalIdx),
    GlobalGet(GlobalIdx),
    GlobalSet(GlobalIdx),

    // TableInstr
    TableGet(TableIdx),
    TableSet(TableIdx),
    TableSize(TableIdx),
    TableGrow(TableIdx),
    TableFill(TableIdx),
    TableCopy {
        source: TableIdx,
        destination: TableIdx,
    },
    TableInit(TableIdx, ElemIdx),
    ElemDrop(ElemIdx),

    // MemoryInstr
    Load(NumOrVecType, MemArg),
    Store(NumOrVecType, MemArg),
    Load8(NumType, Signedness, MemArg),
    Load16(NumType, Signedness, MemArg),
    Load32(Signedness, MemArg),
    Store8(NumType, MemArg),
    Store16(NumType, MemArg),
    Store32(MemArg),
    V128Load8x8(Signedness, MemArg),
    V128Load16x4(Signedness, MemArg),
    V128Load32x2(Signedness, MemArg),
    V128Load32Zero(MemArg),
    V128Load64Zero(MemArg),
    V128LoadSplat(VectorLoadShape, MemArg),
    V128LoadLane(VectorLoadShape, MemArg, LaneIdx),
    V128StoreLane(VectorLoadShape, MemArg, LaneIdx),
    MemorySize,
    MemoryGrow,
    MemoryFill,
    MemoryCopy,
    MemoryInit(DataIdx),
    DataDrop(DataIdx),

    // ControlInstr
    Nop,
    Unreachable,
    Block(BlockType, Vec<Instr>),
    Loop(BlockType, Vec<Instr>),
    If(BlockType, Vec<Instr>, Vec<Instr>),
    Br(LabelIdx),
    BrIf(LabelIdx),
    BrTable(Vec<LabelIdx>, LabelIdx),
    Return,
    Call(FuncIdx),
    CallIndirect(TableIdx, TypeIdx),
}

#[derive(Debug, Clone)]
pub enum ImportOrFunc<Expr: 'static> {
    Import(Mrc<Import>),
    Func(Func<Expr>),
}

type CoreSectionCache<T, Ast> = SectionCache<T, CoreIndexSpace, CoreSectionType, CoreSection<Ast>>;
type CoreSectionIndex<Ast> = SectionIndex<CoreIndexSpace, CoreSectionType, CoreSection<Ast>>;

/// The top-level AST node representing a core WASM module
///
/// Some parts of the AST are customizable by the `Ast` type parameter. See [AstCustomization] for more details.
pub struct Module<Ast: AstCustomization + 'static> {
    sections: Sections<CoreIndexSpace, CoreSectionType, CoreSection<Ast>>,

    types: CoreSectionCache<FuncType, Ast>,
    func_type_refs: CoreSectionCache<FuncTypeRef, Ast>,
    codes: CoreSectionCache<FuncCode<Ast::Expr>, Ast>,
    tables: CoreSectionCache<Table, Ast>,
    mems: CoreSectionCache<Mem, Ast>,
    globals: CoreSectionCache<Global, Ast>,
    elems: CoreSectionCache<Elem<Ast::Expr>, Ast>,
    datas: CoreSectionCache<Ast::Data, Ast>,
    start: CoreSectionCache<Start, Ast>,
    imports: CoreSectionCache<Import, Ast>,
    exports: CoreSectionCache<Export, Ast>,
    customs: CoreSectionCache<Ast::Custom, Ast>,

    type_index: CoreSectionIndex<Ast>,
    func_index: CoreSectionIndex<Ast>,
    code_index: CoreSectionIndex<Ast>,
    table_index: CoreSectionIndex<Ast>,
    mem_index: CoreSectionIndex<Ast>,
    global_index: CoreSectionIndex<Ast>,
    elem_index: CoreSectionIndex<Ast>,
    data_index: CoreSectionIndex<Ast>,
    export_index: CoreSectionIndex<Ast>,
}

#[cfg(feature = "parser")]
impl<Ast> Module<Ast>
where
    Ast: AstCustomization + 'static,
    Ast::Expr: TryFromExprSource,
    Ast::Data: From<Data<Ast::Expr>>,
    Ast::Custom: From<Custom>,
{
    /// Parses a module from a binary WASM byte array
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, String> {
        let parser = wasmparser::Parser::new(0);
        Self::try_from((parser, bytes))
    }
}

#[cfg(feature = "writer")]
impl<Ast> Module<Ast>
where
    Ast: AstCustomization + 'static,
    Ast::Expr: RetainsInstructions,
    Ast::Data: Into<Data<Ast::Expr>>,
    Ast::Custom: Into<Custom>,
{
    /// Serializes the module into a binary WASM byte array
    pub fn into_bytes(self) -> Result<Vec<u8>, String> {
        let encoder: wasm_encoder::Module = self.try_into()?;
        Ok(encoder.finish())
    }
}

impl<Ast: AstCustomization> Module<Ast> {
    /// Creates an empty module
    pub fn empty() -> Self {
        Self::new(Sections::new())
    }

    pub(crate) fn new(
        sections: Sections<CoreIndexSpace, CoreSectionType, CoreSection<Ast>>,
    ) -> Self {
        Self {
            sections,
            types: new_core_section_cache!(Type),
            func_type_refs: new_core_section_cache!(Func),
            codes: new_core_section_cache!(Code),
            tables: new_core_section_cache!(Table),
            mems: new_core_section_cache!(Mem),
            globals: new_core_section_cache!(Global),
            elems: new_core_section_cache!(Elem),
            datas: new_core_section_cache!(Data),
            start: new_core_section_cache!(Start),
            imports: new_core_section_cache!(Import),
            exports: new_core_section_cache!(Export),
            customs: new_core_section_cache!(Custom),
            type_index: SectionIndex::new(CoreIndexSpace::Type),
            func_index: SectionIndex::new(CoreIndexSpace::Func),
            code_index: SectionIndex::new(CoreIndexSpace::Code),
            table_index: SectionIndex::new(CoreIndexSpace::Table),
            mem_index: SectionIndex::new(CoreIndexSpace::Mem),
            global_index: SectionIndex::new(CoreIndexSpace::Global),
            elem_index: SectionIndex::new(CoreIndexSpace::Elem),
            data_index: SectionIndex::new(CoreIndexSpace::Data),
            export_index: SectionIndex::new(CoreIndexSpace::Export),
        }
    }

    /// Gets all the function types in the module
    pub fn types(&self) -> Vec<Mrc<FuncType>> {
        self.types.populate(&self.sections);
        self.types.all()
    }

    /// Gets all the function type references in the module
    ///
    /// A more useful function is [funcs] that combines this and [codes] together.
    pub fn func_type_refs(&self) -> Vec<Mrc<FuncTypeRef>> {
        self.func_type_refs.populate(&self.sections);
        self.func_type_refs.all()
    }

    /// Gets all the function codes in the module.
    ///
    /// A more useful function is [funcs] that combines this and [codes] together.
    pub fn codes(&self) -> Vec<Mrc<FuncCode<Ast::Expr>>> {
        self.codes.populate(&self.sections);
        self.codes.all()
    }

    /// Gets all the functions defined in the module
    pub fn funcs(&self) -> Vec<Func<Ast::Expr>> {
        self.func_type_refs()
            .into_iter()
            .zip(self.codes())
            .map(|(func_type, code)| Func {
                type_idx: func_type.type_idx,
                code,
            })
            .collect()
    }

    /// Gets all the tables defined in the module
    pub fn tables(&self) -> Vec<Mrc<Table>> {
        self.tables.populate(&self.sections);
        self.tables.all()
    }

    /// Gets all the memories defined in the module
    pub fn mems(&self) -> Vec<Mrc<Mem>> {
        self.mems.populate(&self.sections);
        self.mems.all()
    }

    /// Gets all the globals defined in the module
    pub fn globals(&self) -> Vec<Mrc<Global>> {
        self.globals.populate(&self.sections);
        self.globals.all()
    }

    /// Gets all the elems defined in the module
    pub fn elems(&self) -> Vec<Mrc<Elem<Ast::Expr>>> {
        self.elems.populate(&self.sections);
        self.elems.all()
    }

    /// Gets all the data sections defined in the module
    pub fn datas(&self) -> Vec<Mrc<Ast::Data>> {
        self.datas.populate(&self.sections);
        self.datas.all()
    }

    /// Gets the start section of the module
    pub fn start(&self) -> Option<Mrc<Start>> {
        self.start.populate(&self.sections);
        self.start.all().pop()
    }

    /// Gets all the imports of the module
    pub fn imports(&self) -> Vec<Mrc<Import>> {
        self.imports.populate(&self.sections);
        self.imports.all()
    }

    /// Gets all the exports of the module
    pub fn exports(&self) -> Vec<Mrc<Export>> {
        self.exports.populate(&self.sections);
        self.exports.all()
    }

    /// Gets all the custom sections of the module
    pub fn customs(&self) -> Vec<Mrc<Ast::Custom>> {
        self.customs.populate(&self.sections);
        self.customs.all()
    }

    /// Adds a new data section
    pub fn add_data(&mut self, data: Ast::Data) {
        self.datas.invalidate();
        self.data_index.invalidate();
        self.sections.add_to_last_group(CoreSection::Data(data));
        self.datas.populate(&self.sections);
        let count = self.datas.count();
        self.sections.clear_group(&CoreSectionType::DataCount);
        self.sections
            .add_to_last_group(CoreSection::DataCount(DataCount {
                count: (count + 1) as u32,
            }));
    }

    /// Adds a new elem
    pub fn add_elem(&mut self, elem: Elem<Ast::Expr>) {
        self.elems.invalidate();
        self.elem_index.invalidate();
        self.sections.add_to_last_group(CoreSection::Elem(elem));
    }

    /// Adds a new export
    pub fn add_export(&mut self, export: Export) {
        self.exports.invalidate();
        self.export_index.invalidate();
        self.sections.add_to_last_group(CoreSection::Export(export));
    }

    /// Adds a new function
    pub fn add_function(
        &mut self,
        func_type: FuncType,
        locals: Vec<ValType>,
        body: Ast::Expr,
    ) -> FuncIdx {
        let existing_type_idx = self.type_idx_of(&func_type);
        let type_idx = match existing_type_idx {
            Some(idx) => idx as TypeIdx,
            None => {
                let idx = self.types.count() as TypeIdx;
                self.types.invalidate();
                self.type_index.invalidate();
                self.sections
                    .add_to_last_group(CoreSection::Type(func_type));
                idx
            }
        };
        let func_type_ref = FuncTypeRef { type_idx };
        let func_code = FuncCode { locals, body };
        self.codes.invalidate();
        self.code_index.invalidate();
        self.func_type_refs.invalidate();
        self.func_index.invalidate();
        self.sections
            .add_to_last_group(CoreSection::Func(func_type_ref));
        self.sections
            .add_to_last_group(CoreSection::Code(func_code));
        self.func_type_refs.populate(&self.sections);
        (self.func_type_refs.count() - 1) as FuncIdx
    }

    /// Adds a new global
    pub fn add_global(&mut self, global: Global) {
        self.globals.invalidate();
        self.global_index.invalidate();
        self.sections.add_to_last_group(CoreSection::Global(global));
    }

    /// Adds a new memory
    pub fn add_memory(&mut self, mem: Mem) {
        self.mems.invalidate();
        self.mem_index.invalidate();
        self.sections.add_to_last_group(CoreSection::Mem(mem));
    }

    /// Adds a new table
    pub fn add_table(&mut self, table: Table) {
        self.tables.invalidate();
        self.sections.add_to_last_group(CoreSection::Table(table));
    }

    /// Adds a new function type
    pub fn add_type(&mut self, func_type: FuncType) {
        self.types.invalidate();
        self.type_index.invalidate();
        self.sections
            .add_to_last_group(CoreSection::Type(func_type));
    }

    /// Gets a function body by its index
    pub fn get_code(&mut self, func_idx: FuncIdx) -> Option<Mrc<FuncCode<Ast::Expr>>> {
        self.code_index.populate(&self.sections);
        match self.code_index.get(&func_idx) {
            Some(section) => match &*section {
                CoreSection::Code(_) => Some(Mrc::map(section, |section| section.as_code())),
                _ => None,
            },
            None => None,
        }
    }

    /// Gets a data section by its index
    pub fn get_data(&mut self, data_idx: DataIdx) -> Option<Mrc<Ast::Data>> {
        self.data_index.populate(&self.sections);
        match self.data_index.get(&data_idx) {
            Some(section) => match &*section {
                CoreSection::Data(_) => Some(Mrc::map(section, |section| section.as_data())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets an elem by its index
    pub fn get_elem(&mut self, elem_idx: ElemIdx) -> Option<Mrc<Elem<Ast::Expr>>> {
        self.elem_index.populate(&self.sections);
        match self.elem_index.get(&elem_idx) {
            Some(section) => match &*section {
                CoreSection::Elem(_) => Some(Mrc::map(section, |section| section.as_elem())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets an export by its index
    pub fn get_export(&mut self, export_idx: ExportIdx) -> Option<Mrc<Export>> {
        self.export_index.populate(&self.sections);
        match self.export_index.get(&export_idx) {
            Some(section) => match &*section {
                CoreSection::Export(_) => Some(Mrc::map(section, |section| section.as_export())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets a function by its index
    ///
    /// In a core WASM module the function index space holds both defined functions and imported functions.
    pub fn get_function(&mut self, func_idx: FuncIdx) -> Option<ImportOrFunc<Ast::Expr>> {
        self.func_index.populate(&self.sections);
        match self.func_index.get(&func_idx) {
            Some(section) => match &*section {
                CoreSection::Func(_) => {
                    let code = self.get_code(func_idx).unwrap();
                    let func_type_ref = section.as_func();
                    let func = Func {
                        type_idx: func_type_ref.type_idx,
                        code,
                    };
                    Some(ImportOrFunc::Func(func))
                }
                CoreSection::Import(_) => {
                    Some(ImportOrFunc::Import(Mrc::map(section, |section| {
                        section.as_import()
                    })))
                }
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets a global by its index
    pub fn get_global(&mut self, global_idx: GlobalIdx) -> Option<Mrc<Global>> {
        self.global_index.populate(&self.sections);
        match self.global_index.get(&global_idx) {
            Some(section) => match &*section {
                CoreSection::Global(_) => Some(Mrc::map(section, |section| section.as_global())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets a memory by its index
    pub fn get_memory(&mut self, mem_idx: MemIdx) -> Option<Mrc<Mem>> {
        self.mem_index.populate(&self.sections);
        match self.mem_index.get(&mem_idx) {
            Some(section) => match &*section {
                CoreSection::Mem(_) => Some(Mrc::map(section, |section| section.as_mem())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Gets a table by its index
    pub fn get_table(&mut self, table_idx: TableIdx) -> Option<Mrc<Table>> {
        self.table_index.populate(&self.sections);
        match self.table_index.get(&table_idx) {
            Some(section) => match &*section {
                CoreSection::Table(_) => Some(Mrc::map(section, |section| section.as_table())),
                _ => None,
            },
            _ => None,
        }
    }

    /// Checks whether a given function type is already defined in the module, and returs its type index if so.
    pub fn type_idx_of(&self, func_type: &FuncType) -> Option<TypeIdx> {
        self.types.populate(&self.sections);
        self.types
            .all()
            .into_iter()
            .position(|ft| *ft == *func_type)
            .map(|idx| idx as TypeIdx)
    }

    /// Converts the module into a sequence of sections
    pub fn into_sections(mut self) -> Vec<Mrc<CoreSection<Ast>>> {
        self.sections.take_all()
    }

    /// Converts the module into a grouped sequence of sections, exactly as it should be written to a binary WASM file
    pub fn into_grouped(self) -> Vec<(CoreSectionType, Vec<Mrc<CoreSection<Ast>>>)> {
        self.sections.into_grouped()
    }
}

impl<Ast> Module<Ast>
where
    Ast: AstCustomization,
    Ast::Custom: RetainsCustomSection,
{
    /// Gets all the metadata supported by the `wasm-metadata` crate defined in this module's custom sections
    #[cfg(feature = "metadata")]
    pub fn get_metadata(&self) -> Option<metadata::Metadata> {
        let mut producers = None;
        let mut registry_metadata = None;
        let mut name = None;

        for custom in self.customs() {
            if custom.name() == "producers" {
                producers = wasm_metadata::Producers::from_bytes(custom.data(), 0).ok();
            } else if custom.name() == "registry-metadata" {
                registry_metadata =
                    wasm_metadata::RegistryMetadata::from_bytes(custom.data(), 0).ok();
            } else if custom.name() == "name" {
                name = wasm_metadata::ModuleNames::from_bytes(custom.data(), 0)
                    .ok()
                    .and_then(|n| n.get_name().cloned());
            }
        }

        if producers.is_some() || registry_metadata.is_some() || name.is_some() {
            Some(metadata::Metadata {
                name,
                producers: producers.map(|p| p.into()),
                registry_metadata,
            })
        } else {
            None
        }
    }
}

impl<Ast: AstCustomization> Debug for Module<Ast> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.sections.fmt(f)
    }
}

impl<Ast: AstCustomization> PartialEq for Module<Ast> {
    fn eq(&self, other: &Self) -> bool {
        self.sections.eq(&other.sections)
    }
}

impl<Ast: AstCustomization> From<Sections<CoreIndexSpace, CoreSectionType, CoreSection<Ast>>>
    for Module<Ast>
{
    fn from(value: Sections<CoreIndexSpace, CoreSectionType, CoreSection<Ast>>) -> Self {
        Self::new(value)
    }
}

impl<Ast: AstCustomization> Clone for Module<Ast> {
    fn clone(&self) -> Self {
        Module::from(self.sections.clone())
    }
}

#[cfg(feature = "component")]
impl<Ast: AstCustomization>
    Section<crate::component::ComponentIndexSpace, crate::component::ComponentSectionType>
    for Module<Ast>
{
    fn index_space(&self) -> crate::component::ComponentIndexSpace {
        crate::component::ComponentIndexSpace::Module
    }

    fn section_type(&self) -> crate::component::ComponentSectionType {
        crate::component::ComponentSectionType::Module
    }
}