Beispiel #1
0
        public override sealed IObject Construct(IEnvironment environment, IArgs args)
        {
            var patternArg = args[0];
            var flagsArg = args[1];
            var pattern = "";
            var flags = "";
            bool global, ignoreCase, multiline;

            var patternObject = patternArg as IObject;
            if (patternObject != null && patternObject.Class == "RegExp")
            {
                if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    throw environment.CreateTypeError("");
                }
                global = ((IBoolean)patternObject.Get("global")).BaseValue;
                ignoreCase = ((IBoolean)patternObject.Get("ignoreCase")).BaseValue;
                multiline = ((IBoolean)patternObject.Get("multiline")).BaseValue;
                pattern = ((IString)patternObject.Get("source")).BaseValue;
                if (global) flags += "g";
                if (ignoreCase) flags += "i";
                if (multiline) flags += "m";
            }
            else
            {
                if (patternArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    pattern = patternArg.ConvertToString().BaseValue;
                }
                if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    flags = flagsArg.ConvertToString().BaseValue;
                }
                global = flags.Contains("g");
                ignoreCase = flags.Contains("i");
                multiline = flags.Contains("m");
            }

            var regExpObj = new NRegExp(environment);
            regExpObj.Class = "RegExp";
            regExpObj.Extensible = true;
            regExpObj.Prototype = environment.RegExpPrototype;
            regExpObj.Body = pattern;
            regExpObj.Flags = flags;
            regExpObj.RegExpMatcher = Machete.Compiler.RegExpParser.Parse(Environment, pattern, flags);
            regExpObj.DefineOwnProperty("source", environment.CreateDataDescriptor(environment.CreateString(pattern), false, false, false), false);
            regExpObj.DefineOwnProperty("global", environment.CreateDataDescriptor(environment.CreateBoolean(global), false, false, false), false);
            regExpObj.DefineOwnProperty("ignoreCase", environment.CreateDataDescriptor(environment.CreateBoolean(ignoreCase), false, false, false), false);
            regExpObj.DefineOwnProperty("multiline", environment.CreateDataDescriptor(environment.CreateBoolean(multiline), false, false, false), false);
            regExpObj.DefineOwnProperty("lastIndex", environment.CreateDataDescriptor(environment.CreateNumber(0.0), true, false, false), false);
            return regExpObj;
        }
Beispiel #2
0
        public sealed override IObject Construct(IEnvironment environment, IArgs args)
        {
            var  patternArg = args[0];
            var  flagsArg = args[1];
            var  pattern = "";
            var  flags = "";
            bool global, ignoreCase, multiline;

            var patternObject = patternArg as IObject;

            if (patternObject != null && patternObject.Class == "RegExp")
            {
                if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    throw environment.CreateTypeError("");
                }
                global     = ((IBoolean)patternObject.Get("global")).BaseValue;
                ignoreCase = ((IBoolean)patternObject.Get("ignoreCase")).BaseValue;
                multiline  = ((IBoolean)patternObject.Get("multiline")).BaseValue;
                pattern    = ((IString)patternObject.Get("source")).BaseValue;
                if (global)
                {
                    flags += "g";
                }
                if (ignoreCase)
                {
                    flags += "i";
                }
                if (multiline)
                {
                    flags += "m";
                }
            }
            else
            {
                if (patternArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    pattern = patternArg.ConvertToString().BaseValue;
                }
                if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
                {
                    flags = flagsArg.ConvertToString().BaseValue;
                }
                global     = flags.Contains("g");
                ignoreCase = flags.Contains("i");
                multiline  = flags.Contains("m");
            }

            var regExpObj = new NRegExp(environment);

            regExpObj.Class         = "RegExp";
            regExpObj.Extensible    = true;
            regExpObj.Prototype     = environment.RegExpPrototype;
            regExpObj.Body          = pattern;
            regExpObj.Flags         = flags;
            regExpObj.RegExpMatcher = Machete.Compiler.RegExpParser.Parse(Environment, pattern, flags);
            regExpObj.DefineOwnProperty("source", environment.CreateDataDescriptor(environment.CreateString(pattern), false, false, false), false);
            regExpObj.DefineOwnProperty("global", environment.CreateDataDescriptor(environment.CreateBoolean(global), false, false, false), false);
            regExpObj.DefineOwnProperty("ignoreCase", environment.CreateDataDescriptor(environment.CreateBoolean(ignoreCase), false, false, false), false);
            regExpObj.DefineOwnProperty("multiline", environment.CreateDataDescriptor(environment.CreateBoolean(multiline), false, false, false), false);
            regExpObj.DefineOwnProperty("lastIndex", environment.CreateDataDescriptor(environment.CreateNumber(0.0), true, false, false), false);
            return(regExpObj);
        }