Anyone tried doing a maths expression parser with ...
# cfml-general
a
Anyone tried doing a maths expression parser with CFML? Found a JS library https://mathjs.org/ and a old Java one https://github.com/fasseg/exp4j. Knocked up a super simple CFML version (uses evaluate) but defence against arbitrary code execution is my main worry.
🙌 1
Tried the exp4j library and seems to work nicely. Example code:
Copy code
var ExpressionBuilder = createObject("java","net.objecthunter.exp4j.ExpressionBuilder").init("3 * sin(y) - 2 / (x - 2)");
var meh = javacast("java.lang.String[]", ["x", "y"]);
var Expression = ExpressionBuilder.variables(meh).build();
Expression.setVariable("x", 2.3);
Expression.setVariable("y", 3.14);
var result = Expression.evaluate();
z
your concern is due to the function name evaluate?
a
Yeah - I was worried about it running any old code that someone managed to stick in