Custom Robust eval Function: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Zero-Proof eval moved to Custom Robust eval Function: No longer just zero-proof.)
m (Taustin moved page custom Robust eval Function to Custom Robust eval Function without leaving a redirect)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{TrustedFunction}}
{{TrustedFunction}}
This user defined function redefines the standard {{func|eval}} function, allowing it to be given a zero and not throw an exception.
This user defined function redefines the standard {{func|eval}} function, allowing it to be given a number, empty string, or JSON object/array and not throw an exception.


===Macros===
===Macros===
Place both of these macros on the same library token.
Place both of these macros on the same library token.
'''1.3b56+'''
<hr>'''onCampaignLoad'''
<hr>'''onCampaignLoad'''
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro" line>
[defineFunction("eval", "evalFunction@this")]
[ defineFunction( "eval", "evalFunction@this", 1, 0 ) ]
</source>
</syntaxhighlight>
<hr><br>
<hr><br>
<hr>'''evalFunction'''
<hr>'''evalFunction'''
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro" line>
[h: assert( argCount() == 1, "eval() requires one parameter.")]
//  Error handling
[h, if ( arg(0) == 0 ), code:
[ 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:
{
{
     [h: macro.return = 0]
     [ macro.return = X_Expression_X ]
};{
};{
     [h: macro.return = oldFunction( arg(0) ) ]
     [ macro.return = oldFunction( X_Expression_X ) ]
}]
} ]  
</source>
 
</syntaxhighlight>
<hr>
<hr>
<br>
<br>
Alternately, this version also handles the empty string ("") gracefully.
<hr>'''evalFunction'''
<source lang="mtmacro">
<!--
[H: assert( argCount() == 1, "eval() requires one parameter.")]
[H, IF( arg(0) == 0 ): macro.return = 0]
[H, IF( arg(0) == "" ): macro.return = ""]
[H, IF( arg(0) != 0 && arg(0) != ""): macro.return = oldFunction( arg(0) )]
-->
</source>
[[Category:Cookbook]]
[[Category:Cookbook]]

Latest revision as of 18:29, 2 May 2023

 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 ) ]
} ]