Reputation: 6624
I have a file a /path/to/my_js_functions.js that contains a Javascript function my_js_functions(), amongst other functions.
How can I go about reading the function myJsFunction
as a String in C++?
I am looking for a way to get the whole function and only that function, and then contain it in a 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.
Upvotes: 1
Views: 822
Reputation: 3277
To do this you need to read your javascript code file and parse it. It is highly to use some parser library to do that like cashew,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
#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 << "\n" << getFunction(fileData, name) << std::endl;
}
}
return 0;
}
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(){{{{}}}
Upvotes: 1
Reputation: 394
You can simply doing this
For example /path/code.js is the path your code stored
Your code.js
function myJsFunction(stringParam) {
return stringParam // The function returns a stringParam from the parameter
}
function anotherJsFunction(stringParam) {
return stringParam // Another function
}
module.exports = {
myJsFunction,
anotherJsFunction
}
And this is the file that you use to read the function you write
index.js
const code = require('/path/code.js');
console.log(code), it will be showing your whole code in your code.js file
If you want to make it string, you can use syntax like this. Add this in code below on your index.js file and it will make string version of your code.
String(code.myJsFunction)
Upvotes: 0
Reputation: 426
Using fstream, I would read line by line and check whether each line contains the sequence myJsFunction. If a line does contain this sequence, then you begin aggregating to a string until you reach the next function (stopping after you reach the next keyword "function" or something of that sort). Note that using }
as a keyword may not work as functions are likely to have multiple closing braces.
Another possible solution could include identifying the end of the function by noticing that when a newline is immediately followed by non-whitespace a new function is beginning, assuming the code in your file is formatted where anything lower in scope is tabbed over correctly.
Upvotes: 1