diff options
author | Alon Zakai <azakai@google.com> | 2019-03-20 15:52:19 -0700 |
---|---|---|
committer | Alon Zakai <azakai@google.com> | 2019-03-20 15:52:19 -0700 |
commit | 1690311955c5add86d634ecc47e937315b3b6c41 (patch) | |
tree | 429a0df2e87d41f46ed2408c90657b221294e22f /src/wasm/wasm-io.cpp | |
parent | fec88b85e44b49ac3273b0b7d4e06fba060df36f (diff) | |
parent | fe0b16aa222318588f3bfd84e549b4a1528be296 (diff) | |
download | binaryen-1690311955c5add86d634ecc47e937315b3b6c41.tar.gz binaryen-1690311955c5add86d634ecc47e937315b3b6c41.tar.bz2 binaryen-1690311955c5add86d634ecc47e937315b3b6c41.zip |
Merge remote-tracking branch 'origin/master' into nans
Diffstat (limited to 'src/wasm/wasm-io.cpp')
-rw-r--r-- | src/wasm/wasm-io.cpp | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index 95ffe6e89..057798d2e 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -30,18 +30,21 @@ namespace wasm { -void ModuleReader::readText(std::string filename, Module& wasm) { - if (debug) std::cerr << "reading text from " << filename << "\n"; - auto input(read_file<std::string>(filename, Flags::Text, debug ? Flags::Debug : Flags::Release)); +static void readTextData(std::string& input, Module& wasm) { SExpressionParser parser(const_cast<char*>(input.c_str())); Element& root = *parser.root; SExpressionWasmBuilder builder(wasm, *root[0]); + } -void ModuleReader::readBinary(std::string filename, Module& wasm, - std::string sourceMapFilename) { - if (debug) std::cerr << "reading binary from " << filename << "\n"; - auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release)); +void ModuleReader::readText(std::string filename, Module& wasm) { + if (debug) std::cerr << "reading text from " << filename << "\n"; + auto input(read_file<std::string>(filename, Flags::Text, debug ? Flags::Debug : Flags::Release)); + readTextData(input, wasm); +} + +static void readBinaryData(std::vector<char>& input, Module& wasm, + std::string sourceMapFilename, bool debug) { std::unique_ptr<std::ifstream> sourceMapStream; WasmBinaryBuilder parser(wasm, input, debug); if (sourceMapFilename.size()) { @@ -55,6 +58,13 @@ void ModuleReader::readBinary(std::string filename, Module& wasm, } } +void ModuleReader::readBinary(std::string filename, Module& wasm, + std::string sourceMapFilename) { + if (debug) std::cerr << "reading binary from " << filename << "\n"; + auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release)); + readBinaryData(input, wasm, sourceMapFilename, debug); +} + bool ModuleReader::isBinaryFile(std::string filename) { std::ifstream infile; std::ios_base::openmode flags = std::ifstream::in | std::ifstream::binary; @@ -67,6 +77,11 @@ bool ModuleReader::isBinaryFile(std::string filename) { void ModuleReader::read(std::string filename, Module& wasm, std::string sourceMapFilename) { + // empty filename means read from stdin + if (!filename.size()) { + readStdin(wasm, sourceMapFilename); + return; + } if (isBinaryFile(filename)) { readBinary(filename, wasm, sourceMapFilename); } else { @@ -78,6 +93,23 @@ void ModuleReader::read(std::string filename, Module& wasm, } } +// TODO: reading into a vector<char> then copying into a string is unnecessarily +// inefficient. It would be better to read just once into a stringstream. +void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) { + std::vector<char> input = read_stdin(debug ? Flags::Debug : Flags::Release); + if (input.size() >= 4 && input[0] == '\0' && input[1] == 'a' && + input[2] == 's' && input[3] == 'm') { + readBinaryData(input, wasm, sourceMapFilename, debug); + } else { + std::ostringstream s; + s.write(input.data(), input.size()); + s << '\0'; + std::string input_str = s.str(); + readTextData(input_str, wasm); + } +} + + void ModuleWriter::writeText(Module& wasm, Output& output) { WasmPrinter::printModule(&wasm, output.getStream()); } |