summaryrefslogtreecommitdiff
path: root/src/tools/wasm2js.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-12-06 17:22:52 -0800
committerGitHub <noreply@github.com>2018-12-06 17:22:52 -0800
commit68952631cb04de95154708277b98c1fdba397907 (patch)
tree761e742f05439da34fdfeb400b3d4097d6f1a2c8 /src/tools/wasm2js.cpp
parentb4503a23f28551acfc725a6e3aba2d72036eff4f (diff)
downloadbinaryen-68952631cb04de95154708277b98c1fdba397907.tar.gz
binaryen-68952631cb04de95154708277b98c1fdba397907.tar.bz2
binaryen-68952631cb04de95154708277b98c1fdba397907.zip
Wasm2js error handling improvement (#1807)
Move the code around so the assertions are not caught in the big try-catch that reports errors as parsing errors.
Diffstat (limited to 'src/tools/wasm2js.cpp')
-rw-r--r--src/tools/wasm2js.cpp46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp
index 8a57a5a1a..762a89985 100644
--- a/src/tools/wasm2js.cpp
+++ b/src/tools/wasm2js.cpp
@@ -55,9 +55,17 @@ int main(int argc, const char *argv[]) {
options.parse(argc, argv);
if (options.debug) builderFlags.debug = true;
- Element* root;
+ Element* root = nullptr;
Module wasm;
Ref asmjs;
+ std::unique_ptr<SExpressionParser> sexprParser;
+ std::unique_ptr<SExpressionWasmBuilder> sexprBuilder;
+
+ auto &input = options.extra["infile"];
+ std::string suffix(".wasm");
+ bool binaryInput =
+ input.size() >= suffix.size() &&
+ input.compare(input.size() - suffix.size(), suffix.size(), suffix) == 0;
try {
// If the input filename ends in `.wasm`, then parse it in binary form,
@@ -68,36 +76,19 @@ int main(int argc, const char *argv[]) {
// `--allow-asserts` flag which means we need to parse the extra
// s-expressions that come at the end of the `*.wast` file after the module
// is defined.
- auto &input = options.extra["infile"];
- std::string suffix(".wasm");
- if (input.size() >= suffix.size() &&
- input.compare(input.size() - suffix.size(), suffix.size(), suffix) == 0) {
+ if (binaryInput) {
ModuleReader reader;
reader.setDebug(options.debug);
reader.read(input, wasm, "");
-
- if (options.debug) std::cerr << "asming..." << std::endl;
- Wasm2JSBuilder wasm2js(builderFlags);
- asmjs = wasm2js.processWasm(&wasm);
-
} else {
auto input(
read_file<std::vector<char>>(options.extra["infile"], Flags::Text, options.debug ? Flags::Debug : Flags::Release));
if (options.debug) std::cerr << "s-parsing..." << std::endl;
- SExpressionParser parser(input.data());
- root = parser.root;
+ sexprParser = make_unique<SExpressionParser>(input.data());
+ root = sexprParser->root;
if (options.debug) std::cerr << "w-parsing..." << std::endl;
- SExpressionWasmBuilder builder(wasm, *(*root)[0]);
-
- if (options.debug) std::cerr << "asming..." << std::endl;
- Wasm2JSBuilder wasm2js(builderFlags);
- asmjs = wasm2js.processWasm(&wasm);
-
- if (options.extra["asserts"] == "1") {
- if (options.debug) std::cerr << "asserting..." << std::endl;
- flattenAppend(asmjs, wasm2js.processAsserts(&wasm, *root, builder));
- }
+ sexprBuilder = make_unique<SExpressionWasmBuilder>(wasm, *(*root)[0]);
}
} catch (ParseException& p) {
p.dump(std::cerr);
@@ -106,6 +97,17 @@ int main(int argc, const char *argv[]) {
Fatal() << "error in building module, std::bad_alloc (possibly invalid request for silly amounts of memory)";
}
+ if (options.debug) std::cerr << "asming..." << std::endl;
+ Wasm2JSBuilder wasm2js(builderFlags);
+ asmjs = wasm2js.processWasm(&wasm);
+
+ if (!binaryInput) {
+ if (options.extra["asserts"] == "1") {
+ if (options.debug) std::cerr << "asserting..." << std::endl;
+ flattenAppend(asmjs, wasm2js.processAsserts(&wasm, *root, *sexprBuilder));
+ }
+ }
+
if (options.debug) {
std::cerr << "a-printing..." << std::endl;
asmjs->stringify(std::cout, true);