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
//! Bindings to the [wabt](https://github.com/WebAssembly/wabt) library.
//!

#![deny(missing_docs)]

extern crate wabt_sys;

use std::os::raw::{c_void, c_int};
use std::ffi::{CString, NulError};
use std::slice;
use std::ptr;

use wabt_sys as ffi;

/// A structure to represent errors coming out from wabt.
///
/// Actual errors are not yet published.
#[derive(Debug, PartialEq, Eq)]
pub struct Error(ErrorKind);

#[derive(Debug, PartialEq, Eq)]
enum ErrorKind {
    Nul,
    Deserialize(String),
    Parse(String),
    WriteText,
    NonUtf8Result,
    WriteBinary,
    ResolveNames(String),
    Validate(String),
}

impl From<NulError> for Error {
    fn from(_e: NulError) -> Error {
        Error(ErrorKind::Nul)
    }
}

struct Lexer {
    _filename: CString,
    _buffer: Vec<u8>,
    raw_lexer: *mut ffi::WastLexer,
}

impl Lexer {
    fn new(filename: &str, buffer: &[u8]) -> Result<Lexer, Error> {
        // TODO: Don't copy.
        let filename = CString::new(filename)?;
        let buffer = buffer.to_owned();
        let lexer = unsafe {
            ffi::wabt_new_wast_buffer_lexer(
                filename.as_ptr(),
                buffer.as_ptr() as *const c_void,
                buffer.len(),
            )
        };

        Ok(Lexer {
            _filename: filename,
            _buffer: buffer,
            raw_lexer: lexer,
        })
    }
}

impl Drop for Lexer {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_wast_lexer(self.raw_lexer);
        }
    }
}

struct ErrorHandler {
    raw_buffer: *mut ffi::ErrorHandlerBuffer,
}

impl ErrorHandler {
    fn new_binary() -> ErrorHandler {
        let raw_buffer = unsafe {
            ffi::wabt_new_binary_error_handler_buffer()
        };
        ErrorHandler {
            raw_buffer,
        }
    }

    fn new_text() -> ErrorHandler {
        let raw_buffer = unsafe {
            ffi::wabt_new_text_error_handler_buffer()
        };
        ErrorHandler {
            raw_buffer,
        }
    }

    fn raw_message(&self) -> &[u8] {
        unsafe {
            let size = ffi::wabt_error_handler_buffer_get_size(self.raw_buffer);
            if size == 0 {
                return &[];
            }

            let data = ffi::wabt_error_handler_buffer_get_data(self.raw_buffer);
            slice::from_raw_parts(data as *const u8, size)
        }
    }
}

impl Drop for ErrorHandler {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_error_handler_buffer(self.raw_buffer);
        }
    }
}

struct ParseWatResult {
    raw_result: *mut ffi::WabtParseWatResult,
}

impl ParseWatResult {
    fn is_ok(&self) -> bool {
        unsafe {
            ffi::wabt_parse_wat_result_get_result(self.raw_result) == ffi::Result::Ok
        }
    }

    fn take_module(self) -> Result<*mut ffi::WasmModule, ()> {
        if self.is_ok() {
            unsafe {
                Ok(ffi::wabt_parse_wat_result_release_module(self.raw_result))
            }
        } else {
            Err(())
        }
    }
}

impl Drop for ParseWatResult {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_parse_wat_result(self.raw_result);
        }
    }
}

fn parse_wat(lexer: &Lexer, error_handler: &ErrorHandler) -> ParseWatResult {
    let raw_result = unsafe {
        ffi::wabt_parse_wat(lexer.raw_lexer, error_handler.raw_buffer)
    };
    ParseWatResult {
        raw_result,
    }
}

struct ReadBinaryResult {
    raw_result: *mut ffi::WabtReadBinaryResult,
}

impl ReadBinaryResult {
    fn is_ok(&self) -> bool {
        unsafe {
            ffi::wabt_read_binary_result_get_result(self.raw_result) == ffi::Result::Ok
        }
    }

    fn take_module(self) -> Result<*mut ffi::WasmModule, ()> {
        if self.is_ok() {
            unsafe {
                Ok(ffi::wabt_read_binary_result_release_module(self.raw_result))
            }
        } else {
            Err(())
        }
    }
}

impl Drop for ReadBinaryResult {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_read_binary_result(self.raw_result);
        }
    }
}

