Custom Robust eval Function: Difference between revisions
Jump to navigation
Jump to search
Verisimilar (talk | contribs) (Updated and fixed.) |
Verisimilar (talk | contribs) mNo edit summary |
||
Line 9: | Line 9: | ||
<hr>'''onCampaignLoad''' | <hr>'''onCampaignLoad''' | ||
<source lang="mtmacro" line> | <source lang="mtmacro" line> | ||
[ defineFunction( "eval", "evalFunction@this", 1 ) ] | [ defineFunction( "eval", "evalFunction@this", 1, 0 ) ] | ||
</source> | </source> | ||
<hr><br> | <hr><br> | ||
Line 53: | Line 53: | ||
<hr> | <hr> | ||
<br> | <br> | ||
[[Category:Cookbook]] | [[Category:Cookbook]] |
Revision as of 21:14, 17 June 2009
Note: This function can only be used in a Trusted Macro
This user defined function redefines the standard eval() function, allowing it to be given a number, empty string, or JSON object/array and not throw an exception.
Macros
Place both of these macros on the same library token.
1.3b56+
onCampaignLoad
[ defineFunction( "eval", "evalFunction@this", 1, 0 ) ]
evalFunction
// 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
[ Expression = arg( argCount()-1 ) ]
[ CancelEval = 0 ]
[ TypeTest = json.type( Expression ) ]
// Handle all numbers
[ if( isNumber( Expression ) == 1 ), code:
{
[ CancelEval = 1 ]
} ]
// Handle empty strings
[ if( TypeTest == "UNKNOWN" ), code:
{
[ if( Expression == "" ), code:
{
[ CancelEval = 1 ]
} ]
} ]
// Handle JSON types
[ if( TypeTest == "ARRAY" || TypeTest == "OBJECT" ), code:
{
[ CancelEval = 1 ]
} ]
// Evaluate or cancel, then return
[ if( CancelEval == 1 ), code:
{
[ macro.return = Expression ]
};{
[ macro.return = oldFunction( Expression ) ]
} ]