Package gnu.mapping
Class CallContext
- java.lang.Object
-
- gnu.mapping.ArgListImpl
-
- gnu.mapping.CallContext
-
- All Implemented Interfaces:
ArgList
,ArgListBuilder
public class CallContext extends ArgListImpl implements ArgList, ArgListBuilder
A procedure activation stack (when compiled with explicit stacks).
-
-
Field Summary
Fields Modifier and Type Field Description MethodHandle
applyMethod
Consumer
consumer
Function results are written to this Consumer.Object[][]
evalFrames
Current stack of evaluation frames for interpreter.static int
MATCH_CHECK
Request to on failure return error code.static int
MATCH_CHECK_ONLY
Request to on failure return error code; on failure return 0.static int
MATCH_THROW_ON_EXCEPTION
Request to throw an exception on a match failure.int
next
Index of next argument.Procedure
proc
-
Fields inherited from class gnu.mapping.ArgListImpl
count
-
-
Constructor Summary
Constructors Constructor Description CallContext()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addArg(Object arg)
void
addArg(Object arg0, Object arg1, Object arg2, Object arg3)
int
checkDone()
void
checkKeywordsDone()
void
cleanupFromContext(int saved)
Cleanup-only part of getFromContext.Object
getArgAsObject(int i)
Index into arguments, not counting keywords.int
getArgCount()
Object[]
getArgs()
Object
getFromContext(int saved)
Routine to extract result and restore state after startFromContext.static CallContext
getInstance()
Get or create a CallContext for the current thread.int
getMode()
Object
getNextArg()
Get the next incoming argument.Object
getNextArg(Object defaultValue)
Get the next incoming argument.static CallContext
getOnlyInstance()
Get but don't create a CallContext for the current thread.Object[]
getRestArgsArray()
Object[]
getRestArgsArray(int next)
Get remaining arguments as an array.LList
getRestArgsList()
LList
getRestArgsList(int next)
Get remaining arguments as a list.ArgListVector
getRestArgsVector()
ArgListVector
getRestArgsVector(int next)
Get remaining arguments are an ArgListVector.Object[]
getRestPlainArray()
LList
getRestPlainList(int next)
static short[]
getSortedKeywords(String[] keywords, int nkeys)
boolean
haveArg()
void
lastArg()
Note that we are done with the input arguments.void
matchError(int code)
int
nextKeyword(String keyword)
Return index of matching keyword argument.Object
nextKeyword(String keyword, Object dfault)
int
nextKeywordAllowOthers(String keyword)
Object
nextKeywordAllowOthers(String keyword, Object dfault)
int
nextKeywordIndex(String keyword, boolean allowOthers)
LList
peekRestArgsList()
void
popArgState()
void
pushArgState()
void
reset()
void
rewind()
void
rewind(int mode)
void
runUntilDone()
Object
runUntilValue()
Run until no more continuations, returning final result.void
runUntilValue(Consumer out)
Run until no more continuations, sending result to a COnsumer.static void
setInstance(CallContext ctx)
void
setNextProcedure(Procedure proc)
void
setNextProcedure(Procedure proc, MethodHandle apply)
void
setupApply(Procedure proc)
void
setupApply(Procedure proc, Object arg0)
void
setupApply(Procedure proc, Object arg0, Object arg1)
void
setupApply(Procedure proc, Object arg0, Object arg1, Object arg2)
void
setupApply(Procedure proc, Object arg0, Object arg1, Object arg2, Object arg3)
void
setupApplyAll(Procedure proc, Object[] args)
void
setupApplyAll(Procedure proc, Object[] args, int fromIndex, int toIndex)
void
shiftArgs(Procedure proc, int toDrop)
int
startFromContext()
Setup routine before calling a method that takes a CallContext.boolean
throwOnException()
void
writeValue(Object value)
Write values (of function result) to current consumer.-
Methods inherited from class gnu.mapping.ArgListImpl
add, add, addAll, addAll, addArgList, addKey, addSequence, asArgList, asFreshArgList, clear, findKeyword, findKeyword, firstKeyword, getKeyword, numArguments, numKeywords, popLast, printArgs, setArgs, setArgs, setArgs, setArgs, setArgs, setArgsAll, setArgsAll, setKeys, shiftArgs
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gnu.mapping.ArgList
findKeyword, firstKeyword, getKeyword, numArguments, numKeywords
-
-
-
-
Field Detail
-
applyMethod
public MethodHandle applyMethod
-
proc
public Procedure proc
-
consumer
public Consumer consumer
Function results are written to this Consumer. This may point to vstack - or some other Consumer.
-
next
public int next
Index of next argument. This is used by methods like getNextArg, used by callees.
-
MATCH_THROW_ON_EXCEPTION
public static final int MATCH_THROW_ON_EXCEPTION
Request to throw an exception on a match failure. This is instead of setting matchState to an error value.- See Also:
- Constant Field Values
-
MATCH_CHECK
public static final int MATCH_CHECK
Request to on failure return error code. Error is indicated by check routine returning this CallContext. Otherwise, execute actual function and return result (or null).- See Also:
- Constant Field Values
-
MATCH_CHECK_ONLY
public static final int MATCH_CHECK_ONLY
Request to on failure return error code; on failure return 0. Regardless, don't invoke body method.- See Also:
- Constant Field Values
-
evalFrames
public Object[][] evalFrames
Current stack of evaluation frames for interpreter.
-
-
Method Detail
-
setInstance
public static void setInstance(CallContext ctx)
-
getOnlyInstance
public static CallContext getOnlyInstance()
Get but don't create a CallContext for the current thread.
-
getInstance
public static CallContext getInstance()
Get or create a CallContext for the current thread.
-
setNextProcedure
public final void setNextProcedure(Procedure proc, MethodHandle apply)
-
setNextProcedure
public final void setNextProcedure(Procedure proc)
-
getArgAsObject
public Object getArgAsObject(int i)
Description copied from interface:ArgList
Index into arguments, not counting keywords. I.e. for [a b k1: c k2: d e] the index 0 returns a, 2 returns c, 3 returns d, 4 returns e.- Specified by:
getArgAsObject
in interfaceArgList
- Overrides:
getArgAsObject
in classArgListImpl
-
rewind
public void rewind(int mode)
-
rewind
public void rewind()
-
reset
public void reset()
-
shiftArgs
public void shiftArgs(Procedure proc, int toDrop)
-
getMode
public int getMode()
-
matchError
public void matchError(int code)
-
throwOnException
public boolean throwOnException()
-
haveArg
public boolean haveArg()
-
checkKeywordsDone
public void checkKeywordsDone()
-
checkDone
public int checkDone()
-
getArgCount
public int getArgCount()
-
getNextArg
public Object getNextArg()
Get the next incoming argument.
-
getNextArg
public Object getNextArg(Object defaultValue)
Get the next incoming argument. Return defaultValue if there are no more arguments.
-
getRestPlainArray
public final Object[] getRestPlainArray()
-
getRestArgsArray
public final Object[] getRestArgsArray()
-
getRestArgsArray
public final Object[] getRestArgsArray(int next)
Get remaining arguments as an array.
-
peekRestArgsList
public final LList peekRestArgsList()
-
getRestArgsVector
public final ArgListVector getRestArgsVector()
-
getRestArgsList
public final LList getRestArgsList()
-
getRestArgsList
public final LList getRestArgsList(int next)
Get remaining arguments as a list. Used for Scheme and Lisp rest args.
-
getRestPlainList
public final LList getRestPlainList(int next)
-
getRestArgsVector
public ArgListVector getRestArgsVector(int next)
Get remaining arguments are an ArgListVector.- Parameters:
next
- The number of arguments that should be skipped. Assume either no keywords have been processed, or they all have.
-
lastArg
public void lastArg()
Note that we are done with the input arguments. Throw WrongArguments if there are unprocessed arguments.
-
getArgs
public Object[] getArgs()
-
getSortedKeywords
public static short[] getSortedKeywords(String[] keywords, int nkeys)
-
nextKeyword
public int nextKeyword(String keyword)
Return index of matching keyword argument. Must be called with keywords in increasing lexicographic order.- Returns:
dfault
if no matching keyword argument, or the corresponding keyword value.
-
nextKeywordAllowOthers
public int nextKeywordAllowOthers(String keyword)
-
nextKeywordIndex
public int nextKeywordIndex(String keyword, boolean allowOthers)
-
setupApply
public void setupApply(Procedure proc)
-
setupApply
public void setupApply(Procedure proc, Object arg0, Object arg1, Object arg2, Object arg3)
-
addArg
public void addArg(Object arg)
-
startFromContext
public final int startFromContext()
Setup routine before calling a method that takes a CallContext. The compiler emits a call to this before a call to a method that takes a CallContext, when it wants the function result as an Object. It pushes the CallContest state so it can uses the vstack for a temporary, After the method, getFromContext extract the method's result from the vstack and restores the state.
-
getFromContext
public final Object getFromContext(int saved) throws Throwable
Routine to extract result and restore state after startFromContext.- Throws:
Throwable
-
cleanupFromContext
public final void cleanupFromContext(int saved)
Cleanup-only part of getFromContext. This can be in an exception handler as an alternative to getFromContext, which is called in the non-exception case. (Alternatively, the compiler could call cleanupFromContext from a finally clause but that is less efficient, partly because the JVM stack must be empty before a finally subroutine.)
-
runUntilValue
public final Object runUntilValue() throws Throwable
Run until no more continuations, returning final result. Assume that the current applyHandle is from proc.- Throws:
Throwable
-
runUntilValue
public final void runUntilValue(Consumer out) throws Throwable
Run until no more continuations, sending result to a COnsumer.- Throws:
Throwable
-
writeValue
public void writeValue(Object value)
Write values (of function result) to current consumer.
-
pushArgState
public final void pushArgState()
-
popArgState
public final void popArgState()
-
-