nanowasm 0.0.2

A small, stand-alone WebAssembly interpreter.
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
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
use std;
use std::collections::HashMap;

use byteorder;
use byteorder::ByteOrder;
use parity_wasm::elements;

use types::*;
use util::*;
use loader::*;

/// A label to a particular block: just an instruction index.
/// The label index is implicit in the labels stack; label 0 is always
/// the top of the stack.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct BlockLabel(usize);

/// The activation record for an executing function.
#[derive(Debug, Clone, Default)]
struct StackFrame {
    value_stack: Vec<Value>,
    labels: Vec<BlockLabel>,
    locals: Vec<Value>,
    /// Where in the current function execution is.
    ip: usize,
}

impl StackFrame {
    /// Takes a FuncInstance and allocates a stack frame for it, then pushes
    /// the given args to its locals.
    fn from_func_instance(func: &FuncInstance, args: &[Value]) -> Self {
        // Allocate space for locals+params
        let locals_size = func.locals.len() + func.functype.params.len();
        let mut locals = Vec::with_capacity(locals_size);
        assert_eq!(func.functype.params.len(), args.len(), "Tried to create stack frame for func with different number of parameters than the type says it takes!");

        // Push params
        locals.extend(args.into_iter());
        // Fill remaining space with 0's
        let iter = func.functype
            .params
            .iter()
            .map(|t| Value::default_from_type(*t));
        locals.extend(iter);

        Self {
            value_stack: vec![],
            labels: vec![],
            locals: locals,
            ip: 0,
        }
    }

    /// Push a new BlockLabel to the label stack.
    fn push_label(&mut self, ip: BlockLabel) {
        self.labels.push(ip);
    }

    /// Pops to the given label index and returns
    /// the BlockLabel of the destination instruction index.
    /// Passing it 0 jumps to the first containing label, etc.
    ///
    /// Panics if an invalid/too large index is given.
    fn pop_label(&mut self, label_idx: usize) -> BlockLabel {
        let i = 0;
        while i < label_idx {
            self.labels.pop();
        }
        self.labels.pop().unwrap()
    }

    /// Get a local variable in the stack frame by index.
    /// Panics if out of bounds.
    fn get_local(&mut self, idx: usize) -> Value {
        assert!(idx < self.locals.len());
        self.locals[idx]
    }

    /// Set a local variable in the stack frame by index.
    /// Panics if out of bounds or if the type of the new
    /// variable does not match the old one(?).
    fn set_local(&mut self, idx: usize, vl: Value) {
        assert!(idx < self.locals.len());
        assert_eq!(self.locals[idx].get_type(), vl.get_type());
        self.locals[idx] = vl;
    }

    /// Pop the top of the value_stack and returns the value.
    ///
    /// Panics if the stack is empty.
    fn pop(&mut self) -> Value {
        assert!(!self.value_stack.is_empty());
        self.value_stack.pop().unwrap()
    }

    /// Pops the top of the value_stack and returns the value as a number.
    ///
    /// Panics if the stack is empty or the Value is not the right
    /// numeric type.
    fn pop_as<T>(&mut self) -> T
    where
        T: From<Value>,
    {
        self.pop().into()
    }

    /// Pops the top two values of the value_stack and returns them
    /// cast into the given types.
    ///
    /// The top of the stack is the second value returned, the first
    /// is one down from the top.
    ///
    /// Panics if the stack is empty or the Value is not the right
    /// numeric type.
    fn pop2_as<T1, T2>(&mut self) -> (T1, T2)
    where
        T1: From<Value>,
        T2: From<Value>,
    {
        let a = self.pop().into();
        let b = self.pop().into();
        (b, a)
    }

    /// Pushes the given value to the top of the value_stack.
    /// Basically just for symmetry with `pop()`.
    fn push(&mut self, vl: Value) {
        self.value_stack.push(vl)
    }

    /// Returns the value from the top of the value_stack
    /// without altering the stack.
    ///
    /// Panics if the stack is empty.
    fn peek(&self) -> Value {
        assert!(!self.value_stack.is_empty());
        *self.value_stack.last().unwrap()
    }
}

macro_rules! impl_address_new {
    ($t: ident) => {
	impl $t {
	    pub fn new(i: usize) -> Self {
		$t(i)
	    }
	}
    }
}

/// Function address type; refers to a particular `FuncInstance` in the Store.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FunctionAddress(pub usize);
/// Table address type
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TableAddress(pub usize);
/// Memory address type
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MemoryAddress(pub usize);
/// Global address type
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct GlobalAddress(pub usize);
/// Module instance address type
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ModuleAddress(pub usize);

impl_address_new!(FunctionAddress);

/// For forward jumps (if, block) we need to know where to jump TO.
/// Serialized wasm doesn't store this information explicitly,
/// and searching for it mid-execution is a wasteful PITA,
/// so we find it ahead of time and then store it when the
/// function is instantiated.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct JumpTarget {
    block_start_instruction: usize,
    block_end_instruction: usize,
    /// Only used for if/else statements.
    else_instruction: usize,
}

/// Contains all the runtime information needed to execute a function
#[derive(Debug, Clone)]
struct FuncInstance {
    functype: FuncType,
    locals: Vec<elements::ValueType>,
    body: FuncBody,
    module: ModuleAddress,
    /// A vec of jump targets sorted by source instruction,
    /// so we can just binary-search in it.  A HashMap would
    /// work too, but I suspect this is faster?  And is trivial
    /// to construct, so.
    jump_table: Vec<JumpTarget>,
}

impl FuncInstance {
    /// Iterate through a function's body and construct the jump table for it.
    /// If we find a block or if instruction, the target is the matching end instruction.
    ///
    /// Panics on invalid (improperly nested) blocks.
    fn compute_jump_table(body: &FuncBody) -> Vec<JumpTarget> {
        match *body {
            FuncBody::Opcodes(ref opcodes) => {
                use parity_wasm::elements::Opcode::*;
                // TODO: I would be sort of happier properly walking a sequence, OCaml
                // style, but oh well.
                let mut offset = 0;
                let mut accm = vec![];
                while offset < opcodes.len() {
                    let op = &opcodes[offset];
                    // println!("Computing jump table: {}, {:?}", offset, op);
                    match *op {
                        Block(_) | If(_) => {
                            offset = FuncInstance::find_block_close(opcodes, offset, &mut accm);
                        }
                        _ => (),
                    }
                    offset += 1;
                }
                accm
            }
            FuncBody::HostFunction(_) => vec![],
        }
    }

    /// Recursively walk through opcodes starting from the given offset, and
    /// accumulate jump targets into the given vec.  This way we only have
    /// to walk the function once.
    ///
    /// Returns the last instruction index of the block, so you can start
    /// there and go on to find the next block.
    fn find_block_close(
        body: &[elements::Opcode],
        start_offset: usize,
        accm: &mut Vec<JumpTarget>,
    ) -> usize {
        use std::usize;
        let mut offset = start_offset;
        // TODO: Potentially invalid here, but, okay.
        let mut else_offset = usize::MAX;
        loop {
            let op = &body[offset];
            match *op {
                elements::Opcode::End => {
                    // Found matching end, yay.
                    let jt = JumpTarget {
                        block_start_instruction: start_offset,
                        block_end_instruction: offset,
                        else_instruction: else_offset,
                    };
                    accm.push(jt);
                    return offset;
                }
                elements::Opcode::Else => {
                    else_offset = offset;
                }
                // Opening another block, recurse.
                elements::Opcode::Block(_) => {
                    offset = FuncInstance::find_block_close(body, offset, accm);
                }
                _ => (),
            }
            offset += 1;
            assert!(offset < body.len(), "Unclosed block, should never happen!");
        }
    }
}

