/// <summary> /// https://tc39.es/ecma262/#sec-setintegritylevel /// </summary> private static bool SetIntegrityLevel(ObjectInstance o, IntegrityLevel level) { var status = o.PreventExtensions(); if (!status) { return(false); } var keys = o.GetOwnPropertyKeys(); if (level == IntegrityLevel.Sealed) { for (var i = 0; i < keys.Count; i++) { var k = keys[i]; o.DefinePropertyOrThrow(k, new PropertyDescriptor { Configurable = false }); } } else { for (var i = 0; i < keys.Count; i++) { var k = keys[i]; var currentDesc = o.GetOwnProperty(k); if (currentDesc != PropertyDescriptor.Undefined) { PropertyDescriptor desc; if (currentDesc.IsAccessorDescriptor()) { desc = new PropertyDescriptor { Configurable = false }; } else { desc = new PropertyDescriptor { Configurable = false, Writable = false }; } o.DefinePropertyOrThrow(k, desc); } } } return(true); }
/// <summary> /// https://tc39.es/ecma262/#sec-objectdefineproperties /// </summary> private JsValue ObjectDefineProperties(ObjectInstance o, JsValue properties) { var props = TypeConverter.ToObject(_realm, properties); var keys = props.GetOwnPropertyKeys(); var descriptors = new List <KeyValuePair <JsValue, PropertyDescriptor> >(); for (var i = 0; i < keys.Count; i++) { var nextKey = keys[i]; var propDesc = props.GetOwnProperty(nextKey); if (propDesc == PropertyDescriptor.Undefined || !propDesc.Enumerable) { continue; } var descObj = props.UnwrapJsValue(propDesc); var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, descObj); descriptors.Add(new KeyValuePair <JsValue, PropertyDescriptor>(nextKey, desc)); } foreach (var pair in descriptors) { o.DefinePropertyOrThrow(pair.Key, pair.Value); } return(o); }