GetOwnPropertyDescriptor() public method

Gets a descriptor for the property with the given name.
The prototype chain is not searched.
public GetOwnPropertyDescriptor ( object key ) : PropertyDescriptor
key object The property key (either a string or a Symbol).
return PropertyDescriptor
Beispiel #1
0
        public static ObjectInstance DefineProperty([JSParameter(JSParameterFlags.DoNotConvert)] ObjectInstance obj, string propertyName, [JSParameter(JSParameterFlags.DoNotConvert)] ObjectInstance attributes)
        {
            var defaults   = obj.GetOwnPropertyDescriptor(propertyName);
            var descriptor = PropertyDescriptor.FromObject(attributes, defaults);

            obj.DefineProperty(propertyName, descriptor, true);
            return(obj);
        }
Beispiel #2
0
        public static ObjectInstance GetOwnPropertyDescriptor([JSParameter(JSParameterFlags.DoNotConvert)] ObjectInstance obj, string propertyName)
        {
            var descriptor = obj.GetOwnPropertyDescriptor(propertyName);

            if (descriptor.Exists == false)
            {
                return(null);
            }
            return(descriptor.ToObject(obj.Engine));
        }
Beispiel #3
0
        public static ObjectInstance GetOwnPropertyDescriptor(ObjectInstance obj, object key)
        {
            var descriptor = obj.GetOwnPropertyDescriptor(TypeConverter.ToPropertyKey(key));

            if (descriptor.Exists == false)
            {
                return(null);
            }
            return(descriptor.ToObject(obj.Engine));
        }
Beispiel #4
0
        public static bool DefineProperty(ObjectInstance target, object propertyKey, object attributes)
        {
            propertyKey = TypeConverter.ToPropertyKey(propertyKey);
            var defaults = target.GetOwnPropertyDescriptor(propertyKey);

            if (!(attributes is ObjectInstance))
            {
                throw new JavaScriptException(ErrorType.TypeError, $"Invalid property descriptor '{attributes}'.");
            }
            var descriptor = PropertyDescriptor.FromObject((ObjectInstance)attributes, defaults);

            return(target.DefineProperty(propertyKey, descriptor, throwOnError: false));
        }
        public static ObjectInstance DefineProperty(ObjectInstance obj, object key, object attributes)
        {
            key = TypeConverter.ToPropertyKey(key);
            var defaults = obj.GetOwnPropertyDescriptor(key);

            if (!(attributes is ObjectInstance))
            {
                throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Invalid descriptor for property '{propertyName}'.");
            }
            var descriptor = PropertyDescriptor.FromObject((ObjectInstance)attributes, defaults);

            obj.DefineProperty(key, descriptor, true);
            return(obj);
        }
Beispiel #6
0
        public static ArrayInstance Keys(ObjectInstance obj)
        {
            var result = obj.Engine.Array.New();

            foreach (var key in obj.OwnKeys)
            {
                if (key is string)
                {
                    var propertyDescriptor = obj.GetOwnPropertyDescriptor(key);
                    if (propertyDescriptor.Exists && propertyDescriptor.IsEnumerable)
                    {
                        result.Push(key);
                    }
                }
            }
            return(result);
        }
 public static ObjectInstance DefineProperties(object obj, ObjectInstance properties)
 {
     if (obj is ObjectInstance objectInstance)
     {
         foreach (var key in properties.OwnKeys)
         {
             var descriptor = properties.GetOwnPropertyDescriptor(key);
             if (descriptor.IsEnumerable)
             {
                 // Spec says call [[Get]] instead of just using the value from the descriptor.
                 DefineProperty(objectInstance, key, properties[key]);
             }
         }
         return(objectInstance);
     }
     throw new JavaScriptException(ErrorType.TypeError, "Object.defineProperties called on non-object.");
 }