/// While a LoadedModule contains the specification of a module
/// in a convenient form, this is a runtime structure that contains
/// the relationship between module-local indices and global addresses.
/// So it relates its own local address space to the address space of the
/// `Store`
///
/// It also contains a bit of module-local data, mainly type vectors, that
/// don't need to be in the Store since they're never communicated between
/// modules.
#[derive(Debug, Clone)]
struct ModuleInstance {
    name: String,
    // These might be somewhat redundant with the
    // LoadedModule's
    exported_functions: Vec<Export<FuncIdx>>,
    exported_tables: Option<Export<()>>,
    exported_memories: Option<Export<()>>,
    exported_globals: Vec<Export<GlobalIdx>>,

    types: Vec<FuncType>,
    functions: Vec<FunctionAddress>,
    table: Option<TableAddress>,
    memory: Option<MemoryAddress>,
    globals: Vec<GlobalAddress>,
    start: Option<FunctionAddress>,
}

impl ModuleInstance {
    /// Takes a loaded-but-not-instantiated module and a slice of other modules
    /// loaded before it, and checks to see whether the module's imports are
    /// all provided by other modules.
    fn resolve_imports(
        &mut self,
        module: &LoadedModule,
        other_modules: &[ModuleInstance],
    ) -> Result<(), Error> {
        // Breaking imports/exports apart into separate arrays by type makes
        // life somewhat easier; instead of having a big for loop that checks
        // whether it exists and whether the types match, we just have to check
        // for existence in the appropriate array.
        // TODO: Validate the External memory/table/function/etc junk more

        macro_rules! returning_closures_sucks_rancid_donkey_dong {
            ($import: expr, $module: expr) => {
                || Error::ModuleNotFound {
                    module: $import.module_name.clone(),
                    dependent_module: $module.name.clone()
                }
            }
        }

        macro_rules! generate_not_exported_error {
            ($name: expr, $import: expr, $dependent_module: expr, $typ: expr) => {
                || Error::NotExported {
                    name: $name.clone(),
                    module: $import.clone(),
                    dependent_module: $dependent_module.to_owned(),
                    typ: $typ.clone(),
                }
            }
        }
        for import in &module.imported_functions {
            let target_module = other_modules
                .iter()
                .find(|m| import.module_name == m.name)
                .ok_or_else(returning_closures_sucks_rancid_donkey_dong!(import, module))?;

            let export_idx = target_module
                .exported_functions
                .iter()
                .position(|e| e.name == import.field_name)
                .ok_or_else(generate_not_exported_error!(
                    target_module.name,
                    import.field_name,
                    "function",
                    module.name
                ))?;

            // TODO: Assert that the import and export types match

            let addr = target_module.functions[export_idx];
            self.functions.push(addr);
        }

        for import in &module.imported_tables {
            let target_module = other_modules
                .iter()
                .find(|m| import.module_name == m.name)
                .ok_or_else(returning_closures_sucks_rancid_donkey_dong!(import, module))?;

            let export = target_module
                .exported_tables
                .iter()
                .find(|e| e.name == import.field_name)
                .ok_or_else(generate_not_exported_error!(
                    target_module.name,
                    import.field_name,
                    "table",
                    module.name
                ))?;

            // TODO: The "unwrap" here and for Memory
            // forms our ghetto error-checking;
            // since we can only have one memory or table,
            // the index is irrelevant.
            let addr = target_module.table.unwrap();
            self.table = Some(addr);
        }

        for import in &module.imported_memories {
            let target_module = other_modules
                .iter()
                .find(|m| import.module_name == m.name)
                .ok_or_else(returning_closures_sucks_rancid_donkey_dong!(import, module))?;

            let export = target_module
                .exported_memories
                .iter()
                .find(|e| e.name == import.field_name)
                .ok_or_else(generate_not_exported_error!(
                    target_module.name,
                    import.field_name,
                    "memory",
                    module.name
                ))?;

            // TODO: The "unwrap" here and for Memory
            // forms our ghetto error-checking;
            // since we can only have one memory or table,
            // the index is irrelevant.
            let addr = target_module.memory.unwrap();
            self.memory = Some(addr);
        }

        for import in &module.imported_globals {
            let target_module = other_modules
                .iter()
                .find(|m| import.module_name == m.name)
                .ok_or_else(returning_closures_sucks_rancid_donkey_dong!(import, module))?;

            let export = target_module
                .exported_globals
                .iter()
                .find(|e| e.name == import.field_name)
                .ok_or_else(generate_not_exported_error!(
                    target_module.name,
                    import.field_name,
                    "global",
                    module.name
                ))?;

            let addr = target_module.globals[export.value.0];
            self.globals.push(addr);
        }

        self.exported_functions = module.exported_functions.clone();
        self.exported_tables = module.exported_tables.clone();
        self.exported_memories = module.exported_memories.clone();
        self.exported_globals = module.exported_globals.clone();

        Ok(())
    }
}

/// All the *mutable* parts of the interpreter state.
/// This slightly wacky structure helps keep borrows from
/// being awful, a little bit.
///
/// Also see: `State`.
#[derive(Debug, Clone, Default)]
pub struct Store {
    tables: Vec<Table>,
    mems: Vec<Memory>,
    globals: Vec<Global>,
    // We don't have explicit StackFrame's in the Store for Reasons.
    // Borrowing reasons.  Namely, a function needs
    // a mut reference to its StackFrame, naturally.
    // but it also has to be able to push new StackFrame's
    // to the stack when a new function is called, and so
    // will mutate the vec it has a reference
    // into.  *We* know that it will never do anything
    // to invalidate its own StackFrame, but Rust doesn't.
    // So instead we basically just use Rust's stack and
    // have each wasm `Call` instruction allocate a new
    // StackFrame and pass it to the thing it's calling.
    // I feel like this may cause problems with potential
    // threading applications somewhere down the line
    // (see Python), but for now oh well.
    // Trivially gotten around with unsafe, if we want to.
    // stack: Vec<StackFrame>,
}

/// All the *immutable* parts of the interpreter state.
///
/// Also see: `Store`.
#[derive(Debug, Clone, Default)]
pub struct State {
    funcs: Vec<FuncInstance>,
    module_instances: Vec<ModuleInstance>,
    modules: HashMap<String, LoadedModule>,
}