/// Buffer returned by wabt.
/// 
/// # Examples 
/// 
/// You can convert it either to `Vec`:
/// 
/// ```rust
/// # extern crate wabt;
/// # let wabt_buf = wabt::Wat2Wasm::new().convert("(module)").unwrap();
/// let vec: Vec<u8> = wabt_buf.as_ref().to_vec();
/// ```
/// 
/// Or in `String`:
/// 
/// ```rust
/// # extern crate wabt;
/// # let wabt_buf = wabt::Wat2Wasm::new().convert("(module)").unwrap();
/// let text = String::from_utf8(wabt_buf.as_ref().to_vec()).unwrap();
/// ```
/// 
pub struct WabtBuf {
    raw_buffer: *mut ffi::OutputBuffer,
}

impl AsRef<[u8]> for WabtBuf {
    fn as_ref(&self) -> &[u8] {
        unsafe {
            let size = ffi::wabt_output_buffer_get_size(self.raw_buffer);
            if size == 0 {
                return &[];
            }
            
            let data = ffi::wabt_output_buffer_get_data(self.raw_buffer) as *const u8;

            slice::from_raw_parts(data, size)
        }
    }
}

impl Drop for WabtBuf {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_output_buffer(self.raw_buffer);
        }
    }
}

struct WriteModuleResult {
    raw_result: *mut ffi::WabtWriteModuleResult,
}

impl WriteModuleResult {
    fn is_ok(&self) -> bool {
        unsafe {
            ffi::wabt_write_module_result_get_result(self.raw_result) == ffi::Result::Ok
        }
    }

    fn take_wabt_buf(self) -> Result<WabtBuf, ()> {
        if self.is_ok() {
            let raw_buffer = unsafe {
                ffi::wabt_write_module_result_release_output_buffer(self.raw_result)
            };
            Ok(WabtBuf {
                raw_buffer,
            })
        } else {
            Err(())
        }
    }
}

impl Drop for WriteModuleResult {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_write_module_result(self.raw_result)
        }
    }
}

struct WriteBinaryOptions {
    log: bool,
    canonicalize_lebs: bool,
    relocatable: bool,
    write_debug_names: bool,
}

impl Default for WriteBinaryOptions {
    fn default() -> WriteBinaryOptions {
        WriteBinaryOptions {
            log: false,
            canonicalize_lebs: true,
            relocatable: false,
            write_debug_names: false,
        }
    }
}

struct WriteTextOptions {
    fold_exprs: bool,
    inline_export: bool,    
}

impl Default for WriteTextOptions {
    fn default() -> WriteTextOptions {
        WriteTextOptions {
            fold_exprs: false,
            inline_export: false,
        }
    }
}

/// Options for reading read binary.
pub struct ReadBinaryOptions {
    read_debug_names: bool,
}

impl Default for ReadBinaryOptions {
    fn default() -> ReadBinaryOptions {
        ReadBinaryOptions {
            read_debug_names: false,
        }
    }
}

/// WebAssembly module.
pub struct Module {
    raw_module: *mut ffi::WasmModule,
    lexer: Option<Lexer>,
}

impl Module {
    /// Parse source in WebAssembly text format.
    pub fn parse_wat<S: AsRef<[u8]>>(filename: &str, source: S) -> Result<Module, Error> {
        let lexer = Lexer::new(filename, source.as_ref())?;
        let error_handler = ErrorHandler::new_text();
        match parse_wat(&lexer, &error_handler).take_module() {
            Ok(module) => Ok(
                Module {
                    raw_module: module,
                    lexer: Some(lexer),
                }
            ),
            Err(()) => {
                let msg = String::from_utf8_lossy(error_handler.raw_message()).to_string();
                Err(Error(ErrorKind::Parse(msg)))
            }
        }
    }
    
    /// Read WebAssembly binary.
    /// 
    /// `read_binary` doesn't do any validation. If you want to validate, you can the module you can
    /// call [`validate`].
    /// 
    /// [`validate`]: #method.validate
    pub fn read_binary<S: AsRef<[u8]>>(wasm: S, options: &ReadBinaryOptions) -> Result<Module, Error> {
        let error_handler = ErrorHandler::new_binary();
        let result = {
            let wasm = wasm.as_ref();
            let raw_result = unsafe {
                ffi::wabt_read_binary(
                    wasm.as_ptr(), 
                    wasm.len(), 
                    options.read_debug_names as c_int, 
                    error_handler.raw_buffer
                )
            };
            ReadBinaryResult {
                raw_result,
            }
        };
        match result.take_module() {
            Ok(module) => Ok(
                Module {
                    raw_module: module,
                    lexer: None
                }
            ),
            Err(()) => {
                let msg = String::from_utf8_lossy(error_handler.raw_message()).to_string();
                Err(Error(ErrorKind::Deserialize(msg)))
            }
        }
    }

