Please make eval less evil

Post Reply
marbledaemon
Newbie
Posts: 7
Joined: Mon Sep 24, 2012 12:28 pm

Please make eval less evil

Post by marbledaemon »

Hello,



With the lack of ability to call another script from within a script, we had to read the script from a file then execute it using "eval".



However, if the syntax is bad, it would lead to a crash of Script Executor.



Could you fix that by making it only throw exceptions instead?



This is quite important.



Thanks!
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Please make eval less evil

Post by freddyp »

I can reproduce the fact that SwitchScriptExecutor crashes when there is a syntax error in the string you are sending through eval. I have logged this.



In my experiment I defined a function, but that function is not available after eval, only directly executable Javascript code works.



Now, I thought you would be using this external script for storing interesting functions that you are using all the time and that you do not want to duplicate in the different scripts you are writing. But as that cannot work this leads me to ask what it is you are doing with the external scripts and if there is really a compelling reason for working this way? I would like to understand more. Can you please explain?



Freddy
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Please make eval less evil

Post by freddyp »

The crash bug has been fixed, so it will be in Switch 11 update 4.



I remain curious as to why you are using the external scripts., though.



Freddy
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Please make eval less evil

Post by dkelly »

Maybe they built a library of frequently used routines?
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Please make eval less evil

Post by freddyp »

That is also what I thought (see my first reply), but I did an experiment and eval works fine when the string contains directly executable Javascript. However, when you define a function in the string, eval "works" (i.e. no complaints, no crashes), but the function is not available to your code after the eval. This means that the use case of a library of frequently used routines is not possible.



So, either I am doing something wrong, in which case I would like to know what, or marbledaemon is using this eval method for something else, and in that case I am curious what it is, so I can judge whether it makes sense, or whether a different approach is more appropriate.



Freddy
marbledaemon
Newbie
Posts: 7
Joined: Mon Sep 24, 2012 12:28 pm

Please make eval less evil

Post by marbledaemon »

Hello,



I was moved out of our workflow team for a while. Now let me come back with the answer.



I used 'eval javascript' in order to achieve what we don't have in Power Switch: include / import libraries and dynamic scripts.



I created a generic workflow which accepted customer files in various and dynamic locations, which in turn were not predetermined at the moment of flow deployment. So basically, different configurations (in form of xml) can be provided to the flow dynamically (run-time) without the need of stopping the workflow (proven to be highly unpredictable). Moreover, the scripts to process of the customer job files can be added, modified and removed dynamically in the same way (by modifying the configuration files). It helps us avoid the need to stop the processing of PowerSwitch every time we add / modify a script.



So as you might guess, all provided pieces of javascript need to be executed by "eval" in our main flow script.



I hope it clarifies :)



Regards!
Post Reply