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.)
(Updated and fixed.)
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">
<source lang="mtmacro" line>
[defineFunction("eval", "evalFunction@this")]
[ defineFunction( "eval", "evalFunction@this", 1 ) ]
</source>
</source>
<hr><br>
<hr><br>
<hr>'''evalFunction'''
<hr>'''evalFunction'''
<source lang="mtmacro">
<source 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
[ Expression = arg( argCount()-1 ) ]
[ CancelEval = 0 ]
[ TypeTest = json.type( Expression ) ]
 
//  Handle all numbers
[ if( isNumber( Expression ) == 1 ), code:
{
{
     [h: macro.return = 0]
  [ 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 ]
};{
};{
     [h: macro.return = oldFunction( arg(0) ) ]
     [ macro.return = oldFunction( Expression ) ]
}]
} ]
</source>
</source>
<hr>
<hr>
<br>
<br>
Alternately, this version also handles the empty string ("") gracefully.
'''1.3b51 - 1.3b55'''
 
<hr>'''onCampaignLoad'''
<source lang="mtmacro" line>
[ defineFunction( "eval", "evalFunction@this" ) ]
</source>
<hr><br>
<hr>'''evalFunction'''
<hr>'''evalFunction'''
<source lang="mtmacro">
<source lang="mtmacro" line>
<!--
[h: assert( argCount() >= 1, "<b>eval()</b> - Invalid number of parameters <i>0</i>, expected at least <i>1</i> parameter.", 0 ) ]
[H: assert( argCount() == 1, "eval() requires one parameter.")]
 
[H, IF( arg(0) == 0 ): macro.return = 0]
[h: Expression = arg( argCount()-1 ) ]
[H, IF( arg(0) == "" ): macro.return = ""]
[h: CancelEval = 0 ]
[H, IF( arg(0) != 0 && arg(0) != ""): macro.return = oldFunction( arg(0) )]
[h: TypeTest = json.type( Expression ) ]
-->
 
[h: if( isNumber( Expression ) == 1 ), code:
{
  [h: CancelEval = 1 ]
} ]
 
[h: if( TypeTest == "UNKNOWN" ), code:
{
    [h: if( Expression == "" ), code:
    {
        [h: CancelEval = 1 ]
    } ]
} ]
 
[h: if( TypeTest == "ARRAY" || TypeTest == "OBJECT" ), code:
{
    [h: CancelEval = 1 ]
} ]
 
[h: if( CancelEval == 1 ), code:
{
    [h: macro.return = Expression ]
};{
    [h: macro.return = oldFunction( Expression ) ]
} ]
</source>
</source>
<hr>
[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 17:37, 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 ) ]



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


1.3b51 - 1.3b55


onCampaignLoad

[ defineFunction( "eval", "evalFunction@this" ) ]



evalFunction

[h: assert( argCount() >= 1, "<b>eval()</b> - Invalid number of parameters <i>0</i>, expected at least <i>1</i> parameter.", 0 ) ]

[h: Expression = arg( argCount()-1 ) ]
[h: CancelEval = 0 ]
[h: TypeTest = json.type( Expression ) ]

[h: if( isNumber( Expression ) == 1 ), code:
{
   [h: CancelEval = 1 ]
} ]

[h: if( TypeTest == "UNKNOWN" ), code:
{
    [h: if( Expression == "" ), code:
    {
        [h: CancelEval = 1 ]
    } ]
} ]

[h: if( TypeTest == "ARRAY" || TypeTest == "OBJECT" ), code:
{
    [h: CancelEval = 1 ]
} ]

[h: if( CancelEval == 1 ), code:
{
    [h: macro.return = Expression ]
};{
    [h: macro.return = oldFunction( Expression ) ]
} ]