package nxm.sys.lib;

import nxm.sys.inc.Constants;

/**
  Base class for intrinsics.  This is a container for methods that are specific
  to intrinsic commands. These are run-to-completion commands that generally do
  not process data. They execute in the current shell's thread.

  @author Jeff Schoen
*/
public class Intrinsic extends Command implements Constants {

  /** The next macro index for IF/LOOP/WHILE statements. */
  public int nextIndex;

  /** Who is handling messages (usually the Macro). */
  public Command messageHandler;

  /** default constructor */
  public Intrinsic () {}

  protected MessageQueue getQueue () {
    if (M.macro==null) {
      messageHandler = this;
      return M.queue;
    } else {
      messageHandler = M.macro;
      return M.macro.queue;
    }
  }

  public int process() {
    return (FINISH);
  }

  public void run() {
    int status;
    runEnter();
    // the two getInterrupt() checks added for BUG 2150
    if (!getInterrupt()) {
      try {
        state=OPEN; status=open();

        while (!getInterrupt() && (status != FINISH) && (status != ABORT)) {
          state=PROCESS; status=process();
          if (status==NOOP) Time.sleep(pollTime);
          else if (status==SLEEP) Time.sleep(0.1);
        }
        state=CLOSE; status=close();
      }
      catch (Exception e) { processException(e); }
    }
    runExit();

    // instrinsics do not checkout of a Macro, the Macro checkout() code does this
    if (M.macro==null) checkout();
  }

}
