39
39
using namespace wabt ;
40
40
41
41
static const char * s_infile;
42
- static const char * s_outfile;
42
+ static std::string s_outfile;
43
43
static int s_verbose;
44
44
static WriteBinaryOptions s_write_binary_options;
45
45
static bool s_validate = true ;
@@ -68,7 +68,7 @@ static void ParseOptions(int argc, char* argv[]) {
68
68
parser.AddOption (" debug-parser" , " Turn on debugging the parser of wast files" ,
69
69
[]() { s_debug_parsing = true ; });
70
70
s_features.AddOptions (&parser);
71
- parser.AddOption (' o' , " output" , " FILE" , " output wasm binary file" ,
71
+ parser.AddOption (' o' , " output" , " FILE" , " output JSON file" ,
72
72
[](const char * argument) { s_outfile = argument; });
73
73
parser.AddOption (
74
74
' r' , " relocatable" ,
@@ -89,6 +89,14 @@ static void ParseOptions(int argc, char* argv[]) {
89
89
parser.Parse (argc, argv);
90
90
}
91
91
92
+ static std::string DefaultOuputName (string_view input_name) {
93
+ // Strip existing extension and add .json
94
+ std::string result (StripExtension (GetBasename (input_name)));
95
+ result += " .json" ;
96
+
97
+ return result;
98
+ }
99
+
92
100
int ProgramMain (int argc, char ** argv) {
93
101
InitStdio ();
94
102
@@ -116,19 +124,20 @@ int ProgramMain(int argc, char** argv) {
116
124
}
117
125
118
126
if (Succeeded (result)) {
127
+ if (s_outfile.empty ()) {
128
+ s_outfile = DefaultOuputName (s_infile);
129
+ }
130
+
119
131
std::vector<FilenameMemoryStreamPair> module_streams;
120
132
MemoryStream json_stream;
121
133
122
- std::string module_filename_noext =
123
- StripExtension (s_outfile ? s_outfile : s_infile).to_string ();
134
+ std::string output_basename = StripExtension (s_outfile).to_string ();
124
135
s_write_binary_options.features = s_features;
125
136
result = WriteBinarySpecScript (
126
- &json_stream, script.get (), s_infile, module_filename_noext ,
137
+ &json_stream, script.get (), s_infile, output_basename ,
127
138
s_write_binary_options, &module_streams, s_log_stream.get ());
128
139
129
- if (s_outfile) {
130
- json_stream.WriteToFile (s_outfile);
131
- }
140
+ json_stream.WriteToFile (s_outfile);
132
141
133
142
for (auto iter = module_streams.begin (); iter != module_streams.end ();
134
143
++iter) {
0 commit comments