Features this custom version has that the standard eval() lacks:
- Returns numbers instead of providing an error.
- Returns empty strings instead of providing an error.
- Returns JSON objects/arrays instead of providing an error.
Place the following two macros on a library token:
1.3b56+
onCampaignLoad
Code: Select all
[ defineFunction( "eval", "[email protected]", 1, 0 ) ]
evalFunction
Code: Select all
// Error handling
[ assert( argCount() >= 1, "<b>eval()</b> - Invalid number of parameters <i>0</i>,
expected at least <i>1</i> parameter.", 0 ) ]
// Initialise variables
[ X_Expression_X = arg( argCount()-1 ) ]
[ X_CancelEval_X = 0 ]
[ X_TypeTest_X = json.type( X_Expression_X ) ]
// Handle all numbers
[ if( isNumber( X_Expression_X ) == 1 ), code:
{
[ X_CancelEval_X = 1 ]
} ]
// Handle empty strings
[ if( X_TypeTest_X == "UNKNOWN" ), code:
{
[ if( X_Expression_X == "" ), code:
{
[ X_CancelEval_X = 1 ]
} ]
} ]
// Handle JSON types
[ if( X_TypeTest_X == "ARRAY" || X_TypeTest_X == "OBJECT" ), code:
{
[ X_CancelEval_X = 1 ]
} ]
// Evaluate or cancel, then return
[ if( X_CancelEval_X == 1 ), code:
{
[ macro.return = X_Expression_X ]
};{
[ macro.return = oldFunction( X_Expression_X ) ]
} ]
[spoiler=Original Post]I see this problem crop up from time to time, and I've run into it myself; eval() chokes when fed a zero. To combat this I used to use a user defined function called myEval(), but I was recently(today) exposed to a function that would allow it to be far more elegant. So, I present to you, a custom eval().
Place the following in an onCampaignLoad macro, modifying the library token to suit your needs.
Code: Select all
[defineFunction("eval", "[email protected]:Utilities")]
On the library token referenced in the defineFunction() call, create a macro named evalFunction, and paste the following into the Command:
Code: Select all
[h: assert( argCount() == 1, "eval() requires one parameter.")]
[h, if ( arg(0) == 0 ), code:
{
[h: macro.return = 0]
};{
[h: macro.return = oldFunction( arg(0) ) ]
}]
You can now continue to use eval() as you normally would, and never again worry about feeding it a zero.[/spoiler]