| java.lang.Object pnuts.lang.Context
All known Subclasses: pnuts.compiler.TranslateContext, pnuts.tools.CancelableContext, pnuts.compiler.CompileContext, pnuts.tools.DebugContext,
Context | public class Context implements Cloneable(Code) | | Context represents an internal state of a particular script
execution. A Context is created when start executing a script and passed
around during the execution.
A pnuts.lang.Context object contains the following information.
- Current Package (which Pnuts-package being used)
- Imported Java-package list
- Writer to which print() write data
- Writer to which error() write message
- ClassLoader
- Modules added with use() function.
- Units
- Environments (accessed by Context.get() and set())
- Stack frame (for the pure interpreter)
- Encoding
A clone is created when eval(), load(), or loadFile() is called in a script.
When a clone is created, (1) and (2) of the clone are reset to the default
value.
|
Method Summary | |
Object | _getId(String symbol) | void | addClassToImport(String className) | void | addPackageToImport(String pkgName) | void | addStaticMembers(String name, boolean wildcard) | public void | autoload(String name, String file) Registers an autoload script for the name. | public void | autoload(String name, AutoloadHook hook) Registers an AutoloadHook for the name in the current package. | final void | bind(String symbol, Object obj) | void | catchException(Class t, PnutsFunction f) | public synchronized void | clearPackages() | public Object | clone() | public Object | clone(boolean clear_attributes, boolean clear_locals) | protected void | close(Function func, Object args) | void | closeLocal() | public boolean | defined(String name) Checks if the name is defined in the context. | public Object | get(String symbol) Gets an environemnt variable associated with this context.
Parameters: symbol - the name of the variable, which must be intern'ed. | public ClassLoader | getClassLoader() Gets the current class loader. | public ClassLoader | getCodeLoader() Gets the current class loader for class geneartion
The initial value is null. | public Configuration | getConfiguration() | public Package | getCurrentPackage() | public PrintWriter | getErrorStream() | public PrintWriter | getErrorWriter() | public Executable | getExitHook() | public Object | getId(String interned) Gets the value of a symbol. | public Implementation | getImplementation() | public String | getName() | public OutputStream | getOutputStream() Get the standard output stream of the context, to which write() writes
data. | public PnutsImpl | getPnutsImpl() | public String | getScriptEncoding() | protected Object | getScriptSource() Get the source of the script. | public PrintWriter | getTerminalStream() | public PrintWriter | getTerminalWriter() | protected Object | getValue(String symbol) | public PrintWriter | getWriter() Get the standard writer of the context, to which print()/println() write
messages. | public boolean | isVerbose() | public Enumeration | keys() Returns an enumeration of the keys in the environment of this context. | long | lastModified(String file) | protected void | loadModule(String name, Package pkg) Loads a module is it has not been loaded yet.
The initialization script is: 1) Replace :: and . | synchronized ModuleList | localModuleList() | protected void | onError(Throwable t) This method is called when an exception is thrown. | protected void | onExit(Object arg) This method is called when the excecution is terminated normally. | protected void | open(Function f, Object args) | void | openLocal(String locals) | synchronized void | popFile() | void | provide(String file) | synchronized void | pushFile(Object file) | public void | registerQuantityFactory(String unit, QuantityFactory fac) Defines a unit. | void | require(String file, boolean checkForUpdate) Loads a script file only if it has not been loaded. | void | resetImportEnv() | void | resetStackFrame() | public Class | resolveClass(String symbol) | public Object | resolveSymbol(String interned) | void | revoke(String file) | public void | set(String symbol, Object value) Defines an environemnt variable associated with this context
To access those environment variables, Context.get(String) should be
called. | public void | setClassLoader(ClassLoader loader) Changes the current class loader for this context. | public void | setCodeLoader(ClassLoader loader) Sets the current class loader for class geneartion
Expressions that generate class create a classloader to load generated
classes based on the current class loader. | public void | setConfiguration(Configuration config) Changes the configuration for this context. | public void | setCurrentPackage(Package pkg) | public void | setErrorStream(Object errorStream, boolean autoFlush) Set an OutputStream or a Writer to which error() write messages If
errorStream is null, exception is thrown out of eval loop. | public void | setErrorStream(Object errorStream) | public void | setErrorWriter(Writer w, boolean autoFlush) | public void | setErrorWriter(Writer w) | public void | setExitHook(Executable hook) Sets a hook to be executed at the end of a script. | void | setFinallyFunction(PnutsFunction func) | public void | setImplementation(Implementation impl) Changes the Implementation object associated with this context
Parameters: impl - The Implementation object, which defines the implementation ofthe interpreter. | public void | setName(String name) | public void | setOutputStream(Object out, boolean autoFlush) | public void | setOutputStream(Object outputStream) | public void | setOutputStream(OutputStream out) Set the specified OutputStream as the standard output stream of the
context, to which write() writes data. | public void | setPnutsImpl(PnutsImpl impl) Changes the PnutsImpl object associated with this context
Parameters: impl - The PnutsImpl object, which defines the implementation of theinterpreter. | public void | setScriptEncoding(String encoding) | public void | setTerminalStream(Object str, boolean autoFlush) Set the terminal stream of the context, in which the prompt is shown. | public void | setTerminalStream(Object stream) | public void | setTerminalWriter(Writer w) | public void | setTerminalWriter(Writer w, boolean autoFlush) | protected void | setValue(String symbol, Object obj) | public void | setVerbose(boolean b) | public void | setWriter(Writer out) Set the specified Writer as the standard writer of the context.
PrintWriter is created from the specified Writer if the Writer is not an
instance of PrintWriter. | public void | setWriter(Writer out, boolean autoFlush) Set the specified Writer as the standard writer of the context.
PrintWriter is created from the specified Writer if the Writer is not an
instance of PrintWriter. | Object | undefined(String sym) | public synchronized boolean | unusePackage(Package pkg) | protected void | updateColumn(int column) | void | updateLine(SimpleNode node) | protected void | updateLine(SimpleNode node, int beginLine, int beginColumn) AST interpreter calls this method when line number changes, giving AST nodes
and line information
Not that compiler does not call this method. | protected void | updateLine(int line) Both AST interpreter and compiler call this method when line number changes.
Subclasses may override this method to interact with
running script. | public boolean | usePackage(Package pkg, boolean checkException) | public boolean | usePackage(String name) Add a package to the use()'d package list. | public boolean | usePackage(String name, boolean checkException) Add a package to the use()'d package list. | public String[] | usedPackages() |
beginColumn | protected int beginColumn(Code) | | |
beginLine | protected int beginLine(Code) | | |
defaultVerboseMode | static boolean defaultVerboseMode(Code) | | |
depth | protected int depth(Code) | | |
endLine | protected int endLine(Code) | | |
exceptionHandlerTableSymbol | static String exceptionHandlerTableSymbol(Code) | | |
finallyFunctionSymbol | static String finallyFunctionSymbol(Code) | | |
inGeneratorClosure | boolean inGeneratorClosure(Code) | | |
loadingResource | protected Cell loadingResource(Code) | | |
namespaceRefreshed | boolean namespaceRefreshed(Code) | | |
primitiveTypes | static Map primitiveTypes(Code) | | |
Context | public Context()(Code) | | Create a new context
|
Context | public Context(String pkg)(Code) | | Creates a context.
Parameters: pkg - the name of the package. |
Context | public Context(Package pkg)(Code) | | Creates a context.
Parameters: pkg - the initial package of the context. If null, the globalpackage is used. |
Context | public Context(Context context)(Code) | | Creates a context from a template
Parameters: context - The template since: 1.0beta9 |
addClassToImport | void addClassToImport(String className)(Code) | | |
addPackageToImport | void addPackageToImport(String pkgName)(Code) | | |
addStaticMembers | void addStaticMembers(String name, boolean wildcard)(Code) | | |
autoload | public void autoload(String name, String file)(Code) | | Registers an autoload script for the name. If name
is not defined when accessed, the registerred file is loaded.
Parameters: name - variable name Parameters: file - the file |
autoload | public void autoload(String name, AutoloadHook hook)(Code) | | Registers an AutoloadHook for the name in the current package.
Parameters: name - variable name Parameters: hook - the AutoloadHook |
clearPackages | public synchronized void clearPackages()(Code) | | Unregisteres all use()'d packages
|
clone | public Object clone()(Code) | | Make a clone of the context
|
clone | public Object clone(boolean clear_attributes, boolean clear_locals)(Code) | | Make a clone of the context
Parameters: clear_attributes - If true, import() state and current package are reset to thedefault values. Parameters: clear_locals - If true, local stack is reset. |
closeLocal | void closeLocal()(Code) | | |
defined | public boolean defined(String name)(Code) | | Checks if the name is defined in the context.
|
get | public Object get(String symbol)(Code) | | Gets an environemnt variable associated with this context.
Parameters: symbol - the name of the variable, which must be intern'ed. the value of the variable |
getClassLoader | public ClassLoader getClassLoader()(Code) | | Gets the current class loader.
The initial value is set to
Thread.currentThread().getContextClassLoader() when the instance is
created.
the class loader |
getCodeLoader | public ClassLoader getCodeLoader()(Code) | | Gets the current class loader for class geneartion
The initial value is null. Expressions that generate class
create a classloader to load generated classes based on the current
class loader.
the class loader |
getCurrentPackage | public Package getCurrentPackage()(Code) | | get the current package
|
getErrorStream | public PrintWriter getErrorStream()(Code) | | Get an OutputStream or a Writer to which error() write messages
|
getErrorWriter | public PrintWriter getErrorWriter()(Code) | | Get an PrintWriter to which error() write messages
|
getExitHook | public Executable getExitHook()(Code) | | Gets the hook to be executed at the end of a script
|
getId | public Object getId(String interned)(Code) | | Gets the value of a symbol.
Parameters: interned - a symbol (interned string) the value of the symbol exception: PnutsException - if the specified symbol is not defined |
getImplementation | public Implementation getImplementation()(Code) | | Gets the Implementation object associated with this context
|
getName | public String getName()(Code) | | Gets the name of the context
The name of the context. |
getOutputStream | public OutputStream getOutputStream()(Code) | | Get the standard output stream of the context, to which write() writes
data. This method returns the OutputStream previously set by
setOutputStream(). If setWriter() has been called, getOutputStream()
returns null.
the standard output stream of the context |
getPnutsImpl | public PnutsImpl getPnutsImpl()(Code) | | Gets the PnutsImpl object associated with this context
|
getScriptEncoding | public String getScriptEncoding()(Code) | | Gets the current script encoding
the current script encoding |
getScriptSource | protected Object getScriptSource()(Code) | | Get the source of the script.
java.net.URL object, when the script is not precompiledpnuts.lang.Runtime object, when the script is precompiled |
getTerminalStream | public PrintWriter getTerminalStream()(Code) | | get terminal-output-stream of the context
|
getTerminalWriter | public PrintWriter getTerminalWriter()(Code) | | get terminal-output-stream of the context
|
getWriter | public PrintWriter getWriter()(Code) | | Get the standard writer of the context, to which print()/println() write
messages.
the standard writer of the context |
isVerbose | public boolean isVerbose()(Code) | | Check the current verbose mode
the current verbose mode |
keys | public Enumeration keys()(Code) | | Returns an enumeration of the keys in the environment of this context.
|
loadModule | protected void loadModule(String name, Package pkg) throws IOException(Code) | | Loads a module is it has not been loaded yet.
The initialization script is: 1) Replace :: and . with / then append
"/init", e.g. pnuts.lib => pnuts/lib/init 2) The 1st line in
META-INF/pnuts/module/
Parameters: name - the name of the module Parameters: pkg - the associated package (name space) exception: FileNotFoundException - thrown when the initialization script is not found. |
onError | protected void onError(Throwable t)(Code) | | This method is called when an exception is thrown.
|
onExit | protected void onExit(Object arg)(Code) | | This method is called when the excecution is terminated normally.
|
popFile | synchronized void popFile()(Code) | | |
registerQuantityFactory | public void registerQuantityFactory(String unit, QuantityFactory fac)(Code) | | Defines a unit.
Parameters: unit - The unit symbol Parameters: fac - A QuantityFactory object which defines what kind of object iscreated when a decimal number with this unit symbol isevaluated. |
require | void require(String file, boolean checkForUpdate) throws FileNotFoundException(Code) | | Loads a script file only if it has not been loaded. It is guaranteed that
the script runs at most once in this context.
Parameters: file - the script file, which must be an intern'ed String. |
resetImportEnv | void resetImportEnv()(Code) | | |
resetStackFrame | void resetStackFrame()(Code) | | |
resolveSymbol | public Object resolveSymbol(String interned)(Code) | | Resolves the value of a symbol in the following order:
(1) current package (2) builtin functions, primitive types, pnuts_version
(3) module exports (4) imported classes (5) parent packages
Parameters: interned - a symbol (interned string) the value of the symbol, or null if it is not defined. |
set | public void set(String symbol, Object value)(Code) | | Defines an environemnt variable associated with this context
To access those environment variables, Context.get(String) should be
called. Note that those variables can not be accessed just by specifying
their names in Pnuts interpreter.
Since the environment varariables are bound to the executing context,
they are accessible from various modules that the script uses. Therefore,
the name of environment variables should have prefixes so that name
conflict is unlikely to occur. The name that starts with "pnuts." is
reserved.
Parameters: symbol - the name of the variable, which must be intern'ed. Parameters: value - the value of the variable since: 1.0beta8 |
setClassLoader | public void setClassLoader(ClassLoader loader)(Code) | | Changes the current class loader for this context.
The initial value is set to
Thread.currentThread().getContextClassLoader() when the instance is
created.
Parameters: loader - the class loader |
setCodeLoader | public void setCodeLoader(ClassLoader loader)(Code) | | Sets the current class loader for class geneartion
Expressions that generate class create a classloader to load generated
classes based on the current class loader.
Parameters: loader - the class loader |
setConfiguration | public void setConfiguration(Configuration config)(Code) | | Changes the configuration for this context.
Parameters: config - the configuration |
setCurrentPackage | public void setCurrentPackage(Package pkg)(Code) | | set the current package
|
setErrorStream | public void setErrorStream(Object errorStream, boolean autoFlush)(Code) | | Set an OutputStream or a Writer to which error() write messages If
errorStream is null, exception is thrown out of eval loop.
|
setErrorStream | public void setErrorStream(Object errorStream)(Code) | | Set ar PrintWriter to which error() write messages
|
setErrorWriter | public void setErrorWriter(Writer w, boolean autoFlush)(Code) | | |
setExitHook | public void setExitHook(Executable hook)(Code) | | Sets a hook to be executed at the end of a script. The default value is
null.
Parameters: hook - the hook |
setImplementation | public void setImplementation(Implementation impl)(Code) | | Changes the Implementation object associated with this context
Parameters: impl - The Implementation object, which defines the implementation ofthe interpreter. eval(), load(), and loadFile() ofpnuts.lang.Pnuts select an implementation (pure interpreter,on-the-fly compiler, etc.), according to the context passed tothe methods. |
setName | public void setName(String name)(Code) | | Sets the name of the context
Parameters: name - The name of the context. |
setOutputStream | public void setOutputStream(Object out, boolean autoFlush)(Code) | | set output stream of the context
|
setOutputStream | public void setOutputStream(Object outputStream)(Code) | | set output stream of the context
|
setOutputStream | public void setOutputStream(OutputStream out)(Code) | | Set the specified OutputStream as the standard output stream of the
context, to which write() writes data. A PrintWriter is created from the
specified OutputStream, which is returned by getWriter(). If null is
specified, both getOutputStream() and getWriter() return null.
Parameters: out - the OutputStream |
setPnutsImpl | public void setPnutsImpl(PnutsImpl impl)(Code) | | Changes the PnutsImpl object associated with this context
Parameters: impl - The PnutsImpl object, which defines the implementation of theinterpreter. eval(), load(), and loadFile() ofpnuts.lang.Pnuts select an implementation (pure interpreter,on-the-fly compiler, etc.), according to the context passed tothe methods. |
setScriptEncoding | public void setScriptEncoding(String encoding)(Code) | | Changes the script encoding for the context
Parameters: encoding - the encoding |
setTerminalStream | public void setTerminalStream(Object str, boolean autoFlush)(Code) | | Set the terminal stream of the context, in which the prompt is shown.
|
setTerminalStream | public void setTerminalStream(Object stream)(Code) | | Set the terminal stream of the context
|
setTerminalWriter | public void setTerminalWriter(Writer w)(Code) | | Set the terminal writer of the context
Parameters: w - the Writer |
setTerminalWriter | public void setTerminalWriter(Writer w, boolean autoFlush)(Code) | | Set the terminal writer of the context
Parameters: w - the Writer |
setVerbose | public void setVerbose(boolean b)(Code) | | Sets the verbose mode
|
setWriter | public void setWriter(Writer out)(Code) | | Set the specified Writer as the standard writer of the context.
PrintWriter is created from the specified Writer if the Writer is not an
instance of PrintWriter. If this method has been called,
getOutputStream() returns null. If null is specifed to this method, both
getWriter() and getOutputStream() return null.
Parameters: out - the Writer |
setWriter | public void setWriter(Writer out, boolean autoFlush)(Code) | | Set the specified Writer as the standard writer of the context.
PrintWriter is created from the specified Writer if the Writer is not an
instance of PrintWriter. If this method has been called,
getOutputStream() returns null. If null is specifed to this method, both
getWriter() and getOutputStream() return null.
Parameters: out - the Writer Parameters: autoFlush - A boolean; if true, the PrintWriter.println() methods willflush the output buffer |
unusePackage | public synchronized boolean unusePackage(Package pkg)(Code) | | |
updateColumn | protected void updateColumn(int column)(Code) | | |
updateLine | protected void updateLine(SimpleNode node, int beginLine, int beginColumn)(Code) | | AST interpreter calls this method when line number changes, giving AST nodes
and line information
Not that compiler does not call this method.
Parameters: node - the current AST node Parameters: beginLine - the line number at which the current expression starts. Parameters: beginColumn - the column number at which theh current expression ends. |
updateLine | protected void updateLine(int line)(Code) | | Both AST interpreter and compiler call this method when line number changes.
Subclasses may override this method to interact with
running script. For example, a subclass may redefine this
method so that it can stop the execution if Thread.interrupt()
has been called.
Parameters: line - the line number |
usePackage | public boolean usePackage(Package pkg, boolean checkException)(Code) | | |
usePackage | public boolean usePackage(String name)(Code) | | Add a package to the use()'d package list.
Parameters: name - the package name true if successfully use()'d. |
usePackage | public boolean usePackage(String name, boolean checkException)(Code) | | Add a package to the use()'d package list.
Parameters: name - the package name Parameters: checkException - if false exceptions are ignored true if successfully use()'d. |
usedPackages | public String[] usedPackages()(Code) | | Returns the list of use()'d packages
|
|
|