HasProperty() public method

Determines if a property with the given name exists.
public HasProperty ( object key ) : bool
key object The property key (either a string or a Symbol).
return bool
Beispiel #1
0
        /// <summary>
        /// Determines if a property with the given name exists.
        /// </summary>
        /// <param name="key"> The property key (either a string or a Symbol). </param>
        /// <returns> <c>true</c> if the property exists on this object or in the prototype chain;
        /// <c>false</c> otherwise. </returns>
        public override bool HasProperty(object key)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'has' on a proxy that has been revoked.");
            }

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

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

            // Validate.
            if (!result)
            {
                var targetDescriptor = target.GetOwnPropertyDescriptor(key);
                if (targetDescriptor.Exists)
                {
                    if (!targetDescriptor.IsConfigurable)
                    {
                        throw new JavaScriptException(ErrorType.TypeError, $"'has' on proxy: trap returned falsish for property '{TypeConverter.ToString(key)}' which exists in the proxy target as non-configurable.");
                    }
                    if (!target.IsExtensible)
                    {
                        throw new JavaScriptException(ErrorType.TypeError, $"'has' on proxy: trap returned falsish for property '{TypeConverter.ToString(key)}' but the proxy target is not extensible.");
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        //     OBJECT SERIALIZATION AND DESERIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a property descriptor from an object containing any of the following
        /// properties: configurable, writable, enumerable, value, get, set.
        /// </summary>
        /// <param name="obj"> The object to get the property values from. </param>
        /// <param name="defaults"> The values to use if the relevant value is not specified. </param>
        /// <returns> A PropertyDescriptor that corresponds to the object. </returns>
        public static PropertyDescriptor FromObject(ObjectInstance obj, PropertyDescriptor defaults)
        {
            if (obj == null)
            {
                return(PropertyDescriptor.Undefined);
            }

            // Read configurable attribute.
            bool configurable = defaults.IsConfigurable;

            if (obj.HasProperty("configurable"))
            {
                configurable = TypeConverter.ToBoolean(obj["configurable"]);
            }

            // Read writable attribute.
            bool writable = defaults.IsWritable;

            if (obj.HasProperty("writable"))
            {
                writable = TypeConverter.ToBoolean(obj["writable"]);
            }

            // Read enumerable attribute.
            bool enumerable = defaults.IsEnumerable;

            if (obj.HasProperty("enumerable"))
            {
                enumerable = TypeConverter.ToBoolean(obj["enumerable"]);
            }

            // Read property value.
            object value = defaults.Value;

            if (obj.HasProperty("value"))
            {
                value = obj["value"];
            }

            // The descriptor is an accessor if get or set is present.
            bool isAccessor = false;

            // Read get accessor.
            FunctionInstance getter = defaults.Getter;

            if (obj.HasProperty("get"))
            {
                if (obj.HasProperty("value"))
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptors cannot have both 'get' and 'value' set");
                }
                if (obj.HasProperty("writable"))
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptors with 'get' or 'set' defined must not have 'writable' set");
                }
                if (obj["get"] is FunctionInstance)
                {
                    getter = (FunctionInstance)obj["get"];
                }
                else if (TypeUtilities.IsUndefined(obj["get"]) == true)
                {
                    getter = null;
                }
                else
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptor 'get' must be a function");
                }
                isAccessor = true;
            }

            // Read set accessor.
            FunctionInstance setter = defaults.Setter;

            if (obj.HasProperty("set"))
            {
                if (obj.HasProperty("value"))
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptors cannot have both 'set' and 'value' set");
                }
                if (obj.HasProperty("writable"))
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptors with 'get' or 'set' defined must not have 'writable' set");
                }
                if (obj["set"] is FunctionInstance)
                {
                    setter = (FunctionInstance)obj["set"];
                }
                else if (TypeUtilities.IsUndefined(obj["set"]) == true)
                {
                    setter = null;
                }
                else
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Property descriptor 'set' must be a function");
                }
                isAccessor = true;
            }

            // Build up the attributes enum.
            PropertyAttributes attributes = PropertyAttributes.Sealed;

            if (configurable == true)
            {
                attributes |= PropertyAttributes.Configurable;
            }
            if (writable == true)
            {
                attributes |= PropertyAttributes.Writable;
            }
            if (enumerable == true)
            {
                attributes |= PropertyAttributes.Enumerable;
            }

            // Either a value or an accessor is possible.
            object descriptorValue = value;

            if (isAccessor == true)
            {
                descriptorValue = new PropertyAccessorValue(getter, setter);
            }

            // Create the new property descriptor.
            return(new PropertyDescriptor(descriptorValue, attributes));
        }
        public void SendEmail(ObjectInstance email)
        {
            if (email == null)
                throw new ArgumentNullException("email");

            foreach (var requiredProperty in new[] { "BodyText", "Subject", "Recipients" })
            {
                if (email.HasProperty(requiredProperty))
                    continue;
                throw new ArgumentException(string.Format("Property {0} is mandatory", requiredProperty));
            }

            MailItem newMail = application.CreateItem(OlItemType.olMailItem);

            newMail.Body = email.GetPropertyValue("BodyText").ToString();
            newMail.Subject = email.GetPropertyValue("Subject").ToString();

            var recipients = email.GetPropertyValue("Recipients").ToString().Split(',');

            foreach (var rec in recipients.Select(recipient => newMail.Recipients.Add(recipient)))
                rec.Resolve();

            newMail.Send();
        }
        //     OBJECT SERIALIZATION AND DESERIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a property descriptor from an object containing any of the following
        /// properties: configurable, writable, enumerable, value, get, set.
        /// </summary>
        /// <param name="obj"> The object to get the property values from. </param>
        /// <param name="defaults"> The values to use if the relevant value is not specified. </param>
        /// <returns> A PropertyDescriptor that corresponds to the object. </returns>
        public static PropertyDescriptor FromObject(ObjectInstance obj, PropertyDescriptor defaults)
        {
            if (obj == null)
                return PropertyDescriptor.Undefined;

            // Read configurable attribute.
            bool configurable = defaults.IsConfigurable;
            if (obj.HasProperty("configurable"))
                configurable = TypeConverter.ToBoolean(obj["configurable"]);

            // Read writable attribute.
            bool writable = defaults.IsWritable;
            if (obj.HasProperty("writable"))
                writable = TypeConverter.ToBoolean(obj["writable"]);

            // Read enumerable attribute.
            bool enumerable = defaults.IsEnumerable;
            if (obj.HasProperty("enumerable"))
                enumerable = TypeConverter.ToBoolean(obj["enumerable"]);

            // Read property value.
            object value = defaults.Value;
            if (obj.HasProperty("value"))
                value = obj["value"];

            // The descriptor is an accessor if get or set is present.
            bool isAccessor = false;

            // Read get accessor.
            FunctionInstance getter = defaults.Getter;
            if (obj.HasProperty("get"))
            {
                if (obj.HasProperty("value"))
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptors cannot have both 'get' and 'value' set");
                if (obj.HasProperty("writable"))
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptors with 'get' or 'set' defined must not have 'writable' set");
                if (obj["get"] is FunctionInstance)
                    getter = (FunctionInstance)obj["get"];
                else if (TypeUtilities.IsUndefined(obj["get"]) == true)
                    getter = null;
                else
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptor 'get' must be a function");
                isAccessor = true;
            }

            // Read set accessor.
            FunctionInstance setter = defaults.Setter;
            if (obj.HasProperty("set"))
            {
                if (obj.HasProperty("value"))
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptors cannot have both 'set' and 'value' set");
                if (obj.HasProperty("writable"))
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptors with 'get' or 'set' defined must not have 'writable' set");
                if (obj["set"] is FunctionInstance)
                    setter = (FunctionInstance)obj["set"];
                else if (TypeUtilities.IsUndefined(obj["set"]) == true)
                    setter = null;
                else
                    throw new JavaScriptException(obj.Engine, "TypeError", "Property descriptor 'set' must be a function");
                isAccessor = true;
            }

            // Build up the attributes enum.
            PropertyAttributes attributes = PropertyAttributes.Sealed;
            if (configurable == true)
                attributes |= PropertyAttributes.Configurable;
            if (writable == true)
                attributes |= PropertyAttributes.Writable;
            if (enumerable == true)
                attributes |= PropertyAttributes.Enumerable;

            // Either a value or an accessor is possible.
            object descriptorValue = value;
            if (isAccessor == true)
                descriptorValue = new PropertyAccessorValue(getter, setter);

            // Create the new property descriptor.
            return new PropertyDescriptor(descriptorValue, attributes);
        }
        public ArrayInstance GetMailsForFolder(ObjectInstance parameters)
        {
            if (parameters.HasProperty("UniqueId"))
                return GetMailsForFolderInternal(parameters.GetPropertyValue("UniqueId").ToString(), null);

            if (parameters.HasProperty("FolderName"))
            {
                var folderPath = parameters.GetPropertyValue("FolderName").ToString().Split('/');

                if (folderPath.Length != 2)
                    throw new NotSupportedException("At the moment only paths like 'Personal Folders/Foo' are supported");

                var root = folderPath.First();
                var folderName = folderPath.Last();

                var children = GetChildrenFor(root);
                foreach (FolderInstance child in children.ElementValues.Cast<FolderInstance>()
                                                            .Where(child => child.GetPropertyValue("Name").ToString() == folderName))
                    return GetMailsForFolderInternal(child.GetPropertyValue("UniqueId").ToString(), null);
                return Engine.Array.New();
            }

            throw new NotSupportedException(string.Format("Requires an object with either 'UniqueId' or 'FolderName' as parameter"));
        }