X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fjs%2FJSPrimitive.java;h=e011453869dddf1ec0190775a025c0976ef6a6d0;hb=ae38bda49e6a8953a1443aab777c98d18adb5328;hp=575b60b18823a6e917bfd338059316cc80abb300;hpb=9ed8d93bbb0b40791a4ce31a9a8d79d24a379eb9;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSPrimitive.java b/src/org/ibex/js/JSPrimitive.java index 575b60b..e011453 100644 --- a/src/org/ibex/js/JSPrimitive.java +++ b/src/org/ibex/js/JSPrimitive.java @@ -1,90 +1,93 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + 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 { - //#switch(JS.toString(method)) +class JSPrimitive extends JS.Immutable { + private static final JS.Method METHOD = new JS.Method(); + + public JS call(JS method, JS[] args) throws JSExn { + String s = coerceToString(); + int slength = s.length(); + + //#switch(JSU.str(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": return this instanceof JSString ? this : JS.S(JS.toString(this)); - //#end - - String s = coerceToString(); - int slength = s.length(); - - //#switch(JS.toString(method)) + case "toString": return this instanceof JSString ? this : JSU.S(JSU.toString(this)); case "substring": { - int a = alength >= 1 ? JS.toInt(arg0) : 0; - int b = alength >= 2 ? JS.toInt(arg1) : slength; + int a = args.length >= 1 ? JSU.toInt(args[0]) : 0; + int b = args.length >= 2 ? JSU.toInt(args[1]) : 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 JS.S(s.substring(a,b)); + return JSU.S(s.substring(a,b)); } case "substr": { - int start = alength >= 1 ? JS.toInt(arg0) : 0; - int len = alength >= 2 ? JS.toInt(arg1) : Integer.MAX_VALUE; + int start = args.length >= 1 ? JSU.toInt(args[0]) : 0; + int len = args.length >= 2 ? JSU.toInt(args[1]) : 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 JS.S(""); - return JS.S(s.substring(start,start+len)); + if (len <= 0) return JSU.S(""); + return JSU.S(s.substring(start,start+len)); } case "charAt": { - int p = alength >= 1 ? JS.toInt(arg0) : 0; - if (p < 0 || p >= slength) return JS.S(""); - return JS.S(s.substring(p,p+1)); + int p = args.length >= 1 ? JSU.toInt(args[0]) : 0; + if (p < 0 || p >= slength) return JSU.S(""); + return JSU.S(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)); + int p = args.length >= 1 ? JSU.toInt(args[0]) : 0; + if (p < 0 || p >= slength) return JSU.N(Double.NaN); + return JSU.N(s.charAt(p)); } case "concat": { StringBuffer sb = new StringBuffer(slength*2).append(s); - for(int i=0;i= 1 ? JS.toString(arg0) : "null"; - int start = alength >= 2 ? JS.toInt(arg1) : 0; + String search = args.length >= 1 ? JSU.toString(args[0]) : "null"; + int start = args.length >= 2 ? JSU.toInt(args[1]) : 0; // Java's indexOf handles an out of bounds start index, it'll return -1 - return JS.N(s.indexOf(search,start)); + return JSU.N(s.indexOf(search,start)); } case "lastIndexOf": { - String search = alength >= 1 ? JS.toString(arg0) : "null"; - int start = alength >= 2 ? JS.toInt(arg1) : 0; + String search = args.length >= 1 ? JSU.toString(args[0]) : "null"; + int start = args.length >= 2 ? JSU.toInt(args[1]) : 0; // Java's indexOf handles an out of bounds start index, it'll return -1 - return JS.N(s.lastIndexOf(search,start)); + return JSU.N(s.lastIndexOf(search,start)); } - case "match": return JSRegexp.stringMatch(this,arg0); - case "replace": return JSRegexp.stringReplace(this,arg0,arg1); - case "search": return JSRegexp.stringSearch(this,arg0); - case "split": return JSRegexp.stringSplit(this,arg0,arg1,alength); - case "toLowerCase": return JS.S(s.toLowerCase()); - case "toUpperCase": return JS.S(s.toUpperCase()); + case "match": return JSRegexp.stringMatch(this,args[0]); + case "replace": return JSRegexp.stringReplace(this,args[0],args[1]); + case "search": return JSRegexp.stringSearch(this,args[0]); + case "split": return JSRegexp.stringSplit(this,args[0],args[1],args.length); + case "toLowerCase": return JSU.S(s.toLowerCase()); + case "toUpperCase": return JSU.S(s.toUpperCase()); case "slice": { - int a = alength >= 1 ? JS.toInt(arg0) : 0; - int b = alength >= 2 ? JS.toInt(arg1) : slength; + int a = args.length >= 1 ? JSU.toInt(args[0]) : 0; + int b = args.length >= 2 ? JSU.toInt(args[1]) : 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 JS.S(""); - return JS.S(s.substring(a,b)); + if (a > b) return JSU.S(""); + return JSU.S(s.substring(a,b)); } //#end - return super.callMethod(method,arg0,arg1,arg2,rest,alength); + return super.call(method, args); } public JS get(JS key) throws JSExn { - //#switch(JS.toString(key)) - case "length": return JS.N(JS.toString(this).length()); + //#switch(JSU.toString(key)) + case "length": return JSU.N(JSU.toString(this).length()); case "substring": return METHOD; case "charAt": return METHOD; case "charCodeAt": return METHOD;