public static Object evaluate(Object context,Object self,String expr,List<String> engineConfigs) throws CWLExpressionException {
  String trimmedExpr=StringUtils.trim(expr);
  if (trimmedExpr.startsWith("$")) {
    trimmedExpr=trimmedExpr.substring(1);
  }
  String function=trimmedExpr;
  if (trimmedExpr.startsWith("{")) {
    function="(function()%expr)()";
    function=function.replace("%expr",trimmedExpr);
  }
  Context cx=Context.enter();
  cx.setOptimizationLevel(OPTIMIZATION_LEVEL);
  cx.setMaximumInterpreterStackDepth(MAX_STACK_DEPTH);
  cx.setClassShutter(new CWLExpressionDenyAllClassShutter());
  try {
    Scriptable globalScope=cx.initStandardObjects();
    if (engineConfigs != null) {
      for (int i=0; i < engineConfigs.size(); i++) {
        Reader engineConfigReader=new StringReader(engineConfigs.get(i));
        cx.evaluateReader(globalScope,engineConfigReader,"engineConfig_" + i + ".js",1,null);
      }
    }
    putToScope(EXPR_CONTEXT_NAME,context,cx,globalScope);
    putToScope(EXPR_SELF_NAME,self,cx,globalScope);
    Scriptable resultScope=cx.newObject(globalScope);
    resultScope.setPrototype(globalScope);
    resultScope.setParentScope(globalScope);
    Object result=cx.evaluateString(resultScope,function,"script",1,null);
    if (result == null || result instanceof Undefined) {
      return null;
    }
    Object wrappedResult=Context.javaToJS(result,globalScope);
    putToScope("$result",wrappedResult,cx,globalScope);
    ScriptableObject.putProperty(globalScope,"$result",wrappedResult);
    String finalFunction="(function() { " + "           var result = $result;" + "           var type = result instanceof Array? \"array\" : typeof result;"+ "           return JSON.stringify({ \"result\" : result, \"type\" : type }); "+ "     })()";
    Scriptable wrapScope=cx.newObject(globalScope);
    wrapScope.setPrototype(globalScope);
    wrapScope.setParentScope(globalScope);
    result=cx.evaluateString(wrapScope,finalFunction,"script",1,null);
    return castResult(result);
  }
 catch (  Exception e) {
    String msg=String.format("Failed evaluating expression %s.",expr);
    throw new CWLExpressionException(msg,e);
  }
 finally {
    Context.exit();
  }
}

public static Object evaluate ( Object context , Object self , String expr , List < String > engineConfigs ) throws CWLExpressionException { String trimmedExpr = StringUtils . trim ( expr ) ; if ( trimmedExpr . startsWith ( "$" ) ) { trimmedExpr = trimmedExpr . substring ( 1 ) ; } String function = trimmedExpr ; if ( trimmedExpr . startsWith ( "{" ) ) { function = "(function()%expr)()" ; function = function . replace ( "%expr" , trimmedExpr ) ; } Context cx = Context . enter ( ) ; cx . setOptimizationLevel ( OPTIMIZATION_LEVEL ) ; cx . setMaximumInterpreterStackDepth ( MAX_STACK_DEPTH ) ; cx . setClassShutter ( new CWLExpressionDenyAllClassShutter ( ) ) ; try { Scriptable globalScope = cx . initStandardObjects ( ) ; if ( engineConfigs != null ) { for ( int i = 0 ; i < engineConfigs . size ( ) ; i ++ ) { Reader engineConfigReader = new StringReader ( engineConfigs . get ( i ) ) ; cx . evaluateReader ( globalScope , engineConfigReader , "engineConfig_" + i + ".js" , 1 , null ) ; } } putToScope ( EXPR_CONTEXT_NAME , context , cx , globalScope ) ; putToScope ( EXPR_SELF_NAME , self , cx , globalScope ) ; Scriptable resultScope = cx . newObject ( globalScope ) ; resultScope . setPrototype ( globalScope ) ; resultScope . setParentScope ( globalScope ) ; Object result = cx . evaluateString ( resultScope , function , "script" , 1 , null ) ; if ( result == null || result instanceof Undefined ) { return null ; } Object wrappedResult = Context . javaToJS ( result , globalScope ) ; putToScope ( "$result" , wrappedResult , cx , globalScope ) ; ScriptableObject . putProperty ( globalScope , "$result" , wrappedResult ) ; String finalFunction = "(function() { " + "           var result = $result;" + "           var type = result instanceof Array? \"array\" : typeof result;" + "           return JSON.stringify({ \"result\" : result, \"type\" : type }); " + "     })()" ; Scriptable wrapScope = cx . newObject ( globalScope ) ; wrapScope . setPrototype ( globalScope ) ; wrapScope . setParentScope ( globalScope ) ; result = cx . evaluateString ( wrapScope , finalFunction , "script" , 1 , null ) ; return castResult ( result ) ; } catch ( Exception e ) { String msg = String . format ( "Failed evaluating expression %s." , expr ) ; throw new CWLExpressionException ( msg , e ) ; } finally { Context . exit ( ) ; } }
evaluate js script ( function or statement )
