From a2dd7c4feae3c4da3a1f8de90171795d12eb8879 Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Mon, 18 Mar 2019 17:30:33 -0700 Subject: Allow tools to read from stdin (#1950) This is necessary to write tests that don't require temporary files, such as in #1948, and is generally useful. --- src/wasm/wasm-io.cpp | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'src/wasm/wasm-io.cpp') 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(filename, Flags::Text, debug ? Flags::Debug : Flags::Release)); +static void readTextData(std::string& input, Module& wasm) { SExpressionParser parser(const_cast(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>(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(filename, Flags::Text, debug ? Flags::Debug : Flags::Release)); + readTextData(input, wasm); +} + +static void readBinaryData(std::vector& input, Module& wasm, + std::string sourceMapFilename, bool debug) { std::unique_ptr 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>(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 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 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()); } -- cgit v1.2.3