public override IScriptable Construct (Context cx, IScriptable scope, object [] args)
 {
     BuiltinRegExp re = new BuiltinRegExp ();
     re.compile (cx, scope, args);
     ScriptRuntime.setObjectProtoAndParent (re, scope);
     return re;
 }
Example #2
0
        /// <summary> Analog of C match_or_replace.</summary>
        private static object matchOrReplace (Context cx, IScriptable scope, IScriptable thisObj, object [] args, RegExpImpl reImpl, GlobData data, bool forceFlat)
        {
            BuiltinRegExp re;

            string str = ScriptConvert.ToString (thisObj);
            data.str = str;
            IScriptable topScope = ScriptableObject.GetTopLevelScope (scope);

            if (args.Length == 0) {
                object compiled = BuiltinRegExp.compileRE ("", "", false);
                re = new BuiltinRegExp (topScope, compiled);
            }
            else if (args [0] is BuiltinRegExp) {
                re = (BuiltinRegExp)args [0];
            }
            else {
                string src = ScriptConvert.ToString (args [0]);
                string opt;
                if (data.optarg < args.Length) {
                    args [0] = src;
                    opt = ScriptConvert.ToString (args [data.optarg]);
                }
                else {
                    opt = null;
                }
                object compiled = BuiltinRegExp.compileRE (src, opt, forceFlat);
                re = new BuiltinRegExp (topScope, compiled);
            }
            data.regexp = re;

            data.global = (re.Flags & BuiltinRegExp.JSREG_GLOB) != 0;
            int [] indexp = new int [] { 0 };
            object result = null;
            if (data.mode == EcmaScript.NET.RegExpActions.Search) {
                result = re.executeRegExp (cx, scope, reImpl, str, indexp, BuiltinRegExp.TEST);
                if (result != null && result.Equals (true))
                    result = (int)reImpl.leftContext.length;
                else
                    result = -1;
            }
            else if (data.global) {
                re.lastIndex = 0;
                for (int count = 0; indexp [0] <= str.Length; count++) {
                    result = re.executeRegExp (cx, scope, reImpl, str, indexp, BuiltinRegExp.TEST);
                    if (result == null || !result.Equals (true))
                        break;
                    if (data.mode == EcmaScript.NET.RegExpActions.Match) {
                        match_glob (data, cx, scope, count, reImpl);
                    }
                    else {
                        if (data.mode != EcmaScript.NET.RegExpActions.Replace)
                            Context.CodeBug ();
                        SubString lastMatch = reImpl.lastMatch;
                        int leftIndex = data.leftIndex;
                        int leftlen = lastMatch.index - leftIndex;
                        data.leftIndex = lastMatch.index + lastMatch.length;
                        replace_glob (data, cx, scope, reImpl, leftIndex, leftlen);
                    }
                    if (reImpl.lastMatch.length == 0) {
                        if (indexp [0] == str.Length)
                            break;
                        indexp [0]++;
                    }
                }
            }
            else {
                result = re.executeRegExp (cx, scope, reImpl, str, indexp, ((data.mode == EcmaScript.NET.RegExpActions.Replace) ? BuiltinRegExp.TEST : BuiltinRegExp.MATCH));
            }

            return result;
        }
        public static void Init(IScriptable scope, bool zealed)
        {
            BuiltinRegExp proto = new BuiltinRegExp ();
            proto.re = (RECompiled)compileRE ("", null, false);
            proto.ActivatePrototypeMap (MAX_PROTOTYPE_ID);
            proto.ParentScope = scope;
            proto.SetPrototype (GetObjectPrototype (scope));

            BuiltinRegExpCtor ctor = new BuiltinRegExpCtor ();

            ScriptRuntime.setFunctionProtoAndParent (ctor, scope);

            ctor.ImmunePrototypeProperty = proto;

            if (zealed) {
                proto.SealObject ();
                ctor.SealObject ();
            }

            DefineProperty (scope, "RegExp", ctor, ScriptableObject.DONTENUM);
        }