Context
Context is an internal state in Pnuts runtime environment, which is represented by pnuts.lang.Context class. A context has the following information:
- Stack frame (in AST interpreter only)
- Which Pnuts-package being used
- Imported Java-package list
- OutputStream to which print() write data
- OutputStream to which error() write message
- OutputStream to which the prompt and the results are written
- ClassLoader by which load() find a script
- A Implementation object
- Context-local variables
- Modules added by use()
- File list loaded by load()
- Configuration
See "Pnuts API Overview" for the overview of the class.
getContext() refers to the currently executing context, which is created when an interactive session starts. All other executions refer to the current context.
When context is specified to the parameter of these functions, a clone of the context is created and the script is executed with the clone.
When context is not specified, a clone of the current context is created and the script is executed with the clone. In this case, the import() state and the current package are reset to the default values.
OutputStream for Messages
For each context the following can be specified.
- Standard output (Writer / OutputStream)
- Error output (Writer)
- Where result of evaluation is printed (Writer)
context . getOutputStream()
context . setWriter (Writer aWriter )
context . getWriter()
context . setErrorWriter
(Writer writer)
context . getErrorWriter()
context . setTerminalWriter
(Writer writer)
context . getTerminalWriter()
Context-local Variables
Context-local variables are environment variables assiciated with a
context. Context-local variables are used typically when some kind of
state should be kept between function calls.
e.g.
function foo(){
getContext().foo = true
}
function bar(){
if (getContext().foo != null){
println("foo has been called")
}
}
For other attributes of Context, See Pnuts API Overview.