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);
}
}
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
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 { }
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
}*/
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<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],oldSize+i);
- return N(oldSize + nargs);
- }
- case "unshift":
- for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],i);
- return N(size());
- case "splice":
- JSArray array = new JSArray();
- for(int i=0; i<nargs; i++) array.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
- return splice(array);
- //#end
+ //#jswitch(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<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],oldSize+i);
+ return N(oldSize + nargs);
+ }
+ case "unshift":
+ for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],i);
+ return N(size());
+ case "splice":
+ JSArray array = new JSArray();
+ for(int i=0; i<nargs; i++) array.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
+ return splice(array);
+ //#end
return super.callMethod(method, a0, a1, a2, rest, nargs);
}
if (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);
}
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));
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
default: {
JS[] args = new JS[nargs];
for(int i=0; i<nargs; i++) args[i] = i==0 ? a0 : i==1 ? a1 : i==2 ? a2 : rest[i-3];
- //#switch(method)
+ //#jswitch(method)
case "setMilliseconds": return N(this.makeTime(args, 1, true));
case "setUTCMilliseconds": return N(this.makeTime(args, 1, false));
case "setSeconds": return N(this.makeTime(args, 2, true));
//#end
}
}
- 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 "toString": return METHOD;
case "toTimeString": return METHOD;
case "toDateString": return METHOD;
case "setFullYear": return METHOD;
case "setUTCFullYear": return METHOD;
//#end
- return super.get(key_);
+ return super.get(key);
}
/* ECMA helper functions */
private static final JS SQRT1_2 = JS.N(1/java.lang.Math.sqrt(2));
private static final JS SQRT2 = JS.N(java.lang.Math.sqrt(2));
- 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 "random": return JS.N(java.lang.Math.random());
//#end
break;
}
case 1: {
- //#switch(method)
+ //#jswitch(method)
case "ceil": return JS.N((long)java.lang.Math.ceil(toDouble(a0)));
case "floor": return JS.N((long)java.lang.Math.floor(toDouble(a0)));
case "round": return JS.N((long)java.lang.Math.round(toDouble(a0)));
break;
}
case 2: {
- //#switch(method)
+ //#jswitch(method)
case "min": return JS.N(java.lang.Math.min(toDouble(a0), toDouble(a1)));
case "max": return JS.N(java.lang.Math.max(toDouble(a0), toDouble(a1)));
case "pow": return JS.N(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
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 "E": return E;
case "LN10": return LN10;
case "LN2": return LN2;
case "log": return METHOD;
case "random": return METHOD;
//#end
- return super.get(key_);
+ return super.get(key);
}
}
package org.ibex.js;
class JSPrimitive extends JS {
- public JS callMethod(JS method_, JS arg0, JS arg1, JS arg2, JS[] rest, int alength) throws JSExn {
- if(!JS.isString(method_)) return super.callMethod(method_,arg0,arg1,arg2,rest,alength);
- String method = JS.toString(method_);
-
- //#switch(method)
+ public JS callMethod(JS method, JS arg0, JS arg1, JS arg2, JS[] rest, int alength) throws JSExn {
+ //#jswitch(method)
case "toFixed": throw new JSExn("toFixed() not implemented");
case "toExponential": throw new JSExn("toExponential() not implemented");
case "toPrecision": throw new JSExn("toPrecision() not implemented");
String s = coerceToString();
int slength = s.length();
- //#switch(method)
+ //#jswitch(method)
case "substring": {
int a = alength >= 1 ? JS.toInt(arg0) : 0;
int b = alength >= 2 ? JS.toInt(arg1) : slength;
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;
case "toExponential": return METHOD;
case "toFixed": return METHOD;
//#end
- return super.get(key_);
+ return super.get(key);
}
}
}
}
- 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;
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;
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 {
}
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;
}
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; }
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(')');
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];
out.print("} "); /* switch */
if (Default != null) out.print(" " + Default);
out.print(" } while(false);\n"); /* OUTER */
+ if(jswitch) out.print("}");
enumSwitch++;
} else {