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.
1.3b56+
onCampaignLoad
Code: Select all
[ defineFunction( "eval", "evalFunction@this", 1, 0 ) ]
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 ) ]
} ]
Original Post
Place the following in an onCampaignLoad macro, modifying the library token to suit your needs.
Code: Select all
[defineFunction("eval", "evalFunction@Lib:Utilities")]
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) ) ]
}]