Custom Robust eval Function: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 15: Line 15:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
//  Error handling
//  Error handling
[ assert( argCount() >= 1, "<b>eval()</b> - Invalid number of parameters <i>0</i>, expected at least <i>1</i> parameter.", 0 ) ]
[ assert( argCount() >= 1, "<b>eval()</b> - Invalid number of parameters <i>0</i>,
                            expected at least <i>1</i> parameter.", 0 ) ]


//  Initialise variables
//  Initialise variables
[ Expression = arg( argCount()-1 ) ]
[ X_Expression_X = arg( argCount()-1 ) ]
[ CancelEval = 0 ]
[ X_CancelEval_X = 0 ]
[ TypeTest = json.type( Expression ) ]
[ X_TypeTest_X = json.type( X_Expression_X ) ]


//  Handle all numbers
//  Handle all numbers
[ if( isNumber( Expression ) == 1 ), code:
[ if( isNumber( X_Expression_X ) == 1 ), code:
{
{
   [ CancelEval = 1 ]
   [ X_CancelEval_X = 1 ]
} ]
} ]


//  Handle empty strings
//  Handle empty strings
[ if( TypeTest == "UNKNOWN" ), code:
[ if( X_TypeTest_X == "UNKNOWN" ), code:
{
{
     [ if( Expression == "" ), code:
     [ if( X_Expression_X == "" ), code:
     {
     {
         [ CancelEval = 1 ]
         [ X_CancelEval_X = 1 ]
     } ]
     } ]
} ]
} ]


//  Handle JSON types
//  Handle JSON types
[ if( TypeTest == "ARRAY" || TypeTest == "OBJECT" ), code:
[ if( X_TypeTest_X == "ARRAY" || X_TypeTest_X == "OBJECT" ), code:
{
{
     [ CancelEval = 1 ]
     [ X_CancelEval_X = 1 ]
} ]
} ]


//  Evaluate or cancel, then return
//  Evaluate or cancel, then return
[ if( CancelEval == 1 ), code:
[ if( X_CancelEval_X == 1 ), code:
{
{
     [ macro.return = Expression ]
     [ macro.return = X_Expression_X ]
};{
};{
     [ macro.return = oldFunction( Expression ) ]
     [ macro.return = oldFunction( X_Expression_X ) ]
} ]
} ]  
 
</source>
</source>
<hr>
<hr>
<br>
<br>
[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 21:24, 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
[ 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 ) ]
} ]