Skip to content

Commit 59972bd

Browse files
committed
refactor file flags into enums
1 parent d0ae4a2 commit 59972bd

8 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/asm2wasm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, const char *argv[]) {
7777

7878
Asm2WasmPreProcessor pre;
7979
auto input(
80-
read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
80+
read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
8181
char *start = pre.process(input.data());
8282

8383
if (options.debug) std::cerr << "parsing..." << std::endl;
@@ -101,7 +101,7 @@ int main(int argc, const char *argv[]) {
101101
}
102102

103103
if (options.debug) std::cerr << "printing..." << std::endl;
104-
Output output(options.extra["output"], false, options.debug);
104+
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
105105
printWasm(&wasm, output.getStream());
106106

107107
if (mappedGlobals) {

src/binaryen-shell.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int main(int argc, const char* argv[]) {
372372
}
373373
options.parse(argc, argv);
374374

375-
auto input(read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
375+
auto input(read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
376376

377377
if (options.debug) std::cerr << "parsing text to s-expressions...\n";
378378
SExpressionParser parser(input.data());

src/s2wasm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int main(int argc, const char *argv[]) {
7474
});
7575
options.parse(argc, argv);
7676

77-
auto input(read_file<std::string>(options.extra["infile"], false, options.debug));
77+
auto input(read_file<std::string>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
7878

7979
if (options.debug) std::cerr << "Parsing and wasming..." << std::endl;
8080
AllocatingModule wasm;
@@ -103,7 +103,7 @@ int main(int argc, const char *argv[]) {
103103
s2wasm.emscriptenGlue(meta);
104104

105105
if (options.debug) std::cerr << "Printing..." << std::endl;
106-
Output output(options.extra["output"], false, options.debug);
106+
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
107107
printWasm(&wasm, output.getStream());
108108
output << meta.str() << std::endl;
109109

src/support/file.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
#include <limits>
2121

2222
template <typename T>
23-
T wasm::read_file(const std::string &filename, bool binary, bool debug) {
24-
if (debug) std::cerr << "Loading '" << filename << "'..." << std::endl;
23+
T wasm::read_file(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug) {
24+
if (debug == Flags::Debug) std::cerr << "Loading '" << filename << "'..." << std::endl;
2525
std::ifstream infile;
2626
auto flags = std::ifstream::in;
27-
if (binary) flags |= std::ifstream::binary;
27+
if (binary == Flags::Binary) flags |= std::ifstream::binary;
2828
infile.open(filename, flags);
2929
if (!infile.is_open()) {
3030
std::cerr << "Failed opening '" << filename << "'" << std::endl;
@@ -44,16 +44,16 @@ T wasm::read_file(const std::string &filename, bool binary, bool debug) {
4444
}
4545

4646
// Explicit instantiations for the explicit specializations.
47-
template std::string wasm::read_file<>(const std::string &, bool, bool);
48-
template std::vector<char> wasm::read_file<>(const std::string &, bool, bool);
47+
template std::string wasm::read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption);
48+
template std::vector<char> wasm::read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption);
4949

50-
wasm::Output::Output(const std::string &filename, bool binary, bool debug)
50+
wasm::Output::Output(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug)
5151
: outfile(), out([this, filename, binary, debug]() {
5252
std::streambuf *buffer;
5353
if (filename.size()) {
54-
if (debug) std::cerr << "Opening '" << filename << std::endl;
54+
if (debug == Flags::Debug) std::cerr << "Opening '" << filename << std::endl;
5555
auto flags = std::ofstream::out | std::ofstream::trunc;
56-
if (binary) flags |= std::ofstream::binary;
56+
if (binary == Flags::Binary) flags |= std::ofstream::binary;
5757
outfile.open(filename, flags);
5858
if (!outfile.is_open()) {
5959
std::cerr << "Failed opening '" << filename << "'" << std::endl;

src/support/file.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,28 @@
2828
#include <vector>
2929

3030
namespace wasm {
31+
32+
namespace Flags {
33+
enum BinaryOption {
34+
Binary,
35+
Text
36+
};
37+
enum DebugOption {
38+
Debug,
39+
Release
40+
};
41+
}
42+
3143
template <typename T>
32-
T read_file(const std::string &filename, bool binary, bool debug);
44+
T read_file(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug);
3345
// Declare the valid explicit specializations.
34-
extern template std::string read_file<>(const std::string &, bool, bool);
35-
extern template std::vector<char> read_file<>(const std::string &, bool, bool);
46+
extern template std::string read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption);
47+
extern template std::vector<char> read_file<>(const std::string &, Flags::BinaryOption, Flags::DebugOption);
3648

3749
class Output {
3850
public:
3951
// An empty filename will open stdout instead.
40-
Output(const std::string &filename, bool binary, bool debug);
52+
Output(const std::string &filename, Flags::BinaryOption binary, Flags::DebugOption debug);
4153
~Output() = default;
4254
template <typename T>
4355
std::ostream &operator<<(const T &v) {

src/wasm-as.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, const char *argv[]) {
4141
});
4242
options.parse(argc, argv);
4343

44-
auto input(read_file<std::string>(options.extra["infile"], false, options.debug));
44+
auto input(read_file<std::string>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
4545

4646
if (options.debug) std::cerr << "s-parsing..." << std::endl;
4747
SExpressionParser parser(const_cast<char*>(input.c_str()));
@@ -57,7 +57,7 @@ int main(int argc, const char *argv[]) {
5757
writer.write();
5858

5959
if (options.debug) std::cerr << "writing to output..." << std::endl;
60-
Output output(options.extra["output"], true, options.debug);
60+
Output output(options.extra["output"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release);
6161
buffer.writeTo(output);
6262

6363
if (options.debug) std::cerr << "Done." << std::endl;

src/wasm-dis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ int main(int argc, const char *argv[]) {
4141
});
4242
options.parse(argc, argv);
4343

44-
auto input(read_file<std::vector<char>>(options.extra["infile"], true, options.debug));
44+
auto input(read_file<std::vector<char>>(options.extra["infile"], Flags::Binary, options.debug ? Flags::Debug : Flags::Release));
4545

4646
if (options.debug) std::cerr << "parsing binary..." << std::endl;
4747
AllocatingModule wasm;
4848
WasmBinaryBuilder parser(wasm, input, options.debug);
4949
parser.read();
5050

5151
if (options.debug) std::cerr << "Printing..." << std::endl;
52-
Output output(options.extra["output"], false, options.debug);
52+
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
5353
printWasm(&wasm, output.getStream());
5454
output << '\n';
5555

src/wasm2asm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(int argc, const char *argv[]) {
4444
options.parse(argc, argv);
4545

4646
auto input(
47-
read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
47+
read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
4848

4949
if (options.debug) std::cerr << "s-parsing..." << std::endl;
5050
SExpressionParser parser(input.data());
@@ -67,7 +67,7 @@ int main(int argc, const char *argv[]) {
6767
if (options.debug) std::cerr << "j-printing..." << std::endl;
6868
JSPrinter jser(true, true, asmjs);
6969
jser.printAst();
70-
Output output(options.extra["output"], false, options.debug);
70+
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
7171
output << jser.buffer << std::endl;
7272

7373
if (options.debug) std::cerr << "done." << std::endl;

0 commit comments

Comments
 (0)