- }
- }
-
-
- // Helpers for Number, String, and Boolean ////////////////////////////////////////
-
- private static Object getFromString(final String o, final Object v) {
- if (v.equals("length")) return new Integer(((String)o).length());
- else if (v.equals("substring")) return new JS.Callable() {
- public Object call(JS.Array args) {
- if (args.length() == 1) return ((String)o).substring(JS.toNumber(args.elementAt(0)).intValue());
- else if (args.length() == 2) return ((String)o).substring(JS.toNumber(args.elementAt(0)).intValue(),
- JS.toNumber(args.elementAt(1)).intValue());
- else throw new JS.Exn("String.substring() can only take one or two arguments");
- }
- };
- else if (v.equals("toLowerCase")) return new JS.Callable() {
- public Object call(JS.Array args) {
- return ((String)o).toLowerCase();
- } };
- else if (v.equals("toUpperCase")) return new JS.Callable() {
- public Object call(JS.Array args) {
- return ((String)o).toString().toUpperCase();
- } };
- else if (v.equals("charAt")) return new JS.Callable() {
- public Object call(JS.Array args) {
- return ((String)o).charAt(JS.toNumber(args.elementAt(0)).intValue()) + "";
- } };
- else if (v.equals("lastIndexOf")) return new JS.Callable() {
- public Object call(JS.Array args) {
- if (args.length() != 1) return null;
- return new Integer(((String)o).lastIndexOf(args.elementAt(0).toString()));
- } };
- else if (v.equals("indexOf")) return new JS.Callable() {
- public Object call(JS.Array args) {
- if (args.length() != 1) return null;
- return new Integer(((String)o).indexOf(args.elementAt(0).toString()));
- } };
- // This is just a quick and dirty split implementation. its could be optimized a lot and it doesn't
- // do regexps yet. this will need to be rewritten when we get regexp support anyway
- else if (v.equals("split")) return new JS.Callable() {
- public Object call(JS.Array args) {
- String sep;
- int limit, s,p,matches=0;
- int seplen;
- JS.Array ret;
-
- if(args.length() > 1) limit = JS.toNumber(args.elementAt(1)).intValue();
- else limit = 0;
- if(args.length() > 0) sep = args.elementAt(0).toString();
- else sep = null;
-
- seplen = sep!=null ? sep.length() : 0;
-
- // special case sep == null, split up chars
- if(seplen == 0) {
- ret = new JS.Array(o.length());
- for(int i=0;i<o.length();i++) ret.setElementAt(o.substring(i,i+1),i);
- return ret;