2002/03/21 01:19:33
[org.ibex.core.git] / src / org / mozilla / javascript / regexp / NativeRegExpCtor.java
diff --git a/src/org/mozilla/javascript/regexp/NativeRegExpCtor.java b/src/org/mozilla/javascript/regexp/NativeRegExpCtor.java
new file mode 100644 (file)
index 0000000..3b8e7c0
--- /dev/null
@@ -0,0 +1,271 @@
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
+ *\r
+ * The contents of this file are subject to the Netscape Public\r
+ * License Version 1.1 (the "License"); you may not use this file\r
+ * except in compliance with the License. You may obtain a copy of\r
+ * the License at http://www.mozilla.org/NPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS\r
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
+ * implied. See the License for the specific language governing\r
+ * rights and limitations under the License.\r
+ *\r
+ * The Original Code is Rhino code, released\r
+ * May 6, 1998.\r
+ *\r
+ * The Initial Developer of the Original Code is Netscape\r
+ * Communications Corporation.  Portions created by Netscape are\r
+ * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
+ * Rights Reserved.\r
+ *\r
+ * Contributor(s): \r
+ *\r
+ * Alternatively, the contents of this file may be used under the\r
+ * terms of the GNU Public License (the "GPL"), in which case the\r
+ * provisions of the GPL are applicable instead of those above.\r
+ * If you wish to allow use of your version of this file only\r
+ * under the terms of the GPL and not to allow others to use your\r
+ * version of this file under the NPL, indicate your decision by\r
+ * deleting the provisions above and replace them with the notice\r
+ * and other provisions required by the GPL.  If you do not delete\r
+ * the provisions above, a recipient may use your version of this\r
+ * file under either the NPL or the GPL.\r
+ */\r
+\r
+package org.mozilla.javascript.regexp;\r
+\r
+import org.mozilla.javascript.*;\r
+import java.lang.reflect.Method;\r
+\r
+/**\r
+ * This class implements the RegExp constructor native object.\r
+ *\r
+ * Revision History:\r
+ * Implementation in C by Brendan Eich\r
+ * Initial port to Java by Norris Boyd from jsregexp.c version 1.36\r
+ * Merged up to version 1.38, which included Unicode support.\r
+ * Merged bug fixes in version 1.39.\r
+ * Merged JSFUN13_BRANCH changes up to 1.32.2.11\r
+ *\r
+ * @author Brendan Eich\r
+ * @author Norris Boyd\r
+ */\r
+public class NativeRegExpCtor extends NativeFunction {\r
+\r
+    public NativeRegExpCtor() {\r
+        functionName = "RegExp";\r
+    }\r
+    \r
+    public String getClassName() {\r
+        return "Function";\r
+    }\r
+\r
+    public Object call(Context cx, Scriptable scope, Scriptable thisObj,\r
+                       Object[] args)\r
+    {\r
+        if (args.length > 0 && args[0] instanceof NativeRegExp &&\r
+            (args.length == 1 || args[1] == Undefined.instance))\r
+          {\r
+            return args[0];\r
+        }\r
+        return construct(cx, parent, args);\r
+    }\r
+\r
+    public Scriptable construct(Context cx, Scriptable scope, Object[] args) {\r
+        NativeRegExp re = new NativeRegExp();\r
+        re.compile(cx, scope, args);\r
+        re.setPrototype(getClassPrototype(scope, "RegExp"));\r
+        re.setParentScope(getParentScope());\r
+        return re;\r
+    }\r
+\r
+    static RegExpImpl getImpl() {\r
+        Context cx = Context.getCurrentContext();\r
+        return (RegExpImpl) ScriptRuntime.getRegExpProxy(cx);\r
+    }\r
+\r
+    protected int getIdDefaultAttributes(int id) {\r
+        int shifted = id - idBase;\r
+        if (1 <= shifted && shifted <= MAX_INSTANCE_ID) { \r
+            switch (shifted) {\r
+                case Id_multiline:\r
+                case Id_STAR:\r
+                case Id_input:\r
+                case Id_UNDERSCORE:\r
+                    return PERMANENT;\r
+            }\r
+            return PERMANENT | READONLY;\r
+        }\r
+        return super.getIdDefaultAttributes(id);\r
+    }\r
+    \r
+    private static String stringResult(Object obj) {\r
+        return (obj == null) ? "" : obj.toString();\r
+    }\r
+\r
+    protected Object getIdValue(int id) {\r
+        int shifted = id - idBase;\r
+        if (1 <= shifted && shifted <= MAX_INSTANCE_ID) { \r
+            RegExpImpl impl = getImpl();\r
+            switch (shifted) {\r
+                case Id_multiline:\r
+                case Id_STAR:\r
+                    return wrap_boolean(impl.multiline);\r
+\r
+                case Id_input:\r
+                case Id_UNDERSCORE: \r
+                    return stringResult(impl.input);\r
+\r
+                case Id_lastMatch:\r
+                case Id_AMPERSAND:\r
+                    return stringResult(impl.lastMatch);\r
+\r
+                case Id_lastParen:\r
+                case Id_PLUS:\r
+                    return stringResult(impl.lastParen);\r
+\r
+                case Id_leftContext:\r
+                case Id_BACK_QUOTE:\r
+                    return stringResult(impl.leftContext);\r
+\r
+                case Id_rightContext:\r
+                case Id_QUOTE:\r
+                    return stringResult(impl.rightContext);\r
+            }\r
+            // Must be one of $1..$9, convert to 0..8\r
+            int substring_number = shifted - DOLLAR_ID_BASE - 1;\r
+            return impl.getParenSubString(substring_number).toString();\r
+        }\r
+        return super.getIdValue(id);\r
+    }\r
+    \r
+    protected void setIdValue(int id, Object value) {\r
+        switch (id - idBase) {\r
+            case Id_multiline:\r
+            case Id_STAR:\r
+                getImpl().multiline = ScriptRuntime.toBoolean(value);\r
+                return;\r
+\r
+            case Id_input:\r
+            case Id_UNDERSCORE: \r
+                getImpl().input = ScriptRuntime.toString(value); \r
+                return;\r
+        }\r
+        super.setIdValue(id, value);\r
+    }\r
+\r
+    protected String getIdName(int id) {\r
+        int shifted = id - idBase;\r
+        if (1 <= shifted && shifted <= MAX_INSTANCE_ID) { \r
+            switch (shifted) {\r
+                case Id_multiline:    return "multiline";\r
+                case Id_STAR:         return "$*";\r
+\r
+                case Id_input:        return "input";\r
+                case Id_UNDERSCORE:   return "$_";\r
+\r
+                case Id_lastMatch:    return "lastMatch";\r
+                case Id_AMPERSAND:    return "$&";\r
+\r
+                case Id_lastParen:    return "lastParen";\r
+                case Id_PLUS:         return "$+";\r
+\r
+                case Id_leftContext:  return "leftContext";\r
+                case Id_BACK_QUOTE:   return "$`";\r
+\r
+                case Id_rightContext: return "rightContext";\r
+                case Id_QUOTE:        return "$'";\r
+            }\r
+            // Must be one of $1..$9, convert to 0..8\r
+            int substring_number = shifted - DOLLAR_ID_BASE - 1;\r
+            char[] buf = { '$', (char)('1' + substring_number) };\r
+            return new String(buf);\r
+        }\r
+        return super.getIdName(id);\r
+    }\r
+\r
+    protected int maxInstanceId() {\r
+        // Note: check for idBase == 0 can not be done in constructor, \r
+        // because IdScriptable calls maxInstanceId in its constructor\r
+        // before NativeRegExpCtor constructor gets chance to run any code\r
+        if (idBase == 0) { idBase = super.maxInstanceId(); }\r
+        return idBase + MAX_INSTANCE_ID; \r
+    }\r
+\r
+// #string_id_map#\r
+\r
+    private static final int\r
+        Id_multiline     = 1,\r
+        Id_STAR          = 2,  // #string=$*#\r
+\r
+        Id_input         = 3,\r
+        Id_UNDERSCORE    = 4,  // #string=$_#\r
+\r
+        Id_lastMatch     = 5,\r
+        Id_AMPERSAND     = 6,  // #string=$&#\r
+\r
+        Id_lastParen     = 7,\r
+        Id_PLUS          = 8,  // #string=$+#\r
+\r
+        Id_leftContext   = 9,\r
+        Id_BACK_QUOTE    = 10, // #string=$`#\r
+\r
+        Id_rightContext  = 11,\r
+        Id_QUOTE         = 12, // #string=$'#\r
+        \r
+        DOLLAR_ID_BASE   = 12;\r
+        \r
+    private static final int\r
+        Id_DOLLAR_1 = DOLLAR_ID_BASE + 1, // #string=$1#\r
+        Id_DOLLAR_2 = DOLLAR_ID_BASE + 2, // #string=$2#\r
+        Id_DOLLAR_3 = DOLLAR_ID_BASE + 3, // #string=$3#\r
+        Id_DOLLAR_4 = DOLLAR_ID_BASE + 4, // #string=$4#\r
+        Id_DOLLAR_5 = DOLLAR_ID_BASE + 5, // #string=$5#\r
+        Id_DOLLAR_6 = DOLLAR_ID_BASE + 6, // #string=$6#\r
+        Id_DOLLAR_7 = DOLLAR_ID_BASE + 7, // #string=$7#\r
+        Id_DOLLAR_8 = DOLLAR_ID_BASE + 8, // #string=$8#\r
+        Id_DOLLAR_9 = DOLLAR_ID_BASE + 9, // #string=$9#\r
+\r
+        MAX_INSTANCE_ID = DOLLAR_ID_BASE + 9;\r
+\r
+    protected int mapNameToId(String s) {\r
+        int id;\r
+// #generated# Last update: 2001-05-24 16:09:31 GMT+02:00\r
+        L0: { id = 0; String X = null; int c;\r
+            L: switch (s.length()) {\r
+            case 2: switch (s.charAt(1)) {\r
+                case '&': if (s.charAt(0)=='$') {id=Id_AMPERSAND; break L0;} break L;\r
+                case '\'': if (s.charAt(0)=='$') {id=Id_QUOTE; break L0;} break L;\r
+                case '*': if (s.charAt(0)=='$') {id=Id_STAR; break L0;} break L;\r
+                case '+': if (s.charAt(0)=='$') {id=Id_PLUS; break L0;} break L;\r
+                case '1': if (s.charAt(0)=='$') {id=Id_DOLLAR_1; break L0;} break L;\r
+                case '2': if (s.charAt(0)=='$') {id=Id_DOLLAR_2; break L0;} break L;\r
+                case '3': if (s.charAt(0)=='$') {id=Id_DOLLAR_3; break L0;} break L;\r
+                case '4': if (s.charAt(0)=='$') {id=Id_DOLLAR_4; break L0;} break L;\r
+                case '5': if (s.charAt(0)=='$') {id=Id_DOLLAR_5; break L0;} break L;\r
+                case '6': if (s.charAt(0)=='$') {id=Id_DOLLAR_6; break L0;} break L;\r
+                case '7': if (s.charAt(0)=='$') {id=Id_DOLLAR_7; break L0;} break L;\r
+                case '8': if (s.charAt(0)=='$') {id=Id_DOLLAR_8; break L0;} break L;\r
+                case '9': if (s.charAt(0)=='$') {id=Id_DOLLAR_9; break L0;} break L;\r
+                case '_': if (s.charAt(0)=='$') {id=Id_UNDERSCORE; break L0;} break L;\r
+                case '`': if (s.charAt(0)=='$') {id=Id_BACK_QUOTE; break L0;} break L;\r
+                } break L;\r
+            case 5: X="input";id=Id_input; break L;\r
+            case 9: c=s.charAt(4);\r
+                if (c=='M') { X="lastMatch";id=Id_lastMatch; }\r
+                else if (c=='P') { X="lastParen";id=Id_lastParen; }\r
+                else if (c=='i') { X="multiline";id=Id_multiline; }\r
+                break L;\r
+            case 11: X="leftContext";id=Id_leftContext; break L;\r
+            case 12: X="rightContext";id=Id_rightContext; break L;\r
+            }\r
+            if (X!=null && X!=s && !X.equals(s)) id = 0;\r
+        }\r
+// #/generated#\r
+// #/string_id_map#\r
+\r
+        return (id != 0) ? idBase + id : super.mapNameToId(s); \r
+    }\r
+    \r
+    private static int idBase;\r
+}\r