Beispiel #8
0
        /// <summary>
        /// Gets a descriptor for the property with the given name.
        /// </summary>
        /// <param name="key"> The property key (either a string or a Symbol). </param>
        /// <returns> A property descriptor containing the property value and attributes. </returns>
        /// <remarks>
        /// Enforces the following invariants:
        /// * The result of [[GetOwnProperty]] must be either an Object or undefined.
        /// * A property cannot be reported as non-existent, if it exists as a non-configurable own
        ///   property of the target object.
        /// * A property cannot be reported as non-existent, if the target object is not
        ///   extensible, unless it does not exist as an own property of the target object.
        /// * A property cannot be reported as existent, if the target object is not extensible,
        ///   unless it exists as an own property of the target object.
        /// * A property cannot be reported as non-configurable, unless it exists as a
        ///   non-configurable own property of the target object.
        /// * A property cannot be reported as both non-configurable and non-writable, unless it
        ///   exists as a non-configurable, non-writable own property of the target object.
        /// </remarks>
        public override PropertyDescriptor GetOwnPropertyDescriptor(object key)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'getOwnPropertyDescriptor' on a proxy that has been revoked.");
            }

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

            if (trap == null)
            {
                return(target.GetOwnPropertyDescriptor(key));
            }
            var result = trap.CallLateBound(handler, target, key);

            // Validate.
            var targetDescriptor = target.GetOwnPropertyDescriptor(key);

            if (result is ObjectInstance propertyDescriptorObject)
            {
                var propertyDescriptor = PropertyDescriptor.FromObject(propertyDescriptorObject, PropertyDescriptor.Undefined);
                if (!IsCompatiblePropertyDescriptor(target.IsExtensible, propertyDescriptor, targetDescriptor))
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap returned descriptor for property '{TypeConverter.ToString(key)}' that is incompatible with the existing property in the proxy target.");
                }
                if (!propertyDescriptor.IsConfigurable)
                {
                    if (!targetDescriptor.Exists || targetDescriptor.IsConfigurable)
                    {
                        throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property '{TypeConverter.ToString(key)}' which is either non-existent or configurable in the proxy target.");
                    }
                    if (!propertyDescriptor.IsWritable && targetDescriptor.IsWritable)
                    {
                        throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap reported non-configurable and writable for property '{TypeConverter.ToString(key)}' which is non-configurable, non-writable in the proxy target.");
                    }
                }
                return(propertyDescriptor);
            }
            else if (result == null || result == Undefined.Value)
            {
                if (!targetDescriptor.Exists)
                {
                    return(PropertyDescriptor.Missing);
                }
                if (!targetDescriptor.IsConfigurable)
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap returned undefined for property '{TypeConverter.ToString(key)}' which is non-configurable in the proxy target.");
                }
                if (!target.IsExtensible)
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap returned undefined for property '{TypeConverter.ToString(key)}' which exists in the non-extensible proxy target.");
                }
                return(PropertyDescriptor.Missing);
            }
            else
            {
                throw new JavaScriptException(ErrorType.TypeError, $"'getOwnPropertyDescriptor' on proxy: trap returned neither object nor undefined for property '{TypeConverter.ToString(key)}'.");
            }
        }
 public static ObjectInstance GetOwnPropertyDescriptor(ObjectInstance obj, object key)
 {
     var descriptor = obj.GetOwnPropertyDescriptor(TypeConverter.ToPropertyKey(key));
     if (descriptor.Exists == false)
         return null;
     return descriptor.ToObject(obj.Engine);
 }
 public static ObjectInstance DefineProperty(ObjectInstance obj, object key, object attributes)
 {
     key = TypeConverter.ToPropertyKey(key);
     var defaults = obj.GetOwnPropertyDescriptor(key);
     if (!(attributes is ObjectInstance))
         throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Invalid descriptor for property '{propertyName}'.");
     var descriptor = PropertyDescriptor.FromObject((ObjectInstance)attributes, defaults);
     obj.DefineProperty(key, descriptor, true);
     return obj;
 }
 /// <summary>
 /// Sets the value of a object literal property to a setter.  If the value already has a
 /// getter then it will be retained.
 /// </summary>
 /// <param name="obj"> The object to set the property on. </param>
 /// <param name="key"> The property key (can be a string or a symbol).</param>
 /// <param name="setter"> The setter function. </param>
 public static void SetObjectLiteralSetter(ObjectInstance obj, object key, UserDefinedFunction setter)
 {
     var descriptor = obj.GetOwnPropertyDescriptor(key);
     if (descriptor.Exists == false || !descriptor.IsAccessor)
         obj.DefineProperty(key, new PropertyDescriptor(null, setter, Library.PropertyAttributes.FullAccess), throwOnError: false);
     else
         obj.DefineProperty(key, new PropertyDescriptor(descriptor.Getter, setter, Library.PropertyAttributes.FullAccess), throwOnError: false);
 }