From dadeb15519afddcb158cbe3cf83a8be9a393d377 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 3 May 2024 20:37:19 +0300 Subject: [PATCH] Remove Enum.HasFlag usage (#1850) * cleanup Array capacity usage --- Jint/Native/Array/ArrayOperations.cs | 1 - Jint/Native/Array/ArrayPrototype.cs | 7 +++- .../Runtime/Descriptors/PropertyDescriptor.cs | 37 +------------------ .../Interop/ObjectWrapper.Specialized.cs | 2 +- .../Interop/Reflection/FieldAccessor.cs | 2 +- .../Expressions/JintYieldExpression.cs | 2 +- 6 files changed, 10 insertions(+), 41 deletions(-) diff --git a/Jint/Native/Array/ArrayOperations.cs b/Jint/Native/Array/ArrayOperations.cs index e03e046434..1db3e8bf59 100644 --- a/Jint/Native/Array/ArrayOperations.cs +++ b/Jint/Native/Array/ArrayOperations.cs @@ -629,7 +629,6 @@ public override void CreateDataPropertyOrThrow(ulong index, JsValue value) public override void Set(ulong index, JsValue value, bool updateLength = false, bool throwOnError = true) { - EnsureCapacity(index + 1); _target.SetAt((int)index, value); } diff --git a/Jint/Native/Array/ArrayPrototype.cs b/Jint/Native/Array/ArrayPrototype.cs index d386950eaa..890129881f 100644 --- a/Jint/Native/Array/ArrayPrototype.cs +++ b/Jint/Native/Array/ArrayPrototype.cs @@ -1019,7 +1019,12 @@ private JsValue Unshift(JsValue thisObject, JsValue[] arguments) ExceptionHelper.ThrowTypeError(_realm, "Invalid array length"); } - o.EnsureCapacity(len + argCount); + // only prepare for larger if we cannot rely on default growth algorithm + if (len + argCount > 2 * len) + { + o.EnsureCapacity(len + argCount); + } + var minIndex = o.GetSmallestIndex(len); for (var k = len; k > minIndex; k--) { diff --git a/Jint/Runtime/Descriptors/PropertyDescriptor.cs b/Jint/Runtime/Descriptors/PropertyDescriptor.cs index 3b3f290dd9..77cab8d9da 100644 --- a/Jint/Runtime/Descriptors/PropertyDescriptor.cs +++ b/Jint/Runtime/Descriptors/PropertyDescriptor.cs @@ -396,7 +396,7 @@ public bool IsAccessorDescriptor() [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsDataDescriptor() { - if (_flags.HasFlag(PropertyFlag.NonData)) + if ((_flags & PropertyFlag.NonData) != PropertyFlag.None) { return false; } @@ -415,41 +415,6 @@ public bool IsGenericDescriptor() return !IsDataDescriptor() && !IsAccessorDescriptor(); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal bool TryGetValue(ObjectInstance thisArg, out JsValue value) - { - value = JsValue.Undefined; - - // IsDataDescriptor logic inlined - if ((_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != PropertyFlag.None) - { - var val = (_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None - ? CustomValue - : _value; - - if (!ReferenceEquals(val, null)) - { - value = val; - return true; - } - } - - if (this == Undefined) - { - return false; - } - - var getter = Get; - if (!ReferenceEquals(getter, null) && !getter.IsUndefined()) - { - // if getter is not undefined it must be ICallable - var callable = (ICallable) getter; - value = callable.Call(thisArg, Arguments.Empty); - } - - return true; - } - private sealed class UndefinedPropertyDescriptor : PropertyDescriptor { public UndefinedPropertyDescriptor() : base(PropertyFlag.None | PropertyFlag.CustomJsValue) diff --git a/Jint/Runtime/Interop/ObjectWrapper.Specialized.cs b/Jint/Runtime/Interop/ObjectWrapper.Specialized.cs index 5baa4317d1..0fa0ebe667 100644 --- a/Jint/Runtime/Interop/ObjectWrapper.Specialized.cs +++ b/Jint/Runtime/Interop/ObjectWrapper.Specialized.cs @@ -81,7 +81,7 @@ public void SetAt(int index, JsValue value) { if (_engine.Options.Interop.AllowWrite) { - EnsureCapacity(index); + EnsureCapacity(index + 1); DoSetAt(index, ConvertToItemType(value)); } } diff --git a/Jint/Runtime/Interop/Reflection/FieldAccessor.cs b/Jint/Runtime/Interop/Reflection/FieldAccessor.cs index 3bc9f85a24..3d189f2996 100644 --- a/Jint/Runtime/Interop/Reflection/FieldAccessor.cs +++ b/Jint/Runtime/Interop/Reflection/FieldAccessor.cs @@ -12,7 +12,7 @@ public FieldAccessor(FieldInfo fieldInfo, PropertyInfo? indexer = null) _fieldInfo = fieldInfo; } - public override bool Writable => !_fieldInfo.Attributes.HasFlag(FieldAttributes.InitOnly); + public override bool Writable => (_fieldInfo.Attributes & FieldAttributes.InitOnly) == (FieldAttributes) 0; protected override object? DoGetValue(object target, string memberName) { diff --git a/Jint/Runtime/Interpreter/Expressions/JintYieldExpression.cs b/Jint/Runtime/Interpreter/Expressions/JintYieldExpression.cs index 37113056bd..a80ea1f1de 100644 --- a/Jint/Runtime/Interpreter/Expressions/JintYieldExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/JintYieldExpression.cs @@ -13,7 +13,7 @@ public JintYieldExpression(YieldExpression expression) : base(expression) protected override object EvaluateInternal(EvaluationContext context) { - if (!context.Engine.Options.ExperimentalFeatures.HasFlag(ExperimentalFeature.Generators)) + if ((context.Engine.Options.ExperimentalFeatures & ExperimentalFeature.Generators) == ExperimentalFeature.None) { ExceptionHelper.ThrowJavaScriptException( context.Engine.Intrinsics.Error,