コード例 #1
0
        public static ArrayInstance From(ScriptEngine engine, ObjectInstance iterable, FunctionInstance mapFunction, object thisArg)
        {
            var result   = new List <object>();
            var iterator = TypeUtilities.GetIterator(engine, iterable);

            if (iterator != null)
            {
                // Initialize the array from an iterator.
                int index = 0;
                foreach (var item in TypeUtilities.Iterate(engine, iterator))
                {
                    object mappedValue = mapFunction?.Call(thisArg ?? Undefined.Value, item, index) ?? item;
                    result.Add(mappedValue);
                    index++;
                }
            }
            else
            {
                // Initialize the array from an array-like object.
                uint length = ArrayInstance.GetLength(iterable);
                for (int i = 0; i < length; i++)
                {
                    object mappedValue = mapFunction?.Call(thisArg ?? Undefined.Value, iterable[i], i) ?? iterable[i];
                    result.Add(mappedValue);
                }
            }
            return(engine.Array.New(result.ToArray()));
        }
コード例 #2
0
ファイル: JSONSerializer.cs プロジェクト: oujunke/jurassic
        /// <summary>
        /// Serializes an array into a JSON string.
        /// </summary>
        /// <param name="value"> The array to serialize. </param>
        /// <param name="result"> The StringBuilder to write the JSON representation of the
        /// array to. </param>
        private void SerializeArray(ObjectInstance value, StringBuilder result)
        {
            // Add the spacer string to the current separator string.
            string previousSeparator = this.separator;

            this.separator += this.Indentation;

            // Check for cyclical references.
            if (this.arrayStack.Contains(value) == true)
            {
                throw new JavaScriptException(ErrorType.TypeError, "The given object must not contain cyclical references");
            }
            this.arrayStack.Push(value);

            result.Append('[');
            uint length = ArrayInstance.GetLength(value);

            for (uint i = 0; i < length; i++)
            {
                // Append the separator.
                if (i > 0)
                {
                    result.Append(',');
                }
                result.Append(this.separator);

                // Transform the value using the replacer function or toJSON().
                object elementValue = TransformPropertyValue(value, i);

                if (elementValue == null || elementValue == Undefined.Value || elementValue is FunctionInstance || elementValue is Symbol)
                {
                    // Undefined is serialized as "null".
                    result.Append("null");
                }
                else
                {
                    // Serialize the element value to the output.
                    SerializePropertyValue(elementValue, result);
                }
            }
            if (length > 0)
            {
                result.Append(previousSeparator);
            }
            result.Append(']');

            // Remove this object from the stack.
            this.arrayStack.Pop();

            // Restore the separator to it's previous value.
            this.separator = previousSeparator;
        }