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
//! Direct bindings to `wasm-opt`.
//!
//! These are bindings to `wasm-opt`,
//! as built by the [`wasm-opt-sys`] crate.
//! The bindings are created by the [`cxx`] crate,
//! and all go through a custom C++ shim layer
//! that provides a `cxx`-compatible C++ API.
//!
//! Most users will not want to use this crate directly,
//! but instead the [`wasm-opt`] crate.
//!
//! [`wasm-opt-sys`]: https://docs.rs/wasm-opt-sys
//! [`cxx`]: https://docs.rs/cxx
//! [`wasm-opt`]: https://docs.rs/wasm-opt
//!
//! The version of `cxx` used by these bindings is
//! reexported here.

pub use cxx;

// Establish linking with wasm_opt_sys, which contains no Rust code.
extern crate wasm_opt_sys;

#[cxx::bridge(namespace = "Colors")]
pub mod colors {
    unsafe extern "C++" {
        include!("shims.h");

        fn setEnabled(enabled: bool);
    }
}

#[cxx::bridge(namespace = "wasm_shims")]
pub mod wasm {
    unsafe extern "C++" {
        include!("shims.h");
    }

    unsafe extern "C++" {
        type Module;

        fn newModule() -> UniquePtr<Module>;

        fn validateWasm(wasm: Pin<&mut Module>) -> bool;
    }

    unsafe extern "C++" {
        type ModuleReader;

        fn newModuleReader() -> UniquePtr<ModuleReader>;

        fn setDebugInfo(self: Pin<&mut Self>, debug: bool);

        fn setDwarf(self: Pin<&mut Self>, dwarf: bool);

        fn readText(
            self: Pin<&mut Self>,
            filename: Pin<&mut CxxString>,
            wasm: Pin<&mut Module>,
        ) -> Result<()>;

        fn readBinary(
            self: Pin<&mut Self>,
            filename: Pin<&mut CxxString>,
            wasm: Pin<&mut Module>,
            sourceMapFilename: Pin<&mut CxxString>,
        ) -> Result<()>;

        fn read(
            self: Pin<&mut Self>,
            filename: Pin<&mut CxxString>,
            wasm: Pin<&mut Module>,
            sourceMapFilename: Pin<&mut CxxString>,
        ) -> Result<()>;
    }

    unsafe extern "C++" {
        type ModuleWriter;

        fn newModuleWriter() -> UniquePtr<ModuleWriter>;

        fn setDebugInfo(self: Pin<&mut Self>, debug: bool);

        fn setSourceMapFilename(self: Pin<&mut Self>, source_map_filename: Pin<&mut CxxString>);

        fn setSourceMapUrl(self: Pin<&mut Self>, source_map_url: Pin<&mut CxxString>);

        fn writeText(
            self: Pin<&mut Self>,
            wasm: Pin<&mut Module>,
            filename: Pin<&mut CxxString>,
        ) -> Result<()>;

        fn writeBinary(
            self: Pin<&mut Self>,
            wasm: Pin<&mut Module>,
            filename: Pin<&mut CxxString>,
        ) -> Result<()>;
    }

    unsafe extern "C++" {
        fn getRegisteredNames() -> UniquePtr<CxxVector<CxxString>>;

        fn getPassDescription(name: Pin<&mut CxxString>) -> UniquePtr<CxxString>;

        fn isPassHidden(name: Pin<&mut CxxString>) -> bool;
    }

    unsafe extern "C++" {
        type InliningOptions;

        fn newInliningOptions() -> UniquePtr<InliningOptions>;

        fn setAlwaysInlineMaxSize(self: Pin<&mut Self>, size: u32);

        fn setOneCallerInlineMaxSize(self: Pin<&mut Self>, size: u32);

        fn setFlexibleInlineMaxSize(self: Pin<&mut Self>, size: u32);

        fn setAllowFunctionsWithLoops(self: Pin<&mut Self>, allow: bool);

        fn setPartialInliningIfs(self: Pin<&mut Self>, number: u32);
    }

    unsafe extern "C++" {
        type PassOptions;

        fn newPassOptions() -> UniquePtr<PassOptions>;

        fn setValidate(self: Pin<&mut Self>, validate: bool);

        fn setValidateGlobally(self: Pin<&mut Self>, validate: bool);

        fn setOptimizeLevel(self: Pin<&mut Self>, level: i32);

        fn setShrinkLevel(self: Pin<&mut Self>, level: i32);

        fn setInliningOptions(self: Pin<&mut Self>, inlining: UniquePtr<InliningOptions>);

        fn setTrapsNeverHappen(self: Pin<&mut Self>, ignoreTraps: bool);

        fn setLowMemoryUnused(self: Pin<&mut Self>, memoryUnused: bool);

        fn setFastMath(self: Pin<&mut Self>, fastMath: bool);

        fn setZeroFilledMemory(self: Pin<&mut Self>, zeroFilledMemory: bool);

        fn setDebugInfo(self: Pin<&mut Self>, debugInfo: bool);

        fn setArguments(self: Pin<&mut Self>, key: Pin<&mut CxxString>, value: Pin<&mut CxxString>);
    }

    unsafe extern "C++" {
        type WasmFeatureSet;

        fn newFeatureSet() -> UniquePtr<WasmFeatureSet>;

        fn setMVP(self: Pin<&mut Self>);

        fn setAll(self: Pin<&mut Self>);

        fn set(self: Pin<&mut Self>, feature: u32, val: bool);

        fn has(self: &Self, features: &WasmFeatureSet) -> bool;

        fn as_int(self: &Self) -> u32;

        fn getFeatureArray() -> UniquePtr<CxxVector<u32>>;

        fn applyFeatures(
            wasm: Pin<&mut Module>,
            enabled_features: UniquePtr<WasmFeatureSet>,
            disabled_features: UniquePtr<WasmFeatureSet>,
        );
    }

    unsafe extern "C++" {
        type PassRunner<'wasm>;

        fn newPassRunner<'wasm>(wasm: Pin<&'wasm mut Module>) -> UniquePtr<PassRunner<'wasm>>;

        fn newPassRunnerWithOptions<'wasm>(
            wasm: Pin<&'wasm mut Module>,
            options: UniquePtr<PassOptions>,
        ) -> UniquePtr<PassRunner<'wasm>>;

        fn add(self: Pin<&mut Self>, pass_name: Pin<&mut CxxString>);

        fn addDefaultOptimizationPasses(self: Pin<&mut Self>);

        fn run(self: Pin<&mut Self>);

        fn passRemovesDebugInfo(name: Pin<&mut CxxString>) -> bool;
    }

    unsafe extern "C++" {
        fn checkInliningOptionsDefaults(inlining_options: UniquePtr<InliningOptions>) -> bool;

        fn checkPassOptionsDefaults(pass_options: UniquePtr<PassOptions>) -> bool;

        fn checkPassOptionsDefaultsOs(pass_options: UniquePtr<PassOptions>) -> bool;
    }
}