001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package org.gjt.sp.jedit.bsh;
033:
034: class BSHBlock extends SimpleNode {
035: public boolean isSynchronized = false;
036:
037: BSHBlock(int id) {
038: super (id);
039: }
040:
041: public Object eval(CallStack callstack, Interpreter interpreter)
042: throws EvalError {
043: return eval(callstack, interpreter, false);
044: }
045:
046: /**
047: @param overrideNamespace if set to true the block will be executed
048: in the current namespace (not a subordinate one).
049: <p>
050: If true *no* new BlockNamespace will be swapped onto the stack and
051: the eval will happen in the current
052: top namespace. This is used by BshMethod, TryStatement, etc.
053: which must intialize the block first and also for those that perform
054: multiple passes in the same block.
055: */
056: public Object eval(CallStack callstack, Interpreter interpreter,
057: boolean overrideNamespace) throws EvalError {
058: Object syncValue = null;
059: if (isSynchronized) {
060: // First node is the expression on which to sync
061: SimpleNode exp = ((SimpleNode) jjtGetChild(0));
062: syncValue = exp.eval(callstack, interpreter);
063: }
064:
065: Object ret;
066: if (isSynchronized) // Do the actual synchronization
067: synchronized (syncValue) {
068: ret = evalBlock(callstack, interpreter,
069: overrideNamespace, null/*filter*/);
070: }
071: else
072: ret = evalBlock(callstack, interpreter, overrideNamespace,
073: null/*filter*/);
074:
075: return ret;
076: }
077:
078: Object evalBlock(CallStack callstack, Interpreter interpreter,
079: boolean overrideNamespace, NodeFilter nodeFilter)
080: throws EvalError {
081: Object ret = Primitive.VOID;
082: NameSpace enclosingNameSpace = null;
083: if (!overrideNamespace) {
084: enclosingNameSpace = callstack.top();
085: BlockNameSpace bodyNameSpace = new BlockNameSpace(
086: enclosingNameSpace);
087:
088: callstack.swap(bodyNameSpace);
089: }
090:
091: int startChild = isSynchronized ? 1 : 0;
092: int numChildren = jjtGetNumChildren();
093:
094: try {
095: /*
096: Evaluate block in two passes:
097: First do class declarations then do everything else.
098: */
099: for (int i = startChild; i < numChildren; i++) {
100: SimpleNode node = ((SimpleNode) jjtGetChild(i));
101:
102: if (nodeFilter != null && !nodeFilter.isVisible(node))
103: continue;
104:
105: if (node instanceof BSHClassDeclaration)
106: node.eval(callstack, interpreter);
107: }
108: for (int i = startChild; i < numChildren; i++) {
109: SimpleNode node = ((SimpleNode) jjtGetChild(i));
110: if (node instanceof BSHClassDeclaration)
111: continue;
112:
113: // filter nodes
114: if (nodeFilter != null && !nodeFilter.isVisible(node))
115: continue;
116:
117: ret = node.eval(callstack, interpreter);
118:
119: // statement or embedded block evaluated a return statement
120: if (ret instanceof ReturnControl)
121: break;
122: }
123: } finally {
124: // make sure we put the namespace back when we leave.
125: if (!overrideNamespace)
126: callstack.swap(enclosingNameSpace);
127: }
128: return ret;
129: }
130:
131: public interface NodeFilter {
132: public boolean isVisible(SimpleNode node);
133: }
134:
135: }
|