/// An interpreter which runs a particular program.
///
/// Per the wasm spec, this contains the **Store**, defined as all the
/// runtime data for a collection of modules: memory's, tables, globals,
/// and stack.  In this implementation, stack frames are locals in the
/// `exec()` method, not an explicit structure, because otherwise
/// borrowing gets tricky.  We essentially use the Rust stack instead
/// of constructing a separate one.
///
/// The WASM spec has a not-immediately-obvious gap in semantics
/// between the environment in which programs are defined, loaded
/// and validated, where all references are *purely module-local*,
/// and the environment in which programs are executed, where all
/// references are *global*; modules are loaded and all their resources
/// are just shoved
/// into the Store.  It distinguishes these environments by using the
/// term "index" to mean an offset into a module-local environment,
/// and "address" to mean an offset into a global environment.
/// See <https://webassembly.github.io/spec/core/exec/runtime.html>
///
/// A module thus becomes a **module instance** when ready to execute,
/// which ceases to be a collection of data and becomes a collection
/// of index-to-address mappings.  A **function instance** then is
/// the original function definition, plus the a reference to the
/// module instance to allow it to resolve its indices to addresses.
#[derive(Debug, Clone)]
pub struct Interpreter {
    store: Store,
    state: State,
}

impl Interpreter {
    pub fn new() -> Self {
        // Don't know if there's a better place to put this, or a less annoying way of doing it
        // while still making it always visible, but this is fine for now.
        #[cfg(target_endian = "big")]
        eprintln!("WARNING: Running on big-endian target architecture!  Results are *not* guarenteed to be correct!");

        Self {
            store: Store::default(),
            state: State::default(),
        }
    }

    /// Builder function to add a loaded and validated module to the
    /// program.
    ///
    /// Essentially, this does the dynamic linking, and should cause
    /// errors to happen if there are invalid/dangling references.
    /// So, you have to load all the modules in order of dependencies.
    ///
    /// We could load all the modules in arbitrary order, then validate+link
    /// them at the end, but that's a PITA.
    ///
    /// This DOES run the module's start function, which potentially
    /// takes forever, soooooo.  That may not be what we want.
    /// However it IS what the spec proscribes, so!
    pub fn with_module(mut self, module: ValidatedModule) -> Result<Self, Error> {
        let module: LoadedModule = module.into_inner();
        let module_instance_address = ModuleAddress(self.state.module_instances.len());

        // We MUST load imports first because they consume the first indices
        // before all local definitions.
        // "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."

        let types = module.types.clone();
        let name = module.name.clone();
        let mut inst = ModuleInstance {
            name: name,
            types: types,
            exported_functions: vec![],
            exported_tables: None,
            exported_memories: None,
            exported_globals: vec![],
            functions: vec![],
            table: None,
            memory: None,
            globals: vec![],
            start: None,
        };
        inst.resolve_imports(&module, &self.state.module_instances)?;

        for func in module.funcs.iter() {
            let address = FunctionAddress(self.state.funcs.len());
            let functype = module.types[func.typeidx.0].clone();
            let instance = FuncInstance {
                functype: functype,
                locals: func.locals.clone(),
                body: func.body.clone(),
                module: module_instance_address,
                jump_table: FuncInstance::compute_jump_table(&func.body),
            };
            println!("Created function instance: {:?}", instance);
            self.state.funcs.push(instance);
            inst.functions.push(address);
        }

        // It's sorta meaningless to define a memory when we already
        // import one, since we can only have one.
        assert!(inst.memory.is_none());
        // If the module has a memory, clone it, initialize it, shove
        // it into the store, and return the address of it.  Otherwise,
        // return None.
        inst.memory = if let Some(mut memory) = module.mem.clone() {
            let store = &mut self.store;
            for &(ref offset_expr, ref val) in &module.mem_initializers {
                let offset_value = Interpreter::eval_constexpr(&offset_expr, store).unwrap();
                // TODO: This will panic on failure;
                // replacing it with TryFrom may be apropos.
                let offset_i: u32 = offset_value.into();
                memory
                    .initialize(offset_i, &val)
                    .expect("Invalid memory init");
            }
            let mem_addr = MemoryAddress(store.mems.len());
            store.mems.push(memory);
            Some(mem_addr)
        } else {
            None
        };

        // Same as memory's above; meaningless to define one if we
        // import one.
        assert!(inst.table.is_none());
        // Like memories, if the module has a table, clone it, initialize it, shove
        // it into the store, and return the address of it.  Otherwise,
        // return None.
        inst.table = if let Some(mut table) = module.tables.clone() {
            table
                .initialize(&module.table_initializers)
                .expect("Invalid table init");
            let table_addr = TableAddress(self.store.tables.len());
            self.store.tables.push(table);
            Some(table_addr)
        } else {
            None
        };

        // This has to be in its own block 'cause we borrow `module`
        // and don't clone all of it.
        inst.globals = {
            // Borrow this so we don't have wacky borrowing problems
            // associated with `self` in a closure and whatever.
            let store = &mut self.store;
            // Create an iterator of initialized Global values
            let initialized_globals = module
                .globals
                .iter()
                .map(|&(ref global, ref init)| {
                    let mut g = global.clone();
                    let init_value =
                        Interpreter::eval_constexpr(init, store).expect("TODO: Handle failures");
                    println!("Initializing global {:?} to {:?}", g, init_value);
                    g.initialize(init_value);
                    g
                })
                .collect::<Vec<_>>();

            // Get the address of the next Global slot,
            // shove all the initialized Global's into it,
            // and then get the address again, and that's the
            // mapping for our GlobalAddress's for this module.
            let global_addr_start = store.globals.len();
            store.globals.extend(initialized_globals);
            let global_addr_end = store.globals.len();
            (global_addr_start..global_addr_end)
                .map(GlobalAddress)
                .collect()
        };

        // Start function.
        inst.start = module.start.map(|start_idx| inst.functions[start_idx.0]);
        //println!("Instance start function: {:?}, module start function: {:?}", inst.start, module.start);
        // Save it for later too.
        let start_function = inst.start;

        // Great, instance is created, add it to the State
        self.state.modules.insert(module.name.to_owned(), module);
        self.state.module_instances.push(inst);

        // Run start function.
        if let Some(function_addr) = start_function {
            Interpreter::exec(&mut self.store, &self.state, function_addr, &[]);
        }

        Ok(self)
    }

    /// Evaluates the constexpr in the current context.
    /// This is a PITA 'cause a constexpr might be `get_global`, but hey.
    fn eval_constexpr(expr: &ConstExpr, store: &Store) -> Result<Value, Error> {
        // I have no damn idea why a constexpr is defined to be a sequence
        // when it seems to only ever actually use the last value.
        let expr = expr.0.last().unwrap();
        match *expr {
            ConstOpcode::I32Const(v) => Ok(Value::I32(v)),
            ConstOpcode::I64Const(v) => Ok(Value::I64(v)),
            ConstOpcode::F32Const(v) => Ok(Value::F32(v)),
            ConstOpcode::F64Const(v) => Ok(Value::F64(v)),
            ConstOpcode::GetGlobal(i) => unimplemented!(),
        }
    }

    /// Returns a GlobalAddress from a given index
    fn resolve_global(state: &State, module_addr: ModuleAddress, idx: GlobalIdx) -> GlobalAddress {
        assert!(module_addr.0 < state.module_instances.len());
        let module_instance = &state.module_instances[module_addr.0];
        assert!(idx.0 < module_instance.globals.len());
        module_instance.globals[idx.0]
    }

