PageRenderTime 61ms CodeModel.GetById 20ms app.highlight 1ms RepoModel.GetById 29ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/commands/eval.bsh

#
Unknown | 75 lines | 65 code | 10 blank | 0 comment | 0 complexity | 0251c9dade841afedcc85f89d1ecc313 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	<p/>
52
53	Here is a cute example of how to use eval to implement a dynamic cast.  
54	i.e. to cast a script to an arbitrary type by name at run-time where the
55	type is not known when you are writing the script.  In this case the type
56	is in the variable interfaceType.
57	<pre>
58    reference = eval( "("+interfaceType+")this" );
59	</pre>
60
61	<p>
62	Returns the value of the expression.
63	<p>
64	Throws bsh.EvalError on error
65	<p>
66	@return the value of the expression.
67	@throws bsh.EvalError on error
68*/
69
70bsh.help.eval = "usage: eval( String expression )";
71
72Object eval( String expression ) {
73    return this.interpreter.eval( expression, this.caller.namespace );
74}
75