diff --git a/ts2panda/ts2abc/main.cpp b/ts2panda/ts2abc/main.cpp index 724a6536d26de45f8b9f5638ddd96c2d2cee7141..3eaef44c989ce1c1ad75cdbd1a3d4b2c30d2a441 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 6d5e7b14a7497f4edb2dff5b16972799ccc0d49e..ea12bc8f7535d79920ddb273a20f38e1684f0ba9 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 691e87a0de11bf81af137d734610d46d65c92005..37c83d29ce0b7c56e346295f43bbfbf08ecfd5fc 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();