    /// Returns a FunctionAddress from a given index
    fn resolve_function(
        state: &State,
        module_addr: ModuleAddress,
        idx: FuncIdx,
    ) -> FunctionAddress {
        assert!(module_addr.0 < state.module_instances.len());
        let module_instance = &state.module_instances[module_addr.0];
        assert!(idx.0 < module_instance.functions.len());
        module_instance.functions[idx.0]
    }

    /// Returns a reference to a `FuncType` from a given index
    ///
    /// This is somewhat asymmetric with everything else, but there is no
    /// explicit "type address" type described in wasm, since types are completely
    /// local to modules.
    fn resolve_type(state: &State, module_addr: ModuleAddress, idx: TypeIdx) -> &FuncType {
        assert!(module_addr.0 < state.module_instances.len());
        let module_instance = &state.module_instances[module_addr.0];
        assert!(idx.0 < module_instance.types.len());
        &module_instance.types[idx.0]
    }

    /// Returns a MemoryAddress for the Memory of a given ModuleInstance.
    /// Modules can currently only have one Memory, so it's pretty easy.
    fn resolve_memory(state: &State, module_addr: ModuleAddress) -> MemoryAddress {
        assert!(module_addr.0 < state.module_instances.len());
        let module_instance = &state.module_instances[module_addr.0];
        assert!(module_instance.memory.is_some());
        module_instance.memory.unwrap()
    }

    /// Returns a TableAddress for the Table of a given ModuleInstance.
    /// Modules can currently only have one Table, so it's pretty easy.
    fn resolve_table(state: &State, module_addr: ModuleAddress) -> TableAddress {
        assert!(module_addr.0 < state.module_instances.len());
        let module_instance = &state.module_instances[module_addr.0];
        assert!(module_instance.table.is_some());
        module_instance.table.unwrap()
    }

    /// Get a global variable by *index*.  Needs a module instance
    /// address to look up the global variable's address.
    /// Panics if out of bounds.
    ///
    /// This is unused since it creates irritating double-borrows.
    fn get_global(
        globals: &[Global],
        state: &State,
        module_addr: ModuleAddress,
        idx: GlobalIdx,
    ) -> Value {
        let global_addr = Interpreter::resolve_global(state, module_addr, idx);
        globals[global_addr.0].value
    }

    /// Sets a global variable by *index*.  Needs a module instance
    /// address to look up the global variable's address.
    /// Panics if out of bounds or if the type of the new
    /// variable does not match the old one(?).
    fn set_global(
        globals: &mut [Global],
        state: &State,
        module_addr: ModuleAddress,
        idx: GlobalIdx,
        vl: Value,
    ) {
        let global_addr = Interpreter::resolve_global(state, module_addr, idx);
        assert!(globals[global_addr.0].mutable);
        assert_eq!(globals[global_addr.0].variable_type, vl.get_type());
        globals[global_addr.0].value = vl;
    }

    /// Assigns a value to the given `memory` with the given function.
    fn set_memory_with<F, N>(
        mems: &mut [Memory],
        state: &State,
        module_addr: ModuleAddress,
        offset: usize,
        f: F,
        vl: N,
    ) where
        F: Fn(&mut [u8], N),
    {
        let memory_address = Interpreter::resolve_memory(state, module_addr);
        let mem = &mut mems[memory_address.0];
        assert!(offset + std::mem::size_of::<N>() < mem.data.len());
        f(&mut mem.data[offset..], vl)
    }

    /// Reads data from a slice of the given `memory` with the given function
    fn get_memory_with<F, N>(
        mems: &[Memory],
        state: &State,
        module_addr: ModuleAddress,
        offset: usize,
        f: F,
    ) -> N
    where
        F: Fn(&[u8]) -> N,
    {
        let memory_address = Interpreter::resolve_memory(state, module_addr);
        let mem = &mems[memory_address.0];
        assert!(offset + std::mem::size_of::<N>() < mem.data.len());
        f(&mem.data[offset..])
    }

    fn trap() {
        panic!("Trap occured!  Aieee!")
    }

    fn exec_const(frame: &mut StackFrame, vl: Value) {
        frame.push(vl);
    }

    /// Executes a load instruction, using the given function to
    /// convert the memory's `&[u8]` into the given Value type.
    fn exec_load<F, N>(
        frame: &mut StackFrame,
        store: &mut Store,
        state: &State,
        module: ModuleAddress,
        offset: u32,
        func: F,
    ) where
        F: Fn(&[u8]) -> N,
        N: Into<Value>,
    {
        let address = frame.pop_as::<i32>();
        // BUGGO: Make sure wrap-arounds don't happen here!
        let effective_address = address + offset as i32;
        let mem_contents = Interpreter::get_memory_with(
            &mut store.mems,
            &state,
            module,
            effective_address as usize,
            func,
        ).into();
        frame.push(mem_contents);
    }

    /// Executes a load instruction, using the given function to
    /// convert the memory's `&[u8]` into the the SourceN type,
    /// then sign-extending it (based on whether it's signed or unsigned)
    /// into DestN.
    fn exec_load_extend<F, SourceN, DestN>(
        frame: &mut StackFrame,
        store: &mut Store,
        state: &State,
        module: ModuleAddress,
        offset: u32,
        func: F,
    ) where
        F: Fn(&[u8]) -> SourceN,
        SourceN: Extend<DestN>,
        DestN: Into<Value>,
    {
        let address = frame.pop_as::<i32>();
        // BUGGO: Make sure wrap-arounds don't happen here!
        let effective_address = address + offset as i32;
        let mem_contents = Interpreter::get_memory_with(
            &mut store.mems,
            &state,
            module,
            effective_address as usize,
            func,
        ).extend()
            .into();
        frame.push(mem_contents);
    }

    /// Executes a store instruction, using the given function to
    /// write the Value type into the memory's `&mut [u8]`
    fn exec_store<F, N>(
        frame: &mut StackFrame,
        store: &mut Store,
        state: &State,
        module: ModuleAddress,
        offset: u32,
        func: F,
    ) where
        F: Fn(&mut [u8], N),
        N: From<Value>,
    {
        let vl = frame.pop_as::<N>();
        let address = frame.pop_as::<i32>();
        // BUGGO: Make sure wrap-arounds don't happen here!
        let effective_address = address + offset as i32;
        Interpreter::set_memory_with(
            &mut store.mems,
            &state,
            module,
            effective_address as usize,
            func,
            vl,
        );
    }

    /// Wraps/truncates the the Value on the stack from the given SourceN type
    /// to the DestN type, then stores it in memory.
    fn exec_store_wrap<F, SourceN, DestN>(
        frame: &mut StackFrame,
        store: &mut Store,
        state: &State,
        module: ModuleAddress,
        offset: u32,
        func: F,
    ) where
        F: Fn(&mut [u8], DestN),
        SourceN: From<Value> + Wrap<DestN>,
    {
        let vl: DestN = frame.pop_as::<SourceN>().wrap();
        let address = frame.pop_as::<i32>();
        // BUGGO: Make sure wrap-arounds don't happen here!
        let effective_address = address + offset as i32;
        Interpreter::set_memory_with(
            &mut store.mems,
            &state,
            module,
            effective_address as usize,
            func,
            vl,
        );
    }