    fn resolve_names(&mut self) -> Result<(), Error> {
        let error_handler = ErrorHandler::new_text();
        unsafe {
            let raw_lexer = self.lexer.as_ref().map(|lexer| lexer.raw_lexer).unwrap_or(ptr::null_mut());
            let result = ffi::wabt_resolve_names_module(raw_lexer, self.raw_module, error_handler.raw_buffer);
            if result == ffi::Result::Error {
                let msg = String::from_utf8_lossy(error_handler.raw_message()).to_string();
                return Err(Error(ErrorKind::ResolveNames(msg)));
            }
        }
        Ok(())
    }

    /// Validate the module.
    pub fn validate(&self) -> Result<(), Error> {
        let error_handler = ErrorHandler::new_text();
        unsafe {
            let raw_lexer = self.lexer.as_ref().map(|lexer| lexer.raw_lexer).unwrap_or(ptr::null_mut());
            let result = ffi::wabt_validate_module(raw_lexer, self.raw_module, error_handler.raw_buffer);
            if result == ffi::Result::Error {
                let msg = String::from_utf8_lossy(error_handler.raw_message()).to_string();
                return Err(Error(ErrorKind::Validate(msg)));
            }
        }
        Ok(())
    }

    fn write_binary(&self, options: &WriteBinaryOptions) -> Result<WabtBuf, Error> {
        let result = unsafe {
            let raw_result = ffi::wabt_write_binary_module(
                self.raw_module,
                options.log as c_int,
                options.canonicalize_lebs as c_int,
                options.relocatable as c_int,
                options.write_debug_names as c_int,
            );
            WriteModuleResult { raw_result }
        };
        result
            .take_wabt_buf()
            .map_err(|_| Error(ErrorKind::WriteBinary))
    }

    fn write_text(&self, options: &WriteTextOptions) -> Result<WabtBuf, Error> {
        let result = unsafe {
            let raw_result = ffi::wabt_write_text_module(
                self.raw_module, 
                options.fold_exprs as c_int, 
                options.inline_export as c_int,
            );
            WriteModuleResult { raw_result }
        };
        result
            .take_wabt_buf()
            .map_err(|_| Error(ErrorKind::WriteText))
    }
}

impl Drop for Module {
    fn drop(&mut self) {
        unsafe {
            ffi::wabt_destroy_module(self.raw_module);
        }
    }
}

/// A builder for translate wasm text source to wasm binary format.
/// 
/// This version allows you to tweak parameters. If you need simple version
/// check out [`wat2wasm`].
/// 
/// [`wat2wasm`]: fn.wat2wasm.html
/// 
/// # Examples
/// 
/// ```rust
/// extern crate wabt;
/// use wabt::Wat2Wasm;
///
/// fn main() {
///     let wasm_binary = Wat2Wasm::new()
///         .canonicalize_lebs(false)
///         .write_debug_names(true)
///         .convert(
///             r#"
///                 (module
///                     (import "spectest" "print" (func $print (param i32)))
///                     (func (export "main")
///                         i32.const 1312
///                         call $print
///                     )
///                 )
///             "#
///         ).unwrap();
/// 
///     # wasm_binary;
/// }
/// ```
/// 
pub struct Wat2Wasm {
    validate: bool,
    write_binary_options: WriteBinaryOptions,
}

impl Wat2Wasm {
    /// Create `Wat2Wasm` with default configuration.
    pub fn new() -> Wat2Wasm {
        Wat2Wasm {
            write_binary_options: WriteBinaryOptions::default(),
            validate: true,
        }
    }

    /// Write canonicalized LEB128 for var ints.
    /// 
    /// Set this to `false` to write all LEB128 sizes as 5-bytes instead of their minimal size.
    /// `true` by default.
    pub fn canonicalize_lebs(&mut self, canonicalize_lebs: bool) -> &mut Wat2Wasm {
        self.write_binary_options.canonicalize_lebs = canonicalize_lebs;
        self
    }

    /// Create a relocatable wasm binary 
    /// 
    /// (suitable for linking with wasm-link).
    /// `false` by default.
    pub fn relocatable(&mut self, relocatable: bool) -> &mut Wat2Wasm {
        self.write_binary_options.relocatable = relocatable;
        self
    }

