/jEdit/tags/jedit-4-1-pre5/bsh/commands/eval.bsh
Unknown | 66 lines | 57 code | 9 blank | 0 comment | 0 complexity | 0ae2d85ec59f9b352bbf6b1f2e988c68 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /**
- Evaluate the string in the current interpreter (see source()).
- Returns the result of the evaluation or null.
- <p>
- Evaluate a string as if it were written directly in the current scope,
- with side effects in the current scope.
- <p>
- e.g.
- <code><pre>
- a=5;
- eval("b=a*2");
- print(b); // 10
- </pre></code>
- <p>
- eval() acts just like invoked text except that any exceptions generated
- by the code are captured in a bsh.EvalError. This includes ParseException
- for syntactic errors and TargetError for exceptions thrown by the evaluated
- code.
- <p>
- e.g.
- <pre>
- try {
- eval("foo>>><>M>JK$LJLK$");
- } catch ( EvalError e ) {
- // ParseException caught here
- }
- try {
- eval("(Integer)true"); // illegal cast
- } catch ( EvalError e ) {
- // TargetException caught here
- print( e.getTarget() ) // prints ClassCastException
- }
- </pre>
- <p>
-
- If you want eval() to throw target exceptions directly, without wrapping
- them, you can simply redefine own eval like so:
- <pre>
- myeval( String expression ) {
- try {
- return eval( expression );
- } catch ( TargetError e ) {
- throw e.getTarget();
- }
- }
- </pre>
- <p>
- Returns the value of the expression.
- <p>
- Throws bsh.EvalError on error
- <p>
- @return the value of the expression.
- @throws bsh.EvalError on error
- */
- bsh.help.eval = "usage: eval( String expression )";
- Object eval( String expression ) {
- return this.interpreter.eval( expression, this.caller.namespace );
- }