    /// Helper function for running binary operations that pop
    /// two values from the stack and push one result
    fn exec_binop<T1, T2, Res, F>(frame: &mut StackFrame, op: F)
    where
        T1: From<Value>,
        T2: From<Value>,
        Res: Into<Value>,
        F: Fn(T1, T2) -> Res,
    {
        let (a, b) = frame.pop2_as::<T1, T2>();
        frame.push(op(a, b).into());
    }

    /// Helper function for running binary operations that pop
    /// two values from the stack and push one result
    fn exec_uniop<T, Res, F>(frame: &mut StackFrame, op: F)
    where
        T: From<Value>,
        Res: Into<Value>,
        F: Fn(T) -> Res,
    {
        let a = frame.pop_as::<T>();
        frame.push(op(a).into());
    }

    /// Helper function for running a function call.
    fn exec_call(
        frame: &mut StackFrame,
        store: &mut Store,
        state: &State,
        function_addr: FunctionAddress,
    ) {
        // Typecheck and get appropriate arguments off the stack to pass
        // to the called function.
        let f = &state.funcs[function_addr.0];
        let return_val = {
            assert!(f.functype.params.len() <= frame.value_stack.len());
            let params_slice = if f.functype.params.len() == 0 {
                &[]
            } else {
                let params_end = frame.value_stack.len();
                let params_start = params_end - f.functype.params.len();
                let params_slice = &frame.value_stack[params_start..params_end];
                for (param, desired_type) in params_slice.iter().zip(&f.functype.params) {
                    assert_eq!(param.get_type(), *desired_type);
                }
                params_slice
            };
            // Recurse into `exec()`, which creates a new stack frame.
            Interpreter::exec(store, state, function_addr, params_slice)
        };

        // Because a function call must actually pop values off the stack,
        // we have to remove the values that were passed to the function in
        // `params_slice`
        // TODO: Might be easier to just slice them off directly, since they get
        // copied anyway?
        let new_stack_len = frame.value_stack.len() - f.functype.params.len();
        frame.value_stack.truncate(new_stack_len);

        // Great, now check that the return value matches the stated
        // return type, and push it to the values stack.
        let return_type = return_val.map(|v| v.get_type());
        assert_eq!(return_type, f.functype.return_type);
        if let Some(v) = return_val {
            frame.value_stack.push(v);
        }
    }

