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)
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;
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; }
+}
+
}
/** 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);
-// 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.*;
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)
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;
}
}
- 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)
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;
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 {
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);
}
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;
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)
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);
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);
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);
/** 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();
* 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);
* 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) {
/** 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();
* 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();
* 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();
/** Parses a comment and returns its body. The CharStream should
* be positioned immediately after the <!--
*/
- private static String parseComment(CharStream cs) throws IOException {
+ private static String parseComment(CharStream cs) throws IOException, JSExn {
int dashes = 0;
String ret = "";
while(true) {
}
/** 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;
}
/** 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;
}
}
- 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) {