I realized a javascript parser built in a web page. A user can put a javascript code in the text-area like:
var i = 0;
i++;
var y = i * 10;
document.write(y);
that is parsed in order to generate some outputs (e.g., document.write stream, and so on).
The function parse is called when textarea change event is generated.
function parse(text) {
try {
....
eval(text);
} catch (e) {
....
return {
status : false, output : ..., ... : ...
};
}
return {
status : true, output : ...., ... : ...
};
}
Everything works well but I have problem when a user writes a loop in the text area (e.g., for(var i=0; i<10; )
, while(true)
)
because the application goes in loop.
To avoid this problem, I would ask you all some questions/solutions for this problem:
- Are there some javascript libraries or functions that allow to
eval
a javascript code but are loop-free? - Can I ask to execute my parse function but in a fixed time? After such time I would generate an exception that stops the eval function.
- Before to call the
eval(text)
I can call acheckIfThereAreLoops
function that analyses the text seeking for patterns likefor(var i=0; i<10; )
orwhile(true)
. Is this a good solution? Can I use a regular expression to seek these patterns?
eval
method after a timeout?