/*
* $Id: StackModule.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime.stack;
import anvil.core.Any;
import anvil.core.AnyTuple;
import anvil.core.AnyString;
import anvil.core.io.AnyOutputStream;
import anvil.core.runtime.AnyScope;
import anvil.core.runtime.AnyFunction;
import anvil.core.runtime.AnyStackTraceElement;
import anvil.Log;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.Module;
import anvil.script.StackFrame;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.TimeZone;
///
/// @module anvil.runtime.stack
/// Set of functions for investigating the contents of stack
///
public class StackModule
{
public static final anvil.core.RuntimePermission CAN_READ = new anvil.core.RuntimePermission("stack", false);
/// @function printStack�
/// Informative print from stack for debugging.
/// @synopsis void printStack()
public static final Any printStack(Context ctx)
{
anvil.script.StackFrame frame;
anvil.script.Module script;
anvil.script.Function function;
int line;
int n = ctx.size();
for(int i=n-1; i>=0; i--) {
frame = ctx.peek(i);
script = frame.getModule();
function = frame.getFunction();
ctx.print("[");
ctx.print(script.getPathinfo());
line = frame.getLine();
if (line != 0) {
ctx.print(": ");
ctx.print(Integer.toString(line));
}
ctx.print("] ");
ctx.print(script.getName());
ctx.print(".");
ctx.print(function.toString());
ctx.print("\n");
}
return Any.NULL;
}
/// @function getStack
/// Returns the full dump of stack trace as list of StackTraceElement:s.
/// @synopsis list getStack()
/// @return Dump of stack
public static final Any getStack(Context context)
{
context.checkAccess(CAN_READ);
return context.getStackTrace();
}
/// @function getFrameCount
/// Returns the number of frames in stack.
/// @synopsis int getFrameCount()
/// @return Number of frames
public static final Any getFrameCount(Context context)
{
context.checkAccess(CAN_READ);
return Any.create(context.size());
}
/// @function getPathinfo
/// Returns the pathinfo of given frame
/// @synopsis string getPathinfo() ; returns pathinfo of current frame
/// @synopsis string getPathinfo(int frame) ; returns pathinfo of given frame
/// @param frame Frame number, zero is oldest.
/// @return Pathinfo
public static final Object[] p_getPathinfo = { null, "*frame", new Integer(-1) };
public static final Any getPathinfo(Context context, int frame)
{
context.checkAccess(CAN_READ);
if (frame == -1) {
return Any.create(context.frame().getPathinfo());
} else {
if (frame >= 0 && frame < context.size()) {
return Any.create(context.peek(frame).getPathinfo());
}
}
return Any.UNDEFINED;
}
/// @function getLine
/// Returns the line number of given frame. Line numbers are
/// not guaranteed to be accurate.
/// @synopsis string getLine() ; returns line of current frame
/// @synopsis string getLine(int frame) ; returns line of given frame
/// @param frame Frame number, zero is oldest.
/// @return Line number
public static final Object[] p_getLine = { null, "*frame", new Integer(-1) };
public static final Any getLine(Context context, int frame)
{
context.checkAccess(CAN_READ);
if (frame == -1) {
return Any.create(context.frame().getLine());
} else {
if (frame >= 0 && frame < context.size()) {
return Any.create(context.peek(frame).getLine());
}
}
return Any.UNDEFINED;
}
/// @function getModule
/// Returns the script of given frame
/// @synopsis module getModule() ; returns module of current frame
/// @synopsis module getModule(int frame) ; returns module of given frame
/// @param frame Frame number, zero is oldest.
/// @return Instance of module
public static final Object[] p_getModule = { null, "*frame", new Integer(-1) };
public static final Any getModule(Context context, int frame)
{
context.checkAccess(CAN_READ);
Module script = null;
if (frame == -1) {
script = context.script();
} else {
if (frame >= 0 && frame < context.size()) {
script = context.peek(frame).getModule();
} else {
return Any.UNDEFINED;
}
}
context.checkImport(script.getPathinfo());
return new AnyScope(script);
}
/// @function getThis
/// Returns the 'this' of given frame
/// @synopsis class getClass() ; returns 'this' of current frame
/// @synopsis class getClass(int frame) ; returns 'this' of given frame
/// @param frame Frame number, zero is oldest.
/// @return Instance of class
public static final Object[] p_getThis = { null, "*frame", new Integer(-1) };
public static final Any getThis(Context context, int frame)
{
context.checkAccess(CAN_READ);
if (frame == -1) {
return Any.create(context.frame().getSelf());
} else {
if (frame >= 0 && frame < context.size()) {
return Any.create(context.peek(frame).getSelf());
}
}
return Any.UNDEFINED;
}
/// @function getFunction
/// Returns the function of given frame
/// @synopsis Function getFunction() ; returns function of current frame
/// @synopsis Function getFunction(int frame) ; returns function of given frame
/// @param frame Frame number, zero is oldest.
/// @return Instance of function
public static final Object[] p_getFunction = { null, "*frame", new Integer(-1) };
public static final Any getFunction(Context context, int frame)
{
context.checkAccess(CAN_READ);
if (frame == -1) {
return new AnyFunction(context.frame().getFunction());
} else {
if (frame >= 0 && frame < context.size()) {
return new AnyFunction(context.peek(frame).getFunction());
}
}
return Any.UNDEFINED;
}
/// @function getFrame
/// Returns the contents of of given frame
/// @synopsis StackFrameElement getFrame() ; returns contents of current frame
/// @synopsis StackFrameElement getFrame(int frame) ; returns contents of given frame
/// @param frame Frame number, zero is oldest.
public static final Object[] p_getFrame = { null, "*frame", new Integer(-1) };
public static final Any getFrame(Context context, int frame)
{
context.checkAccess(CAN_READ);
StackFrame stackframe = null;
if (frame == -1) {
stackframe = context.frame();
} else {
if (frame >= 0 && frame < context.size()) {
stackframe = context.peek(frame);
}
}
if (stackframe != null) {
return new AnyStackTraceElement(stackframe);
} else {
return Any.UNDEFINED;
}
}
/// @function getZone
/// Returns the zone of of given frame
/// @synopsis list getZone() ; returns zone of current frame
/// @synopsis list getZone(int frame) ; returns zone of given frame
/// @param frame Frame number, zero is oldest.
/// @return Zone
public static final Object[] p_getZone = { null, "*frame", new Integer(-1) };
public static final Any getZone(Context context, int frame)
{
context.checkAccess(CAN_READ);
context.checkAccess(anvil.core.system.AnyConfigurable.CAN_READ);
StackFrame stackframe = null;
if (frame == -1) {
return new anvil.core.system.AnyConfigurable(
context.frame().getModule().getAddress().getZone());
} else {
if (frame >= 0 && frame < context.size()) {
return new anvil.core.system.AnyConfigurable(
context.peek(frame).getModule().getAddress().getZone());
}
}
return Any.UNDEFINED;
}
public static final anvil.script.compiler.NativeNamespace __module__ =
new anvil.script.compiler.NativeNamespace(
"stack",
StackModule.class,
new Class[] {
},
//DOC{{
""+
"\n" +
" @module anvil.runtime.stack\n" +
" Set of functions for investigating the contents of stack\n" +
"\n" +
" @function printStack�\n" +
" Informative print from stack for debugging.\n" +
" @synopsis void printStack()\n" +
" @function getStack\n" +
" Returns the full dump of stack trace as list of StackTraceElement:s.\n" +
" @synopsis list getStack()\n" +
" @return Dump of stack\n" +
" @function getFrameCount\n" +
" Returns the number of frames in stack.\n" +
" @synopsis int getFrameCount()\n" +
" @return Number of frames\n" +
" @function getPathinfo\n" +
" Returns the pathinfo of given frame\n" +
" @synopsis string getPathinfo() ; returns pathinfo of current frame\n" +
" @synopsis string getPathinfo(int frame) ; returns pathinfo of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Pathinfo\n" +
" @function getLine\n" +
" Returns the line number of given frame. Line numbers are\n" +
" not guaranteed to be accurate.\n" +
" @synopsis string getLine() ; returns line of current frame\n" +
" @synopsis string getLine(int frame) ; returns line of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Line number\n" +
" @function getModule\n" +
" Returns the script of given frame\n" +
" @synopsis module getModule() ; returns module of current frame\n" +
" @synopsis module getModule(int frame) ; returns module of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Instance of module\n" +
" @function getThis\n" +
" Returns the 'this' of given frame\n" +
" @synopsis class getClass() ; returns 'this' of current frame\n" +
" @synopsis class getClass(int frame) ; returns 'this' of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Instance of class\n" +
" @function getFunction\n" +
" Returns the function of given frame\n" +
" @synopsis Function getFunction() ; returns function of current frame\n" +
" @synopsis Function getFunction(int frame) ; returns function of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Instance of function\n" +
" @function getFrame\n" +
" Returns the contents of of given frame\n" +
" @synopsis StackFrameElement getFrame() ; returns contents of current frame\n" +
" @synopsis StackFrameElement getFrame(int frame) ; returns contents of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @function getZone\n" +
" Returns the zone of of given frame\n" +
" @synopsis list getZone() ; returns zone of current frame\n" +
" @synopsis list getZone(int frame) ; returns zone of given frame\n" +
" @param frame Frame number, zero is oldest.\n" +
" @return Zone\n"
//}}DOC
);
}