SetPropertyValue() public method

Sets the value of the property with the given name. If a property with the given name does not exist, or exists in the prototype chain (and is not a setter) then a new property is created.
public SetPropertyValue ( object key, object value, bool throwOnError ) : void
key object The property key of the property to set.
value object The value to set the property to. This must be a javascript /// primitive (double, string, etc) or a class derived from .
throwOnError bool true to throw an exception if the property could not /// be set (i.e. if the property is read-only or if the object is not extensible and a new /// property needs to be created).
return void
        public static ObjectInstance Assign(ScriptEngine engine, ObjectInstance target, params object[] sources)
        {
            foreach (var rawSource in sources)
            {
                // Ignore undefined or null sources.
                if (rawSource == null || rawSource == Undefined.Value || rawSource == Null.Value)
                    continue;
                var source = TypeConverter.ToObject(engine, rawSource);

                // Copy the enumerable properties from the source object.
                foreach (var property in source.Properties)
                    if (property.IsEnumerable == true)
                        target.SetPropertyValue(property.Key, property.Value, throwOnError: true);
            }
            return target;
        }
Beispiel #2
0
        public static ObjectInstance Assign(ScriptEngine engine, ObjectInstance target, params object[] sources)
        {
            foreach (var rawSource in sources)
            {
                // Ignore undefined or null sources.
                if (rawSource == null || rawSource == Undefined.Value || rawSource == Null.Value)
                {
                    continue;
                }
                var source = TypeConverter.ToObject(engine, rawSource);

                // Copy the enumerable properties from the source object.
                foreach (var property in source.Properties)
                {
                    if (property.IsEnumerable == true)
                    {
                        target.SetPropertyValue(property.Key, property.Value, target, throwOnError: true);
                    }
                }
            }
            return(target);
        }
Beispiel #3
0
        /// <summary>
        /// Sets the value of the property with the given name.  If a property with the given name
        /// does not exist, or exists in the prototype chain (and is not a setter) then a new
        /// property is created.
        /// </summary>
        /// <param name="key"> The property key of the property to set. </param>
        /// <param name="value"> The value to set the property to.  This must be a javascript
        /// primitive (double, string, etc) or a class derived from <see cref="ObjectInstance"/>. </param>
        /// <param name="thisValue"> The value of the "this" keyword inside a setter. </param>
        /// <param name="throwOnError"> <c>true</c> to throw an exception if the property could not
        /// be set (i.e. if the property is read-only or if the object is not extensible and a new
        /// property needs to be created). </param>
        /// <returns> <c>false</c> if <paramref name="throwOnError"/> is false and an error
        /// occurred; <c>true</c> otherwise. </returns>
        public override bool SetPropertyValue(object key, object value, object thisValue, bool throwOnError)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'set' on a proxy that has been revoked.");
            }

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

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

            if (!result)
            {
                return(false);
            }

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

            if (targetDescriptor.Exists && !targetDescriptor.IsConfigurable)
            {
                if (!targetDescriptor.IsAccessor && !targetDescriptor.IsWritable && !TypeComparer.SameValue(value, targetDescriptor.Value))
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"'set' on proxy: trap returned truish for property '{TypeConverter.ToString(key)}' which exists in the proxy target as a non-configurable and non-writable data property with a different value.");
                }
                if (targetDescriptor.IsAccessor && targetDescriptor.Setter == null)
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"'set' on proxy: trap returned truish for property '{TypeConverter.ToString(key)}' which exists in the proxy target as a non-configurable and non-writable accessor property without a setter.");
                }
            }
            return(true);
        }
        public static ObjectInstance Assign(ScriptEngine engine, ObjectInstance target, params object[] sources)
        {
            foreach (var rawSource in sources)
            {
                // Ignore undefined or null sources.
                if (rawSource == null || rawSource == Undefined.Value || rawSource == Null.Value)
                {
                    continue;
                }
                var source = TypeConverter.ToObject(engine, rawSource);

                // Copy the enumerable properties from the source object.
                foreach (var key in source.OwnKeys)
                {
                    var descriptor = source.GetOwnPropertyDescriptor(key);
                    if (descriptor.IsEnumerable)
                    {
                        // Spec says call [[Get]] instead of using the value from the descriptor.
                        target.SetPropertyValue(key, source[key], target, throwOnError: true);
                    }
                }
            }
            return(target);
        }
Beispiel #5
0
 /// <summary>
 /// Sets the number of items in the array.
 /// </summary>
 /// <param name="thisObj"> The array that is being operated on. </param>
 /// <param name="value"> The new value of the length property. </param>
 private static void SetLength(ObjectInstance thisObj, uint value)
 {
     if (thisObj is ArrayInstance)
         ((ArrayInstance)thisObj).Length = value;
     else
         thisObj.SetPropertyValue("length", (double)value, true);
 }
Beispiel #6
0
 public static bool Set(ObjectInstance target, object propertyKey, object value, object receiver)
 {
     propertyKey = TypeConverter.ToPropertyKey(propertyKey);
     return(target.SetPropertyValue(propertyKey, value, throwOnError: false));
 }