CallLateBound() public abstract méthode

Calls this function, passing in the given "this" value and zero or more arguments.
public abstract CallLateBound ( object thisObject ) : object
thisObject object The value of the "this" keyword within the function.
Résultat object
Exemple #1
0
        /// <summary>
        /// Calls this function, passing in the given "this" value and zero or more arguments.
        /// </summary>
        /// <param name="thisObject"> The value of the "this" keyword within the function. </param>
        /// <param name="argumentValues"> An array of argument values. </param>
        /// <returns> The value that was returned from the function. </returns>
        public override object CallLateBound(object thisObject, params object[] argumentValues)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'apply' on a proxy that has been revoked.");
            }

            // Call the handler, if one exists.
            var trap = handler.GetMethod("apply");

            if (trap == null)
            {
                return(target.CallLateBound(thisObject, argumentValues));
            }
            var array = Engine.Array.New(argumentValues);

            return(trap.CallLateBound(handler, target, thisObject, array));
        }
Exemple #2
0
        /// <summary>
        /// Returns a copy of this string with text replaced using a replacement function.
        /// </summary>
        /// <param name="substr"> The text 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. </returns>
        public static string Replace(string thisObject, string substr, FunctionInstance replaceFunction)
        {
            // Find the first occurrance of substr.
            int start = thisObject.IndexOf(substr, StringComparison.Ordinal);

            if (start == -1)
            {
                return(thisObject);
            }
            int end = start + substr.Length;

            // Get the replacement text from the provided function.
            var replaceText = TypeConverter.ToString(replaceFunction.CallLateBound(null, substr, start, thisObject));

            // Replace only the first match.
            var result = new System.Text.StringBuilder(thisObject.Length + (replaceText.Length - substr.Length));

            result.Append(thisObject, 0, start);
            result.Append(replaceText);
            result.Append(thisObject, end, thisObject.Length - end);
            return(result.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Returns a copy of the given string with text replaced using a regular expression.
        /// </summary>
        /// <param name="input"> The string on which to perform the search. </param>
        /// <param name="replaceFunction"> A function that is called to produce the text to replace
        /// for every successful match. </param>
        /// <returns> A copy of the given string with text replaced using a regular expression. </returns>
        public string Replace(string input, FunctionInstance replaceFunction)
        {
            return(this.value.Replace(input, match =>
            {
                // Set the deprecated RegExp properties.
                this.Engine.RegExp.SetDeprecatedProperties(input, match);

                object[] parameters = new object[match.Groups.Count + 2];
                for (int i = 0; i < match.Groups.Count; i++)
                {
                    if (match.Groups[i].Success == false)
                    {
                        parameters[i] = Undefined.Value;
                    }
                    else
                    {
                        parameters[i] = match.Groups[i].Value;
                    }
                }
                parameters[match.Groups.Count] = match.Index;
                parameters[match.Groups.Count + 1] = input;
                return TypeConverter.ToString(replaceFunction.CallLateBound(null, parameters));
            }, this.Global == true ? int.MaxValue : 1));
        }
Exemple #4
0
        public static bool Some(ObjectInstance thisObj, FunctionInstance callbackFunction, [DefaultParameterValue(null)] ObjectInstance context = null)
        {
            // callbackFunction must be a valid function.
            if (callbackFunction == null)
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid callback function");

            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // This method only supports arrays of length up to 2^31-1.
            if (arrayLength > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The array is too long");

            for (int i = 0; i < arrayLength; i++)
            {
                // Get the value of the array element.
                object elementValue = thisObj[(uint)i];

                // Only call the callback function for array elements that exist in the array.
                if (elementValue != null)
                {
                    // Call the callback function.
                    if (TypeConverter.ToBoolean(callbackFunction.CallLateBound(context, elementValue, i, thisObj)) == true)
                        return true;
                }
            }
            return false;
        }
Exemple #5
0
        public static object ReduceRight(ObjectInstance thisObj, FunctionInstance callbackFunction, [DefaultParameterValue(null)] object initialValue = null)
        {
            // callbackFunction must be a valid function.
            if (callbackFunction == null)
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid callback function");

            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // This method only supports arrays of length up to 2^31-1.
            if (arrayLength > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The array is too long");

            // If an initial value is not provided, the initial value is the last (defined) element.
            int i = (int)arrayLength - 1;
            object accumulatedValue = initialValue;
            if (accumulatedValue == null)
            {
                // Scan for a defined element.
                for (; i >= 0; i--)
                {
                    if (thisObj[(uint)i] != null)
                    {
                        accumulatedValue = thisObj[(uint)(i--)];
                        break;
                    }
                }
                if (accumulatedValue == null)
                    throw new JavaScriptException(thisObj.Engine, "TypeError", "Reduce of empty array with no initial value");
            }

            // Scan from high to to low.
            for (; i >= 0; i--)
            {
                // Get the value of the array element.
                object elementValue = thisObj[(uint)i];

                // Only call the callback function for array elements that exist in the array.
                if (elementValue != null)
                {
                    // Call the callback function.
                    accumulatedValue = callbackFunction.CallLateBound(Undefined.Value, accumulatedValue, elementValue, i, thisObj);
                }
            }

            return accumulatedValue;
        }
Exemple #6
0
        public static ArrayInstance Map(ObjectInstance thisObj, FunctionInstance callbackFunction, [DefaultParameterValue(null)] ObjectInstance context = null)
        {
            // callbackFunction must be a valid function.
            if (callbackFunction == null)
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid callback function");

            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // This method only supports arrays of length up to 2^31-1.
            if (arrayLength > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The array is too long");

            // Create a new array to hold the new values.
            // The length of the output array is always equal to the length of the input array.
            var resultArray = new ArrayInstance(thisObj.Engine.Array.InstancePrototype, arrayLength, arrayLength);

            for (int i = 0; i < arrayLength; i++)
            {
                // Get the value of the array element.
                object elementValue = thisObj[(uint)i];

                // Only call the callback function for array elements that exist in the array.
                if (elementValue != null)
                {
                    // Call the callback function.
                    object result = callbackFunction.CallLateBound(context, elementValue, i, thisObj);

                    // Store the result.
                    resultArray[(uint)i] = result;
                }
            }

            return resultArray;
        }
Exemple #7
0
        public static ArrayInstance Filter(ObjectInstance thisObj, FunctionInstance callbackFunction, [DefaultParameterValue(null)] ObjectInstance context = null)
        {
            // callbackFunction must be a valid function.
            if (callbackFunction == null)
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid callback function");

            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // This method only supports arrays of length up to 2^31-1.
            if (arrayLength > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The array is too long");

            // Create a new array to hold the new values.
            var result = thisObj.Engine.Array.New();

            for (int i = 0; i < arrayLength; i++)
            {
                // Get the value of the array element.
                object elementValue = thisObj[(uint)i];

                // Only call the callback function for array elements that exist in the array.
                if (elementValue != null)
                {
                    // Call the callback function.
                    bool includeInArray = TypeConverter.ToBoolean(callbackFunction.CallLateBound(context, elementValue, i, thisObj));

                    // Store the result if the callback function returned true.
                    if (includeInArray == true)
                        result.Push(elementValue);
                }
            }
            return result;
        }
Exemple #8
0
 public static object Apply(FunctionInstance target, object thisArgument, ObjectInstance argumentsList)
 {
     return(target.CallLateBound(thisArgument, TypeUtilities.CreateListFromArrayLike(argumentsList)));
 }
Exemple #9
0
        /// <summary>
        /// Returns a copy of this string with text replaced using a replacement function.
        /// </summary>
        /// <param name="substr"> The text 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. </returns>
        public static string Replace(string thisObject, string substr, FunctionInstance replaceFunction)
        {
            // Find the first occurrance of substr.
            int start = thisObject.IndexOf(substr, StringComparison.Ordinal);
            if (start == -1)
                return thisObject;
            int end = start + substr.Length;

            // Get the replacement text from the provided function.
            var replaceText = TypeConverter.ToString(replaceFunction.CallLateBound(null, substr, start, thisObject));

            // Replace only the first match.
            var result = new System.Text.StringBuilder(thisObject.Length + (replaceText.Length - substr.Length));
            result.Append(thisObject, 0, start);
            result.Append(replaceText);
            result.Append(thisObject, end, thisObject.Length - end);
            return result.ToString();
        }
Exemple #10
0
        /// <summary>
        /// Returns a copy of the given string with text replaced using a regular expression.
        /// </summary>
        /// <param name="input"> The string on which to perform the search. </param>
        /// <param name="replaceFunction"> A function that is called to produce the text to replace
        /// for every successful match. </param>
        /// <returns> A copy of the given string with text replaced using a regular expression. </returns>
        public string Replace(string input, FunctionInstance replaceFunction)
        {
            return this.value.Replace(input, match =>
            {
                // Set the deprecated RegExp properties.
                this.Engine.RegExp.SetDeprecatedProperties(input, match);

                object[] parameters = new object[match.Groups.Count + 2];
                for (int i = 0; i < match.Groups.Count; i++)
                {
                    if (match.Groups[i].Success == false)
                        parameters[i] = Undefined.Value;
                    else
                        parameters[i] = match.Groups[i].Value;
                }
                parameters[match.Groups.Count] = match.Index;
                parameters[match.Groups.Count + 1] = input;
                return TypeConverter.ToString(replaceFunction.CallLateBound(null, parameters));
            }, this.Global == true ? int.MaxValue : 1);
        }
Exemple #11
0
        public static ObjectInstance Sort([JSParameter(JSParameterFlags.Mutated)] ObjectInstance thisObj, FunctionInstance comparisonFunction = null)
        {
            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // An array of size 1 or less is already sorted.
            if (arrayLength <= 1)
                return thisObj;

            // Create a comparer delegate.
            Func<object, object, double> comparer;
            if (comparisonFunction == null)
                comparer = (a, b) =>
                {
                    if (a == null && b == null)
                        return 0f;
                    if (a == null)
                        return 1f;
                    if (b == null)
                        return -1f;
                    if (a == Undefined.Value && b == Undefined.Value)
                        return 0f;
                    if (a == Undefined.Value)
                        return 1f;
                    if (b == Undefined.Value)
                        return -1f;
                    return string.Compare(TypeConverter.ToString(a), TypeConverter.ToString(b), StringComparison.Ordinal);
                };
            else
                comparer = (a, b) =>
                {
                    if (a == null && b == null)
                        return 0f;
                    if (a == null)
                        return 1f;
                    if (b == null)
                        return -1f;
                    if (a == Undefined.Value && b == Undefined.Value)
                        return 0f;
                    if (a == Undefined.Value)
                        return 1f;
                    if (b == Undefined.Value)
                        return -1f;
                    return TypeConverter.ToNumber(comparisonFunction.CallLateBound(thisObj.Engine.Global, a, b));
                }; 

            try
            {
                // Sort the array.
                QuickSort(thisObj, comparer, 0, arrayLength - 1);
            }
            catch (IndexOutOfRangeException)
            {
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid comparison function");
            }

            return thisObj;
        }
Exemple #12
0
        public static void ForEach(ObjectInstance thisObj, FunctionInstance callbackFunction, ObjectInstance context = null)
        {
            // callbackFunction must be a valid function.
            if (callbackFunction == null)
                throw new JavaScriptException(thisObj.Engine, "TypeError", "Invalid callback function");

            // Get the length of the array.
            uint arrayLength = GetLength(thisObj);

            // This method only supports arrays of length up to 2^31-1.
            if (arrayLength > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The array is too long");

            for (int i = 0; i < arrayLength; i++)
            {
                // Get the value of the array element.
                object elementValue = thisObj[(uint)i];

                // Only call the callback function for array elements that exist in the array.
                if (elementValue != null)
                {
                    // Call the callback function.
                    callbackFunction.CallLateBound(context, elementValue, i, thisObj);
                }
            }
        }