예제 #1
0
 public static object GetPrototypeOf(ObjectInstance target)
 {
     return(ObjectConstructor.GetPrototypeOf(target));
 }
예제 #2
0
        /// <summary>
        /// Serializes an object into a JSON string.
        /// </summary>
        /// <param name="value"> The object to serialize. </param>
        /// <param name="result"> The StringBuilder to write the JSON representation of the
        /// object to. </param>
        private void SerializeObject(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.objectStack.Contains(value) == true)
            {
                throw new JavaScriptException(ErrorType.TypeError, "The given object must not contain cyclical references");
            }
            this.objectStack.Push(value);

            // Create a list of property names to serialize.
            // Only properties that are enumerable and have property names are serialized.
            var propertiesToSerialize = this.SerializableProperties;

            if (propertiesToSerialize == null)
            {
                propertiesToSerialize = ObjectConstructor.Keys(value).ElementValues.Cast <string>();
            }

            result.Append('{');
            int serializedPropertyCount = 0;

            foreach (string propertyName in propertiesToSerialize)
            {
                // Transform the value using the replacer function or toJSON().
                object propertyValue = TransformPropertyValue(value, propertyName);

                // Undefined values are not serialized.
                if (propertyValue == null || propertyValue == Undefined.Value || propertyValue is FunctionInstance || propertyValue is Symbol)
                {
                    continue;
                }

                // Append the separator.
                if (serializedPropertyCount > 0)
                {
                    result.Append(',');
                }
                result.Append(this.separator);

                // Append the property name and value to the result.
                QuoteString(propertyName, result);
                result.Append(':');
                if (string.IsNullOrEmpty(this.Indentation) == false)
                {
                    result.Append(' ');
                }
                SerializePropertyValue(propertyValue, result);

                // Keep track of how many properties we have serialized.
                serializedPropertyCount++;
            }
            if (serializedPropertyCount > 0)
            {
                result.Append(previousSeparator);
            }
            result.Append('}');

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

            // Restore the separator to it's previous value.
            this.separator = previousSeparator;
        }
예제 #3
0
 public static ObjectInstance GetOwnPropertyDescriptor(ObjectInstance target, object propertyKey)
 {
     return(ObjectConstructor.GetOwnPropertyDescriptor(target, propertyKey));
 }
예제 #4
0
 /// <summary>
 /// Initializes the prototype properties.
 /// </summary>
 /// <param name="obj"> The object to set the properties on. </param>
 /// <param name="constructor"> A reference to the constructor that owns the prototype. </param>
 internal static void InitializePrototypeProperties(ObjectInstance obj, ObjectConstructor constructor)
 {
     var engine = obj.Engine;
     var properties = GetDeclarativeProperties(engine);
     properties.Add(new PropertyNameAndValue("constructor", constructor, PropertyAttributes.NonEnumerable));
     obj.FastSetProperties(properties);
 }