Skip to content

Commit

Permalink
Teach cxx to catch (Map)ParseException
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 15, 2022
1 parent c7c2b92 commit 2ca8391
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions components/wasm-opt-cxx-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ readme = "README.md"

[dependencies]
anyhow = "1.0.58"
cxx = "1.0.72"
cxx = "1.0.79"
wasm-opt-sys = { path = "../wasm-opt-sys", version = "0.110.1" }

[build-dependencies]
anyhow = "1.0.58"
cxx-build = { version = "1.0.72", features = ["parallel"] }
cxx-build = { version = "1.0.79", features = ["parallel"] }
51 changes: 17 additions & 34 deletions components/wasm-opt-cxx-sys/src/shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
#include <stdexcept> // runtime_error
#include <memory> // unique_ptr

namespace wasm_shims {
std::runtime_error parse_exception_to_runtime_error(const wasm::ParseException& e) {
namespace rust::behavior {
template <typename Try, typename Fail>
static void trycatch(Try &&func, Fail &&fail) noexcept try {
func();
} catch (const std::exception &e) {
fail(e.what());
} catch (const wasm::ParseException &e) {
Colors::setEnabled(false);

std::ostringstream buf;
e.dump(buf);

return std::runtime_error(buf.str());
}

std::runtime_error map_parse_exception_to_runtime_error(const wasm::MapParseException& e) {
fail(buf.str());
} catch (const wasm::MapParseException &e) {
Colors::setEnabled(false);

std::ostringstream buf;
e.dump(buf);

return std::runtime_error(buf.str());
fail(buf.str());
}
}

Expand Down Expand Up @@ -57,39 +56,23 @@ namespace wasm_shims {
}

void readText(const std::string& filename, Module& wasm) {
try {
inner.readText(std::string(filename), wasm);
} catch (const wasm::ParseException &e) {
throw parse_exception_to_runtime_error(e);
}
inner.readText(std::string(filename), wasm);
}

void readBinary(const std::string& filename,
Module& wasm,
const std::string& sourceMapFilename) {
try {
inner.readBinary(std::string(filename),
wasm,
std::string(sourceMapFilename));
} catch (const wasm::ParseException &e) {
throw parse_exception_to_runtime_error(e);
} catch (const wasm::MapParseException &e) {
throw map_parse_exception_to_runtime_error(e);
}
inner.readBinary(std::string(filename),
wasm,
std::string(sourceMapFilename));
}

void read(const std::string& filename,
Module& wasm,
const std::string& sourceMapFilename) {
try {
inner.read(std::string(filename),
wasm,
std::string(sourceMapFilename));
} catch (const wasm::ParseException &e) {
throw parse_exception_to_runtime_error(e);
} catch (const wasm::MapParseException &e) {
throw map_parse_exception_to_runtime_error(e);
}
inner.read(std::string(filename),
wasm,
std::string(sourceMapFilename));
}
};

Expand Down
4 changes: 2 additions & 2 deletions components/wasm-opt-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ links = "binaryen"
[build-dependencies]
anyhow = "1.0.58"
cc = { version = "1.0.73", features = ["parallel"] }
cxx-build = "1.0.72"
cxx-build = "1.0.79"
regex = "1.6.0"

[dependencies]
cxx = "1.0.72"
cxx = "1.0.79"

0 comments on commit 2ca8391

Please sign in to comment.