Skip to content

Commit 168ba91

Browse files
authored
Add default output name for wast2json (#1287)
Without this the default was to write the wasm files but not the json
1 parent 82e64ba commit 168ba91

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/tools/wast2json.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
using namespace wabt;
4040

4141
static const char* s_infile;
42-
static const char* s_outfile;
42+
static std::string s_outfile;
4343
static int s_verbose;
4444
static WriteBinaryOptions s_write_binary_options;
4545
static bool s_validate = true;
@@ -68,7 +68,7 @@ static void ParseOptions(int argc, char* argv[]) {
6868
parser.AddOption("debug-parser", "Turn on debugging the parser of wast files",
6969
[]() { s_debug_parsing = true; });
7070
s_features.AddOptions(&parser);
71-
parser.AddOption('o', "output", "FILE", "output wasm binary file",
71+
parser.AddOption('o', "output", "FILE", "output JSON file",
7272
[](const char* argument) { s_outfile = argument; });
7373
parser.AddOption(
7474
'r', "relocatable",
@@ -89,6 +89,14 @@ static void ParseOptions(int argc, char* argv[]) {
8989
parser.Parse(argc, argv);
9090
}
9191

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+
92100
int ProgramMain(int argc, char** argv) {
93101
InitStdio();
94102

@@ -116,19 +124,20 @@ int ProgramMain(int argc, char** argv) {
116124
}
117125

118126
if (Succeeded(result)) {
127+
if (s_outfile.empty()) {
128+
s_outfile = DefaultOuputName(s_infile);
129+
}
130+
119131
std::vector<FilenameMemoryStreamPair> module_streams;
120132
MemoryStream json_stream;
121133

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();
124135
s_write_binary_options.features = s_features;
125136
result = WriteBinarySpecScript(
126-
&json_stream, script.get(), s_infile, module_filename_noext,
137+
&json_stream, script.get(), s_infile, output_basename,
127138
s_write_binary_options, &module_streams, s_log_stream.get());
128139

129-
if (s_outfile) {
130-
json_stream.WriteToFile(s_outfile);
131-
}
140+
json_stream.WriteToFile(s_outfile);
132141

133142
for (auto iter = module_streams.begin(); iter != module_streams.end();
134143
++iter) {

test/help/wast2json.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ options:
2828
--enable-reference-types Enable Reference types (anyref)
2929
--enable-annotations Enable Custom annotation syntax
3030
--enable-all Enable all features
31-
-o, --output=FILE output wasm binary file
31+
-o, --output=FILE output JSON file
3232
-r, --relocatable Create a relocatable wasm binary (suitable for linking with e.g. lld)
3333
--no-canonicalize-leb128s Write all LEB128 sizes as 5-bytes instead of their minimal size
3434
--debug-names Write debug names to the generated binary file

0 commit comments

Comments
 (0)