|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--autohit.vm.VM
The abstract base class for virtual machines. A derived class will implement the abstract execute() method to actually run the VM. Also, it would normally use the pause() and resume() methods to control timing (rather than overloading and re-implementing them).
The derived class MAY overload the method prepare() if it has anything it wants to do before the FIRST instruction (and only the first) is executed. For instance, it could add environment variables.
The pause() and resume() methods are not designed to be called by external threads. If you plan to wrap the derived vm in a threaded class, you may want to overload or just not use those methods outside of the vm. Also, these methods only manage state and timing; it is up the he execute() method in the derived class to actually stop execution.
This base class offers the following services:
- Instruction pointer (ip) field
- Variable space allocation (vars). The variables are put into a hashtable.
some convenience methods are provided.
- Scope Stack space and convenience methods.
- Scope stack dirty flag.
USE THESE SERVICES! Do not make your own ip, for instance! Methods of this class depend upon it.
Field Summary | |
int |
ip
Current instruction address/pointer. |
protected boolean |
scDirty
Scope stack cache dirty flag. |
protected java.util.Stack |
scope
Scope stack. |
protected int |
state
VM state. |
static int |
STATE_DONE
|
static int |
STATE_NEW
State values for the VM. |
static int |
STATE_NO_VM
|
static int |
STATE_PAUSED
|
static int |
STATE_RUNNING
|
static int |
TIME_GRAN
Granulatiry for each tick of the VM's clock. |
protected java.util.Hashtable |
vars
Variable space. |
Constructor Summary | |
VM()
Constructor. |
Method Summary | |
void |
discardScopeFrame()
Discard scope frame. |
abstract void |
execute()
Absract method for VM execution. |
int |
getIntegerVar(java.lang.String name)
Get an Integer variable. |
int |
getState()
Get VM state. |
java.lang.String |
getVar(java.lang.String name)
Get a string variable. |
void |
pause()
Pause execution in the VM. |
java.lang.Object |
popScope()
Pop an object off the stack. |
void |
prepare()
Prepare for execution of the first instruction. |
void |
pushScope(java.lang.Object o)
Push an object onto the scope stack. |
void |
removeVar(java.lang.String name)
Remove a variable. |
void |
resume()
Resume execution in the VM. |
void |
setVar(java.lang.String name,
java.lang.String value)
Set a variable. |
void |
start()
Start the VM. |
java.lang.String |
subVar(java.lang.String in)
Variable substitution. |
int |
ticks()
Number of ticks the VM has been running. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
public static final int TIME_GRAN
public static final int STATE_NEW
public static final int STATE_RUNNING
public static final int STATE_PAUSED
public static final int STATE_DONE
public static final int STATE_NO_VM
protected int state
public int ip
protected java.util.Hashtable vars
protected java.util.Stack scope
protected boolean scDirty
Constructor Detail |
public VM()
Method Detail |
public void start() throws VMException
Calling this method consecutively will effectively reset the state and timing info. It is probibly a REAL BAD IDEA to call this from the execute method.
It throws any exceptions that are thrown out of execute().
public int getState()
You may call this from another thread, but it isn't very reliable.
public void pause()
It will only pause if the VM is running.
public void resume()
It will only resume if the VM is paused.
public int ticks()
Note that it returns an int rather than a long like system time usually is. This means that the VM timing. This technically could cause some overflow problems, but I doubt a VM would ever run that long.
public void setVar(java.lang.String name, java.lang.String value)
name
- the variable name.value
- the variable value.public void removeVar(java.lang.String name)
name
- the variable name.public java.lang.String getVar(java.lang.String name) throws VMException
name
- the variable name.VMException
public int getIntegerVar(java.lang.String name) throws VMException
name
- the variable name.VMException
public java.lang.String subVar(java.lang.String in) throws VMException
There won't be any errors if a substitution isn't found. It will only do one level of substitution. Either call this again to resolve a variable in the newly substituted text, or just assume it is plain text.
It will throw a VMException if the variable isn't set.
in
- text to find substitution.VMException
public void pushScope(java.lang.Object o)
i
- the objectpublic java.lang.Object popScope() throws java.lang.Exception
public void discardScopeFrame()
If it encounters any variable references, the variable will discarded.
It will pop the whole damned stack if it doesn't find one...
public abstract void execute() throws VMException
The implimentation of this method should only execute ONE INSTRUCTION. Successive calls would then execute the entire program. If you do not impliment it this way, you are likely to ghost the vm's.
NOTE! An implementing method MUST throw a VMException(VMException.DONE) when it reaches the end of execution.
If the derived-class VM encounters an instruction that it does now support, it should throw a VMException.INVALID_INSTRUCTION.
VMException
public void prepare() throws java.lang.Exception
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |