public String toString() { return "byte[]"; }
public String getCacheKey() throws NotCacheableException { return cacheKey; }
public InputStream getInputStream(String path) throws IOException {
- if (!"".equals(path)) throw new JSExn("can't get subresources of a byte[] resource");
+ if (!"".equals(path)) throw new IOException("can't get subresources of a byte[] resource");
return new ByteArrayInputStream(bytes);
}
}
ZipInputStream zis = new ZipInputStream(pis);
ZipEntry ze = zis.getNextEntry();
while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
- if (ze == null) throw new JSExn("requested file (" + path + ") not found in archive");
+ if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
}
}
public int read(byte[] b, int off, int len) throws IOException {
int ret = super.read(b, off, len);
if (ret != 1) bytesDownloaded += ret;
- Scheduler.add(new Scheduler.Task() { public void perform() {
+ Scheduler.add(new Scheduler.Task() { public void perform() throws Exception {
callback.call(N(bytesDownloaded),
N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
} });
} else if (parent != null && parent instanceof JS) {
objects.removeElementAt(objects.size() - 1);
- ((JS)parent).put(name, me);
+ try {
+ ((JS)parent).put(name, me);
+ } catch (JSExn e) {
+ throw new Error("this should never happen");
+ }
}
// Public API Exposed to org.xwt /////////////////////////////////////////////////
private static Scheduler singleton;
- public static abstract class Task { public abstract void perform(); }
+ public static abstract class Task { public abstract void perform() throws Exception; }
public static void add(Task t) { singleton.runnable.append(t); }
public static void init() {
if (singleton != null) return;
package org.xwt;
import org.bouncycastle.util.encoders.Base64;
+import org.xwt.js.*;
import org.xwt.util.*;
import java.io.*;
import java.util.*;
this.root = root;
Surface old = fromBox(root);
if (old != null) old.dispose(false);
- else root.put("thisbox", null);
+ else try {
+ root.put("thisbox", null);
+ } catch (JSExn e) {
+ throw new Error("this should never happen");
+ }
// make sure the root is properly sized
do { abort = false; root.reflow(root.width, root.height); } while(abort);
public static Res resolveStringToResource(String str, XWT xwt, boolean permitAbsolute) {
// URL
if (str.indexOf("://") != -1) {
- if (permitAbsolute) return (Res)xwt.callMethod("res.url", str, null, null, null, 1);
+ try {
+ if (permitAbsolute) return (Res)xwt.callMethod("res.url", str, null, null, null, 1);
+ } catch (JSExn jse) {
+ throw new Error("this should never happen");
+ }
Log.log(Template.class, "absolute URL " + str + " not permitted here");
return null;
}
if (staticscript == null) return staticJSScope;
JSFunction temp = staticscript;
staticscript = null;
- temp.cloneWithNewParentScope(staticJSScope).call(null, null, null, null, 0);
+ try {
+ temp.cloneWithNewParentScope(staticJSScope).call(null, null, null, null, 0);
+ } catch (JSExn jse) {
+ throw new Error("this should never happen");
+ }
return staticJSScope;
}
* @param pboxes a vector of all box parents on which to put $-references
* @param ptemplates a vector of the fileNames to recieve private references on the pboxes
*/
- void apply(Box b, XWT xwt) { apply(b, xwt, null); }
- void apply(Box b, XWT xwt, PerInstantiationJSScope parentPis) {
+ void apply(Box b, XWT xwt) throws JSExn { apply(b, xwt, null); }
+ void apply(Box b, XWT xwt, PerInstantiationJSScope parentPis) throws JSExn {
getStatic();
XWT xwt = null;
PerInstantiationJSScope parentBoxPis = null;
JSScope myStatic = null;
- void putDollar(String key, Box target) {
+ void putDollar(String key, Box target) throws JSExn {
if (parentBoxPis != null) parentBoxPis.putDollar(key, target);
declare("$" + key);
put("$" + key, target);
this.xwt = xwt;
this.myStatic = myStatic;
}
- public Object get(Object key) {
+ public Object get(Object key) throws JSExn {
if (super.has(key)) return super.get(key);
if (key.equals("xwt")) return xwt;
if (key.equals("static")) return myStatic;
return super.get(key);
}
- public void put(Object key, Object val) {
+ public void put(Object key, Object val) throws JSExn {
if (super.has(key)) super.put(key, val);
else super.put(key, val);
}
Object memberValue = objects.elementAt(objects.size() - 1);
String memberName = (String)objects.elementAt(objects.size() - 2);
JS struct = (JS)objects.elementAt(objects.size() - 3);
- struct.put(memberName, memberValue);
+ try {
+ struct.put(memberName, memberValue);
+ } catch (JSExn e) {
+ throw new Error("this should never happen");
+ }
objects.setSize(objects.size() - 2);
case "data":
int i;
for(i=objects.size() - 1; objects.elementAt(i) != null; i--);
JSArray arr = new JSArray();
- for(int j = i + 1; j<objects.size(); j++) arr.put(new Integer(j - i - 1), objects.elementAt(j));
+ try {
+ for(int j = i + 1; j<objects.size(); j++) arr.put(new Integer(j - i - 1), objects.elementAt(j));
+ } catch (JSExn e) {
+ throw new Error("this should never happen");
+ }
objects.setElementAt(arr, i);
objects.setSize(i + 1);
//#end
new java.lang.Thread() {
public void run() {
try {
- final Object ret = call_(args);
- Scheduler.add(new Scheduler.Task() {
- public void perform() {
- try {
- callback.unpause(null);
- } catch (JS.PausedException pe) {
- // okay
+ try {
+ final Object ret = call_(args);
+ Scheduler.add(new Scheduler.Task() {
+ public void perform() {
+ try {
+ callback.unpause(null);
+ } catch (JS.PausedException pe) {
+ // okay
+ } catch (JSExn e) {
+ // FIXME
+ throw new Error("FIXME");
+ }
}
- }
- });
- } catch (IOException se) {
- if (Log.on) Log.log(this, se);
- throw new JSExn("socket exception: " + se);
+ });
+ } catch (IOException se) {
+ if (Log.on) Log.log(this, se);
+ throw new JSExn("socket exception: " + se);
+ }
+ } catch (JSExn e) {
+ // FIXME
+ throw new Error("FIXME");
}
} }.start();
return null;
public String toString() { return "XWTSUB " + key; }
public void put(Object key, Object val) { XWT.this.put(this.key + "." + key, val); }
public Object get(Object key) { return XWT.this.get(this.key + "." + key); }
- 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 {
return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
}
- 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 {
return XWT.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
}
}
//#switch(name)
case "thread":
Scheduler.add(new Scheduler.Task() {
- public void perform() {
+ public void perform() throws JSExn {
try {
JS.invokePauseable((JSFunction)value);
} catch (JS.PausedException pe) {
return null;
}
- public static void sleep(final int i) {
+ public static void sleep(final int i) throws JSExn {
try {
final JS.UnpauseCallback callback = JS.pause();
final long currentTime = System.currentTimeMillis();
public void run() {
try { Thread.sleep(i); } catch (InterruptedException e) { }
Scheduler.add(new Scheduler.Task() {
- public void perform() {
+ public void perform() throws JSExn {
try {
callback.unpause(null);
} catch (JS.PausedException pe) {
public static final JSMath xwtMath = new JSMath() {
private JS gs = new JSScope.Global();
public String toString() { return "XWTMATH"; }
- public Object get(Object key) {
+ public Object get(Object key) throws JSExn {
//#switch(key)
case "isNaN": return gs.get("isNaN");
case "isFinite": return gs.get("isFinite");
public static final JS xwtString = new JS() {
private JS gs = new JSScope.Global();
public void put(Object key, Object val) { }
- public Object get(Object key) {
+ public Object get(Object key) throws JSExn {
//#switch(key)
case "parseInt": return gs.get("parseInt");
case "parseFloat": return gs.get("parseFloat");
Vector obStack = new Vector();
public XMLHelper() { super(BUFFER_SIZE); }
public void startElement(XML.Element c) throws XML.SchemaException {
- JS o = new JS();
- o.put("$name", c.localName);
- for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
- o.put("$numchildren", new Integer(0));
- obStack.addElement(o);
+ try {
+ JS o = new JS();
+ o.put("$name", c.localName);
+ for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
+ o.put("$numchildren", new Integer(0));
+ obStack.addElement(o);
+ } catch (JSExn jse) {
+ throw new Error("this should never happen");
+ }
}
public void endElement(XML.Element c) throws XML.SchemaException {
- if (obStack.size() == 1) return;
- JS me = (JS)obStack.lastElement();
- obStack.setSize(obStack.size() - 1);
- JS parent = (JS)obStack.lastElement();
- int numchildren = ((Integer)parent.get("$numchildren")).intValue();
- parent.put("$numchildren", new Integer(numchildren + 1));
- parent.put(new Integer(numchildren), me);
+ try {
+ if (obStack.size() == 1) return;
+ JS me = (JS)obStack.lastElement();
+ obStack.setSize(obStack.size() - 1);
+ JS parent = (JS)obStack.lastElement();
+ int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+ parent.put("$numchildren", new Integer(numchildren + 1));
+ parent.put(new Integer(numchildren), me);
+ } catch (JSExn jse) {
+ throw new Error("this should never happen");
+ }
}
public void characters(char[] ch, int start, int length) throws XML.SchemaException {
- String s = new String(ch, start, length);
- JS parent = (JS)obStack.lastElement();
- int numchildren = ((Integer)parent.get("$numchildren")).intValue();
- Object lastChild = parent.get(new Integer(numchildren - 1));
- if (lastChild instanceof String) {
- parent.put(new Integer(numchildren - 1), lastChild + s);
- } else {
- parent.put("$numchildren", new Integer(numchildren + 1));
- parent.put(new Integer(numchildren), s);
+ try {
+ String s = new String(ch, start, length);
+ JS parent = (JS)obStack.lastElement();
+ int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+ Object lastChild = parent.get(new Integer(numchildren - 1));
+ if (lastChild instanceof String) {
+ parent.put(new Integer(numchildren - 1), lastChild + s);
+ } else {
+ parent.put("$numchildren", new Integer(numchildren + 1));
+ parent.put(new Integer(numchildren), s);
+ }
+ } catch (JSExn jse) {
+ throw new Error("this should never happen");
}
}
public void whitespace(char[] ch, int start, int length) {}
}
/** this is the only synchronization point we need in order to be threadsafe */
- synchronized Object resume() {
+ synchronized Object resume() throws JSExn {
Thread t = Thread.currentThread();
Interpreter old = (Interpreter)threadToInterpreter.get(t);
threadToInterpreter.put(t, this);
// Operations on Primitives //////////////////////////////////////////////////////////////////////
- static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) {
+ static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) throws JSExn {
if (o instanceof Number) {
//#switch(method)
case "toFixed": throw new JSExn("toFixed() not implemented");
throw new JSExn("Attempted to call non-existent method: " + method);
}
- static Object getFromPrimitive(Object o, Object key) {
+ static Object getFromPrimitive(Object o, Object key) throws JSExn {
boolean returnJS = false;
if (o instanceof Boolean) {
throw new JSExn("cannot call methods on Booleans");
case "toLowerCase": returnJS = true; break;
case "toUpperCase": returnJS = true; break;
case "toString": returnJS = true; break;
- case "substr": returnJS = true; break;
- //#end
+ case "substr": returnJS = true; break;
+ //#end
}
if (returnJS) {
final Object target = o;
final String method = key.toString();
return new JS() {
- 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 {
if (nargs > 2) throw new JSExn("cannot call that method with that many arguments");
return callMethodOnPrimitive(target, method, a0, a1, a2, rest, nargs);
}
private Object method;
JS obj;
public Stub(JS obj, Object method) { this.obj = obj; this.method = method; }
- 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 {
return ((JS)obj).callMethod(method, a0, a1, a2, rest, nargs);
}
}
public static class PausedException extends Exception { PausedException() { } }
- public static void invokePauseable(JSFunction function) throws JS.PausedException {
+ public static void invokePauseable(JSFunction function) throws JS.PausedException, JSExn {
Interpreter i = new Interpreter(function, true, new JSArray());
int oldpausecount = i.pausecount;
i.resume();
public static class UnpauseCallback {
Interpreter i;
UnpauseCallback(Interpreter i) { this.i = i; }
- public void unpause(Object o) throws PausedException {
+ public void unpause(Object o) throws PausedException, JSExn {
i.stack.push(o);
i.resume();
}
};
private Hash entries = null;
- public Enumeration keys() { return entries == null ? emptyEnumeration : entries.keys(); }
- public Object get(Object key) { return entries == null ? null : entries.get(key, null); }
- public void put(Object key, Object val) { if (entries == null) entries = new Hash(); entries.put(key, null, val); }
+ public Enumeration keys() throws JSExn {
+ return entries == null ? emptyEnumeration : entries.keys();
+ }
+ public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
+ public void put(Object key, Object val) throws JSExn {
+ if (entries == null) entries = new Hash();
+ entries.put(key, null, val);
+ }
// Trap support //////////////////////////////////////////////////////////////////////////////
protected boolean isTrappable() { return false; }
/** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
- public final void putAndTriggerTraps(Object key, Object value) {
+ public void putAndTriggerTraps(Object key, Object value) throws JSExn {
Trap t = getTrap(key);
if (t != null) t.invoke(key, value);
else put(key, value);
}
/** performs a get, triggering traps if present; traps are run in an unpauseable interpreter */
- public final Object getAndTriggerTraps(Object key) {
+ public Object getAndTriggerTraps(Object key) throws JSExn {
Trap t = getTrap(key);
if (t != null) return t.invoke(key);
else return get(key);
// return this from get() if the key was actually a method.
public static final Object METHOD = new Object();
- 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 {
throw new JSExn("attempted to call the null value (method "+method+")");
}
- 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 {
throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
}
// Typing Support //////////////////////////////////////////////////////////////////////////////
- public Number coerceToNumber() { throw new JSExn("tried to coerce a JavaScript object to a Number"); }
- public String coerceToString() { throw new JSExn("tried to coerce a JavaScript object to a String"); }
- public boolean coerceToBoolean() { throw new JSExn("tried to coerce a JavaScript object to a Boolean"); }
+ public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); }
+ public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); }
+ public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); }
public String typeName() { return "object"; }
return Integer.parseInt(s);
}
- 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(method)
case "pop": return vec.pop();
case "reverse": return reverse();
return super.get(key);
}
- public void put(Object key, Object val) {
+ public void put(Object key, Object val) throws JSExn {
if (key.equals("length")) vec.setSize(toNumber(val).intValue());
int i = intVal(key);
if (i == Integer.MIN_VALUE) super.put(key, val);
return JS.toString(a).compareTo(JS.toString(b));
}
};
- private Object sort(Object tmp) {
+ private Object sort(Object tmp) throws JSExn {
if(tmp instanceof JS) {
final JSArray funcArgs = new JSArray(2);
final JS jsFunc = (JS) tmp;
vec.sort(new Vec.CompareFunc() {
public int compare(Object a, Object b) {
- funcArgs.setElementAt(a,0);
- funcArgs.setElementAt(b,1);
- return JS.toInt(jsFunc.call(a, b, null, null, 2));
+ try {
+ funcArgs.setElementAt(a,0);
+ funcArgs.setElementAt(b,1);
+ return JS.toInt(jsFunc.call(a, b, null, null, 2));
+ } catch (Exception e) {
+ // FIXME
+ throw new JSRuntimeExn(e.toString());
+ }
}
});
} else {