From: brian Date: Wed, 7 Jul 2004 08:09:29 +0000 (+0000) Subject: jswitch X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=commitdiff_plain;h=85c8f402be33df8440511492fb597fba9c2eb45f jswitch darcs-hash:20040707080929-24bed-a4aa577e662c83ab732f93fb81f7be3c583fbd98.gz --- diff --git a/src/org/ibex/js/Interpreter.java b/src/org/ibex/js/Interpreter.java index 6f163fa..3b4dd28 100644 --- a/src/org/ibex/js/Interpreter.java +++ b/src/org/ibex/js/Interpreter.java @@ -614,13 +614,11 @@ class Interpreter implements ByteCodes, Tokens { super(parent); this.t = t; } public JS get(JS key) throws JSExn { - if(JS.isString(key)) { - //#switch(JS.toString(key)) - case "trapee": return t.target; - case "callee": return t.f; - case "trapname": return t.key; - //#end - } + //#jswitch(key) + case "trapee": return t.target; + case "callee": return t.f; + case "trapname": return t.key; + //#end return super.get(key); } } @@ -651,7 +649,7 @@ class Interpreter implements ByteCodes, Tokens { default: return n>= 0 && n < nargs ? rest[n-3] : null; } } - //#switch(JS.toString(key)) + //#jswitch(key) case "callee": return callee; case "length": return JS.N(nargs); //#end diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index ad13a40..65ba3f6 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -74,13 +74,7 @@ public abstract class JS { public final void replaceNode(int index, Object o) { bt().replaceNode(index,o); } public final int indexNode(Object o) { return bt().indexNode(o); } } - - // FEATURE: JS.StringKeys - /* public static StringKeys extends JS { - public JS get(JS key) { return JS.isString(key) ? get(JS.toString(key) : null; } - ... - */ - + JS _unclone() { return this; } public interface Cloneable { } @@ -120,7 +114,7 @@ public abstract class JS { public final JS nextElement() throws JSExn { return !done ? _nextElement() : parent != null ? parent.nextElement() : null; } public JS get(JS key) throws JSExn { - //#switch(JS.toString(key)) + //#jswitch(key) case "hasMoreElements": return B(hasMoreElements()); case "nextElement": return nextElement(); //#end diff --git a/src/org/ibex/js/JSArray.java b/src/org/ibex/js/JSArray.java index 8aa5325..d0cd550 100644 --- a/src/org/ibex/js/JSArray.java +++ b/src/org/ibex/js/JSArray.java @@ -24,40 +24,38 @@ public class JSArray extends JS.BT { }*/ public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - if(isString(method)) { - //#switch(JS.toString(method)) - case "pop": { - int oldSize = size(); - if(oldSize == 0) return null; - return removeElementAt(oldSize-1); - } - case "reverse": return reverse(); - case "toString": return join(","); - case "shift": - if(length() == 0) return null; - return removeElementAt(0); - case "join": - return join(nargs == 0 ? "," : JS.toString(a0)); - case "sort": - return sort(nargs < 1 ? null : a0); - case "slice": - int start = toInt(nargs < 1 ? null : a0); - int end = nargs < 2 ? length() : toInt(a1); - return slice(start, end); - case "push": { - int oldSize = size(); - for(int i=0; i= size()) return null; return elementAt(i); } - if(isString(key)) { - //#switch(JS.toString(key)) - case "pop": return METHOD; - case "reverse": return METHOD; - case "toString": return METHOD; - case "shift": return METHOD; - case "join": return METHOD; - case "sort": return METHOD; - case "slice": return METHOD; - case "push": return METHOD; - case "unshift": return METHOD; - case "splice": return METHOD; - case "length": return N(size()); - //#end - } + //#jswitch(key) + case "pop": return METHOD; + case "reverse": return METHOD; + case "toString": return METHOD; + case "shift": return METHOD; + case "join": return METHOD; + case "sort": return METHOD; + case "slice": return METHOD; + case "push": return METHOD; + case "unshift": return METHOD; + case "splice": return METHOD; + case "length": return N(size()); + //#end return super.get(key); } diff --git a/src/org/ibex/js/JSDate.java b/src/org/ibex/js/JSDate.java index 600b59d..144e868 100644 --- a/src/org/ibex/js/JSDate.java +++ b/src/org/ibex/js/JSDate.java @@ -57,12 +57,10 @@ public class JSDate extends JS { String coerceToString() { return date_format(date, FORMATSPEC_FULL); } - public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs); - String method = JS.toString(method_); + public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { switch(nargs) { case 0: { - //#switch(method) + //#jswitch(method) case "toString": return JS.S(date_format(date, FORMATSPEC_FULL)); case "toTimeString": return JS.S(date_format(date, FORMATSPEC_TIME)); case "toDateString": return JS.S(date_format(date, FORMATSPEC_DATE)); @@ -91,10 +89,10 @@ public class JSDate extends JS { case "getUTCMilliseconds": return N(msFromTime(date)); case "getTimezoneOffset": return N(getTimezoneOffset(date)); //#end - return super.callMethod(method_, a0, a1, a2, rest, nargs); + return super.callMethod(method, a0, a1, a2, rest, nargs); } case 1: { - //#switch(method) + //#jswitch(method) case "setTime": return N(this.setTime(toDouble(a0))); case "setYear": return N(this.setYear(toDouble(a0))); //#end @@ -103,7 +101,7 @@ public class JSDate extends JS { default: { JS[] args = new JS[nargs]; for(int i=0; i= 1 ? JS.toInt(arg0) : 0; int b = alength >= 2 ? JS.toInt(arg1) : slength; @@ -82,13 +79,11 @@ class JSPrimitive extends JS { return JS.S(s.substring(a,b)); } //#end - return super.callMethod(method_,arg0,arg1,arg2,rest,alength); + return super.callMethod(method,arg0,arg1,arg2,rest,alength); } - public JS get(JS key_) throws JSExn { - if(!JS.isString(key_)) return super.get(key_); - String key = JS.toString(key_); - //#switch(key) + public JS get(JS key) throws JSExn { + //#jswitch(key) case "length": return JS.N(JS.toString(this).length()); case "substring": return METHOD; case "charAt": return METHOD; @@ -109,6 +104,6 @@ class JSPrimitive extends JS { case "toExponential": return METHOD; case "toFixed": return METHOD; //#end - return super.get(key_); + return super.get(key); } } diff --git a/src/org/ibex/js/JSRegexp.java b/src/org/ibex/js/JSRegexp.java index e083902..00a6f40 100644 --- a/src/org/ibex/js/JSRegexp.java +++ b/src/org/ibex/js/JSRegexp.java @@ -40,12 +40,10 @@ public class JSRegexp extends JS { } } - public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs); - String method = JS.toString(method_); + public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { switch(nargs) { case 1: { - //#switch(method) + //#jswitch(method) case "exec": { String s = JS.toString(a0); int start = global ? lastIndex : 0; @@ -70,19 +68,17 @@ public class JSRegexp extends JS { break; } case 2: { - //#switch(method) + //#jswitch(method) case "stringReplace": return stringReplace(a0, a1,a2); //#end break; } } - return super.callMethod(method_, a0, a1, a2, rest, nargs); + return super.callMethod(method, a0, a1, a2, rest, nargs); } - public JS get(JS key_) throws JSExn { - if(!JS.isString(key_)) return super.get(key_); - String key = JS.toString(key_); - //#switch(key) + public JS get(JS key) throws JSExn { + //#jswitch(key) case "exec": return METHOD; case "test": return METHOD; case "toString": return METHOD; @@ -92,7 +88,7 @@ public class JSRegexp extends JS { case "ignoreCase": return B(flags & RE.REG_ICASE); case "multiline": return B(flags & RE.REG_MULTILINE); //#end - return super.get(key_); + return super.get(key); } public void put(JS key, JS value) throws JSExn { diff --git a/src/org/ibex/js/JSScope.java b/src/org/ibex/js/JSScope.java index f7d3d6d..5036220 100644 --- a/src/org/ibex/js/JSScope.java +++ b/src/org/ibex/js/JSScope.java @@ -50,8 +50,7 @@ public class JSScope extends JS.BT { } public JS _get(JS key) throws JSExn { - if(!JS.isString(key)) return super.get(key); - //#switch(JS.toString(key)) + //#jswitch(key) case "NaN": return NaN; case "Infinity": return POSITIVE_INFINITY; case "undefined": return null; @@ -71,8 +70,7 @@ public class JSScope extends JS.BT { } public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs); - //#switch(JS.toString(method)) + //#jswitch(method) case "parseInt": return parseInt(a0, N(0)); case "parseFloat": return parseFloat(a0); case "isNaN": { double d = toDouble(a0); return d == d ? F : T; } diff --git a/src/org/ibex/util/Preprocessor.java b/src/org/ibex/util/Preprocessor.java index ae66770..4698fdc 100644 --- a/src/org/ibex/util/Preprocessor.java +++ b/src/org/ibex/util/Preprocessor.java @@ -171,7 +171,8 @@ PROCESS: sinceLastRepeat = null; replace = save; - } else if (trimmed.startsWith("//#switch")) { + } else if (trimmed.startsWith("//#switch") || trimmed.startsWith("//#jswitch")) { + boolean jswitch = trimmed.startsWith("//#jswitch"); int expStart = trimmed.indexOf('(') +1; if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; } int expEnd = trimmed.lastIndexOf(')'); @@ -179,7 +180,13 @@ PROCESS: if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; } String expr = trimmed.substring(expStart, expEnd); - out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+"); "); + if(jswitch) { + out.print("final org.ibex.js.JS ccSwitchExpr"+enumSwitch+" = " + expr + ";"); + out.print("if(org.ibex.js.JS.isString(ccSwitchExpr"+enumSwitch+")) {"); + out.print("final String ccSwitch"+enumSwitch+" = org.ibex.js.JS.toString(ccSwitchExpr"+enumSwitch+");"); + } else { + out.print("final String ccSwitch"+enumSwitch+" = "+expr+"; "); + } out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n"); Hashtable[] byLength = new Hashtable[255]; @@ -225,6 +232,7 @@ PROCESS: out.print("} "); /* switch */ if (Default != null) out.print(" " + Default); out.print(" } while(false);\n"); /* OUTER */ + if(jswitch) out.print("}"); enumSwitch++; } else {