I'm creating a very primitive, online interpreter/compiler in Node.js for multiple languages just for the experience and I've run in to a very odd problem when running JS code.
When a user posts, I take their input, escape it a little, and feed it directly into the command line (terrible practice, I know, but I'll be moving to a new system later that doesn't involve direct CMD) This escapes double quotes and \n, \r, etc.
When getting input, I child_process.exec
it with the command (yes, I am giving it a callback, but it's a fairly long one that I don't think is necessary to write)
let parentResults = cp.exec(`node ./builders/${this.builder}.js "${this.escapedCode}"`);
// First parameter represents the builder to run the user input with
// and escaped code is self-explanatory
The builder that handles JS only has one line:
eval(process.argv[2]); // Already somewhat-escaped code
Now, when I write something like
function foo(x) {
console.log(x);
}
foo(5);
I get the correct output in the console of 5
.
But when I do something like
let foo = function(x) {
console.log(x);
}
foo(5);
I get an error saying
console.log(x);
^
SyntaxError: Unexpected identifier
The same thing happens when I use arrow syntax as well. I have no clue what could be tripping it up. Any ideas or help?