Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using variables as functions - VM2 #249

Open
strykeio opened this issue Mar 24, 2021 · 2 comments
Open

Using variables as functions - VM2 #249

strykeio opened this issue Mar 24, 2021 · 2 comments

Comments

@strykeio
Copy link

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 pass myFunc 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.

@silentmatt
Copy link
Owner

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.

@strykeio
Copy link
Author

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:
patriksimek/vm2#343

If I find out more I will post back.
Thank you!
Edoardo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants