Exemplo n.º 1
0
        public static object Search(ScriptEngine engine, object thisObject, object substrOrRegExp)
        {
            if (thisObject == Null.Value || thisObject == Undefined.Value)
            {
                throw new JavaScriptException(ErrorType.TypeError, "String.prototype.search called on null or undefined.");
            }

            if (substrOrRegExp != Null.Value && substrOrRegExp != Undefined.Value)
            {
                // Get the [Symbol.search] property value.
                var matchFunctionObj = TypeConverter.ToObject(engine, substrOrRegExp)[Symbol.Search];
                if (matchFunctionObj != Undefined.Value)
                {
                    // If it's a function, call it and return the result.
                    if (matchFunctionObj is FunctionInstance matchFunction)
                    {
                        return(matchFunction.CallLateBound(substrOrRegExp, thisObject));
                    }
                    else
                    {
                        throw new JavaScriptException(ErrorType.TypeError, "Symbol.search value is not a function.");
                    }
                }
            }

            // Convert the argument to a regex.
            var regex = new RegExpInstance(engine.RegExp.InstancePrototype, TypeConverter.ToString(substrOrRegExp, string.Empty));

            // Call the [Symbol.search] function.
            return(regex.Search(TypeConverter.ToString(thisObject)));
        }
Exemplo n.º 2
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new RegExp object.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        internal RegExpConstructor(ObjectInstance prototype)
            : base(prototype, __STUB__Construct, __STUB__Call)
        {
            // Initialize the constructor properties.
            var properties = GetDeclarativeProperties(Engine);

            InitializeConstructorProperties(properties, "RegExp", 2, RegExpInstance.CreatePrototype(Engine, this));
            AddDeprecatedProperties(properties);
            InitializeProperties(properties);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new regular expression instance by copying the pattern and flags from another
        /// RegExp instance.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="existingInstance"> The instance to copy the pattern and flags from. </param>
        internal RegExpInstance(ObjectInstance prototype, RegExpInstance existingInstance)
            : base(prototype)
        {
            if (existingInstance == null)
                throw new ArgumentNullException("existingInstance");
            this.value = existingInstance.value;
            this.globalSearch = existingInstance.globalSearch;

            // Initialize the javascript properties.
            FastSetProperty("lastIndex", 0.0, PropertyAttributes.Writable);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new regular expression instance by copying the pattern and flags from another
        /// RegExp instance.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="existingInstance"> The instance to copy the pattern and flags from. </param>
        internal RegExpInstance(ObjectInstance prototype, RegExpInstance existingInstance)
            : base(prototype)
        {
            if (existingInstance == null)
            {
                throw new ArgumentNullException("existingInstance");
            }
            this.value        = existingInstance.value;
            this.globalSearch = existingInstance.globalSearch;

            // Initialize the javascript properties.
            FastSetProperty("lastIndex", 0.0, PropertyAttributes.Writable);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new regular expression instance by copying the pattern and flags from another
        /// RegExp instance.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="existingInstance"> The instance to copy the pattern and flags from. </param>
        internal RegExpInstance(ObjectInstance prototype, RegExpInstance existingInstance)
            : base(prototype)
        {
            if (existingInstance == null)
            {
                throw new ArgumentNullException(nameof(existingInstance));
            }
            this.value        = existingInstance.value;
            this.globalSearch = existingInstance.globalSearch;

            // Initialize the javascript properties.
            InitializeProperties(new PropertyNameAndValue[]
            {
                new PropertyNameAndValue("lastIndex", 0.0, PropertyAttributes.Writable),
            });
        }
Exemplo n.º 6
0
 /// <summary>
 /// Splits this string into an array of strings by separating the string into substrings.
 /// </summary>
 /// <param name="regExp"> A regular expression that indicates where to split the string. </param>
 /// <param name="limit"> The maximum number of array items to return.  Defaults to unlimited. </param>
 /// <returns> An array containing the split strings. </returns>
 public static ArrayInstance Split(string thisObject, RegExpInstance regExp, [DefaultParameterValue(uint.MaxValue)] uint limit = uint.MaxValue)
 {
     return regExp.Split(thisObject, limit);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Returns a copy of this string with text replaced using a regular expression and a
 /// replacement function.
 /// </summary>
 /// <param name="regExp"> The regular expression to search for. </param>
 /// <param name="replaceFunction"> A function that is called to produce the text to replace
 /// for every successful match. </param>
 /// <returns> A copy of this string with text replaced using a regular expression. </returns>
 public static string Replace(string thisObject, RegExpInstance regExp, FunctionInstance replaceFunction)
 {
     return regExp.Replace(thisObject, replaceFunction);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns a copy of this string with text replaced using a regular expression.
 /// </summary>
 /// <param name="regExp"> The regular expression to search for. </param>
 /// <param name="replaceText"> A string containing the text to replace for every successful match. </param>
 /// <returns> A copy of this string with text replaced using a regular expression. </returns>
 public static string Replace(string thisObject, RegExpInstance regExp, string replaceText)
 {
     return regExp.Replace(thisObject, replaceText);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Splits this string into an array of strings by separating the string into substrings.
 /// </summary>
 /// <param name="thisObject"> The string that is being operated on. </param>
 /// <param name="regExp"> A regular expression that indicates where to split the string. </param>
 /// <param name="limit"> The maximum number of array items to return.  Defaults to unlimited. </param>
 /// <returns> An array containing the split strings. </returns>
 public static ArrayInstance Split(string thisObject, RegExpInstance regExp, uint limit = uint.MaxValue)
 {
     return(regExp.Split(thisObject, limit));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Returns a copy of this string with text replaced using a regular expression and a
 /// replacement function.
 /// </summary>
 /// <param name="thisObject"> The string that is being operated on. </param>
 /// <param name="regExp"> The regular expression to search for. </param>
 /// <param name="replaceFunction"> A function that is called to produce the text to replace
 /// for every successful match. </param>
 /// <returns> A copy of this string with text replaced using a regular expression. </returns>
 public static string Replace(string thisObject, RegExpInstance regExp, FunctionInstance replaceFunction)
 {
     return(regExp.Replace(thisObject, replaceFunction));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Returns a copy of this string with text replaced using a regular expression.
 /// </summary>
 /// <param name="thisObject"> The string that is being operated on. </param>
 /// <param name="regExp"> The regular expression to search for. </param>
 /// <param name="replaceText"> A string containing the text to replace for every successful match. </param>
 /// <returns> A copy of this string with text replaced using a regular expression. </returns>
 public static string Replace(string thisObject, RegExpInstance regExp, string replaceText)
 {
     return(regExp.Replace(thisObject, replaceText));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Splits this string into an array of strings by separating the string into substrings.
 /// </summary>
 /// <param name="regExp"> A regular expression that indicates where to split the string. </param>
 /// <param name="limit"> The maximum number of array items to return.  Defaults to unlimited. </param>
 /// <returns> An array containing the split strings. </returns>
 public static ArrayInstance Split(string thisObject, RegExpInstance regExp, [DefaultParameterValue(uint.MaxValue)] uint limit = uint.MaxValue)
 {
     return(regExp.Split(thisObject, limit));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Splits this string into an array of strings by separating the string into substrings.
 /// </summary>
 /// <param name="thisObject"> The string that is being operated on. </param>
 /// <param name="regExp"> A regular expression that indicates where to split the string. </param>
 /// <param name="limit"> The maximum number of array items to return.  Defaults to unlimited. </param>
 /// <returns> An array containing the split strings. </returns>
 public static ArrayInstance Split(string thisObject, RegExpInstance regExp, uint limit = uint.MaxValue)
 {
     return regExp.Split(thisObject, limit);
 }