import org.bouncycastle.util.encoders.Base64;
/** Singleton class that provides all functionality in the xwt.* namespace */
-public final class XWT extends JSCallable {
+public final class XWT extends JS {
public final Res rr;
public XWT(Res rr) { this.rr = rr; }
/** lets us put multi-level get/put/call keys all in the same method */
- private class Sub extends JSCallable {
+ private class Sub extends JS {
String key;
Sub(String key) { this.key = key; }
+ 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 method, JSArray args) { return XWT.this.call(method == null ? key : this.key + "." + method, args); }
+ public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) {
+ 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) {
+ return XWT.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
+ }
}
public Object get(Object name) {
//#switch(name)
case "math": return xwtMath;
case "string": return xwtString;
- case "date": return new JSDate();
+ case "date": return METHOD;
case "origin": return Main.origin;
- case "box": return new BoxTree();
+ case "box": return new Box();
+ case "log": return new Sub("log");
case "ui": return new Sub("ui");
+ case "thread": return new Sub("thread");
+ case "font": return new Sub("font");
+ case "font.sansserif": return Main.builtin.get("fonts/vera/Vera.ttf");
+ case "font.monospace": return Main.builtin.get("fonts/vera/VeraMono.ttf");
+ case "font.serif": return Main.builtin.get("fonts/vera/VeraSe.ttf");
case "ui.key": return new Sub("ui.key");
case "ui.key.alt": return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
case "ui.key.control": return Surface.control ? Boolean.TRUE : Boolean.FALSE;
case "fs.home": return System.getProperty("user.home");
case "fs.temp": return System.getProperty("java.io.tempdir");
case "ui.mouse": return new Sub("ui.mouse");
+ case "res": return new Sub("res");
case "ui.mouse.button":
if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(2);
else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(3);
- else return new Integer(0);
+ else return ZERO;
case "undocumented": return new Sub("undocumented");
case "undocumented.internal": return new Sub("undocumented.internal");
+ case "thread.yield": return METHOD;
+ case "thread.sleep": return METHOD;
+ case "res.watch": return METHOD;
+ case "soap": return METHOD;
+ case "apply": return METHOD;
+ case "graft": return METHOD;
+ case "ui.browser": return METHOD;
+ case "clone": return METHOD;
+ case "res.unzip": return METHOD;
+ case "res.uncab": return METHOD;
+ case "res.cache": return METHOD;
+ case "res.url": return METHOD;
+ case "log.println": return METHOD;
+ case "log.dump": return METHOD;
+ case "regexp": return METHOD;
+ case "rpc.xml": return METHOD;
+ case "rpc.soap": return METHOD;
+ case "crypto.rsa": return METHOD;
+ case "crypto.md5": return METHOD;
+ case "crypto.sha1": return METHOD;
+ case "crypto.rc4": return METHOD;
+ case "stream.parse.html": return METHOD;
+ case "stream.parse.xml": return METHOD;
+ case "stream.parse.utf8": return METHOD;
//#end
return rr.get(name);
}
public void put(Object name, final Object value) {
//#switch(name)
case "thread":
- Scheduler.add(new Scheduler.Task() { public void perform() { JSContext.invokePauseable((JSFunction)value); } });
+ Scheduler.add(new Scheduler.Task() {
+ public void perform() {
+ try {
+ JS.invokePauseable((JSFunction)value);
+ } catch (JS.PausedException pe) {
+ // okay; wait for ourselves to be re-enqueued
+ }
+ }
+ });
case "ui.clipboard": Platform.setClipBoard((String)value);
case "ui.frame": Platform.createSurface((Box)value, true, true);
case "ui.window": Platform.createSurface((Box)value, false, true);
//#end
}
- public Object call(Object name, JSArray args) throws JS.Exn {
- Object a = args.elementAt(0);
- Object b = args.elementAt(1);
- Object c = args.elementAt(2);
- if (name.equals("date")) return new org.xwt.js.JSDate(args);
- else if (args.length() == 0 && name.equals("thread.yield")) { sleep(0); return null; }
- else if (args.length() == 2) {
+ public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+ if (name.equals("date")) return new JSDate(a, b, c, rest, nargs);
+ else if (nargs == 0 && name.equals("thread.yield")) { sleep(0); return null; }
+ else if (nargs == 2) {
//#switch(name)
case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
- case "soap": return new SOAP((String)args.elementAt(0), "", (String)args.elementAt(1), null);
+ case "soap": return new SOAP((String)a, "", (String)b, null);
case "apply":
- Template.getTemplate((Res)args.elementAt(1)).apply((Box)args.elementAt(0), XWT.this);
- return args.elementAt(0);
+ Template.getTemplate((Res)b).apply((Box)a, XWT.this);
+ return a;
//#end
- } else if (args.length() == 3 && name.equals("soap")) {
- if (name.equals("soap")) {
- return new SOAP((String)args.elementAt(0), "", (String)args.elementAt(1), (String)args.elementAt(2));
- } else if (name.equals("graft")) {
- if (a instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
- if (a instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
- if (a instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
- if (a instanceof Res) return new Res.Graft((Res)a, b, c);
- return new JS.Graft((JS)a, b, c);
- }
- } else if (args.length() == 1) {
+ } else if (nargs == 3 && name.equals("soap")) {
+ if (name.equals("soap"))
+ return new SOAP((String)a, "", (String)b, (String)c);
+
+ } else if (nargs == 1) {
//#switch(name)
case "ui.browser": Platform.newBrowserWindow((String)a); return null;
case "clone": return new XWT((Res)a);
case "res.unzip": return new Res.Zip((Res)a);
case "res.uncab": return new Res.Cab((Res)a);
case "res.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
- catch (Res.NotCacheableException e) { throw new JS.Exn("this resource cannot be cached"); }
- case "res.fromURL": return Res.fromString((String)a);
+ catch (Res.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
+ case "res.url":
+ String url = (String)a;
+ if (url.startsWith("http://")) return new Res.HTTP(url);
+ else if (url.startsWith("https://")) return new Res.HTTP(url);
+ else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
+ else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
+ throw new JSExn("invalid resource specifier " + url);
case "thread.sleep": sleep(JS.toInt(a)); return null;
case "log.println": Log.logJS(this, a== null ? "**null**" : a.toString()); return null;
case "log.dump": Log.recursiveLog("","",a); return null;
}
public static void sleep(final int i) {
- final Callback callback = JSContext.pause();
- final long currentTime = System.currentTimeMillis();
- new Thread() {
- public void run() {
- try { Thread.sleep(i); } catch (InterruptedException e) { }
- Scheduler.add(new Scheduler.Task() { public void perform() { callback.call(null); } });
- }
- }.start();
+ try {
+ final JS.UnpauseCallback callback = JS.pause();
+ final long currentTime = System.currentTimeMillis();
+ new Thread() {
+ public void run() {
+ try { Thread.sleep(i); } catch (InterruptedException e) { }
+ Scheduler.add(new Scheduler.Task() {
+ public void perform() {
+ try {
+ callback.unpause(null);
+ } catch (JS.PausedException pe) {
+ // okay
+ }
+ }
+ });
+ }
+ }.start();
+ } catch (JS.NotPauseableException npe) {
+ throw new JSExn("you cannot sleep or yield in the foreground thread");
+ }
}
public static final JSMath xwtMath = new JSMath() {
- private JS gs = new JSScope.Global(null);
+ private JS gs = new JSScope.Global();
+ public String toString() { return "XWTMATH"; }
public Object get(Object key) {
//#switch(key)
case "isNaN": return gs.get("isNaN");
}
};
- public static final JSObj xwtString = new JSObj() {
- private JS gs = new JSScope.Global(null);
+ 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) {
//#switch(key)
case "unescape": return gs.get("unescape");
case "fromCharCode": return gs.get("stringFromCharCode");
//#end
- return super.get(key);
+ return null;
}
};
Vector obStack = new Vector();
public XMLHelper() { super(BUFFER_SIZE); }
public void startElement(XML.Element c) throws XML.SchemaException {
- JS o = new JSObj();
+ 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));
}
}
public void whitespace(char[] ch, int start, int length) {}
- public JS doParse(InputStream is) throws JS.Exn {
+ public JS doParse(InputStream is) throws JSExn {
try {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
parse(r);
} catch (XML.XMLException e) {
- throw new JS.Exn("error parsing XML: " + e.toString());
+ throw new JSExn("error parsing XML: " + e.toString());
} catch (IOException e) {
if (Log.on) Log.log(this, "IO Exception while reading from file");
if (Log.on) Log.log(this, e);
- throw new JS.Exn("error reading from Resource");
+ throw new JSExn("error reading from Resource");
}
return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
}