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
#![cfg_attr(feature = "wasm-bindgen", feature(proc_macro, wasm_custom_section, wasm_import_module))]
#[macro_use]
extern crate cfg_if;
extern crate tetsy_wasm_gc;

pub const ERR_INVALID_MODULE: u32 = 1;
pub const ERR_OUTPUT_TOO_SMALL: u32 = 2;

#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct WasmGcOptions {
    demangle: bool,
}

#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
impl WasmGcOptions {
    pub fn new() -> WasmGcOptions {
        WasmGcOptions { demangle: true }
    }

    pub fn demangle(&mut self, demangle: bool) {
        self.demangle = demangle;
    }

    pub fn gc(&self, input: &[u8], output: &mut [u8]) -> u32 {
        gc(input, output, self)
    }
}

cfg_if! {
    if #[cfg(feature = "wasm-bindgen")] {
        extern crate wasm_bindgen;
        use wasm_bindgen::prelude::*;

        #[wasm_bindgen]
        pub fn tetsy_wasm_gc(input: &[u8], output: &mut [u8]) -> u32 {
            gc(input, output, &WasmGcOptions::new())
        }
    } else {
        use std::slice;

        #[no_mangle]
        pub unsafe extern fn tetsy_wasm_gc(
            input_ptr: *const u8,
            input_len: usize,
            output_ptr: *mut u8,
            output_len: usize,
        ) -> u32 {
            let opts = WasmGcOptions::new();
            wasm_gc_options(input_ptr, input_len, output_ptr, output_len, &opts)
        }

        #[no_mangle]
        pub unsafe extern fn wasm_gc_options(
            input_ptr: *const u8,
            input_len: usize,
            output_ptr: *mut u8,
            output_len: usize,
            options: *const WasmGcOptions,
        ) -> u32 {
            let input = slice::from_raw_parts(input_ptr, input_len);
            let output = slice::from_raw_parts_mut(output_ptr, output_len);
            gc(input, output, &*options)
        }

        #[no_mangle]
        pub extern fn wasm_gc_options_new() -> *mut WasmGcOptions {
            Box::into_raw(Box::new(WasmGcOptions::new()))
        }

        #[no_mangle]
        pub unsafe extern fn wasm_gc_options_demangle(
            opts: *mut WasmGcOptions,
            demangle: u32,
        ) {
            (*opts).demangle = demangle != 0;
        }

        #[no_mangle]
        pub unsafe extern fn wasm_gc_options_free(opts: *mut WasmGcOptions) {
            drop(Box::from_raw(opts));
        }
    }
}

fn gc(input: &[u8], output: &mut [u8], opts: &WasmGcOptions) -> u32 {
    let result = tetsy_wasm_gc::Config::new()
        .demangle(opts.demangle)
        .gc(input);
    let result = match result {
        Ok(result) => result,
        Err(_) => return ERR_INVALID_MODULE,
    };
    match output.get_mut(..result.len()) {
        Some(buf) => buf.copy_from_slice(&result),
        None => return ERR_OUTPUT_TOO_SMALL,
    }
    return 0
}