PageRenderTime 280ms CodeModel.GetById 277ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1/**
 2	Evaluate the string in the current interpreter (see source()).
 3	Returns the result of the evaluation or null.
 4	<p>
 5
 6	Evaluate a string as if it were written directly in the current scope, 
 7	with side effects in the current scope.
 8	<p>
 9	e.g.
10	<code><pre>
11		a=5;
12		eval("b=a*2");
13		print(b); // 10
14	</pre></code>
15	<p>
16
17	eval() acts just like invoked text except that any exceptions generated
18	by the code are captured in a bsh.EvalError.  This includes ParseException
19	for syntactic errors and TargetError for exceptions thrown by the evaluated
20	code.
21	<p>
22	e.g.
23	<pre>
24		try {
25			eval("foo>>><>M>JK$LJLK$");
26		} catch ( EvalError e ) {
27			// ParseException caught here
28		}
29
30		try {
31			eval("(Integer)true");  // illegal cast
32		} catch ( EvalError e ) {
33			// TargetException caught here
34			print( e.getTarget() )  // prints ClassCastException
35		}
36	</pre>
37	<p>
38	
39	If you want eval() to throw target exceptions directly, without wrapping
40	them, you can simply redefine own eval like so:
41
42	<pre>
43		myeval( String expression ) {
44			try {
45				return eval( expression );
46			} catch ( TargetError e ) {
47				throw e.getTarget();
48			}
49		}
50	</pre>
51
52	<p>
53	Returns the value of the expression.
54	<p>
55	Throws bsh.EvalError on error
56	<p>
57	@return the value of the expression.
58	@throws bsh.EvalError on error
59*/
60
61bsh.help.eval = "usage: eval( String expression )";
62
63Object eval( String expression ) {
64    return this.interpreter.eval( expression, this.caller.namespace );
65}
66