Class AVM
- All Implemented Interfaces:
VariableManager,Closeable,AutoCloseable
- Direct Known Subclasses:
SandboxedAVM
It takes tuples constructed by the intermediate step and executes each tuple in accordance to their instruction semantics. The tuples correspond to the Awk script compiled by the parser. The interpreter consists of an instruction processor (interpreter), a runtime stack, and machinery to support the instruction set contained within the tuples.
The interpreter runs completely independent of the frontend/intermediate step.
In fact, an intermediate file produced by Jawk is sufficient to
execute on this interpreter. The binding data-structure is
the AwkSettings, which can contain options pertinent to
the interpreter. For example, the interpreter must know about
the -v command line argument values, as well as the file/variable list
parameter values (ARGC/ARGV) after the script on the command line.
However, if programmatic access to the AVM is required, meaningful
AwkSettings are not required.
Semantic analysis has occurred prior to execution of the interpreter.
Therefore, the interpreter throws AwkRuntimeExceptions upon most
errors/conditions. It can also throw a java.lang.Error if an
interpreter error is encountered.
AVM instances are reusable, but they are not thread-safe. Reuse the same
interpreter only sequentially, or create one AVM per concurrent execution.
When AVM is used directly, callers own its lifecycle and must
close() it when done.
- Author:
- Danny Daglas
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe value of an address which is not yet assigned a tuple index. -
Constructor Summary
ConstructorsConstructorDescriptionAVM()Construct the interpreter.AVM(AwkSettings parameters, Map<String, JawkExtension> extensionInstances) Construct the interpreter, accepting parameters which may have been set on the command-line arguments to the JVM.AVM(AwkSettings parameters, Map<String, JawkExtension> extensionInstances, boolean profilingEnabled) Construct the interpreter, optionally enabling runtime profiling. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidassignVariable(String name, Object obj) Set the contents of a user-defined AWK variable.voidClears the user-defined globals retained in the current runtime stack.voidclose()Releases any prepared input source and runtime I/O resources owned by this AVM.eval(AwkExpression expression) Evaluates a compiled expression against the AVM state exactly as it currently stands.eval(AwkExpression expression, InputSource inputSource) Evaluates a compiled expression against the supplied input source.eval(AwkExpression expression, InputSource inputSource, Map<String, Object> variableOverrides) Evaluates a compiled expression against the supplied input source with per-call variable overrides.voidexecute(AwkProgram program, InputSource inputSource) Executes a compiled AWK program with the current runtime defaults.voidexecute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments) Executes a compiled AWK program with explicit runtime arguments.voidexecute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments, Map<String, Object> variableOverrides) Executes a compiled AWK program with explicit runtime arguments and variable overrides.voidexecutePersistingGlobals(AwkProgram program, InputSource inputSource) Executes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.voidexecutePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments) Executes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.voidexecutePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments, Map<String, Object> variableOverrides) Executes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.getARGC()getARGC.getARGV()getARGV.Returns the default sink used by this runtime.getCONVFMT.getFS()getFS.getJrt()Returns the JRT (Jawk Runtime) instance associated with this interpreter.final ObjectgetOFS()getOFS.final ObjectgetORS()getORS.Returns an immutable snapshot of the collected profiling statistics.final ObjectgetRS()getRS.final ObjectgetSUBSEP.voidincFNR()Increases the FNR variable by 1.voidincNR()Increases the NR variable by 1.booleanprepareForEval(InputSource inputSource) Resets the interpreter to a fresh eval state and binds at most one record from the provided input source as the current input.booleanprepareForEval(String input) Resets the interpreter to a fresh eval state and binds one text record as the current input.voidresetFNR()Resets the FNR variable to 0.voidClears all collected profiling statistics.voidrestorePersistentMemory(Map<String, Object> snapshot) Restores the user-defined globals retained by this AVM from a previously captured persistent-memory snapshot.voidsetAwkSink(AwkSink sink) Sets the sink used by defaultprintandprintfoperations on this runtime.voidsetErrorStream(PrintStream errorStream) Sets the stream used for the stderr output of spawned processes (e.g.system("...")).voidsetFILENAME(String filename) Set the contents of the FILENAME variable.voidSet the contents of the NF variable.Captures the user-defined globals currently retained by this AVM for persistent execution.
-
Field Details
-
NULL_OFFSET
public static final int NULL_OFFSETThe value of an address which is not yet assigned a tuple index.- See Also:
-
-
Constructor Details
-
AVM
public AVM()Construct the interpreter.Provided to allow programmatic construction of the interpreter outside of the framework which is used by Jawk.
-
AVM
Construct the interpreter, accepting parameters which may have been set on the command-line arguments to the JVM.- Parameters:
parameters- The parameters affecting the behavior of the interpreter.extensionInstances- Map of the extensions to load
-
AVM
public AVM(AwkSettings parameters, Map<String, JawkExtension> extensionInstances, boolean profilingEnabled) Construct the interpreter, optionally enabling runtime profiling.- Parameters:
parameters- The parameters affecting the behavior of the interpreter.extensionInstances- Map of the extensions to loadprofilingEnabled- Whether to collect profiling statistics
-
-
Method Details
-
getJrt
Returns the JRT (Jawk Runtime) instance associated with this interpreter.- Returns:
- the JRT instance, never
null
-
setAwkSink
Sets the sink used by defaultprintandprintfoperations on this runtime.- Parameters:
sink- sink to use
-
setErrorStream
Sets the stream used for the stderr output of spawned processes (e.g.system("...")).- Parameters:
errorStream- stream to receive process stderr
-
getAwkSink
Returns the default sink used by this runtime.- Returns:
- the current AWK sink
-
eval
Evaluates a compiled expression against the AVM state exactly as it currently stands.- Parameters:
expression- compiled expression to evaluate- Returns:
- the resulting value
- Throws:
IOException- if evaluation fails
-
eval
Evaluates a compiled expression against the supplied input source.- Parameters:
expression- compiled expression to evaluateinputSource- input source providing the current record- Returns:
- the resulting value
- Throws:
IOException- if evaluation fails
-
eval
public Object eval(AwkExpression expression, InputSource inputSource, Map<String, Object> variableOverrides) throws IOExceptionEvaluates a compiled expression against the supplied input source with per-call variable overrides.- Parameters:
expression- compiled expression to evaluateinputSource- input source providing the current recordvariableOverrides- additional variable assignments applied on top of the settings-level variables (may benull)- Returns:
- the resulting value
- Throws:
IOException- if evaluation fails
-
execute
Executes a compiled AWK program with the current runtime defaults.- Parameters:
program- compiled program to executeinputSource- input source providing records- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
execute
public void execute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments) throws ExitException, IOException Executes a compiled AWK program with explicit runtime arguments.- Parameters:
program- compiled program to executeinputSource- input source providing recordsruntimeArguments- name=value or filename entries from the command line- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
execute
public void execute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments, Map<String, Object> variableOverrides) throws ExitException, IOExceptionExecutes a compiled AWK program with explicit runtime arguments and variable overrides.- Parameters:
program- compiled program to executeinputSource- input source providing recordsruntimeArguments- name=value or filename entries from the command linevariableOverrides- additional variable assignments applied on top of the settings-level variables (may benull)- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
executePersistingGlobals
public void executePersistingGlobals(AwkProgram program, InputSource inputSource) throws ExitException, IOException Executes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.Before the new program starts, this method imports any user-defined globals currently materialized in the AVM and remaps them onto the incoming program's compiled global slots.
- Parameters:
program- compiled program to executeinputSource- input source providing records- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
executePersistingGlobals
public void executePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments) throws ExitException, IOException Executes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.Before the new program starts, this method imports any user-defined globals currently materialized in the AVM and remaps them onto the incoming program's compiled global slots.
- Parameters:
program- compiled program to executeinputSource- input source providing recordsruntimeArguments- name=value or filename entries from the command line- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
executePersistingGlobals
public void executePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments, Map<String, Object> variableOverrides) throws ExitException, IOExceptionExecutes a compiled AWK program while persisting user-defined global variables across repeated executions on this AVM instance.Before the new program starts, this method imports any user-defined globals currently materialized in the AVM and remaps them onto the incoming program's compiled global slots.
- Parameters:
program- compiled program to executeinputSource- input source providing recordsruntimeArguments- name=value or filename entries from the command linevariableOverrides- additional variable assignments applied on top of the settings-level variables (may benull)- Throws:
ExitException- when the program terminates viaexitIOException- if execution fails
-
clearPersistentGlobals
public void clearPersistentGlobals()Clears the user-defined globals retained in the current runtime stack.The next
executePersistingGlobals(AwkProgram, InputSource, List, Map)call will therefore start from an empty persistent global bank. -
snapshotPersistentMemory
Captures the user-defined globals currently retained by this AVM for persistent execution.The returned snapshot is serializable and can later be fed back into
restorePersistentMemory(Map)on this or another AVM instance.- Returns:
- serializable snapshot of the persistent user-global bank
-
restorePersistentMemory
Restores the user-defined globals retained by this AVM from a previously captured persistent-memory snapshot.Restoring a snapshot replaces the current retained global bank. The next
executePersistingGlobals(AwkProgram, InputSource, List, Map)call will merge these globals into the compiled layout of the incoming program.- Parameters:
snapshot- snapshot to restore
-
prepareForEval
Resets the interpreter to a fresh eval state and binds one text record as the current input.- Parameters:
input- text record to expose as$0- Returns:
truewhen a record was prepared,falsewhen the provided text represents no input- Throws:
IOException- if binding the input fails
-
prepareForEval
Resets the interpreter to a fresh eval state and binds at most one record from the provided input source as the current input. Calling this method again on the same source advances to the next available record.- Parameters:
inputSource- source providing the record to bind- Returns:
truewhen a record was prepared,falsewhen the source is exhausted- Throws:
IOException- if reading the input fails
-
resetProfiling
public void resetProfiling()Clears all collected profiling statistics. -
getProfilingReport
Returns an immutable snapshot of the collected profiling statistics.- Returns:
- profiling report snapshot
-
close
Releases any prepared input source and runtime I/O resources owned by this AVM.Call this when you are done with an AVM obtained through expert-level integration, or after direct
eval(AwkExpression, InputSource)/execute(AwkProgram, InputSource)usage. The AVM may be prepared again afterwards, but callers should treat a closed instance as end-of-use unless they intentionally reinitialize it.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getRS
getRS.
- Specified by:
getRSin interfaceVariableManager- Returns:
- the contents of the RS variable.
-
getOFS
getOFS.
- Specified by:
getOFSin interfaceVariableManager- Returns:
- the contents of the OFS variable.
-
getORS
Description copied from interface:VariableManagergetORS.
- Specified by:
getORSin interfaceVariableManager- Returns:
- the contents of the ORS variable.
-
getSUBSEP
getSUBSEP.
- Specified by:
getSUBSEPin interfaceVariableManager- Returns:
- the contents of the SUBSEP variable.
-
assignVariable
Set the contents of a user-defined AWK variable. Used when processing name=value command-line arguments (either via -v or via ARGV).- Specified by:
assignVariablein interfaceVariableManager- Parameters:
name- The AWK variable name.obj- The new contents of the variable.
-
getFS
getFS.
- Specified by:
getFSin interfaceVariableManager- Returns:
- the contents of the FS variable.
-
getCONVFMT
getCONVFMT.
- Specified by:
getCONVFMTin interfaceVariableManager- Returns:
- the contents of the CONVFMT variable.
-
resetFNR
public void resetFNR()Resets the FNR variable to 0.- Specified by:
resetFNRin interfaceVariableManager
-
incFNR
public void incFNR()Increases the FNR variable by 1.- Specified by:
incFNRin interfaceVariableManager
-
incNR
public void incNR()Increases the NR variable by 1.- Specified by:
incNRin interfaceVariableManager
-
setNF
Set the contents of the NF variable.- Specified by:
setNFin interfaceVariableManager- Parameters:
newNf- Value for NF
-
setFILENAME
Set the contents of the FILENAME variable.- Specified by:
setFILENAMEin interfaceVariableManager- Parameters:
filename- File name
-
getARGV
getARGV.
- Specified by:
getARGVin interfaceVariableManager- Returns:
- the contents of the ARGV variable.
-
getARGC
getARGC.
- Specified by:
getARGCin interfaceVariableManager- Returns:
- the contents of the ARGC variable.
-