Package anvil.script.statements

Source Code of anvil.script.statements.SynchronizedStatement

/*
* $Id: SynchronizedStatement.java,v 1.4 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.statements;

import anvil.core.Any;
import anvil.Location;
import anvil.codec.Code;
import anvil.codec.Source;
import anvil.codec.Target;
import anvil.codec.ExceptionHandler;
import anvil.parser.Tag;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.expression.Expression;
import anvil.script.parser.TemplateParser;
import anvil.script.Grammar;
import java.io.IOException;

/**
* class SynchronizedStatement
*
* @author: Jani Lehtim�ki
*/
public class SynchronizedStatement extends ScopedStatement
{

  private Expression       _expression = null;
  private Statement        _statement = EMPTY;
  private ExceptionHandler _handler;
 


  public SynchronizedStatement(Statement parent, Location location, Expression condition)
  {
    super(parent, location);
    _expression = condition;
  }


  public int typeOf()
  {
    return Statement.ST_SYNCHRONIZED;
  }


  public String name()
  {
    return "synchronized";
  }


  public void parse(TemplateParser parser, Tag tag)
  {
  }


  public boolean onTag(TemplateParser parser, int type, Tag tag)
  {
    return true;
  }


  public Statement getChildStatement()
  {
    return _statement;
  }  


  public void setChildStatement(Statement statement)
  {
    _statement = statement;
  }  


  public void check(ErrorListener context)
  {
    _expression.check(context);
    _statement.check(context);
  }

 
  public Jumps eliminate(ErrorListener context)
  {
    Jumps jumps = _statement.eliminate(context);
    return jumps;
  }


  public void compile(ByteCompiler context)
  {
    Code code = context.getCode();
    int lock = code.addLocal();
    _expression.compile(context, Expression.GET);
    code.astore(lock);
    code.aload(lock);
    code.monitorenter();
    ExceptionHandler handler = code.startExceptionHandler(true);
    _handler = handler;
    if (_statement == null) {
      code.nop();
    } else {
      _statement.compile(context);
    }
    handler.endTry();
    if (!_statement.isBlocked()) {
      handler.callFinally();
      handler.jumpOut();
    }
    handler.endProtectedRegion();
    handler.startCatch(0);
    int thrown = code.addLocal();
    code.astore(thrown);
    handler.callFinally();
    code.aload(thrown);
    code.athrow();
    handler.endCatches();
    _handler = null;

    handler.startFinally();
    int returnto = code.addLocal();
    code.astore(returnto);
    code.aload(lock);
    code.monitorexit();
    code.ret(returnto);
    handler.endFinally();

    handler.end();
    code.endLocal(lock);
  }


  public boolean callFinalizer()
  {
    _handler.callFinally();
    return false;
  }
}
TOP

Related Classes of anvil.script.statements.SynchronizedStatement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.