renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / JSExn.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.js; 
6
7 import org.ibex.util.*;
8 import java.io.*;
9
10 /** An exception which can be thrown and caught by JavaScript code */
11 public class JSExn extends Exception { 
12     private Basket.List backtrace = new Basket.Array();
13     private JS js; 
14     public JSExn(String s) { this(JSU.S(s)); }
15     public JSExn(JS js) { this(js,null); }
16     public JSExn(JS js, Interpreter cx) { this.js = js; fill(cx); }
17     
18     private void fill(Interpreter cx) {
19         if(cx == null) cx = Interpreter.current();
20         if(cx == null) return;
21         addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
22         cx.stack.backtrace(this);
23     }
24     public void printStackTrace() { printStackTrace(System.err); }
25     public void printStackTrace(PrintWriter pw) {
26         for(int i=0; i<backtrace.size(); i++) pw.println("    at " + (String) backtrace.get(i));
27         super.printStackTrace(pw);
28     }
29     public void printStackTrace(PrintStream ps) {
30         for(int i=0; i<backtrace.size(); i++) ps.println("    at " + (String) backtrace.get(i));
31         super.printStackTrace(ps);
32     }
33     public String toString() { return "JSExn: " + JSU.str(js); }
34     public String getMessage() { return toString(); }
35     public JS getObject() { return js; } 
36     
37     void addBacktrace(String line) { backtrace.add(line); }
38     
39     public static class Wrapper extends RuntimeException {
40         public final JSExn e;
41         public Wrapper(JSExn e) { this.e = e; }
42         public JSExn unwrap() {
43             e.addBacktrace("[foreign code]");
44             e.fill(null);
45             return e;
46         }
47     }
48     
49     public static class IO extends JSExn {
50         public IO(java.io.IOException ioe) {
51             super("ibex.io: " + ioe.toString());
52             JSU.warn(ioe);
53         }
54     }
55