Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 23, 2012 14:59
Show Gist options
  • Select an option

  • Save bennadel/2171470 to your computer and use it in GitHub Desktop.

Select an option

Save bennadel/2171470 to your computer and use it in GitHub Desktop.
ColdFusion 10 - Creating A Simple Expression Evaluator
<cfscript>
// I evaluate the given expression in the context of the given
// context object (for variable look-up).
evaluateExpression = function( context, expression ){
// Copy the context object to the look-up.
structAppend( local, context );
// Evaluate the expression in the context of the current
// local scope, which has been augmented with the contents
// of the context object.
return( evaluate( expression ) );
};
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Create a context object.
friend = {
name: "Tricia",
age: 35
};
// Evaluate the expression in the given context.
condition = evaluateExpression(
friend,
"(name eq 'Tricia') && (age gt 30)"
);
// Output the evaluated result.
writeOutput( "Result: " & condition & "<br />" );
// Evaluate the expression in the given context.
condition = evaluateExpression(
friend,
"len( name ) eq 3"
);
// Output the evaluated result.
writeOutput( "Result: " & condition & "<br />" );
</cfscript>
@mark-moran-wne

Copy link
Copy Markdown

I wish CF let you specify a scope, it isn't safe to allow an end user to write expressions:
condition = evaluateExpression(
friend,
"SERVER.coldfusion.productversion EQ '2023...

//Result: YES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment