From 9aff374663aa8e336b3903c2ee97aa09f344ea0c Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 6 Jul 2004 16:00:05 +0000 Subject: [PATCH] better JSArray.sort() exception handling darcs-hash:20040706160005-24bed-66b51bb442508ae503d831183e61999ef70b82a5.gz --- src/org/ibex/js/JS.java | 4 ++-- src/org/ibex/js/JSArray.java | 34 ++++++++++++++++------------------ src/org/ibex/js/JSExn.java | 20 +++++++++++++++----- src/org/ibex/js/JSFunction.java | 3 +-- src/org/ibex/js/Test.java | 2 +- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index 96ab988..a81c419 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -28,7 +28,6 @@ public abstract class JS { throw new JSExn("method not found (" + JS.debugToString(method) + ")"); } - // FIXME: JSArgs objects, pointers into stack frame public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")"); } @@ -42,6 +41,7 @@ public abstract class JS { Trap getTrap(JS key) { return null; } void putTrap(JS key, Trap value) throws JSExn { throw new JSExn("traps cannot be placed on this object (class=" + this.getClass().getName() +")"); } String coerceToString() throws JSExn { throw new JSExn("can't coerce to a string (class=" + getClass().getName() +")"); } + String debugToString() { return "[class=" + getClass().getName() + "]"; } boolean jsequals(JS o) { return this == o; } public static class O extends JS { @@ -209,7 +209,7 @@ public abstract class JS { public static String debugToString(JS o) { try { return toString(o); } - catch(JSExn e) { return "[class=" + o.getClass().getName() + "]"; } + catch(JSExn e) { return o.debugToString(); } } public static boolean isInt(JS o) { diff --git a/src/org/ibex/js/JSArray.java b/src/org/ibex/js/JSArray.java index 2171e4d..8aa5325 100644 --- a/src/org/ibex/js/JSArray.java +++ b/src/org/ibex/js/JSArray.java @@ -185,33 +185,31 @@ public class JSArray extends JS.BT { a.setElementAt(elementAt(start+i),i); return a; } - + private static final Vec.CompareFunc defaultSort = new Vec.CompareFunc() { public int compare(Object a, Object b) { try { return JS.toString((JS)a).compareTo(JS.toString((JS)b)); - } catch(JSExn e) { - // FIXME: See emca about this - throw new RuntimeException(e.toString()); - } + } catch(JSExn e) { throw new JSExn.Wrapper(e); } } }; private JS sort(JS tmp) throws JSExn { Vec vec = toVec(); - if(tmp instanceof JS) { - final JS jsFunc = (JS) tmp; - vec.sort(new Vec.CompareFunc() { - public int compare(Object a, Object b) { - try { - return JS.toInt(jsFunc.call((JS)a, (JS)b, null, null, 2)); - } catch (JSExn e) { - // FIXME: Check ecma to see what we should do here - throw new RuntimeException(e.toString()); + try { + if(tmp instanceof JS) { + final JS jsFunc = (JS) tmp; + vec.sort(new Vec.CompareFunc() { + public int compare(Object a, Object b) { + try { + return JS.toInt(jsFunc.call((JS)a, (JS)b, null, null, 2)); + } catch(JSExn e) { throw new JSExn.Wrapper(e); } } - } - }); - } else { - vec.sort(defaultSort); + }); + } else { + vec.sort(defaultSort); + } + } catch(JSExn.Wrapper e) { + throw e.refill(); } setFromVec(vec); return this; diff --git a/src/org/ibex/js/JSExn.java b/src/org/ibex/js/JSExn.java index 4f5fb31..2afcef1 100644 --- a/src/org/ibex/js/JSExn.java +++ b/src/org/ibex/js/JSExn.java @@ -9,12 +9,12 @@ public class JSExn extends Exception { private Vec backtrace = new Vec(); private JS js; public JSExn(String s) { this(JS.S(s)); } - public JSExn(JS js) { this(js,Interpreter.current()); } - public JSExn(JS js, Interpreter cx) { - this.js = js; - if(cx != null) fill(cx); - } + public JSExn(JS js) { this(js,null); } + public JSExn(JS js, Interpreter cx) { this.js = js; fill(cx); } + private void fill(Interpreter cx) { + if(cx == null) cx = Interpreter.current(); + if(cx == null) return; addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]); cx.stack.backtrace(this); } @@ -33,6 +33,16 @@ public class JSExn extends Exception { void addBacktrace(String line) { backtrace.addElement(line); } + public static class Wrapper extends RuntimeException { + public final JSExn e; + public Wrapper(JSExn e) { this.e = e; } + public JSExn refill() { + e.addBacktrace("[foreign code]"); + e.fill(null); + return e; + } + } + public static class IO extends JSExn { public IO(java.io.IOException ioe) { super("ibex.io: " + ioe.toString()); diff --git a/src/org/ibex/js/JSFunction.java b/src/org/ibex/js/JSFunction.java index 61b5268..b36a592 100644 --- a/src/org/ibex/js/JSFunction.java +++ b/src/org/ibex/js/JSFunction.java @@ -98,8 +98,7 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task { // Debugging ////////////////////////////////////////////////////////////////////// - // FIXME: Put this back in - public String xtoString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; } + String debugToString() { return "JSFunction [" + sourceName + ":" + firstLine + "]"; } String dump() { return dump(""); } private String dump(String prefix) { diff --git a/src/org/ibex/js/Test.java b/src/org/ibex/js/Test.java index 621e08d..8e4be33 100644 --- a/src/org/ibex/js/Test.java +++ b/src/org/ibex/js/Test.java @@ -59,7 +59,7 @@ public class Test extends JS { public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { if(!JS.isString(method)) return null; if("print".equals(JS.toString(method))) { - System.out.println(JS.toString(a0)); + System.out.println(JS.debugToString(a0)); return null; } return null; -- 1.7.10.4