PageRenderTime 48ms CodeModel.GetById 39ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHBlock.java

#
Java | 120 lines | 61 code | 14 blank | 45 comment | 6 complexity | 89ab38363e6ad676c22425832143de3e MD5 | raw file
  1/*****************************************************************************
  2 *                                                                           *
  3 *  This file is part of the BeanShell Java Scripting distribution.          *
  4 *  Documentation and updates may be found at http://www.beanshell.org/      *
  5 *                                                                           *
  6 *  Sun Public License Notice:                                               *
  7 *                                                                           *
  8 *  The contents of this file are subject to the Sun Public License Version  *
  9 *  1.0 (the "License"); you may not use this file except in compliance with *
 10 *  the License. A copy of the License is available at http://www.sun.com    * 
 11 *                                                                           *
 12 *  The Original Code is BeanShell. The Initial Developer of the Original    *
 13 *  Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright     *
 14 *  (C) 2000.  All Rights Reserved.                                          *
 15 *                                                                           *
 16 *  GNU Public License Notice:                                               *
 17 *                                                                           *
 18 *  Alternatively, the contents of this file may be used under the terms of  *
 19 *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
 20 *  provisions of LGPL are applicable instead of those above. If you wish to *
 21 *  allow use of your version of this file only under the  terms of the LGPL *
 22 *  and not to allow others to use your version of this file under the SPL,  *
 23 *  indicate your decision by deleting the provisions above and replace      *
 24 *  them with the notice and other provisions required by the LGPL.  If you  *
 25 *  do not delete the provisions above, a recipient may use your version of  *
 26 *  this file under either the SPL or the LGPL.                              *
 27 *                                                                           *
 28 *  Patrick Niemeyer (pat@pat.net)                                           *
 29 *  Author of Learning Java, O'Reilly & Associates                           *
 30 *  http://www.pat.net/~pat/                                                 *
 31 *                                                                           *
 32 *****************************************************************************/
 33
 34
 35package bsh;
 36
 37class BSHBlock extends SimpleNode
 38{
 39	public boolean isSynchronized = false;
 40
 41	BSHBlock(int id) { super(id); }
 42
 43	public Object eval( CallStack callstack, Interpreter interpreter) 
 44		throws EvalError
 45	{
 46		return eval( callstack, interpreter, false );
 47	}
 48
 49	/**
 50		@param overrideNamespace if set to true the block will be executed
 51		in the current namespace (not a subordinate one).
 52		<p>
 53		If true *no* new BlockNamespace will be swapped onto the stack and 
 54		the eval will happen in the current
 55		top namespace.  This is used by BshMethod, TryStatement, etc.  
 56		which must intialize the block first and also for those that perform 
 57		multiple passes in the same block.
 58	*/
 59	public Object eval( 
 60		CallStack callstack, Interpreter interpreter, 
 61		boolean overrideNamespace ) 
 62		throws EvalError
 63	{
 64		Object syncValue = null;
 65		if ( isSynchronized ) 
 66		{
 67			// First node is the expression on which to sync
 68			SimpleNode exp = ((SimpleNode)jjtGetChild(0));
 69			syncValue = exp.eval(callstack, interpreter);
 70		}
 71
 72		Object ret;
 73		if ( isSynchronized ) // Do the actual synchronization
 74			synchronized( syncValue )
 75			{
 76				ret = evalBlock( callstack, interpreter, overrideNamespace );
 77			}
 78		else
 79				ret = evalBlock( callstack, interpreter, overrideNamespace );
 80
 81		return ret;
 82	}
 83
 84	Object evalBlock( CallStack callstack, Interpreter interpreter, 
 85		boolean overrideNamespace ) 
 86		throws EvalError
 87	{	
 88		Object ret = Primitive.VOID;
 89		NameSpace enclosingNameSpace = null;
 90		if ( !overrideNamespace ) 
 91		{
 92			enclosingNameSpace= callstack.top();
 93			BlockNameSpace bodyNameSpace = 
 94				new BlockNameSpace( enclosingNameSpace );
 95
 96			callstack.swap( bodyNameSpace );
 97		}
 98
 99		int startChild = isSynchronized ? 1 : 0;
100		int numChildren = jjtGetNumChildren();
101
102		try {
103			for(int i=startChild; i<numChildren; i++)
104			{
105				SimpleNode node = ((SimpleNode)jjtGetChild(i));
106				ret = node.eval( callstack, interpreter );
107
108				// some statement or embedded block evaluated a return statement
109				if (ret instanceof ReturnControl)
110					break;
111			}
112		} finally {
113			// make sure we put the namespace back when we leave.
114			if ( !overrideNamespace ) 
115				callstack.swap( enclosingNameSpace );
116		}
117		return ret;
118	}
119}
120