    /// Actually do the interpretation of the given function, assuming
    /// that a stack frame already exists for it with args and locals
    /// and such
    pub fn exec(
        store: &mut Store,
        state: &State,
        func: FunctionAddress,
        args: &[Value],
    ) -> Option<Value> {
        let func = &state.funcs[func.0];
        // println!("Params: {:?}, args: {:?}", func.functype.params, args);
        let frame = &mut StackFrame::from_func_instance(func, args);
        match func.body {
            FuncBody::HostFunction(ref f) => {
                (*f)(&mut frame.value_stack);
            }
            FuncBody::Opcodes(ref opcodes) => {
                use parity_wasm::elements::Opcode::*;
                use std::usize;
                loop {
                    if frame.ip == opcodes.len() {
                        break;
                    }
                    let op = &opcodes[frame.ip];

                    // println!("Frame: {:?}", frame);
                    // println!("Op: {:?}", op);
                    match *op {
                        Unreachable => panic!("Unreachable?"),
                        Nop => (),
                        Block(_blocktype) => {
                            // TODO: Verify blocktype
                            let jump_target_idx = func.jump_table
                                .binary_search_by(|jt| jt.block_start_instruction.cmp(&frame.ip))
                                .expect("Cannot find matching jump table for block statement");
                            let jump_target = &func.jump_table[jump_target_idx];
                            frame.push_label(BlockLabel(jump_target.block_end_instruction));
                        }
                        Loop(_blocktype) => {
                            // TODO: Verify blocktype
                            // Instruction index to jump to on branch or such.
                            let end_idx = frame.ip + 1;
                            frame.push_label(BlockLabel(end_idx));
                        }
                        If(_blocktype) => {
                            // TODO: Verify blocktype
                            let vl = frame.pop_as::<i32>();
                            let jump_target_idx = func.jump_table
                                .binary_search_by(|jt| jt.block_start_instruction.cmp(&frame.ip))
                                .expect("Cannot find matching jump table for if statement");
                            let jump_target = &func.jump_table[jump_target_idx];
                            frame.push_label(BlockLabel(jump_target.block_end_instruction));
                            if vl != 0 {
                                // continue
                            } else {
                                // Jump to instruction after the else section
                                frame.ip = jump_target.else_instruction + 1;
                            }
                        }
                        Else => {
                            // Done with if part of the statement,
                            // skip to (just after) the end.
                            let target_ip = frame.pop_label(0);
                            frame.ip = target_ip.0 + 1;
                        }
                        End => {
                            // Done with whatever block we're in
                            // OR, we are at the end of the function and must return;
                            // if so, popping the label is NOT what we want 'cause we
                            // have no labels.
                            // TODO: This may still be incorrect.
                            if frame.ip != opcodes.len() - 1 {
                                frame.pop_label(0);
                            } // else we're at the end of the function, do nothing
                        }
                        Br(i) => {
                            let target_ip = frame.pop_label(i as usize);
                            frame.ip = target_ip.0;
                        }
                        BrIf(i) => {
                            let i = i as usize;
                            let vl = frame.pop_as::<i32>();
                            if vl != 0 {
                                let target_ip = frame.pop_label(i);
                                frame.ip = target_ip.0;
                            }
                        }
                        BrTable(ref v, i) => {
                            // TODO: Double-check this is correct, I don't fully
                            // understand its goals.  It's a computed jump into
                            // a list of labels, but, needs verification.
                            let i = i as usize;
                            let vl = frame.pop_as::<i32>() as usize;
                            let target_label = if vl < v.len() { v[vl] as usize } else { i };
                            let target_ip = frame.pop_label(target_label);
                            frame.ip = target_ip.0;
                        }
                        Return => {
                            break;
                        }
                        Call(i) => {
                            let i = i as usize;
                            let function_addr =
                                Interpreter::resolve_function(state, func.module, FuncIdx(i));
                            Interpreter::exec_call(frame, store, state, function_addr);
                        }
                        CallIndirect(x, _) => {
                            // Okay, x is the expected type signature of the function we
                            // are trying to call.
                            // So we pop an i32 i from the stack, use that to index into
                            // the table to get a function index, then call that
                            // function
                            let x = x as usize;
                            let func_type =
                                Interpreter::resolve_type(state, func.module, TypeIdx(x));
                            let i = frame.pop_as::<u32>() as usize;
                            let table_addr = Interpreter::resolve_table(state, func.module);
                            let function_index = {
                                let table = &store.tables[table_addr.0];
                                table.data[i]
                            };
                            let function_addr =
                                Interpreter::resolve_function(state, func.module, function_index);
                            // Make sure that the function we've actually retrieved has the same signature as the
                            // type we want.
                            assert_eq!(&state.funcs[function_addr.0].functype, func_type);
                            Interpreter::exec_call(frame, store, state, function_addr);
                        }
                        Drop => {
                            frame.pop();
                        }
                        Select => {
                            let selector = frame.pop_as::<i32>();
                            let v2 = frame.pop();
                            let v1 = frame.pop();
                            if selector != 0 {
                                frame.push(v1);
                            } else {
                                frame.push(v2);
                            }
                        }
                        GetLocal(i) => {
                            let i = i as usize;
                            let vl = frame.get_local(i as usize);
                            frame.push(vl);
                        }
                        SetLocal(i) => {
                            let i = i as usize;
                            let vl = frame.pop();
                            frame.set_local(i, vl);
                        }
                        TeeLocal(i) => {
                            let i = i as usize;
                            let vl = frame.peek();
                            frame.set_local(i, vl);
                        }
                        GetGlobal(i) => {
                            let i = i as usize;
                            let vl = Interpreter::get_global(
                                &store.globals,
                                &state,
                                func.module,
                                GlobalIdx(i),
                            );
                            frame.push(vl);
                        }
                        SetGlobal(i) => {
                            let i = i as usize;
                            let vl = frame.pop();
                            Interpreter::set_global(
                                &mut store.globals,
                                &state,
                                func.module,
                                GlobalIdx(i),
                                vl,
                            );
                        }
                        I32Load(offset, _align) => {
                            Interpreter::exec_load(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_i32,
                            );
                        }
                        I64Load(offset, _align) => {
                            Interpreter::exec_load(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_i64,
                            );
                        }
                        F32Load(offset, _align) => {
                            Interpreter::exec_load(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_f32,
                            );
                        }
                        F64Load(offset, _align) => {
                            Interpreter::exec_load(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_f64,
                            );
                        }
                        I32Load8S(offset, _align) => {
                            Interpreter::exec_load_extend::<_, i8, i32>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem| mem[0] as i8,
                            );
                        }
                        I32Load8U(offset, _align) => {
                            Interpreter::exec_load_extend::<_, u8, i32>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem| mem[0] as u8,
                            );
                        }
                        I32Load16S(offset, _align) => {
                            Interpreter::exec_load_extend::<_, i16, i32>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_i16,
                            );
                        }
                        I32Load16U(offset, _align) => {
                            Interpreter::exec_load_extend::<_, u16, i32>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_u16,
                            );
                        }
                        I64Load8S(offset, _align) => {
                            Interpreter::exec_load_extend::<_, i8, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem| mem[0] as i8,
                            );
                        }
                        I64Load8U(offset, _align) => {
                            Interpreter::exec_load_extend::<_, u8, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem| mem[0] as u8,
                            );
                        }
                        I64Load16S(offset, _align) => {
                            Interpreter::exec_load_extend::<_, i16, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_i16,
                            );
                        }
                        I64Load16U(offset, _align) => {
                            Interpreter::exec_load_extend::<_, u16, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_u16,
                            );
                        }
                        I64Load32S(offset, _align) => {
                            Interpreter::exec_load_extend::<_, i32, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_i32,
                            );
                        }
                        I64Load32U(offset, _align) => {
                            Interpreter::exec_load_extend::<_, u32, i64>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::read_u32,
                            );
                        }
                        I32Store(offset, _align) => {
                            Interpreter::exec_store(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_i32,
                            );
                        }
                        I64Store(offset, _align) => {
                            Interpreter::exec_store(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_i64,
                            );
                        }
                        F32Store(offset, _align) => {
                            Interpreter::exec_store(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_f32,
                            );
                        }
                        F64Store(offset, _align) => {
                            Interpreter::exec_store(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_f64,
                            );
                        }
                        I32Store8(offset, _align) => {
                            // `byteorder` doesn't have write_i8 since it's a bit redundant,
                            // so we make our own.
                            Interpreter::exec_store_wrap::<_, i32, i8>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem, x| mem[0] = x as u8,
                            );
                        }
                        I32Store16(offset, _align) => {
                            Interpreter::exec_store_wrap::<_, i32, i16>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_i16,
                            );
                        }
                        I64Store8(offset, _align) => {
                            Interpreter::exec_store_wrap::<_, i64, i8>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                |mem, x| mem[0] = x as u8,
                            );
                        }
                        I64Store16(offset, _align) => {
                            Interpreter::exec_store_wrap::<_, i64, i16>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_i16,
                            );
                        }
                        I64Store32(offset, _align) => {
                            Interpreter::exec_store_wrap::<_, i64, i32>(
                                frame,
                                store,
                                state,
                                func.module,
                                offset,
                                byteorder::LittleEndian::write_i32,
                            );
                        }
                        CurrentMemory(_) => {
                            let module_addr = func.module;
                            let memory_addr = Interpreter::resolve_memory(state, module_addr);
                            let mem = &store.mems[memory_addr.0];
                            frame.push(mem.len().into())
                        }
                        GrowMemory(_) => {
                            let size_delta = frame.pop_as::<i32>();
                            let module_addr = func.module;
                            let memory_addr = Interpreter::resolve_memory(state, module_addr);
                            let mem = &mut store.mems[memory_addr.0];
                            let prev_size = mem.len();
                            // TODO: We should return -1 if enough memory cannot be allocated.
                            mem.resize(size_delta);
                            frame.push(prev_size.into());
                        }
                        I32Const(i) => Interpreter::exec_const(frame, i.into()),
                        I64Const(l) => Interpreter::exec_const(frame, l.into()),
                        // Why oh why are these floats represented as u32 and u64?
                        // Because this is the serialized representation, sigh.
                        F32Const(i) => {
                            Interpreter::exec_const(frame, Value::from(u32_to_f32(i)));
                        }
                        F64Const(l) => {
                            Interpreter::exec_const(frame, Value::from(u64_to_f64(l)));
                        }
                        I32Eqz => {
                            Interpreter::exec_uniop::<i32, bool, _>(frame, |x| i32::eq(&x, &0));
                        }
                        I32Eq => {
                            Interpreter::exec_binop(frame, |x: i32, y: i32| i32::eq(&x, &y));
                        }
                        I32Ne => {
                            Interpreter::exec_binop(frame, |x: i32, y: i32| i32::ne(&x, &y));
                        }
                        I32LtS => {
                            Interpreter::exec_binop(frame, |x, y| i32::lt(&x, &y));
                        }
                        I32LtU => {
                            Interpreter::exec_binop(frame, |x, y| u32::lt(&x, &y));
                        }
                        I32GtS => {
                            Interpreter::exec_binop(frame, |x, y| i32::gt(&x, &y));
                        }
                        I32GtU => {
                            Interpreter::exec_binop(frame, |x, y| u32::gt(&x, &y));
                        }
                        I32LeS => {
                            Interpreter::exec_binop(frame, |x, y| i32::le(&x, &y));
                        }
                        I32LeU => {
                            Interpreter::exec_binop(frame, |x, y| u32::le(&x, &y));
                        }
                        I32GeS => {
                            Interpreter::exec_binop(frame, |x, y| i32::ge(&x, &y));
                        }
                        I32GeU => {
                            Interpreter::exec_binop(frame, |x, y| u32::ge(&x, &y));
                        }
                        I64Eqz => {
                            Interpreter::exec_uniop::<i64, bool, _>(frame, |x| i64::eq(&x, &0));
                        }
                        I64Eq => {
                            Interpreter::exec_binop(frame, |x: i64, y: i64| i64::eq(&x, &y));
                        }
                        I64Ne => {
                            Interpreter::exec_binop(frame, |x: i64, y: i64| i64::ne(&x, &y));
                        }
                        I64LtS => {
                            Interpreter::exec_binop(frame, |x, y| i64::lt(&x, &y));
                        }
                        I64LtU => {
                            Interpreter::exec_binop(frame, |x, y| u64::lt(&x, &y));
                        }
                        I64GtS => {
                            Interpreter::exec_binop(frame, |x, y| i64::gt(&x, &y));
                        }
                        I64GtU => {
                            Interpreter::exec_binop(frame, |x, y| u64::gt(&x, &y));
                        }
                        I64LeS => {
                            Interpreter::exec_binop(frame, |x, y| i64::le(&x, &y));
                        }
                        I64LeU => {
                            Interpreter::exec_binop(frame, |x, y| u64::le(&x, &y));
                        }
                        I64GeS => {
                            Interpreter::exec_binop(frame, |x, y| i64::ge(&x, &y));
                        }
                        I64GeU => {
                            Interpreter::exec_binop(frame, |x, y| u64::ge(&x, &y));
                        }
                        F32Eq => {
                            Interpreter::exec_binop(frame, |x: f32, y: f32| f32::eq(&x, &y));
                        }
                        F32Ne => {
                            Interpreter::exec_binop(frame, |x: f32, y: f32| f32::ne(&x, &y));
                        }
                        F32Lt => {
                            Interpreter::exec_binop(frame, |x, y| f32::lt(&x, &y));
                        }
                        F32Gt => {
                            Interpreter::exec_binop(frame, |x, y| f32::gt(&x, &y));
                        }
                        F32Le => {
                            Interpreter::exec_binop(frame, |x, y| f32::le(&x, &y));
                        }
                        F32Ge => {
                            Interpreter::exec_binop(frame, |x, y| f32::ge(&x, &y));
                        }
                        F64Eq => {
                            Interpreter::exec_binop(frame, |x: f64, y: f64| f64::eq(&x, &y));
                        }
                        F64Ne => {
                            Interpreter::exec_binop(frame, |x: f64, y: f64| f64::ne(&x, &y));
                        }
                        F64Lt => {
                            Interpreter::exec_binop(frame, |x, y| f64::lt(&x, &y));
                        }
                        F64Gt => {
                            Interpreter::exec_binop(frame, |x, y| f64::gt(&x, &y));
                        }
                        F64Le => {
                            Interpreter::exec_binop(frame, |x, y| f64::le(&x, &y));
                        }
                        F64Ge => {
                            Interpreter::exec_binop(frame, |x, y| f64::ge(&x, &y));
                        }
                        I32Clz => {
                            Interpreter::exec_uniop(frame, i32::leading_zeros);
                        }
                        I32Ctz => {
                            Interpreter::exec_uniop(frame, i32::trailing_zeros);
                        }
                        I32Popcnt => {
                            Interpreter::exec_uniop(frame, i32::count_zeros);
                        }
                        I32Add => {
                            Interpreter::exec_binop(frame, i32::wrapping_add);
                        }
                        I32Sub => {
                            Interpreter::exec_binop(frame, i32::wrapping_sub);
                        }
                        I32Mul => {
                            Interpreter::exec_binop(frame, i32::wrapping_mul);
                        }
                        I32DivS => {
                            Interpreter::exec_binop(frame, i32::wrapping_div);
                        }
                        I32DivU => {
                            Interpreter::exec_binop(frame, u32::wrapping_div);
                        }
                        I32RemS => {
                            Interpreter::exec_binop(frame, i32::wrapping_rem);
                        }
                        I32RemU => {
                            Interpreter::exec_binop(frame, u32::wrapping_rem);
                        }
                        I32And => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i32, i32, _, _>(frame, i32::bitand);
                        }
                        I32Or => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i32, i32, _, _>(frame, i32::bitor);
                        }
                        I32Xor => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i32, i32, _, _>(frame, i32::bitxor);
                        }
                        I32Shl => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i32, i32, _, _>(frame, i32::shl);
                        }
                        I32ShrS => {
                            Interpreter::exec_binop::<i32, u32, _, _>(frame, i32::wrapping_shr);
                        }
                        I32ShrU => {
                            Interpreter::exec_binop::<u32, u32, _, _>(frame, u32::wrapping_shr);
                        }
                        I32Rotl => {
                            Interpreter::exec_binop(frame, i32::rotate_left);
                        }
                        I32Rotr => {
                            Interpreter::exec_binop(frame, i32::rotate_right);
                        }
                        I64Clz => {
                            Interpreter::exec_uniop(frame, i64::leading_zeros);
                        }
                        I64Ctz => {
                            Interpreter::exec_uniop(frame, i64::trailing_zeros);
                        }
                        I64Popcnt => {
                            Interpreter::exec_uniop(frame, i64::count_zeros);
                        }
                        I64Add => {
                            Interpreter::exec_binop(frame, i64::wrapping_add);
                        }
                        I64Sub => {
                            Interpreter::exec_binop(frame, i64::wrapping_sub);
                        }
                        I64Mul => {
                            Interpreter::exec_binop(frame, i64::wrapping_mul);
                        }
                        I64DivS => {
                            Interpreter::exec_binop(frame, i64::wrapping_div);
                        }
                        I64DivU => {
                            Interpreter::exec_binop(frame, u64::wrapping_div);
                        }
                        I64RemS => {
                            Interpreter::exec_binop(frame, i64::wrapping_rem);
                        }
                        I64RemU => {
                            Interpreter::exec_binop(frame, u64::wrapping_rem);
                        }
                        I64And => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i64, i64, _, _>(frame, i64::bitand);
                        }
                        I64Or => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i64, i64, _, _>(frame, i64::bitor);
                        }
                        I64Xor => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i64, i64, _, _>(frame, i64::bitxor);
                        }
                        I64Shl => {
                            use std::ops::*;
                            Interpreter::exec_binop::<i64, i64, _, _>(frame, i64::shl);
                        }
                        I64ShrS => {
                            Interpreter::exec_binop::<i64, u32, _, _>(frame, i64::wrapping_shr);
                        }
                        I64ShrU => {
                            Interpreter::exec_binop::<u64, u32, _, _>(frame, u64::wrapping_shr);
                        }
                        I64Rotl => {
                            Interpreter::exec_binop::<i64, u32, _, _>(frame, i64::rotate_left);
                        }
                        I64Rotr => {
                            Interpreter::exec_binop::<i64, u32, _, _>(frame, i64::rotate_right);
                        }
                        F32Abs => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::abs);
                        }
                        F32Neg => {
                            use std::ops::Neg;
                            Interpreter::exec_uniop::<f32, _, _>(frame, Neg::neg);
                        }
                        F32Ceil => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::ceil);
                        }
                        F32Floor => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::floor);
                        }
                        F32Trunc => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::trunc);
                        }
                        F32Nearest => {
                            // TODO: Double-check rounding behavior is correct
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::round);
                        }
                        F32Sqrt => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f32::sqrt);
                        }
                        F32Add => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::add);
                        }
                        F32Sub => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::sub);
                        }
                        F32Mul => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::mul);
                        }
                        F32Div => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::div);
                        }
                        F32Min => {
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::min);
                        }
                        F32Max => {
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, f32::max);
                        }
                        F32Copysign => {
                            Interpreter::exec_binop::<f32, f32, _, _>(frame, copysign);
                        }
                        F64Abs => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::abs);
                        }
                        F64Neg => {
                            use std::ops::Neg;
                            Interpreter::exec_uniop::<f64, _, _>(frame, Neg::neg);
                        }
                        F64Ceil => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::ceil);
                        }
                        F64Floor => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::floor);
                        }
                        F64Trunc => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::trunc);
                        }
                        F64Nearest => {
                            // TODO: Double-check rounding behavior is correct
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::round);
                        }
                        F64Sqrt => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, f64::sqrt);
                        }
                        F64Add => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::add);
                        }
                        F64Sub => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::sub);
                        }
                        F64Mul => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::mul);
                        }
                        F64Div => {
                            use std::ops::*;
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::div);
                        }
                        F64Min => {
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::min);
                        }
                        F64Max => {
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, f64::max);
                        }
                        F64Copysign => {
                            Interpreter::exec_binop::<f64, f64, _, _>(frame, copysign);
                        }
                        I32WrapI64 => {
                            Interpreter::exec_uniop::<i64, i32, _>(frame, Wrap::wrap);
                        }
                        I32TruncSF32 => {
                            Interpreter::exec_uniop::<f32, i32, _>(frame, truncate_to_int);
                        }
                        I32TruncUF32 => {
                            // TODO: Verify signedness works here
                            Interpreter::exec_uniop::<f32, u32, _>(frame, truncate_to_int);
                        }
                        I32TruncSF64 => {
                            Interpreter::exec_uniop::<f64, i32, _>(frame, truncate_to_int);
                        }
                        I32TruncUF64 => {
                            // TODO: Verify signedness
                            Interpreter::exec_uniop::<f64, u32, _>(frame, truncate_to_int);
                        }
                        I64ExtendSI32 => {
                            Interpreter::exec_uniop::<i32, i64, _>(frame, From::from);
                        }
                        I64ExtendUI32 => {
                            Interpreter::exec_uniop::<u32, i64, _>(frame, From::from);
                        }
                        I64TruncSF32 => {
                            Interpreter::exec_uniop::<f32, i64, _>(frame, truncate_to_int);
                        }
                        I64TruncUF32 => {
                            Interpreter::exec_uniop::<f32, u64, _>(frame, truncate_to_int);
                        }
                        I64TruncSF64 => {
                            Interpreter::exec_uniop::<f64, i64, _>(frame, truncate_to_int);
                        }
                        I64TruncUF64 => {
                            Interpreter::exec_uniop::<f64, u64, _>(frame, truncate_to_int);
                        }
                        F32ConvertSI32 => {
                            Interpreter::exec_uniop::<f32, i32, _>(frame, round_to_int);
                        }
                        F32ConvertUI32 => {
                            Interpreter::exec_uniop::<f32, u32, _>(frame, round_to_int);
                        }
                        F32ConvertSI64 => {
                            Interpreter::exec_uniop::<f32, i64, _>(frame, round_to_int);
                        }
                        F32ConvertUI64 => {
                            Interpreter::exec_uniop::<f32, u64, _>(frame, round_to_int);
                        }
                        F32DemoteF64 => {
                            Interpreter::exec_uniop::<f64, _, _>(frame, |f| f as f32);
                        }
                        F64ConvertSI32 => {
                            Interpreter::exec_uniop::<f64, i32, _>(frame, round_to_int);
                        }
                        F64ConvertUI32 => {
                            Interpreter::exec_uniop::<f64, u32, _>(frame, round_to_int);
                        }
                        F64ConvertSI64 => {
                            Interpreter::exec_uniop::<f64, i64, _>(frame, round_to_int);
                        }
                        F64ConvertUI64 => {
                            Interpreter::exec_uniop::<f64, u64, _>(frame, round_to_int);
                        }
                        F64PromoteF32 => {
                            Interpreter::exec_uniop::<f32, _, _>(frame, f64::from);
                        }
                        I32ReinterpretF32 => {
                            // TODO: Check that this is going the correct direction,
                            // i32 -> f32
                            Interpreter::exec_uniop(frame, f32::from_bits);
                        }
                        I64ReinterpretF64 => {
                            // TODO: Check that this is going the correct direction,
                            // i64 -> f64
                            Interpreter::exec_uniop(frame, f64::from_bits);
                        }
                        F32ReinterpretI32 => {
                            // TODO: Check that this is going the correct direction,
                            // f32 -> i32
                            Interpreter::exec_uniop(frame, f32::to_bits);
                        }
                        F64ReinterpretI64 => {
                            // TODO: Check that this is going the correct direction,
                            // f64 -> i64
                            Interpreter::exec_uniop(frame, f64::to_bits);
                        }
                    }
                    frame.ip += 1;
                }
            }
        }
        // Return the function's return value (if any).
        println!("Value stack is: {:?}", frame.value_stack);
        let return_type = frame.value_stack.last().map(|vl| vl.get_type());
        assert_eq!(return_type, func.functype.return_type);
        frame.value_stack.last().cloned()
    }

    /// A nice shortcut to run `exec()` with appropriate values.
    pub fn run(&mut self, func: FunctionAddress, args: &[Value]) -> Option<Value> {
        let state = &self.state;
        let store = &mut self.store;
        Interpreter::exec(store, state, func, args)
    }

    /// Looks up a function with the given name
    /// and executes it with the given arguments.
    /// Returns the function's return value, if any.
    pub fn run_export(
        &mut self,
        module_name: &str,
        func_name: &str,
        args: &[Value],
    ) -> Result<Option<Value>, Error> {
        let function_addr = {
            // TODO: Probably some duplication with ModuleInstance::resolve_imports()
            // but argh.
            let target_module = self.state
                .module_instances
                .iter()
                .find(|m| module_name == m.name)
                .ok_or(Error::ModuleNotFound {
                    module: module_name.to_owned(),
                    dependent_module: "<Interpreter::run_export()>".to_owned(),
                })?;
            //println!("target module: {:#?}", target_module);
            let function_idx = target_module
                .exported_functions
                .iter()
                .find(|funcs| {
                    // println!("Searching for {}, got {}", func_name, funcs.name);
                    funcs.name == func_name
                })
                .ok_or(Error::NotExported {
                    module: module_name.to_owned(),
                    name: func_name.to_owned(),
                    typ: "function".to_owned(),
                    dependent_module: "<Interpreter::run_export()>".to_owned(),
                })?;
            target_module.functions[function_idx.value.0]
        };
        Ok(self.run(function_addr, args))
    }
}