Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

javascript - 如何读取Javascript文件(How to read a Javascript file)

I have a file a /path/to/my_js_functions.js that contains a Javascript function my_js_functions() , amongst other functions.(我有一个/path/to/my_js_functions.js文件,其中包含Javascript函数my_js_functions()和其他函数。)

How can I go about reading the function myJsFunction as a String in C++ ?(如何在C ++中将函数myJsFunction读取为字符串?) I am looking for a way to get the whole function and only that function, and then contain it in a char *jsFunction .(我正在寻找一种方法来获取整个函数和仅该函数,然后将其包含在char *jsFunction 。) function myJsFunction(stringParam) { return stringParam // The function returns a stringParam from the parameter } function anotherJsFunction(stringParam) { return stringParam // Another function } Thank you all in advance.(谢谢大家。)   ask by Program-Me-Rev translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To do this you need to read your javascript code file and parse it.(为此,您需要阅读并解析JavaScript代码文件。)

It is highly to use some parser library to do that like cashew , esprima-cpp .(使用某些解析器库来完成诸如腰果esprima-cpp之类的操作非常重要 。) I never used that before I never used any of this before, So I can't comment on that.(我以前从未使用过它,所以我无法对此发表评论。) But here is some quick code for parser.(但是这里有一些用于解析器的快速代码。) You can start with this build on this to make it more robust.(您可以从此基础上开始,使其更强大。) main.cpp(main.cpp) #include <fstream> #include <iostream> #include <streambuf> #include <string> #include <vector> std::string getFunction(const std::string &fileData, const std::string &name) { std::string ret; std::size_t start = 0; while (true) { const auto fNameStart = fileData.find(name, start); if (fNameStart != std::string::npos) { auto fStart = fileData.find_last_not_of(" ",fNameStart-1); if(fStart == std::string::npos){ ret = "No Function Defination"; break; } else { fStart = fStart-7; if(fileData.substr(fStart,8) == "function"){ int openBraceCount = 0, closeBraceCount = 0; std::size_t fEnd = fNameStart + name.size(); fEnd = fileData.find_first_of("{}", fEnd); while (fEnd != std::string::npos) { if (fileData.at(fEnd) == '{') { openBraceCount++; } else { closeBraceCount++; } if (openBraceCount == closeBraceCount) { ret = fileData.substr(fStart, fEnd - fStart+1); break; } fEnd++; fEnd = fileData.find_first_of("{}", fEnd); } if(!ret.empty()){ break; } else if(openBraceCount != closeBraceCount){ ret = "Function Parse Error"; break; } } else{ start = fNameStart + name.size(); } } } else { ret = "No Function Defination"; break; } } return ret; } int main(int argc, char **argv) { const std::string jsPath = "module.js"; const std::vector<std::string> vecFuncNames{"funcA", "funcB", "funcC", "funcD", "funcE"}; std::ifstream fs(jsPath); if (fs.is_open()) { std::string fileData((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>()); for (auto &name : vecFuncNames) { std::cout << name << " " << getFunction(fileData, name) << std::endl; } } return 0; } module.js(module.js) function funcA ( ){ funcC(); console.log(" Hello");funcB(); }function funcC(){funcB();console.log("Hello");funcA();} function funcB(a, b, c){ funcA(); setTimeout(function(){ alert("Hello"); }, 3000);funcC(); } funcD(); function funcE(){{{{}}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...