    /// Write debug names to the generated binary file
    /// 
    /// `false` by default.
    pub fn write_debug_names(&mut self, write_debug_names: bool) -> &mut Wat2Wasm {
        self.write_binary_options.write_debug_names = write_debug_names;
        self
    }

    /// Check for validity of module before writing.
    /// 
    /// `true` by default.
    pub fn validate(&mut self, validate: bool) -> &mut Wat2Wasm {
        self.validate = validate;
        self
    }

    // TODO: Add logged version of convert

    /// Perform conversion.
    pub fn convert<S: AsRef<[u8]>>(&self, source: S) -> Result<WabtBuf, Error> {
        let mut module = Module::parse_wat("test.wast", source)?;
        module.resolve_names()?;

        if self.validate {
            module.validate()?;
        }

        let result = module.write_binary(&self.write_binary_options)?;
        Ok(result)
    }
}

/// A builder for converting wasm binary to wasm text format.
/// 
/// # Examples
/// 
/// ```rust
/// extern crate wabt;
/// use wabt::Wasm2Wat;
///
/// fn main() {
///     let wasm_text = Wasm2Wat::new()
///         .fold_exprs(true)
///         .inline_export(true)
///         .convert(
///             &[
///                 0, 97, 115, 109, // \0ASM - magic
///                 1, 0, 0, 0       //  0x01 - version
///             ]
///         ).unwrap();
/// 
///     # wasm_text;
/// }
/// ```
/// 
pub struct Wasm2Wat {
    read_binary_options: ReadBinaryOptions,
    write_text_options: WriteTextOptions,
}

impl Wasm2Wat {
    /// Create `Wasm2Wat` with default configuration.
    pub fn new() -> Wasm2Wat {
        Wasm2Wat {
            read_binary_options: ReadBinaryOptions::default(),
            write_text_options: WriteTextOptions::default(),
        }
    }

    /// Read debug names in the binary file.
    /// 
    /// `false` by default.
    pub fn read_debug_names(&mut self, read_debug_names: bool) -> &mut Wasm2Wat {
        self.read_binary_options.read_debug_names = read_debug_names;
        self
    }

    /// Write folded expressions where possible.
    /// 
    /// Example of folded code (if `true`):
    /// 
    /// ```WebAssembly
    /// (module
    ///     (func (param i32 i32) (result i32)
    ///         (i32.add ;; Add loaded arguments
    ///             (get_local 0) ;; Load first arg
    ///             (get_local 1) ;; Load second arg
    ///         )
    ///     )
    /// )
    /// ```
    /// 
    /// Example of straight code (if `false`):
    /// 
    /// ```WebAssembly
    /// (module
    ///     (func (param i32 i32) (result i32)
    ///         get_local 0 ;; Load first arg
    ///         get_local 1 ;; Load second arg
    ///         i32.add     ;; Add loaded arguments
    ///     )
    /// )
    /// ```
    /// 
    /// `false` by default.
    pub fn fold_exprs(&mut self, fold_exprs: bool) -> &mut Wasm2Wat {
        self.write_text_options.fold_exprs = fold_exprs;
        self
    }

    /// Write all exports inline.
    /// 
    /// Example of code with inline exports (if `true`):
    /// 
    /// ```WebAssembly
    /// (module
    /// (func $addTwo (export "addTwo") (param $p0 i32) (param $p1 i32) (result i32)
    ///   (i32.add
    ///     (get_local $p0)
    ///     (get_local $p1))))
    /// ```
    /// 
    /// Example of code with separate exports (if `false`):
    /// 
    /// ```WebAssembly
    /// (module
    ///   (func $addTwo (param $p0 i32) (param $p1 i32) (result i32)
    ///     (i32.add
    ///       (get_local $p0)
    ///       (get_local $p1)))
    ///   (export "addTwo" (func $addTwo)))
    /// ```
    /// 
    /// `false` by default.
    pub fn inline_export(&mut self, inline_export: bool) -> &mut Wasm2Wat {
        self.write_text_options.inline_export = inline_export;
        self
    }

    /// Perform conversion.
    pub fn convert<S: AsRef<[u8]>>(&self, wasm: S) -> Result<WabtBuf, Error> {
        let module = Module::read_binary(wasm, &self.read_binary_options)?;
        let output_buffer = module.write_text(&self.write_text_options)?;
        Ok(output_buffer)
    }
}

