- // Operations on Primitives //////////////////////////////////////////////////////////////////////
-
- // FIXME: Move these into JSString and JSNumber
- /*static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) throws JSExn {
- if (method == null || !(method instanceof String) || "".equals(method))
- throw new JSExn("attempt to call a non-existant method on a primitive");
-
- if (o instanceof Number) {
- //#switch(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");
- case "toString": {
- int radix = alength >= 1 ? JS.toInt(arg0) : 10;
- return Long.toString(((Number)o).longValue(),radix);
- }
- //#end
- } else if (o instanceof Boolean) {
- // No methods for Booleans
- throw new JSExn("attempt to call a method on a Boolean");
- }
-
- String s = JS.toString(o);
- int slength = s.length();
- //#switch(method)
- case "substring": {
- int a = alength >= 1 ? JS.toInt(arg0) : 0;
- int b = alength >= 2 ? JS.toInt(arg1) : slength;
- if (a > slength) a = slength;
- if (b > slength) b = slength;
- if (a < 0) a = 0;
- if (b < 0) b = 0;
- if (a > b) { int tmp = a; a = b; b = tmp; }
- return s.substring(a,b);
- }
- case "substr": {
- int start = alength >= 1 ? JS.toInt(arg0) : 0;
- int len = alength >= 2 ? JS.toInt(arg1) : Integer.MAX_VALUE;
- if (start < 0) start = slength + start;
- if (start < 0) start = 0;
- if (len < 0) len = 0;
- if (len > slength - start) len = slength - start;
- if (len <= 0) return "";
- return s.substring(start,start+len);
- }
- case "charAt": {
- int p = alength >= 1 ? JS.toInt(arg0) : 0;
- if (p < 0 || p >= slength) return "";
- return s.substring(p,p+1);
- }
- case "charCodeAt": {
- int p = alength >= 1 ? JS.toInt(arg0) : 0;
- if (p < 0 || p >= slength) return JS.N(Double.NaN);
- return JS.N(s.charAt(p));
- }
- case "concat": {
- StringBuffer sb = new StringBuffer(slength*2).append(s);
- for(int i=0;i<alength;i++) sb.append(i==0?arg0:i==1?arg1:i==2?arg2:rest[i-3]);
- return sb.toString();
- }
- case "indexOf": {
- String search = alength >= 1 ? JS.toString(arg0) : "null";
- int start = alength >= 2 ? JS.toInt(arg1) : 0;
- // Java's indexOf handles an out of bounds start index, it'll return -1
- return JS.N(s.indexOf(search,start));
- }
- case "lastIndexOf": {
- String search = alength >= 1 ? JS.toString(arg0) : "null";
- int start = alength >= 2 ? JS.toInt(arg1) : 0;
- // Java's indexOf handles an out of bounds start index, it'll return -1
- return JS.N(s.lastIndexOf(search,start));
- }
- case "match": return JSRegexp.stringMatch(s,arg0);
- case "replace": return JSRegexp.stringReplace(s,arg0,arg1);
- case "search": return JSRegexp.stringSearch(s,arg0);
- case "split": return JSRegexp.stringSplit(s,arg0,arg1,alength);
- case "toLowerCase": return s.toLowerCase();
- case "toUpperCase": return s.toUpperCase();
- case "toString": return s;
- case "slice": {
- int a = alength >= 1 ? JS.toInt(arg0) : 0;
- int b = alength >= 2 ? JS.toInt(arg1) : slength;
- if (a < 0) a = slength + a;
- if (b < 0) b = slength + b;
- if (a < 0) a = 0;
- if (b < 0) b = 0;
- if (a > slength) a = slength;
- if (b > slength) b = slength;
- if (a > b) return "";
- return s.substring(a,b);
- }
- //#end
- throw new JSExn("Attempted to call non-existent method: " + method);
- }
-
- static Object getFromPrimitive(Object o, Object key) throws JSExn {
- boolean returnJS = false;
- if (o instanceof Boolean) {
- throw new JSExn("Booleans do not have properties");
- } else if (o instanceof Number) {
- if (key.equals("toPrecision") || key.equals("toExponential") || key.equals("toFixed"))
- returnJS = true;
- }
- if (!returnJS) {
- // the string stuff applies to everything
- String s = JS.toString(o);
-
- // this is sort of ugly, but this list should never change
- // These should provide a complete (enough) implementation of the ECMA-262 String object
-
- //#switch(key)
- case "length": return JS.N(s.length());
- case "substring": returnJS = true; break;
- case "charAt": returnJS = true; break;
- case "charCodeAt": returnJS = true; break;
- case "concat": returnJS = true; break;
- case "indexOf": returnJS = true; break;
- case "lastIndexOf": returnJS = true; break;
- case "match": returnJS = true; break;
- case "replace": returnJS = true; break;
- case "search": returnJS = true; break;
- case "slice": returnJS = true; break;
- case "split": returnJS = true; break;
- case "toLowerCase": returnJS = true; break;
- case "toUpperCase": returnJS = true; break;
- case "toString": returnJS = true; break;
- case "substr": returnJS = true; break;
- //#end
- }
- if (returnJS) {
- final Object target = o;
- final String method = JS.toString(o);
- return new JS() {
- 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);
- }
- };
- }
- return null;
- }*/
-
- private static class Stub extends JS {