001: /*
002: * $Id: StackModule.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.runtime.stack;
011:
012: import anvil.core.Any;
013: import anvil.core.AnyTuple;
014: import anvil.core.AnyString;
015: import anvil.core.io.AnyOutputStream;
016: import anvil.core.runtime.AnyScope;
017: import anvil.core.runtime.AnyFunction;
018: import anvil.core.runtime.AnyStackTraceElement;
019: import anvil.Log;
020: import anvil.script.Context;
021: import anvil.script.Function;
022: import anvil.script.Module;
023: import anvil.script.StackFrame;
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.util.Locale;
028: import java.util.TimeZone;
029:
030: ///
031: /// @module anvil.runtime.stack
032: /// Set of functions for investigating the contents of stack
033: ///
034: public class StackModule {
035:
036: public static final anvil.core.RuntimePermission CAN_READ = new anvil.core.RuntimePermission(
037: "stack", false);
038:
039: /// @function printStackö
040: /// Informative print from stack for debugging.
041: /// @synopsis void printStack()
042: public static final Any printStack(Context ctx) {
043: anvil.script.StackFrame frame;
044: anvil.script.Module script;
045: anvil.script.Function function;
046: int line;
047: int n = ctx.size();
048: for (int i = n - 1; i >= 0; i--) {
049: frame = ctx.peek(i);
050: script = frame.getModule();
051: function = frame.getFunction();
052: ctx.print("[");
053: ctx.print(script.getPathinfo());
054: line = frame.getLine();
055: if (line != 0) {
056: ctx.print(": ");
057: ctx.print(Integer.toString(line));
058: }
059: ctx.print("] ");
060: ctx.print(script.getName());
061: ctx.print(".");
062: ctx.print(function.toString());
063: ctx.print("\n");
064: }
065: return Any.NULL;
066: }
067:
068: /// @function getStack
069: /// Returns the full dump of stack trace as list of StackTraceElement:s.
070: /// @synopsis list getStack()
071: /// @return Dump of stack
072: public static final Any getStack(Context context) {
073: context.checkAccess(CAN_READ);
074: return context.getStackTrace();
075: }
076:
077: /// @function getFrameCount
078: /// Returns the number of frames in stack.
079: /// @synopsis int getFrameCount()
080: /// @return Number of frames
081: public static final Any getFrameCount(Context context) {
082: context.checkAccess(CAN_READ);
083: return Any.create(context.size());
084: }
085:
086: /// @function getPathinfo
087: /// Returns the pathinfo of given frame
088: /// @synopsis string getPathinfo() ; returns pathinfo of current frame
089: /// @synopsis string getPathinfo(int frame) ; returns pathinfo of given frame
090: /// @param frame Frame number, zero is oldest.
091: /// @return Pathinfo
092: public static final Object[] p_getPathinfo = { null, "*frame",
093: new Integer(-1) };
094:
095: public static final Any getPathinfo(Context context, int frame) {
096: context.checkAccess(CAN_READ);
097: if (frame == -1) {
098: return Any.create(context.frame().getPathinfo());
099: } else {
100: if (frame >= 0 && frame < context.size()) {
101: return Any.create(context.peek(frame).getPathinfo());
102: }
103: }
104: return Any.UNDEFINED;
105: }
106:
107: /// @function getLine
108: /// Returns the line number of given frame. Line numbers are
109: /// not guaranteed to be accurate.
110: /// @synopsis string getLine() ; returns line of current frame
111: /// @synopsis string getLine(int frame) ; returns line of given frame
112: /// @param frame Frame number, zero is oldest.
113: /// @return Line number
114: public static final Object[] p_getLine = { null, "*frame",
115: new Integer(-1) };
116:
117: public static final Any getLine(Context context, int frame) {
118: context.checkAccess(CAN_READ);
119: if (frame == -1) {
120: return Any.create(context.frame().getLine());
121: } else {
122: if (frame >= 0 && frame < context.size()) {
123: return Any.create(context.peek(frame).getLine());
124: }
125: }
126: return Any.UNDEFINED;
127: }
128:
129: /// @function getModule
130: /// Returns the script of given frame
131: /// @synopsis module getModule() ; returns module of current frame
132: /// @synopsis module getModule(int frame) ; returns module of given frame
133: /// @param frame Frame number, zero is oldest.
134: /// @return Instance of module
135: public static final Object[] p_getModule = { null, "*frame",
136: new Integer(-1) };
137:
138: public static final Any getModule(Context context, int frame) {
139: context.checkAccess(CAN_READ);
140: Module script = null;
141: if (frame == -1) {
142: script = context.script();
143: } else {
144: if (frame >= 0 && frame < context.size()) {
145: script = context.peek(frame).getModule();
146: } else {
147: return Any.UNDEFINED;
148: }
149: }
150: context.checkImport(script.getPathinfo());
151: return new AnyScope(script);
152: }
153:
154: /// @function getThis
155: /// Returns the 'this' of given frame
156: /// @synopsis class getClass() ; returns 'this' of current frame
157: /// @synopsis class getClass(int frame) ; returns 'this' of given frame
158: /// @param frame Frame number, zero is oldest.
159: /// @return Instance of class
160: public static final Object[] p_getThis = { null, "*frame",
161: new Integer(-1) };
162:
163: public static final Any getThis(Context context, int frame) {
164: context.checkAccess(CAN_READ);
165: if (frame == -1) {
166: return Any.create(context.frame().getSelf());
167: } else {
168: if (frame >= 0 && frame < context.size()) {
169: return Any.create(context.peek(frame).getSelf());
170: }
171: }
172: return Any.UNDEFINED;
173: }
174:
175: /// @function getFunction
176: /// Returns the function of given frame
177: /// @synopsis Function getFunction() ; returns function of current frame
178: /// @synopsis Function getFunction(int frame) ; returns function of given frame
179: /// @param frame Frame number, zero is oldest.
180: /// @return Instance of function
181: public static final Object[] p_getFunction = { null, "*frame",
182: new Integer(-1) };
183:
184: public static final Any getFunction(Context context, int frame) {
185: context.checkAccess(CAN_READ);
186: if (frame == -1) {
187: return new AnyFunction(context.frame().getFunction());
188: } else {
189: if (frame >= 0 && frame < context.size()) {
190: return new AnyFunction(context.peek(frame)
191: .getFunction());
192: }
193: }
194: return Any.UNDEFINED;
195: }
196:
197: /// @function getFrame
198: /// Returns the contents of of given frame
199: /// @synopsis StackFrameElement getFrame() ; returns contents of current frame
200: /// @synopsis StackFrameElement getFrame(int frame) ; returns contents of given frame
201: /// @param frame Frame number, zero is oldest.
202: public static final Object[] p_getFrame = { null, "*frame",
203: new Integer(-1) };
204:
205: public static final Any getFrame(Context context, int frame) {
206: context.checkAccess(CAN_READ);
207: StackFrame stackframe = null;
208: if (frame == -1) {
209: stackframe = context.frame();
210: } else {
211: if (frame >= 0 && frame < context.size()) {
212: stackframe = context.peek(frame);
213: }
214: }
215: if (stackframe != null) {
216: return new AnyStackTraceElement(stackframe);
217: } else {
218: return Any.UNDEFINED;
219: }
220: }
221:
222: /// @function getZone
223: /// Returns the zone of of given frame
224: /// @synopsis list getZone() ; returns zone of current frame
225: /// @synopsis list getZone(int frame) ; returns zone of given frame
226: /// @param frame Frame number, zero is oldest.
227: /// @return Zone
228: public static final Object[] p_getZone = { null, "*frame",
229: new Integer(-1) };
230:
231: public static final Any getZone(Context context, int frame) {
232: context.checkAccess(CAN_READ);
233: context.checkAccess(anvil.core.system.AnyConfigurable.CAN_READ);
234: StackFrame stackframe = null;
235: if (frame == -1) {
236: return new anvil.core.system.AnyConfigurable(context
237: .frame().getModule().getAddress().getZone());
238: } else {
239: if (frame >= 0 && frame < context.size()) {
240: return new anvil.core.system.AnyConfigurable(context
241: .peek(frame).getModule().getAddress().getZone());
242: }
243: }
244: return Any.UNDEFINED;
245: }
246:
247: public static final anvil.script.compiler.NativeNamespace __module__ = new anvil.script.compiler.NativeNamespace(
248: "stack", StackModule.class,
249: new Class[] {},
250: //DOC{{
251: ""
252: + "\n"
253: + " @module anvil.runtime.stack\n"
254: + " Set of functions for investigating the contents of stack\n"
255: + "\n"
256: + " @function printStackö\n"
257: + " Informative print from stack for debugging.\n"
258: + " @synopsis void printStack()\n"
259: + " @function getStack\n"
260: + " Returns the full dump of stack trace as list of StackTraceElement:s.\n"
261: + " @synopsis list getStack()\n"
262: + " @return Dump of stack\n"
263: + " @function getFrameCount\n"
264: + " Returns the number of frames in stack.\n"
265: + " @synopsis int getFrameCount()\n"
266: + " @return Number of frames\n"
267: + " @function getPathinfo\n"
268: + " Returns the pathinfo of given frame\n"
269: + " @synopsis string getPathinfo() ; returns pathinfo of current frame\n"
270: + " @synopsis string getPathinfo(int frame) ; returns pathinfo of given frame\n"
271: + " @param frame Frame number, zero is oldest.\n"
272: + " @return Pathinfo\n"
273: + " @function getLine\n"
274: + " Returns the line number of given frame. Line numbers are\n"
275: + " not guaranteed to be accurate.\n"
276: + " @synopsis string getLine() ; returns line of current frame\n"
277: + " @synopsis string getLine(int frame) ; returns line of given frame\n"
278: + " @param frame Frame number, zero is oldest.\n"
279: + " @return Line number\n"
280: + " @function getModule\n"
281: + " Returns the script of given frame\n"
282: + " @synopsis module getModule() ; returns module of current frame\n"
283: + " @synopsis module getModule(int frame) ; returns module of given frame\n"
284: + " @param frame Frame number, zero is oldest.\n"
285: + " @return Instance of module\n"
286: + " @function getThis\n"
287: + " Returns the 'this' of given frame\n"
288: + " @synopsis class getClass() ; returns 'this' of current frame\n"
289: + " @synopsis class getClass(int frame) ; returns 'this' of given frame\n"
290: + " @param frame Frame number, zero is oldest.\n"
291: + " @return Instance of class\n"
292: + " @function getFunction\n"
293: + " Returns the function of given frame\n"
294: + " @synopsis Function getFunction() ; returns function of current frame\n"
295: + " @synopsis Function getFunction(int frame) ; returns function of given frame\n"
296: + " @param frame Frame number, zero is oldest.\n"
297: + " @return Instance of function\n"
298: + " @function getFrame\n"
299: + " Returns the contents of of given frame\n"
300: + " @synopsis StackFrameElement getFrame() ; returns contents of current frame\n"
301: + " @synopsis StackFrameElement getFrame(int frame) ; returns contents of given frame\n"
302: + " @param frame Frame number, zero is oldest.\n"
303: + " @function getZone\n"
304: + " Returns the zone of of given frame\n"
305: + " @synopsis list getZone() ; returns zone of current frame\n"
306: + " @synopsis list getZone(int frame) ; returns zone of given frame\n"
307: + " @param frame Frame number, zero is oldest.\n"
308: + " @return Zone\n"
309: //}}DOC
310: );
311:
312: }
|