/// Translate wasm text source to wasm binary format.
/// 
/// If wasm source is valid wasm binary will be returned in the vector.
/// Returned binary is validated and can be executed.
/// 
/// This function will make translation with default parameters. 
/// If you want to find out what default parameters are or you want to tweak them
/// you can use [`Wat2Wasm`]
///
/// For more examples and online demo you can check online version
/// of [wat2wasm](https://cdn.rawgit.com/WebAssembly/wabt/aae5a4b7/demo/wat2wasm/).
/// 
/// [`Wat2Wasm`]: struct.Wat2Wasm.html
///
/// # Examples
///
/// ```rust
/// extern crate wabt;
/// use wabt::wat2wasm;
///
/// fn main() {
///     assert_eq!(
///         wat2wasm("(module)").unwrap(),
///         &[
///             0, 97, 115, 109, // \0ASM - magic
///             1, 0, 0, 0       //  0x01 - version
///         ]
///     );
/// }
/// ```
///
pub fn wat2wasm<S: AsRef<[u8]>>(source: S) -> Result<Vec<u8>, Error> {
    let result_buf = Wat2Wasm::new().convert(source)?;
    Ok(result_buf.as_ref().to_vec())
}

/// Disassemble wasm binary to wasm text format.
///
/// # Examples
///
/// ```rust
/// extern crate wabt;
/// use wabt::wasm2wat;
///
/// fn main() {
///     assert_eq!(
///         wasm2wat(&[
///             0, 97, 115, 109, // \0ASM - magic
///             1, 0, 0, 0       //    01 - version
///         ]),
///         Ok("(module)\n".to_owned()),
///     );
/// }
/// ```
///
pub fn wasm2wat<S: AsRef<[u8]>>(wasm: S) -> Result<String, Error> {
    let result_buf = Wasm2Wat::new().convert(wasm)?;
    let text = String::from_utf8(result_buf.as_ref().to_vec())
            .map_err(|_| Error(ErrorKind::NonUtf8Result))?;
    Ok(text)
}

#[test]
fn module() {
    let binary_module = wat2wasm(r#"
(module
  (import "foo" "bar" (func (param f32)))
  (memory (data "hi"))
  (type (func (param i32) (result i32)))
  (start 1)
  (table 0 1 anyfunc)
  (func)
  (func (type 1)
    i32.const 42
    drop)
  (export "e" (func 1)))
"#).unwrap();

    let mut module = Module::read_binary(&binary_module, &ReadBinaryOptions::default()).unwrap();
    module.resolve_names().unwrap();
    module.validate().unwrap();
}

#[test]
fn test_wat2wasm() {
    assert_eq!(
        wat2wasm("(module)").unwrap(),
        &[0, 97, 115, 109, 1, 0, 0, 0]
    );

    assert_eq!(
        wat2wasm(
            r#"
            (module
            )"#,
        ).unwrap(),
        &[0, 97, 115, 109, 1, 0, 0, 0]
    );

    assert_eq!(wat2wasm("(modu"), Err(Error(ErrorKind::Parse(
r#"test.wast:1:2: error: unexpected token "modu", expected a module field or a module.
(modu
 ^^^^
"#.to_string()))));
}

#[test]
fn test_wasm2wat() {
    assert_eq!(
        wasm2wat(&[
            0, 97, 115, 109, // \0ASM - magic
            1, 0, 0, 0       //    01 - version
        ]),
        Ok("(module)\n".to_owned()),
    );

    assert_eq!(
        wasm2wat(&[
            0, 97, 115, 109, // \0ASM - magic
        ]),
        Err(Error(ErrorKind::Deserialize(
            "0000004: error: unable to read uint32_t: version\n".to_owned()
        ))),
    );
}

#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
fn roundtrip() {
    #[cfg_attr(rustfmt, rustfmt_skip)]
    let factorial: &[u8] = &[
        0, 97, 115, 109, 1, 0, 0, 0, 1, 6, 1, 96, 1, 124, 1, 124, 3, 2, 1, 0, 7, 7, 
        1, 3, 102, 97, 99, 0, 0, 10, 46, 1, 44, 0, 32, 0, 68, 0, 0, 0, 0, 0, 0, 240, 
        63, 99, 4, 124, 68, 0, 0, 0, 0, 0, 0, 240, 63, 5, 32, 0, 32, 0, 68, 0, 0, 0, 
        0, 0, 0, 240, 63, 161, 16, 0, 162, 11, 11
    ];

    let text = wasm2wat(&factorial).unwrap();
    let binary = wat2wasm(&text).unwrap();

    assert_eq!(&*factorial, &*binary);
}