Open strykeio opened 3 years ago
That's an interesting problem. Your variable solution is a good workaround, and while it's using it in a slightly different way than how it's intended, it's fully supported and shouldn't cause a problem. One potential downside is that if you have variable assignment enabled, expressions could overwrite the functions. It requires you to keep passing in an appropriate context object with the functions you need, instead of just having them there automatically.
It's interesting that vm2 is preventing the "standard" way from working. If you figure out why, and it's something we could work around, I'd appreciate an update if you don't mind. I'm not familiar with vm2 at all, but my wild guess is maybe it doesn't like you modifying objects that originated from an external library. If that's the case, maybe an addition to the constructor argument to add functions when you create the parser instead of an additional step after would solve that problem. I might play around with that if I get a chance.
Hi,
Thanks for the answer and confirm that things should be pretty stable even with this "workaround approach". I am not using variable assignments so I should be safe.
I do suspect that the reason VM2 is not allowing the assignment is something along the lines of what you are saying. I have asked them on their github here: https://github.com/patriksimek/vm2/issues/343
If I find out more I will post back. Thank you! Edoardo.
Hi, I have been using expr-eval to evaluate logical formulas that include some basic calculations. I am now trying to introduce functions in my formulas to meet some new requirements. One of the requirements I have is to run inside a vm2 sandbox (https://github.com/patriksimek/vm2). When running inside vm2,
parser.functions.myFunc = myFunc;
does not work (I am trying to get to the bottom of that at the moment). As a workaround, I realized I can passmyFunc
as a variable and it will get called as a function if present in the formula that is being evaluated.While this seems to work well, I understand that I am using variables for a different purpose they were originally intended. Is this safe? If I cannot use
parser.functions.myFunc
in vm2 is this something I can adopt as a solution long term?Thanks for the great module! Edoardo.