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
use std::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::mem;
use std::str;
use std::iter;

use indexmap::IndexMap;
use parity_wasm::elements as pw;
use parity_wasm::elements::Deserialize;

trait IterExt: Iterator + Sized {
    fn enumerate_u32( self ) -> iter::Map< iter::Enumerate< Self >, fn( (usize, Self::Item) ) -> (u32, Self::Item) > {
        self.enumerate().map( |(index, value)| (index as u32, value) )
    }
}

impl< T: Iterator > IterExt for T {}

pub type TypeIndex = u32;
pub type FunctionIndex = u32;
pub type TableIndex = u32;
pub type MemoryIndex = u32;
pub type GlobalIndex = u32;

pub use parity_wasm::elements::ValueType;
pub use parity_wasm::elements::Instruction;

#[derive(Clone, PartialEq, Debug)]
pub struct FnTy {
    pub params: Vec< ValueType >,
    pub return_type: Option< ValueType >
}

impl Eq for FnTy {}
impl Hash for FnTy {
    fn hash< H: Hasher >( &self, state: &mut H ) {
        for param in &self.params {
            (*param as i32).hash( state );
        }
        self.return_type.clone().map( |ret| ret as i32 ).hash( state );
    }
}

pub trait ImportExport {
    fn is_imported( &self ) -> bool;
    fn as_export( &self ) -> &Export;
    fn as_export_mut( &mut self ) -> &mut Export;
    fn is_exported( &self ) -> bool {
        !self.as_export().names.is_empty()
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct Import {
    pub module: String,
    pub field: String
}

#[derive(Clone, PartialEq, Debug)]
pub struct Export {
    pub names: Vec< String >
}

impl Export {
    pub fn none() -> Self {
        Export {
            names: Vec::new()
        }
    }

    pub fn some( name: String ) -> Self {
        Export {
            names: vec![ name ]
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct Local {
    pub count: u32,
    pub ty: ValueType,
    pub name: Option< String >
}

#[derive(Clone, PartialEq, Debug)]
pub enum FunctionKind {
    Import {
        export: Export,
        type_index: TypeIndex,
        name: Option< String >,
        import: Import
    },
    Definition {
        export: Export,
        type_index: TypeIndex,
        name: Option< String >,
        locals: Vec< Local >,
        instructions: Vec< Instruction >
    }
}

impl ImportExport for FunctionKind {
    fn is_imported( &self ) -> bool {
        match self {
            &FunctionKind::Import { .. } => true,
            _ => false
        }
    }

    fn as_export( &self ) -> &Export {
        match self {
            &FunctionKind::Import { ref export, .. } => export,
            &FunctionKind::Definition { ref export, .. } => export,
        }
    }

    fn as_export_mut( &mut self ) -> &mut Export {
        match self {
            &mut FunctionKind::Import { ref mut export, .. } => export,
            &mut FunctionKind::Definition { ref mut export, .. } => export,
        }
    }
}

impl FunctionKind {
    fn name_mut( &mut self ) -> &mut Option< String > {
        match self {
            &mut FunctionKind::Import { ref mut name, .. } => name,
            &mut FunctionKind::Definition { ref mut name, .. } => name
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct Limits {
    pub min: u32,
    pub max: Option< u32 >
}

#[derive(Clone, PartialEq, Debug)]
pub enum TableKind {
    Import {
        export: Export,
        limits: Limits,
        import: Import,
    },
    Definition {
        export: Export,
        limits: Limits
    }
}

impl ImportExport for TableKind {
    fn is_imported( &self ) -> bool {
        match self {
            &TableKind::Import { .. } => true,
            _ => false
        }
    }

    fn as_export( &self ) -> &Export {
        match self {
            &TableKind::Import { ref export, .. } => export,
            &TableKind::Definition { ref export, .. } => export,
        }
    }

    fn as_export_mut( &mut self ) -> &mut Export {
        match self {
            &mut TableKind::Import { ref mut export, .. } => export,
            &mut TableKind::Definition { ref mut export, .. } => export,
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub enum MemoryKind {
    Import {
        export: Export,
        limits: Limits,
        import: Import
    },
    Definition {
        export: Export,
        limits: Limits
    }
}

impl ImportExport for MemoryKind {
    fn is_imported( &self ) -> bool {
        match self {
            &MemoryKind::Import { .. } => true,
            _ => false
        }
    }

    fn as_export( &self ) -> &Export {
        match self {
            &MemoryKind::Import { ref export, .. } => export,
            &MemoryKind::Definition { ref export, .. } => export,
        }
    }

    fn as_export_mut( &mut self ) -> &mut Export {
        match self {
            &mut MemoryKind::Import { ref mut export, .. } => export,
            &mut MemoryKind::Definition { ref mut export, .. } => export,
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct GlobalType {
    ty: ValueType,
    is_mutable: bool
}

#[derive(Clone, PartialEq, Debug)]
pub enum GlobalKind {
    Import {
        export: Export,
        global_type: GlobalType,
        import: Import
    },
    Definition {
        export: Export,
        global_type: GlobalType,
        initializer: Vec< Instruction >
    }
}

impl ImportExport for GlobalKind {
    fn is_imported( &self ) -> bool {
        match self {
            &GlobalKind::Import { .. } => true,
            _ => false
        }
    }

    fn as_export( &self ) -> &Export {
        match self {
            &GlobalKind::Import { ref export, .. } => export,
            &GlobalKind::Definition { ref export, .. } => export,
        }
    }

    fn as_export_mut( &mut self ) -> &mut Export {
        match self {
            &mut GlobalKind::Import { ref mut export, .. } => export,
            &mut GlobalKind::Definition { ref mut export, .. } => export,
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct FnPointerTable {
    members: Vec< FunctionIndex >,
    offset: Vec< Instruction >
}

#[derive(Clone, PartialEq, Debug)]
pub struct Data {
    pub offset: Vec< Instruction >,
    pub value: Vec< u8 >
}

impl Data {
    pub fn constant_offset( &self ) -> Option< i32 > {
        if self.offset.len() != 2 {
            return None;
        }

        match (&self.offset[ 0 ], &self.offset[ 1 ]) {
            (&Instruction::I32Const( offset ), &Instruction::End) => Some( offset ),
            _ => None
        }
    }
}

#[derive(Clone, Debug)]
pub struct Context {
    pub types: IndexMap< TypeIndex, FnTy >,
    pub functions: IndexMap< FunctionIndex, FunctionKind >,
    pub tables: IndexMap< TableIndex, TableKind >,
    pub memories: IndexMap< MemoryIndex, MemoryKind >,
    pub globals: IndexMap< GlobalIndex, GlobalKind >,
    pub start: Option< FunctionIndex >,
    pub fn_pointer_tables: Option< FnPointerTable >,
    pub data: Vec< Data >,
    pub module_name: Option< String >,
    pub source_mapping_url: Option< String >,
    next_function_index: u32,
    next_type_index: u32
}

fn take< T: Default >( reference: &mut T ) -> T {
    let mut out = T::default();
    mem::swap( &mut out, reference );
    out
}

// This is based on code from wasm-gc.
fn decode_name_map< F: for< 'a > FnMut( u32, &'a str ) > ( p: &mut &[u8], mut callback: F ) -> Result< (), pw::Error > {
    let count = u32::from( pw::VarUint32::deserialize( p )? );
    for _ in 0..count {
        let index = u32::from( pw::VarUint32::deserialize( p )? );
        let name_length = u32::from( pw::VarUint32::deserialize( p )? );
        let (name, next_p) = p.split_at( name_length as usize );
        *p = next_p;

        let name = str::from_utf8( name ).expect( "function has an ill-formed name" );
        callback( index, name );
    }

    Ok(())
}

fn write_with_length< F: FnOnce( &mut Vec< u8 > ) >( output: &mut Vec< u8 >, callback: F ) {
    let mut buffer = Vec::new();
    callback( &mut buffer );

    pw::Serialize::serialize( pw::VarUint32::from( buffer.len() ), output ).unwrap();
    output.extend_from_slice( buffer.as_slice() );
}

fn write_name_map( output: &mut Vec< u8 >, map: &[(u32, String)] ) {
    pw::Serialize::serialize( pw::VarUint32::from( map.len() ), output ).unwrap();
    for &(index, ref name) in map {
        pw::Serialize::serialize( pw::VarUint32::from( index ), output ).unwrap();
        write_string( output, name );
    }
}

fn write_string( output: &mut Vec< u8 >, string: &str ) {
    pw::Serialize::serialize( pw::VarUint32::from( string.len() ), output ).unwrap();
    output.extend_from_slice( string.as_bytes() );
}

fn serialize_name_section(
    module_name: Option< String >,
    function_names: Vec< (u32, String) >,
    local_names: Vec< (u32, Vec< (u32, String) >) >
) -> Vec< u8 > {
    let mut output = Vec::new();
    write_with_length( &mut output, move |body| {
        write_string( body, "name" );

        if let Some( module_name ) = module_name {
            pw::Serialize::serialize( pw::VarUint7::from( 0 ), body ).unwrap();
            write_string( body, &module_name );
        }

        if !function_names.is_empty() {
            pw::Serialize::serialize( pw::VarUint7::from( 1 ), body ).unwrap();
            write_with_length( body, |inner_body| {
                write_name_map( inner_body, &function_names );
            });
        }

        if !local_names.is_empty() {
            pw::Serialize::serialize( pw::VarUint7::from( 2 ), body ).unwrap();
            write_with_length( body, |inner_body| {
                pw::Serialize::serialize( pw::VarUint32::from( local_names.len() ), inner_body ).unwrap();
                for (function_index, names) in local_names {
                    pw::Serialize::serialize( pw::VarUint32::from( function_index ), inner_body ).unwrap();
                    write_name_map( inner_body, &names );
                }
            });
        }
    });

    output
}

struct Entities< T > {
    index_map: HashMap< u32, u32 >,
    entries: Vec< (u32, T) >
}

fn preprocess_entities< T: ImportExport >( map: IndexMap< u32, T > ) -> Entities< T > {
    let mut index_map = HashMap::with_capacity( map.len() );
    let (imports, definitions): (Vec< _ >, Vec< _ >) = map.into_iter().partition( |&(_, ref entity)| entity.is_imported() );
    let mut entries = Vec::new();

    let import_count = imports.len() as u32;
    for (new_index, (old_index, entry)) in imports.into_iter().enumerate_u32() {
        index_map.insert( old_index, new_index );
        entries.push( (new_index, entry) );
    }

    for (index_offset, (old_index, entry)) in definitions.into_iter().enumerate_u32() {
        let new_index = import_count + index_offset;
        index_map.insert( old_index, new_index );
        entries.push( (new_index, entry) );
    }

    Entities {
        index_map,
        entries
    }
}

impl Context {
    pub fn new() -> Self {
        Context {
            types: Default::default(),
            functions: Default::default(),
            tables: Default::default(),
            memories: Default::default(),
            globals: Default::default(),
            start: Default::default(),
            fn_pointer_tables: Default::default(),
            data: Default::default(),
            module_name: Default::default(),
            source_mapping_url: Default::default(),
            next_function_index: 0,
            next_type_index: 0
        }
    }

    pub fn from_module( mut module: pw::Module ) -> Self {
        let mut ctx = Self::new();

        let mut next_table_index = 0;
        let mut next_memory_index = 0;
        let mut next_global_index = 0;
        let mut function_imports_count = 0;

        let mut function_sections = Vec::new();
        let mut exports_sections = Vec::new();

        let sections = take( module.sections_mut() );
        for section in sections {
            match section {
                pw::Section::Type( mut section ) => {
                    for ty in take( section.types_mut() ) {
                        let pw::Type::Function( mut ty ) = ty;
                        ctx.types.insert( ctx.next_type_index, FnTy {
                            params: take( ty.params_mut() ),
                            return_type: ty.return_type()
                        });
                        ctx.next_type_index += 1;
                    }
                },
                pw::Section::Import( mut section ) => {
                    for entry in take( section.entries_mut() ) {
                        let import = Import {
                            module: entry.module().to_owned(),
                            field: entry.field().to_owned()
                        };

                        match entry.external() {
                            &pw::External::Function( type_index ) => {
                                assert!( ctx.types.get( &type_index ).is_some() );
                                ctx.functions.insert( ctx.next_function_index, FunctionKind::Import {
                                    export: Export::none(),
                                    type_index,
                                    import,
                                    name: None
                                });
                                ctx.next_function_index += 1;
                                function_imports_count += 1;
                            },
                            &pw::External::Table( ref table_type ) => {
                                let limits = Limits {
                                    min: table_type.limits().initial(),
                                    max: table_type.limits().maximum()
                                };
                                ctx.tables.insert( next_table_index, TableKind::Import {
                                    export: Export::none(),
                                    limits,
                                    import
                                });
                                next_table_index += 1;
                            },
                            &pw::External::Memory( ref memory_type ) => {
                                let limits = Limits {
                                    min: memory_type.limits().initial(),
                                    max: memory_type.limits().maximum()
                                };
                                ctx.memories.insert( next_memory_index, MemoryKind::Import {
                                    export: Export::none(),
                                    limits,
                                    import
                                });
                                next_memory_index += 1;
                            },
                            &pw::External::Global( ref global_type ) => {
                                ctx.globals.insert( next_global_index, GlobalKind::Import {
                                    export: Export::none(),
                                    global_type: GlobalType {
                                        ty: global_type.content_type(),
                                        is_mutable: global_type.is_mutable()
                                    },
                                    import
                                });
                                next_global_index += 1;
                            }
                        }
                    }
                },
                pw::Section::Table( mut section ) => {
                    for table_type in take( section.entries_mut() ) {
                        let limits = Limits {
                            min: table_type.limits().initial(),
                            max: table_type.limits().maximum()
                        };
                        ctx.tables.insert( next_table_index, TableKind::Definition {
                            export: Export::none(),
                            limits
                        });
                        next_table_index += 1;
                    }
                },
                pw::Section::Memory( mut section ) => {
                    for memory_type in take( section.entries_mut() ) {
                        let limits = Limits {
                            min: memory_type.limits().initial(),
                            max: memory_type.limits().maximum()
                        };
                        ctx.memories.insert( next_memory_index, MemoryKind::Definition {
                            export: Export::none(),
                            limits
                        });
                        next_memory_index += 1;
                    }
                },
                pw::Section::Global( mut section ) => {
                    for mut global in take( section.entries_mut() ) {
                        ctx.globals.insert( next_global_index, GlobalKind::Definition {
                            export: Export::none(),
                            global_type: GlobalType {
                                ty: global.global_type().content_type(),
                                is_mutable: global.global_type().is_mutable()
                            },
                            initializer: take( global.init_expr_mut().code_mut() )
                        });
                        next_global_index += 1;
                    }
                },
                pw::Section::Export( mut section ) => {
                    exports_sections.push( section );
                },
                pw::Section::Start( function_index ) => {
                    ctx.start = Some( function_index );
                },
                pw::Section::Element( mut section ) => {
                    let entries = take( section.entries_mut() );
                    if entries.is_empty() {
                        continue;
                    }
                    assert_eq!( entries.len(), 1, "multiple Element tables are not supported" );
                    assert!( ctx.fn_pointer_tables.is_none(), "duplicate Element table" );
                    let mut entry = entries.into_iter().next().unwrap();
                    ctx.fn_pointer_tables = Some( FnPointerTable {
                        members: take( entry.members_mut() ),
                        offset: take( entry.offset_mut().as_mut().unwrap().code_mut() )
                    });
                },
                pw::Section::Code( mut section ) => {
                    for mut body in take( section.bodies_mut() ) {
                        ctx.functions.insert( ctx.next_function_index, FunctionKind::Definition {
                            export: Export::none(),
                            name: None,
                            type_index: 0xFFFFFFFF,
                            locals: take( body.locals_mut() ).into_iter()
                                .map( |local| {
                                    Local {
                                        count: local.count(),
                                        ty: local.value_type(),
                                        name: None
                                    }
                                }).collect(),
                            instructions: take( body.code_mut().elements_mut() )
                        });
                        ctx.next_function_index += 1;
                    }
                },
                pw::Section::Function( mut section ) => {
                    // We'll convert it later since those can appear before
                    // the functions themselves are actually defined.
                    function_sections.push( section );
                },
                pw::Section::Data( mut section ) => {
                    for mut entry in take( section.entries_mut() ) {
                        if ctx.data.last().map( |last_data| {
                            last_data.offset == entry.offset().as_ref().unwrap().code() && last_data.value.is_empty()
                        }).unwrap_or( false ) {
                            // Workaround for a `rustc`/LLVM bug where a duplicate empty data
                            // entries are generated.
                            ctx.data.pop();
                        }

                        ctx.data.push( Data {
                            offset: take( entry.offset_mut().as_mut().unwrap().code_mut() ),
                            value: take( entry.value_mut() )
                        });
                    }
                },
                pw::Section::Custom( mut section ) => {
                    if section.name() == "name" {
                        let payload = take( section.payload_mut() );

                        let mut p: &[u8] = &payload;
                        while p.len() > 0 {
                            let kind = u8::from( pw::VarUint7::deserialize( &mut p ).unwrap() );
                            let payload_length = u32::from( pw::VarUint32::deserialize( &mut p ).unwrap() );
                            let (mut payload, next_p) = p.split_at( payload_length as usize );
                            p = next_p;

                            match kind {
                                0 => {
                                    ctx.module_name = Some(
                                        String::from_utf8( payload.to_vec() )
                                            .expect( "module has an ill-formed name" )
                                    );
                                },
                                1 => {
                                    decode_name_map( &mut payload, |function_index, function_name| {
                                        let name = ctx.functions.get_mut( &function_index ).unwrap().name_mut();
                                        assert!( name.is_none() );
                                        *name = Some( function_name.to_owned() );
                                    }).unwrap();
                                },
                                2 => {
                                    let count = u32::from( pw::VarUint32::deserialize( &mut payload ).unwrap() );
                                    for _ in 0..count {
                                        let function_index = u32::from( pw::VarUint32::deserialize( &mut payload ).unwrap() );
                                        decode_name_map( &mut payload, |local_index, name| {
                                            match ctx.functions.get_mut( &function_index ).unwrap() {
                                                &mut FunctionKind::Definition { ref mut locals, .. } => {
                                                    let local = &mut locals[ local_index as usize ];
                                                    assert!( local.name.is_none(), "duplicate local variable name" );
                                                    local.name = Some( name.to_owned() );
                                                },
                                                _ => panic!()
                                            }
                                        }).unwrap();
                                    }

                                },
                                kind => panic!( "unknown name section chunk type: {}", kind )
                            }
                        }
                    } else if section.name() == "sourceMappingURL" {
                        let payload = take( section.payload_mut() );
                        let mut p: &[u8] = &payload;
                        let url_length = u32::from( pw::VarUint32::deserialize( &mut p ).unwrap() );
                        let url = str::from_utf8( &p[ 0..url_length as usize ] ).expect( "invalid sourceMappingURL" );
                        ctx.source_mapping_url = Some( url.to_owned() );
                    } else if section.name() == "linking" || section.name().starts_with( ".debug_" ) {
                        // TODO: Support this section.
                    } else {
                        debug!( "unsupported custom section: '{}'", section.name() );
                    }
                },
                pw::Section::Name(_) => {
                    // Section::Name is only emitted when calling module.parse_names()
                    unreachable!()
                },
                pw::Section::Reloc(_) |
                pw::Section::Unparsed { .. } => { unimplemented!() },
            }
        }

        for mut section in function_sections {
            let mut function_index = function_imports_count;
            for entry in take( section.entries_mut() ) {
                match ctx.functions.get_mut( &function_index ).unwrap() {
                    &mut FunctionKind::Definition { ref mut type_index, .. } => {
                        assert_eq!( *type_index, 0xFFFFFFFF, "function type was already set" );
                        *type_index = entry.type_ref();
                    },
                    _ => panic!()
                }
                function_index += 1;
            }
        }

        for mut section in exports_sections {
            for mut entry in take( section.entries_mut() ) {
                let name = take( entry.field_mut() );
                match entry.internal() {
                    &pw::Internal::Function( function_index ) => {
                        ctx.functions.get_mut( &function_index ).unwrap().as_export_mut().names.push( name );
                    },
                    &pw::Internal::Table( table_index ) => {
                        ctx.tables.get_mut( &table_index ).unwrap().as_export_mut().names.push( name );
                    },
                    &pw::Internal::Memory( memory_index ) => {
                        ctx.memories.get_mut( &memory_index ).unwrap().as_export_mut().names.push( name );
                    },
                    &pw::Internal::Global( global_index ) => {
                        ctx.globals.get_mut( &global_index ).unwrap().as_export_mut().names.push( name );
                    }
                }
            }
        }

        ctx
    }

    pub fn into_module( self ) -> pw::Module {
        fn process_instructions(
            function_index_map: &HashMap< FunctionIndex, FunctionIndex >,
            type_index_map: &HashMap< TypeIndex, TypeIndex >,
            global_index_map: &HashMap< GlobalIndex, GlobalIndex >,
            instructions: &mut Vec< Instruction >
        ) {
            for instruction in instructions {
                match instruction {
                    &mut Instruction::Call( ref mut index ) => {
                        *index = function_index_map.get( &index ).cloned().unwrap();
                    },
                    &mut Instruction::CallIndirect( ref mut index, _ ) => {
                        *index = type_index_map.get( &index ).cloned().unwrap();
                    },
                    &mut Instruction::GetGlobal( ref mut index ) |
                    &mut Instruction::SetGlobal( ref mut index ) => {
                        *index = global_index_map.get( &index ).cloned().unwrap();
                    },
                    _ => {}
                }
            }
        }

        let mut sections = Vec::new();

        let mut type_map = HashMap::new();
        let mut section_types = Vec::with_capacity( self.types.len() );
        let mut section_imports = Vec::new();
        let mut section_functions = Vec::new();
        let mut section_tables = Vec::with_capacity( self.tables.len() );
        let mut section_memories = Vec::with_capacity( self.memories.len() );
        let mut section_code = Vec::new();
        let mut section_exports = Vec::new();
        let mut section_elements = Vec::new();
        let mut section_globals = Vec::new();
        let mut section_data = Vec::new();
        let mut function_names = Vec::new();
        let mut function_variable_names = Vec::new();

        for (new_type_index, (old_type_index, ty)) in self.types.into_iter().enumerate_u32() {
            type_map.insert( old_type_index, new_type_index );
            let params = ty.params;
            let return_ty = ty.return_type;
            section_types.push( pw::Type::Function( pw::FunctionType::new( params, return_ty ) ) );
        }

        let functions = preprocess_entities( self.functions );
        let tables = preprocess_entities( self.tables );
        let memories = preprocess_entities( self.memories );
        let globals = preprocess_entities( self.globals );

        for (new_index, function) in functions.entries {
            let export = match function {
                FunctionKind::Import { type_index, import, export, name } => {
                    let type_index = type_map.get( &type_index ).cloned().unwrap();
                    section_imports.push( pw::ImportEntry::new(
                        import.module,
                        import.field,
                        pw::External::Function( type_index )
                    ));

                    if let Some( name ) = name {
                        function_names.push( (new_index, name) );
                    }

                    export
                },
                FunctionKind::Definition { name, type_index, locals, mut instructions, export } => {
                    let type_index = type_map.get( &type_index ).cloned().unwrap();
                    let mut local_names = Vec::new();
                    let locals = locals.into_iter().enumerate_u32().map( |(local_index, local)| {
                        if let Some( local_name ) = local.name {
                            local_names.push( (local_index, local_name) );
                        }
                        pw::Local::new( local.count, local.ty )
                    }).collect();

                    process_instructions( &functions.index_map, &type_map, &globals.index_map, &mut instructions );

                    if let Some( name ) = name {
                        function_names.push( (new_index, name) );
                    }
                    if !local_names.is_empty() {
                        function_variable_names.push( (new_index, local_names) );
                    }
                    section_functions.push( pw::Func::new( type_index ) );
                    section_code.push( pw::FuncBody::new( locals, pw::Instructions::new( instructions ) ) );

                    export
                }
            };

            for name in export.names {
                section_exports.push( pw::ExportEntry::new( name, pw::Internal::Function( new_index ) ) );
            }
        }

        for (new_index, table) in tables.entries {
            let export = match table {
                TableKind::Import { import, limits, export } => {
                    section_imports.push( pw::ImportEntry::new(
                        import.module,
                        import.field,
                        pw::External::Table( pw::TableType::new( limits.min, limits.max ) )
                    ));

                    export
                },
                TableKind::Definition { limits, export } => {
                    section_tables.push( pw::TableType::new( limits.min, limits.max ) );
                    export
                }
            };

            for name in export.names {
                section_exports.push( pw::ExportEntry::new( name, pw::Internal::Table( new_index ) ) );
            }
        }

        for (new_index, memory) in memories.entries {
            let export = match memory {
                MemoryKind::Import { import, limits, export } => {
                    section_imports.push( pw::ImportEntry::new(
                        import.module,
                        import.field,
                        pw::External::Memory( pw::MemoryType::new( limits.min, limits.max, false ) )
                    ));
                    export
                },
                MemoryKind::Definition { limits, export } => {
                    section_memories.push( pw::MemoryType::new( limits.min, limits.max, false ) );
                    export
                }
            };

            for name in export.names {
                section_exports.push( pw::ExportEntry::new( name, pw::Internal::Memory( new_index ) ) );
            }
        }

        for (new_index, global) in globals.entries {
            let export = match global {
                GlobalKind::Import { global_type, import, export } => {
                    section_imports.push( pw::ImportEntry::new(
                        import.module,
                        import.field,
                        pw::External::Global( pw::GlobalType::new( global_type.ty, global_type.is_mutable ) )
                    ));

                    export
                },
                GlobalKind::Definition { global_type, mut initializer, export } => {
                    process_instructions( &functions.index_map, &type_map, &globals.index_map, &mut initializer );

                    let global_type = pw::GlobalType::new( global_type.ty, global_type.is_mutable );
                    let entry = pw::GlobalEntry::new( global_type, pw::InitExpr::new( initializer ) );
                    section_globals.push( entry );

                    export
                }
            };

            for name in export.names {
                section_exports.push( pw::ExportEntry::new( name, pw::Internal::Global( new_index ) ) );
            }
        }

        for mut pointer_table in self.fn_pointer_tables {
            process_instructions( &functions.index_map, &type_map, &globals.index_map, &mut pointer_table.offset );
            for function_index in &mut pointer_table.members {
                *function_index = functions.index_map.get( &function_index ).cloned().unwrap();
            }

            let entry = pw::ElementSegment::new( 0, Some( pw::InitExpr::new( pointer_table.offset ) ), pointer_table.members, false );
            section_elements.push( entry );
        }

        for data in self.data {
            section_data.push( pw::DataSegment::new( 0, Some( pw::InitExpr::new( data.offset ) ), data.value, false ) );
        }

        if !section_types.is_empty() {
            sections.push( pw::Section::Type( pw::TypeSection::with_types( section_types ) ) );
        }

        if !section_imports.is_empty() {
            sections.push( pw::Section::Import( pw::ImportSection::with_entries( section_imports ) ) );
        }

        if !section_functions.is_empty() {
            sections.push( pw::Section::Function( pw::FunctionSection::with_entries( section_functions ) ) );
        }

        if !section_tables.is_empty() {
            sections.push( pw::Section::Table( pw::TableSection::with_entries( section_tables ) ) );
        }

        if !section_memories.is_empty() {
            sections.push( pw::Section::Memory( pw::MemorySection::with_entries( section_memories ) ) );
        }

        if !section_globals.is_empty() {
            sections.push( pw::Section::Global( pw::GlobalSection::with_entries( section_globals ) ) );
        }

        if !section_exports.is_empty() {
            sections.push( pw::Section::Export( pw::ExportSection::with_entries( section_exports ) ) );
        }

        if let Some( mut start ) = self.start {
            start = functions.index_map.get( &start ).cloned().unwrap();
            sections.push( pw::Section::Start( start ) );
        }

        if !section_elements.is_empty() {
            sections.push( pw::Section::Element( pw::ElementSection::with_entries( section_elements ) ) );
        }

        if !section_code.is_empty() {
            sections.push( pw::Section::Code( pw::CodeSection::with_bodies( section_code ) ) );
        }

        if !section_data.is_empty() {
            sections.push( pw::Section::Data( pw::DataSection::with_entries( section_data ) ) );
        }

        if self.module_name.is_some() || !function_names.is_empty() || !function_variable_names.is_empty() {
            let name_section_bytes = serialize_name_section( self.module_name, function_names, function_variable_names );
            sections.push( pw::Section::Custom(
                pw::CustomSection::deserialize( &mut name_section_bytes.as_slice() ).unwrap()
            ));
        }

        if let Some( source_mapping_url ) = self.source_mapping_url {
            let mut section_bytes = Vec::new();
            write_with_length( &mut section_bytes, move |body| {
                write_string( body, "sourceMappingURL" );
                write_string( body, &source_mapping_url );
            });
            sections.push( pw::Section::Custom(
                pw::CustomSection::deserialize( &mut section_bytes.as_slice() ).unwrap()
            ));
        }

        pw::Module::new( sections )
    }

    pub fn add_function( &mut self, function: FunctionKind ) -> FunctionIndex {
        let function_index = self.next_function_index;
        self.next_function_index += 1;
        self.functions.insert( function_index, function );
        function_index
    }

    pub fn fn_ty_by_index( &self, index: TypeIndex ) -> Option< &FnTy > {
        self.types.get( &index )
    }

    pub fn get_or_add_fn_type( &mut self, ty: FnTy ) -> u32 {
        if let Some( (&type_index, _) ) = self.types.iter().find( |&(_, rty)| ty == *rty ) {
            return type_index;
        }

        let type_index = self.next_type_index;
        self.next_type_index += 1;
        self.types.insert( type_index, ty );
        type_index
    }

    pub fn patch_code< F >( &mut self, mut callback: F ) where F: for <'r> FnMut( &'r mut Vec< Instruction > ) {
        for function in self.functions.values_mut() {
            match function {
                &mut FunctionKind::Definition { ref mut instructions, .. } => {
                    callback( instructions );
                },
                _ => {}
            }
        }

        // TODO: Other places where instructions are used.
    }
}

#[test]
fn test_serialization_deserialization() {
    let mut ctx = Context::new();
    let type_index = ctx.get_or_add_fn_type( FnTy { params: vec![ ValueType::I32 ], return_type: None } );
    ctx.module_name = Some( "module_1".to_owned() );
    ctx.add_function( FunctionKind::Definition {
        export: Export::some( "foobar".to_owned() ),
        type_index,
        name: Some( "func_1".to_owned() ),
        locals: vec![
            Local {
                count: 1,
                ty: ValueType::I32,
                name: Some( "v1".to_owned() )
            }
        ],
        instructions: vec![]
    });

    let new_ctx = Context::from_module( ctx.clone().into_module() );
    assert_eq!( new_ctx.module_name, ctx.module_name );
    assert_eq!( new_ctx.functions, ctx.functions );
    assert_eq!( new_ctx.types, ctx.types );
}

#[test]
fn test_function_import_removal() {
    let mut ctx = Context::new();
    let type_index = ctx.get_or_add_fn_type( FnTy { params: vec![], return_type: None } );
    ctx.add_function( FunctionKind::Import {
        type_index,
        export: Export::none(),
        import: Import {
            module: "env".to_owned(),
            field: "foobar".to_owned()
        },
        name: Some( "foobar".to_owned() )
    });
    ctx.add_function( FunctionKind::Definition {
        type_index,
        export: Export::some( "main".to_owned() ),
        name: Some( "main".to_owned() ),
        locals: vec![
            Local {
                count: 1,
                ty: ValueType::I32,
                name: Some( "v1".to_owned() )
            }
        ],
        instructions: vec![
            Instruction::Call( 1 )
        ],
    });
    ctx.start = Some( 1 );

    ctx.functions.remove( &0 );
    let new_ctx = Context::from_module( ctx.clone().into_module() );
    assert_eq!( new_ctx.functions.len(), 1 );

    match new_ctx.functions[ &0 ] {
        FunctionKind::Definition { ref name, ref export, ref instructions, .. } => {
            assert_eq!( name.as_ref().unwrap(), "main" );
            assert_eq!( *export, Export::some( "main".to_owned() ) );
            assert_eq!( *instructions, &[Instruction::Call( 0 )] );
        },
        _ => panic!()
    }

    assert_eq!( new_ctx.start, Some( 0 ) );
}