X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcore%2FIbex.java;h=a157239095811e08be0a67a382135c474951efa1;hp=57fe96d7a8cfcddd0ea39393d9e86520a7370852;hb=0db31fc0dc88749cd8022790fd475df8b7b06c27;hpb=586485c446bc6870e08cfca8675a41442cca2c54 diff --git a/src/org/ibex/core/Ibex.java b/src/org/ibex/core/Ibex.java index 57fe96d..a157239 100644 --- a/src/org/ibex/core/Ibex.java +++ b/src/org/ibex/core/Ibex.java @@ -45,14 +45,12 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { Sub(JS key) { this.key = key; } public void put(JS key, JS val) throws JSExn { Ibex.this.put(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key)), val); } public JS get(JS key) throws JSExn { return Ibex.this.get(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key))); } - public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - return Ibex.this.callMethod(this.key, a0, a1, a2, rest, nargs); - } - public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - return Ibex.this.callMethod(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), a0, a1, a2, rest, nargs); + public JS call(JS[] args) throws JSExn { return Ibex.this.call(this.key, args); } + public JS call(JS method, JS[] args) throws JSExn { + return Ibex.this.call(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), args); } } - private Cache subCache = new Cache(20); + private Cache subCache = new Cache(20, true); private Sub getSub(JS key) { Sub ret = (Sub)subCache.get(key); if (ret == null) subCache.put(key, ret = new Sub(key)); @@ -61,7 +59,7 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { public JS get(JS name) throws JSExn { // FIXME: SHouldn't need this (just trap [""]) - if (JS.isString(name) && JSU.toString(name).length() == 0) return rr; + if (JSU.isString(name) && JSU.toString(name).length() == 0) return rr; // FEATURE: Preprocessor hack to generate specialized JS instances (avoid all this string concatenation) //#switch(JSU.toString(name)) case "math": return ibexMath; @@ -82,10 +80,10 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { case "ui.browser": return METHOD; case "ui.mouse": return getSub(name); case "ui.mouse.button": - if (Surface.button1 && !Surface.button2 && !Surface.button3) return N(1); - else if (!Surface.button1 && Surface.button2 && !Surface.button3) return N(2); - else if (!Surface.button1 && !Surface.button2 && Surface.button3) return N(3); - else return ZERO; + if (Surface.button1 && !Surface.button2 && !Surface.button3) return JSU.N(1); + else if (!Surface.button1 && Surface.button2 && !Surface.button3) return JSU.N(2); + else if (!Surface.button1 && !Surface.button2 && Surface.button3) return JSU.N(3); + else return JSU.ZERO; case "ui.key": return getSub(name); case "ui.key.name": return getSub(name); case "ui.key.name.alt": return JSU.S(Platform.altKeyName()); @@ -93,10 +91,10 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { case "ui.key.control": return Surface.control ? JSU.T : JSU.F; case "ui.key.shift": return Surface.shift ? JSU.T : JSU.F; case "ui.clipboard": return JSU.S((String)Platform.getClipBoard()); - case "ui.maxdim": return N(Short.MAX_VALUE); + case "ui.maxdim": return JSU.N(Short.MAX_VALUE); case "ui.screen": return getSub(name); - case "ui.screen.width": return N(Platform.getScreenWidth()); - case "ui.screen.height": return N(Platform.getScreenHeight()); + case "ui.screen.width": return JSU.N(Platform.getScreenWidth()); + case "ui.screen.height": return JSU.N(Platform.getScreenHeight()); case "undocumented": return getSub(name); case "undocumented.initialOrigin": return JSU.S(Main.origin); case "undocumented.initialTemplate": return JSU.S(Main.initialTemplate); @@ -134,7 +132,7 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { public void put(JS name, JS value) throws JSExn { //#switch(JSU.toString(name)) - case "thread": Scheduler.add((Task)value); return; + case "thread": Scheduler.add((Callable)value); return; case "ui.clipboard": Platform.setClipBoard(JSU.toString(value)); return; case "ui.frame": Platform.createSurface((Box)value, true, true); return; case "ui.window": Platform.createSurface((Box)value, false, true); return; @@ -146,41 +144,45 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { throw new JSExn("attempted to put unknown property: ibex."+name); } - public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn { + public JS call(JS method, JS[] args) throws JSExn { try { - //#switch(JSU.toString(name)) - case "date": return new JSDate(a, b, c, rest, nargs); - case "net.rpc.soap": return new SOAP(JSU.toString(a), "", JSU.toString(b), JSU.toString(c)); + //#switch(JSU.toString(method)) + case "date": return new JSDate(args); + case "net.rpc.soap": return new SOAP( + args.length < 1 ? null : JSU.toString(args[0]), + "", + args.length < 2 ? null : JSU.toString(args[1]), + args.length < 3 ? null : JSU.toString(args[2])); // FIXME support object dumping - case "log.debug": JS.debug(a== null ? "**null**" : JS.debugToString(a)); return null; - case "log.info": JS.info(a== null ? "**null**" : JS.debugToString(a)); return null; - case "log.warn": JS.warn(a== null ? "**null**" : JS.debugToString(a)); return null; - case "log.error": JS.error(a== null ? "**null**" : JS.debugToString(a)); return null; + case "log.debug": JSU.debug(args.length < 1 ? "**null**" : JSU.str(args[0])); return null; + case "log.info": JSU.info(args.length < 1 ? "**null**" : JSU.str(args[0])); return null; + case "log.warn": JSU.warn(args.length < 1 ? "**null**" : JSU.str(args[0])); return null; + case "log.error": JSU.error(args.length < 1 ? "**null**" : JSU.str(args[0])); return null; //#end - switch (nargs) { + switch (args.length) { case 0: - //#switch(JSU.toString(name)) + //#switch(JSU.toString(method)) case "thread.yield": sleep(0); return null; //#end break; case 1: - //#switch(JSU.toString(name)) + //#switch(JSU.toString(method)) case "clone": - if(a == null) throw new JSExn("can't clone the null value"); - return ((JS)a).jsclone(); - case "bless": return bless((JS)a); - case "ui.browser": Platform.newBrowserWindow(JSU.toString(a)); return null; - case "stream.unzip": return a == null ? null : new Stream.Zip((Stream)a); + if(args[0] == null) throw new JSExn("can't clone the null value"); + return new JS.Clone((JS)args[0]); + case "bless": return bless((JS)args[0]); + case "ui.browser": Platform.newBrowserWindow(JSU.toString(args[0])); return null; + case "stream.unzip": return args[0] == null ? null : new Stream.Zip((Stream)args[0]); //case "stream.uncab": return a == null ? null : new Stream.Cab(a); case "stream.cache": - try { return a == null ? null : new Stream.CachedStream((Stream)a, "resources", true); } + try { return args[0] == null ? null : new Stream.CachedStream((Stream)args[0], "resources", true); } catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); } case "stream.url": { - String url = JSU.toString(a); + String url = JSU.toString(args[0]); if (url.startsWith("http://")) return new Stream.HTTP(url); else if (url.startsWith("https://")) return new Stream.HTTP(url); - else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null); + else if (url.startsWith("data:")) return new Stream.ByteArray(Encode.fromBase64(url.substring(5)), null); else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null); else if (url.startsWith("file:")) { // FIXME @@ -188,47 +190,47 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { } throw new JSExn("invalid resource specifier " + url); } - case "thread.sleep": sleep(JS.toInt(a)); return null; - case "regexp": return new JSRegexp(a, null); - case "net.rpc.xml": return new XMLRPC(JSU.toString(a), ""); + case "thread.sleep": sleep(JSU.toInt(args[0])); return null; + case "regexp": return new JSRegexp(args[0], null); + case "net.rpc.xml": return new XMLRPC(JSU.toString(args[0]), ""); case "crypto.rsa": /* FEATURE */ return null; case "crypto.md5": /* FEATURE */ return null; case "crypto.sha1": /* FEATURE */ return null; case "crypto.rc4": /* FEATURE */ return null; case "stream.parse.html": throw new JSExn("not implemented yet"); //return null; - case "stream.parse.xml": if(a == null) return null; new XMLHelper(b).doParse(a); return null; + case "stream.parse.xml": if(args[0] == null) return null; new XMLHelper(args[1]).doParse(args[0]); return null; // FIXME backgrounding - case "stream.parse.utf8": if(a == null) return null; - try { return JSU.S(new String(InputStreamToByteArray.convert(a.getInputStream()))); } + case "stream.parse.utf8": if(args[0] == null) return null; + try { return JSU.S(new String(InputStreamToByteArray.convert(args[0].getInputStream()))); } catch (Exception e) { Log.warn(this, e); } //#end break; case 2: - //#switch(JSU.toString(name)) - case "stream.watch": return new Stream.ProgressWatcher((Stream)a, b); - case "regexp": return new JSRegexp(a, b); + //#switch(JSU.toString(method)) + case "stream.watch": return new Stream.ProgressWatcher((Stream)args[0], args[1]); + case "regexp": return new JSRegexp(args[0], args[1]); //#end case 3: - //#switch(JSU.toString(name)) - case "ui.font.height": return N(Font.getFont(a, JS.toInt(b)).textheight(JSU.toString(c))); + //#switch(JSU.toString(method)) + case "ui.font.height": return JSU.N(Font.getFont(args[0], JSU.toInt(args[1])).textheight(JSU.toString(args[3]))); case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented"); - case "ui.font.width": return N(Font.getFont(a, JS.toInt(b)).textwidth(JSU.toString(c))); + case "ui.font.width": return JSU.N(Font.getFont(args[0], JSU.toInt(args[1])).textwidth(JSU.toString(args[3]))); //#end break; } } catch (RuntimeException e) { // FIXME: maybe JSExn should take a second argument, Exception - Log.warn(this, "ibex."+name+"() threw: " + e); - throw new JSExn("invalid argument for ibex object method "+name+"()"); + Log.warn(this, "ibex."+method+"() threw: " + e); + throw new JSExn("invalid argument for ibex object method "+method+"()"); } - throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()"); + throw new JSExn("invalid number of arguments ("+args.length+") for ibex object method "+method+"()"); } public JS url2res(String url) throws JSExn { if (url.startsWith("http://")) return new Stream.HTTP(url); else if (url.startsWith("https://")) return new Stream.HTTP(url); - else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null); + else if (url.startsWith("data:")) return new Stream.ByteArray(Encode.fromBase64(url.substring(5)), null); else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null); else throw new JSExn("invalid resource specifier " + url); // FIXME support file:// via dialog boxes @@ -236,18 +238,18 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { public static void sleep(final int i) throws JSExn { try { - final JS.UnpauseCallback callback = JS.pause(); + final Pausable callback = JSU.pause(); // FEATURE use a single sleeper thread new Thread() { public void run() { try { Thread.sleep(i); } catch (InterruptedException e) { } Scheduler.add(callback); } }.start(); - } catch (JSU.NotPauseableException npe) { + } catch (Pausable.NotPausableException npe) { throw new JSExn("you cannot sleep or yield in the foreground thread"); } } - public static final JS ibexMath = new JS.Obj() { + public static final JS ibexMath = new JS.Immutable() { // FEATURE: find a cleaner way to do this private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope public JS get(JS key) throws JSExn { @@ -257,16 +259,16 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { case "NaN": return METHOD; case "Infinity": return METHOD; //#end - return MATH.get(key); + return JSU.MATH.get(key); } - public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn { - //#switch(JSU.toString(name)) - case "isNaN": return gs.callMethod(name,a,b,c,rest,nargs); - case "isFinite": return gs.callMethod(name,a,b,c,rest,nargs); - case "NaN": return gs.callMethod(name,a,b,c,rest,nargs); - case "Infinity": return gs.callMethod(name,a,b,c,rest,nargs); + public JS call(JS method, JS[] args) throws JSExn { + //#switch(JSU.toString(method)) + case "isNaN": return gs.call(method, args); + case "isFinite": return gs.call(method, args); + case "NaN": return gs.call(method, args); + case "Infinity": return gs.call(method, args); //#end - return MATH.callMethod(name,a,b,c,rest,nargs); + return JSU.MATH.call(method, args); } }; @@ -286,19 +288,19 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { //#end return super.get(key); } - public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn { - //#switch(JSU.toString(name)) - case "parseInt": return gs.callMethod(name,a,b,c,rest,nargs); - case "parseFloat": return gs.callMethod(name,a,b,c,rest,nargs); - case "decodeURI": return gs.callMethod(name,a,b,c,rest,nargs); - case "decodeURIComponent": return gs.callMethod(name,a,b,c,rest,nargs); - case "encodeURI": return gs.callMethod(name,a,b,c,rest,nargs); - case "encodeURIComponent": return gs.callMethod(name,a,b,c,rest,nargs); - case "escape": return gs.callMethod(name,a,b,c,rest,nargs); - case "unescape": return gs.callMethod(name,a,b,c,rest,nargs); - case "fromCharCode": return gs.callMethod(name,a,b,c,rest,nargs); + public JS callMethod(JS method, JS[] args) throws JSExn { + //#switch(JSU.toString(method)) + case "parseInt": return gs.call(method, args); + case "parseFloat": return gs.call(method, args); + case "decodeURI": return gs.call(method, args); + case "decodeURIComponent": return gs.call(method, args); + case "encodeURI": return gs.call(method, args); + case "encodeURIComponent": return gs.call(method, args); + case "escape": return gs.call(method, args); + case "unescape": return gs.call(method, args); + case "fromCharCode": return gs.call(method, args); //#end - return super.callMethod(name,a,b,c,rest,nargs); + return super.call(method, args); } }; @@ -306,31 +308,41 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { private class Wrapper extends XML.Exn { public JSExn wrapee; public Wrapper(JSExn jse) { super(""); wrapee = jse; } } private JS characters, whitespace, endElement, startElement; public XMLHelper(JS b) throws JSExn { - super(BUFFER_SIZE); + super(BUFFER_SIZE, true); startElement = b.getAndTriggerTraps(JSU.S("startElement")); endElement = b.getAndTriggerTraps(JSU.S("endElement")); characters = b.getAndTriggerTraps(JSU.S("characters")); whitespace = b.getAndTriggerTraps(JSU.S("whitespace")); } - public void startElement(XML.Element c) throws XML.Exn { try { - JS attrs = new JS.O(); + private final JS[] callargs1= new JS[1], callargs2= new JS[2], callargs3= new JS[3]; + public void startElement(Tree.Element c) throws XML.Exn { try { + Tree.Attributes a = c.getAttributes(); + JS attrs = new JS.Obj(); // FIXME attribute URIs? add an additional hash? - for(int i=0; i