move to JS interface
[org.ibex.js.git] / src / org / ibex / js / JSRegexp.java
index 617e99f..a987921 100644 (file)
@@ -5,7 +5,9 @@
 package org.ibex.js;
 
 /** A JavaScript regular expression object */
-public class JSRegexp extends JS {
+public class JSRegexp extends JS.Immutable {
+    private static final JS.Method METHOD = new JS.Method();
+
     private boolean global;
     private GnuRegexp.RE re;
     private int lastIndex;
@@ -19,8 +21,8 @@ public class JSRegexp extends JS {
             this.global = r.global;
             this.re = r.re;
             this.lastIndex = r.lastIndex;
-            this.pattern = pattern;
-            this.flags = flags;
+            this.pattern = r.pattern;
+            this.flags = r.flags;
         } else {
             String pattern = JS.toString(arg0);
             String sFlags = null;
@@ -41,10 +43,10 @@ public class JSRegexp extends JS {
         }
     }
 
-    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        switch(nargs) {
+    public JS call(JS method, JS[] args) throws JSExn {
+        switch(args.length) {
             case 1: {
-                //#switch(JS.toString(method))
+                //#switch(JS.str(method))
                 case "exec": {
                     String s = JS.toString(a0);
                     int start = global ? lastIndex : 0;
@@ -62,32 +64,37 @@ public class JSRegexp extends JS {
                     lastIndex = match != null ? s.length() : match.getEndIndex();
                     return B(match != null);
                 }
-                case "toString": return JS.S(a0.coerceToString());
-                case "stringMatch": return stringMatch(a0,a1);
-                case "stringSearch": return stringSearch(a0,a1);
+                case "toString": return JS.S(args[0].coerceToString());
                 //#end
                 break;
             }
             case 2: {
-                //#switch(JS.toString(method))
-                case "stringReplace": return stringReplace(a0, a1,a2);
+                //#switch(JS.str(method))
+                case "stringMatch": return stringMatch(args[0], args[1]);
+                case "stringSearch": return stringSearch(args[0], args[1]);
+                //#end
+                break;
+            }
+            case 3: {
+                //#switch(JS.str(method))
+                case "stringReplace": return stringReplace(args[0], args[1], args[2]);
                 //#end
                 break;
             }
         }
-        return super.callMethod(method, a0, a1, a2, rest, nargs);
+        return super.call(method, args);
     }
     
     public JS get(JS key) throws JSExn {
-        //#switch(JS.toString(key))
+        //#switch(JS.str(key))
         case "exec": return METHOD;
         case "test": return METHOD;
         case "toString": return METHOD;
-        case "lastIndex": return N(lastIndex);
+        case "lastIndex": return JS.N(lastIndex);
         case "source": return pattern;
         case "global": return JS.B(global);
-        case "ignoreCase": return B(flags & GnuRegexp.RE.REG_ICASE);
-        case "multiline": return B(flags & GnuRegexp.RE.REG_MULTILINE);
+        case "ignoreCase": return JS.B(flags & GnuRegexp.RE.REG_ICASE);
+        case "multiline": return JS.B(flags & GnuRegexp.RE.REG_MULTILINE);
         //#end
         return super.get(key);
     }
@@ -104,11 +111,11 @@ public class JSRegexp extends JS {
   
     private static JS matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
         try {
-            JS ret = new JS.O();
-            ret.put(JS.S("index"), N(match.getStartIndex()));
-            ret.put(JS.S("input"),JS.S(s));
+            JS ret = new JS.Obj();
+            ret.put(JS.S("index"), JS.N(match.getStartIndex()));
+            ret.put(JS.S("input"), JS.S(s));
             int n = re.getNumSubs();
-            ret.put(JS.S("length"), N(n+1));
+            ret.put(JS.S("length"), JS.N(n+1));
             ret.put(ZERO,JS.S(match.toString()));
             for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
             return ret;
@@ -117,7 +124,7 @@ public class JSRegexp extends JS {
         }
     }
     
-    String coerceToString() {
+    public String coerceToString() {
         StringBuffer sb = new StringBuffer();
         sb.append('/');
         sb.append(pattern);
@@ -128,6 +135,7 @@ public class JSRegexp extends JS {
         return sb.toString();
     }
     
+    private static final JS[] execarg = new JS[1];
     static JS stringMatch(JS o, JS arg0) throws JSExn {
         String s = JS.toString(o);
         GnuRegexp.RE re;
@@ -143,11 +151,14 @@ public class JSRegexp extends JS {
             GnuRegexp.REMatch match = re.getMatch(s);
             return matchToExecResult(match,re,s);
         }
-        if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
-        
-        JSArray ret = new JSArray();
+        try {
+            execarg[0] = o;
+            if(!regexp.global) return regexp.call(JS.S("exec"), execarg);
+        } finally { execarg[0] = null; }
+
         GnuRegexp.REMatch[] matches = re.getAllMatches(s);
-        for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
+        JSArray ret = new JSArray(matches.length);
+        for(int i=0;i<matches.length;i++) ret.add(JS.S(matches[i].toString()));
         regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
         return ret;
     }
@@ -156,7 +167,7 @@ public class JSRegexp extends JS {
         String s = JS.toString(o);
         GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
         GnuRegexp.REMatch match = re.getMatch(s);
-        return match == null ? N(-1) : N(match.getStartIndex());
+        return match == null ? JS.N(-1) : JS.N(match.getStartIndex());
     }
     
     static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
@@ -202,32 +213,16 @@ public class JSRegexp extends JS {
             if(replaceFunc != null) {
                 int n = (regexp == null ? 0 : re.getNumSubs());
                 int numArgs = 3 + n;
-                JS[] rest = new JS[numArgs - 3];
-                JS a0 = JS.S(match.toString());
-                JS a1 = null;
-                JS a2 = null;
-                for(int j=1;j<=n;j++)
-                    switch(j) {
-                        case 1: a1 = JS.S(match.toString(j)); break;
-                        case 2: a2 = JS.S(match.toString(j)); break;
-                        default: rest[j - 3] = JS.S(match.toString(j)); break;
-                    }
-                switch(numArgs) {
-                    case 3:
-                        a1 = N(match.getStartIndex());
-                        a2 = JS.S(s);
-                        break;
-                    case 4:
-                        a2 = N(match.getStartIndex());
-                        rest[0] = JS.S(s);
-                        break;
-                    default:
-                        rest[rest.length - 2] = N(match.getStartIndex());
-                        rest[rest.length - 1] = JS.S(s);
-                }
+                JS[] args = new JS[3 + n];
+                args[0] = JS.S(match.toString());
+                args[1] = null;
+                args[2] = null;
+                for(int j=1;j<=n;j++) args[j] = JS.S(match.toString(j));
+                args[args.length - 2] = JS.N(match.getStartIndex());
+                args[args.length - 1] = JS.S(s);
 
                 // note: can't perform pausing operations in here
-                sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
+                sb.append(JS.toString(replaceFunc.call(args)));
 
             } else {
                 sb.append(mySubstitute(match,replaceString,s));
@@ -284,7 +279,7 @@ public class JSRegexp extends JS {
         String s = JS.toString(s_);
         int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
         if(limit < 0) limit = Integer.MAX_VALUE;
-        if(limit == 0) return new JSArray();
+        if(limit == 0) return new JSArray(0);
         
         GnuRegexp.RE re = null;
         JSRegexp regexp = null;
@@ -341,5 +336,4 @@ public class JSRegexp extends JS {
             throw new JSExn(e.toString());
         }
     }
-
 }