2003/11/29 03:06:10
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:20 +0000 (07:42 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:42:20 +0000 (07:42 +0000)
darcs-hash:20040130074220-2ba56-abcaa98566ac9efd0496398a346994a9ce76afdd.gz

src/org/xwt/js/JSDate.java
src/org/xwt/js/JSExn.java
src/org/xwt/js/JSFunction.java
src/org/xwt/js/JSMath.java
src/org/xwt/js/JSRegexp.java
src/org/xwt/js/JSScope.java
src/org/xwt/js/Trap.java
src/org/xwt/translators/HTML.java
src/org/xwt/util/Log.java

index d41dfcc..ca9c5b7 100644 (file)
@@ -61,7 +61,7 @@ public class JSDate extends JS {
 
     public String coerceToString() { return date_format(date, FORMATSPEC_FULL); }
 
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 0: {
                 //#switch(method)
@@ -126,7 +126,7 @@ public class JSDate extends JS {
         return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
-    public Object get(Object key) {
+    public Object get(Object key) throws JSExn {
         //#switch(key)
         case "toString": return METHOD;
         case "toTimeString": return METHOD;
index 370da2d..2cc9591 100644 (file)
@@ -7,10 +7,20 @@ import java.io.*;
 import java.util.*;
 
 /** An exception which can be thrown and caught by JavaScript code */
-public class JSExn extends RuntimeException { 
+public class JSExn extends Exception { 
     private Object js = null; 
     public JSExn(Object js) { this.js = js; } 
     public String toString() { return "JSExn: " + js; }
     public String getMessage() { return toString(); }
     public Object getObject() { return js; } 
 } 
+
+/** should only be used for failed coercions */
+class JSRuntimeExn extends RuntimeException {
+    private Object js = null; 
+    public JSRuntimeExn(Object js) { this.js = js; } 
+    public String toString() { return "JSRuntimeExn: " + js; }
+    public String getMessage() { return toString(); }
+    public Object getObject() { return js; } 
+}
+
index a9164b8..c15ef55 100644 (file)
@@ -54,7 +54,7 @@ public class JSFunction extends JS implements ByteCodes, Tokens {
     }
 
     /** Note: code gets run in an <i>unpauseable</i> context. */
-    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         JSArray args = new JSArray();
         if (nargs > 0) args.addElement(a0);
         if (nargs > 1) args.addElement(a1);
index bf34435..9aaaaee 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL ]
 
 package org.xwt.js; 
 import org.xwt.util.*; 
@@ -19,7 +19,7 @@ public class JSMath extends JS {
     private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2));
     private static final Double SQRT2   = new Double(java.lang.Math.sqrt(2));
 
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 0: {
                 //#switch(method)
@@ -60,7 +60,7 @@ public class JSMath extends JS {
 
     public void put(Object key, Object val) { return; }
 
-    public Object get(Object key) {
+    public Object get(Object key) throws JSExn {
         //#switch(key)
         case "E": return E;
         case "LN10": return LN10;
index 37bfe6e..3bb5ab9 100644 (file)
@@ -37,7 +37,7 @@ public class JSRegexp extends JS {
         }
     }
 
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 1: {
                 //#switch(method)
@@ -74,7 +74,7 @@ public class JSRegexp extends JS {
         return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
     
-    public Object get(Object key) {
+    public Object get(Object key) throws JSExn {
         //#switch(key)
         case "exec": return METHOD;
         case "test": return METHOD;
@@ -84,31 +84,39 @@ public class JSRegexp extends JS {
         return super.get(key);
     }
     
-    public void put(Object key, Object value) {
+    public void put(Object key, Object value) throws JSExn {
         if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
         super.put(key,value);
     }
   
     private static Object matchToExecResult(REMatch match, RE re, String s) {
-        JS ret = new JS();
-        ret.put("index", N(match.getStartIndex()));
-        ret.put("input",s);
-        int n = re.getNumSubs();
-        ret.put("length", N(n+1));
-        ret.put("0",match.toString());
-        for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i));
-        return ret;
+        try {
+            JS ret = new JS();
+            ret.put("index", N(match.getStartIndex()));
+            ret.put("input",s);
+            int n = re.getNumSubs();
+            ret.put("length", N(n+1));
+            ret.put("0",match.toString());
+            for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i));
+            return ret;
+        } catch (JSExn e) {
+            throw new Error("this should never happen");
+        }
     }
     
     public String toString() {
-        StringBuffer sb = new StringBuffer();
-        sb.append('/');
-        sb.append(get("source"));
-        sb.append('/');
-        if(global) sb.append('g');
-        if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
-        if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
-        return sb.toString();
+        try {
+            StringBuffer sb = new StringBuffer();
+            sb.append('/');
+            sb.append(get("source"));
+            sb.append('/');
+            if(global) sb.append('g');
+            if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
+            if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
+            return sb.toString();
+        } catch (JSExn e) {
+            throw new Error("this should never happen");
+        }
     }
     
     public static Object stringMatch(Object o, Object arg0) throws JSExn {
index 971d550..3e797f3 100644 (file)
@@ -13,17 +13,17 @@ public class JSScope extends JS {
     private static final Object NULL_PLACEHOLDER = new Object();
 
     public JSScope(JSScope parentScope) { this.parentScope = parentScope; }
-    public void declare(String s) { super.put(s, NULL_PLACEHOLDER); }
+    public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); }
     public JSScope getParentScope() { return parentScope; }
 
-    public Object get(Object key) {
+    public Object get(Object key) throws JSExn {
         Object o = super.get(key);
         if (o != null) return o == NULL_PLACEHOLDER ? null : o;
         else return parentScope == null ? null : parentScope.get(key);
     }
 
-    public boolean has(Object key) { return super.get(key) != null; }
-    public void put(Object key, Object val) {
+    public boolean has(Object key) throws JSExn { return super.get(key) != null; }
+    public void put(Object key, Object val) throws JSExn {
         if (parentScope != null && !has(key)) parentScope.put(key, val);
         else super.put(key, val == null ? NULL_PLACEHOLDER : val);
     }
@@ -33,7 +33,7 @@ public class JSScope extends JS {
         private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
 
         public Global() { super(null); }
-        public Object get(Object key) {
+        public Object get(Object key) throws JSExn {
             //#switch(key)
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
@@ -53,7 +53,7 @@ public class JSScope extends JS {
             return super.get(key);
         }
 
-        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             switch(nargs) {
                 case 0: {
                     //#switch(method)
index a0ed711..e92bad7 100644 (file)
@@ -30,7 +30,7 @@ class Trap {
         putInvoker.add(-1, Tokens.RETURN, null);
     }
     
-    void invoke(Object key, Object value) {
+    void invoke(Object key, Object value) throws JSExn {
         Interpreter i = new Interpreter(putInvoker, false, null);
         i.stack.push(trapee);
         i.stack.push(key);
@@ -38,7 +38,7 @@ class Trap {
         i.resume();
     }
 
-    Object invoke(Object key) {
+    Object invoke(Object key) throws JSExn {
         Interpreter i = new Interpreter(getInvoker, false, null);
         i.stack.push(this);
         i.stack.push(key);
@@ -52,7 +52,7 @@ class Trap {
         Object val = null;
         boolean cascadeHappened = false;
         public TrapScope(JSScope parent, Trap t, Object val) { super(parent); this.t = t; this.val = val; }
-        public Object get(Object key) {
+        public Object get(Object key) throws JSExn {
             if (key.equals("trapee")) return t.trapee;
             if (key.equals("trapname")) return t.name;
             return super.get(key);
index 4801971..6aa8ba2 100644 (file)
@@ -50,7 +50,7 @@ public class HTML {
     /** true iff we have encountered an LI more recently than the last OL/UL */
     private static boolean withinLI = false;
 
-    public static synchronized JS parseReader(Reader r) throws IOException {
+    public static synchronized JS parseReader(Reader r) throws IOException, JSExn {
         CharStream cs = new CharStream(r);
         JS h = new JS();
 
@@ -84,7 +84,7 @@ public class HTML {
      *  facilitate correcting broken HTML. Otherwise, this returns
      *  null.
      */
-    private static String parseElement(CharStream cs, JS h) throws IOException {
+    private static String parseElement(CharStream cs, JS h) throws IOException, JSExn {
         // scan element name
         while(Character.isSpace(cs.peek())) cs.get();
         String elementName = parseElementName(cs);
@@ -135,7 +135,7 @@ public class HTML {
      *  positioned at the character immediately after the right
      *  bracket closing the start-tag
      */
-    private static String parseBody(CharStream cs, JS h, String elementName) throws IOException {
+    private static String parseBody(CharStream cs, JS h, String elementName) throws IOException, JSExn {
         String cdata = "";
         int length = h.get("$numchildren") == null ? 0 : Integer.parseInt(h.get("$numchildren").toString());
         while(true) {
@@ -187,7 +187,7 @@ public class HTML {
     /** Parses an element name and returns it. The CharStream should
      *  be positioned at the first character of the name.
      */
-    private static String parseElementName(CharStream cs) throws IOException {
+    private static String parseElementName(CharStream cs) throws IOException, JSExn {
         String ret = "";
         while (cs.peek() != '>' && !Character.isSpace(cs.peek())) ret += cs.get();
         return ret.toLowerCase();
@@ -197,7 +197,7 @@ public class HTML {
      *  be positioned at the first character of the name, possibly
      *  with intervening whitespace.
      */
-    private static String parseAttributeName(CharStream cs) throws IOException {
+    private static String parseAttributeName(CharStream cs) throws IOException, JSExn {
         while(Character.isSpace(cs.peek())) cs.get();
         String ret = "";
         while(!Character.isSpace(cs.peek()) && cs.peek() != '=' && cs.peek() != '>') ret += cs.get();
@@ -208,7 +208,7 @@ public class HTML {
      *  should be positioned at the equals sign, possibly with
      *  intervening whitespace.
      */
-    private static String parseAttributeValue(CharStream cs) throws IOException {
+    private static String parseAttributeValue(CharStream cs) throws IOException, JSExn {
 
         // eat whitespace and equals sign
         while(Character.isSpace(cs.peek())) cs.get();
@@ -236,7 +236,7 @@ public class HTML {
     /** Parses a comment and returns its body. The CharStream should
      *  be positioned immediately after the &lt;!--
      */
-    private static String parseComment(CharStream cs) throws IOException {
+    private static String parseComment(CharStream cs) throws IOException, JSExn {
         int dashes = 0;
         String ret = "";
         while(true) {
@@ -249,7 +249,7 @@ public class HTML {
     }
 
     /** Expands all SGML entities in string <tt>s</tt> */
-    public static String expandEntities(String s) throws IOException {
+    public static String expandEntities(String s) throws IOException, JSExn {
         if (s.indexOf('&') == -1) return s;
         StringBuffer sb = new StringBuffer();
         int i=0;
@@ -283,7 +283,7 @@ public class HTML {
     }
 
     /** removes all redundant whitespace */
-    private static String removeRedundantWhitespace(String s) {
+    private static String removeRedundantWhitespace(String s) throws JSExn {
 
         if (s.indexOf(' ') == -1 && s.indexOf('\n') == -1 && s.indexOf('\t') == -1 && s.indexOf('\r') == -1) return s;
 
index 269b2f2..e75d8d8 100644 (file)
@@ -68,7 +68,7 @@ public class Log {
         }
     }
 
-    public static void recursiveLog(String indent, String name, Object o) {
+    public static void recursiveLog(String indent, String name, Object o) throws JSExn {
         if (!name.equals("")) name += " : ";
 
         if (o == null) {