From ce6b5f3ba44ca5fa33c603c4365ef5ffee5a0f90 Mon Sep 17 00:00:00 2001 From: hufeng Date: Thu, 3 Mar 2022 11:38:38 +0800 Subject: [PATCH] fixed 6451ebd from https://gitee.com/hufeng20/ark_ts2abc/pulls/133 parse piece of json from pipe directly Signed-off-by: hufeng Change-Id: Ie1d0c78c15fe460ce98b8d3e2fdd2f06485a749f --- ts2panda/ts2abc/main.cpp | 7 +-- ts2panda/ts2abc/ts2abc.cpp | 121 +++++++++++++++++++++++++++---------- ts2panda/ts2abc/ts2abc.h | 6 +- 3 files changed, 94 insertions(+), 40 deletions(-) diff --git a/ts2panda/ts2abc/main.cpp b/ts2panda/ts2abc/main.cpp index 724a6536d2..3eaef44c98 100644 --- a/ts2panda/ts2abc/main.cpp +++ b/ts2panda/ts2abc/main.cpp @@ -45,10 +45,6 @@ int Preprocess(const panda::ts2abc::Options &options, const panda::PandArgParser std::cerr << argParser.GetHelpString(); return RETURN_FAILED; } - - if (!ReadFromPipe(data)) { - return RETURN_FAILED; - } } return RETURN_SUCCESS; } @@ -95,7 +91,8 @@ int main(int argc, const char *argv[]) return RETURN_FAILED; } - if (!GenerateProgram(data, output, options.GetOptLevelArg(), options.GetOptLogLevelArg())) { + if (!GenerateProgram(data, output, options.GetCompileByPipeArg(), + options.GetOptLevelArg(), options.GetOptLogLevelArg())) { std::cerr << "call GenerateProgram fail" << std::endl; return RETURN_FAILED; } diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 6d5e7b14a7..ea12bc8f75 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1031,13 +1031,99 @@ static bool ParseData(const std::string &data, panda::pandasm::Program &prog) return true; } -bool GenerateProgram(const std::string &data, std::string output, int optLevel, std::string optLogLevel) +static bool IsStartOrEndPosition(int idx, char *buff, std::string &data) +{ + if (buff[idx] != '$') { + return false; + } + + if (idx == 0 && (data.empty() || data.back() != '#')) { + return true; + } + + if (idx != 0 && buff[idx - 1] != '#') { + return true; + } + + return false; +} + +static bool HandleBuffer(int &ret, bool &isStartDollar, char *buff, std::string &data, panda::pandasm::Program &prog) +{ + uint32_t startPos = 0; + for (int idx = 0; idx < ret; idx++) { + if (IsStartOrEndPosition(idx, buff, data)) { + if (isStartDollar) { + startPos = idx + 1; + isStartDollar = false; + continue; + } + + std::string substr(buff + startPos, buff + idx); + data += substr; + ReplaceAllDistinct(data, "#$", "$"); + if (ParseSmallPieceJson(data, prog)) { + std::cerr << "fail to parse stringify json" << std::endl; + return false; + } + isStartDollar = true; + // clear data after parsing + data.clear(); + } + } + + if (!isStartDollar) { + std::string substr(buff + startPos, buff + ret); + data += substr; + } + + return true; +} + +static bool ReadFromPipe(panda::pandasm::Program &prog) +{ + std::string data; + bool isStartDollar = true; + const size_t bufSize = 4096; + // the parent process open a pipe to this child process with fd of 3 + const size_t fd = 3; + + char buff[bufSize + 1]; + int ret = 0; + + while ((ret = read(fd, buff, bufSize)) != 0) { + if (ret < 0) { + std::cerr << "Read pipe error" << std::endl; + return false; + } + buff[ret] = '\0'; + + if (!HandleBuffer(ret, isStartDollar, buff, data, prog)) { + std::cerr << "fail to handle buffer" << std::endl; + return false; + } + } + + Logd("finish parsing from pipe"); + return true; +} + +bool GenerateProgram([[maybe_unused]] const std::string &data, std::string output, bool isParsingFromPipe, + int optLevel, std::string optLogLevel) { panda::pandasm::Program prog = panda::pandasm::Program(); prog.lang = panda::pandasm::extensions::Language::ECMASCRIPT; - if (!ParseData(data, prog)) { - std::cerr << "fail to parse Data!" << std::endl; - return false; + + if (isParsingFromPipe) { + if (!ReadFromPipe(prog)) { + std::cerr << "fail to parse Pipe!" << std::endl; + return false; + } + } else { + if (!ParseData(data, prog)) { + std::cerr << "fail to parse Data!" << std::endl; + return false; + } } Logd("parsing done, calling pandasm\n"); @@ -1115,30 +1201,3 @@ bool HandleJsonFile(const std::string &input, std::string &data) return true; } - -bool ReadFromPipe(std::string &data) -{ - const size_t bufSize = 4096; - // the parent process open a pipe to this child process with fd of 3 - const size_t fd = 3; - - char buff[bufSize + 1]; - int ret = 0; - - while ((ret = read(fd, buff, bufSize)) != 0) { - if (ret < 0) { - std::cerr << "Read pipe error" << std::endl; - return false; - } - buff[ret] = '\0'; - data += buff; - } - - if (data.empty()) { - std::cerr << "Nothing has been read from pipe" << std::endl; - return false; - } - - Logd("finish reading from pipe"); - return true; -} diff --git a/ts2panda/ts2abc/ts2abc.h b/ts2panda/ts2abc/ts2abc.h index 691e87a0de..37c83d29ce 100644 --- a/ts2panda/ts2abc/ts2abc.h +++ b/ts2panda/ts2abc/ts2abc.h @@ -46,10 +46,8 @@ enum class OptLevel { }; bool HandleJsonFile(const std::string &input, std::string &data); -bool ReadFromPipe(std::string &data); -bool GenerateProgram(const std::string &data, std::string output, - int optLevel, - std::string optLogLevel); +bool GenerateProgram(const std::string &data, std::string output, bool isParsingFromPipe, + int optLevel, std::string optLogLevel); bool GetDebugLog(); void ParseLogEnable(const Json::Value &rootValue); bool GetDebugModeEnabled(); -- Gitee