Package anvil.script.expression

Source Code of anvil.script.expression.EscapedVariableNode

/*
* $Id: EscapedVariableNode.java,v 1.13 2002/09/16 08:05:04 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.expression;

import anvil.core.Any;
import anvil.ErrorListener;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.StackFrame;
import anvil.script.statements.FunctionStatement;
import anvil.script.statements.LocalVariableStatement;
import java.io.IOException;

/**
* class EscapedVariableNode
*
* @author: Jani Lehtim�ki
*/
public class EscapedVariableNode extends Node
{

  private FunctionStatement _context;
  private LocalVariableStatement _local;
  private int _index = 0;
  private int _depth = 0;

  public EscapedVariableNode(String variable, LocalVariableStatement local, FunctionStatement context)
  {
    super();
    _local = local;
    _index = local.getSlot();
    _context = context;
    local.markEscaped();
    FunctionStatement function = context;
    while(function != null && function != local.getParent()) {
      _depth++;
      function = function.getContext();
      if (function != null) {
        function.markEscaped();
      }
    }
  }
 
  public LocalVariableStatement getVariable()
  {
    return _local;
  }

 
  public int getDepth()
  {
    return _depth;
  }
   
 
  public int typeOf()
  {
    return Node.EXPR_VARIABLE;
  }


  public boolean isConstant()
  {
    return false;
  }

 
  public Node optimize()
  {
    return this;
  }


  public void compile(ByteCompiler context, Node child)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    code.aload(_context.getFrameIndex());
    code.iconst(_depth);
    code.iconst(_index);
    child.compile(context, GET);
    code.invokevirtual(code.getPool().addMethodRef(context.TYPE_STACKFRAME,
      "setLocal", "(IILanvil/core/Any;)Lanvil/core/Any;"));
  }


  public void compile(ByteCompiler context, int operation)
  {
    Code code = context.getCode();
    ConstantPool pool = code.getPool();
    switch(operation) {
    case GET:
    case CHECK:
      code.aload(_context.getFrameIndex());
      code.iconst(_depth);
      code.iconst(_index);
      code.invokevirtual(code.getPool().addMethodRef(context.TYPE_STACKFRAME,
        "getLocal", "(II)Lanvil/core/Any;"));
      break;

    case GET_REF:
      int refclass = pool.addClass("anvil/core/AnyEscapedLocalRef");
      code.anew(refclass);
      code.dup();
      code.aload(_context.getFrameIndex());
      code.iconst(_depth);
      code.iconst(_index);
      code.invokespecial(pool.addMethodRef(refclass, "<init>", "(Lanvil/script/StackFrame;II)V"));
      break;

    case GET_BOOLEAN:
      code.aload(_context.getFrameIndex());
      code.iconst(_depth);
      code.iconst(_index);
      code.invokevirtual(code.getPool().addMethodRef(context.TYPE_STACKFRAME,
        "getLocal", "(II)Lanvil/core/Any;"));
      context.any2boolean();
      break;

    case DELETE:
      code.iconst(false);
      break;
    }
  }

}
TOP

Related Classes of anvil.script.expression.EscapedVariableNode

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.