public static FromObject ( |
||
obj | The object to get the property values from. | |
defaults | The values to use if the relevant value is not specified. | |
return |
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); }
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); }
/// <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)}'."); } }