Browse Source

Added readonly modifier to fields that aren't being changed (#1610)

Co-authored-by: Lehonti Ramos <john@doe>
Lehonti Ramos 2 years ago
parent
commit
2ebfedf76e

+ 1 - 1
Jint.Benchmark/DromaeoBenchmark.cs

@@ -16,7 +16,7 @@ public class DromaeoBenchmark
         {"dromaeo-string-base64", null}
     };
 
-    private Dictionary<string, Script> _prepared = new();
+    private readonly Dictionary<string, Script> _prepared = new();
 
     private Engine engine;
 

+ 1 - 1
Jint.Benchmark/EngineConstructionBenchmark.cs

@@ -21,4 +21,4 @@ public class EngineConstructionBenchmark
         var engine = new Engine();
         return engine.Evaluate(_program).AsNumber();
     }
-}
+}

+ 4 - 4
Jint.Tests/Runtime/ExtensionMethods/ObservableExtensions.cs

@@ -6,7 +6,7 @@ namespace Jint.Tests.Runtime.ExtensionMethods
         private readonly Action<Exception> onError;
         private readonly Action onCompleted;
 
-        int isStopped = 0;
+        readonly int isStopped = 0;
 
         public Subscribe(Action<T> onNext, Action<Exception> onError, Action onCompleted)
         {
@@ -65,7 +65,7 @@ namespace Jint.Tests.Runtime.ExtensionMethods
 
     public class BaseObservable<T> : IObservable<T>
     {
-        private List<IObserver<T>> observers = new List<IObserver<T>>();
+        private readonly List<IObserver<T>> observers = new List<IObserver<T>>();
 
         public T Last { get; private set; }
 
@@ -78,8 +78,8 @@ namespace Jint.Tests.Runtime.ExtensionMethods
 
         private class Unsubscriber : IDisposable
         {
-            private List<IObserver<T>> _observers;
-            private IObserver<T> _observer;
+            private readonly List<IObserver<T>> _observers;
+            private readonly IObserver<T> _observer;
 
             public Unsubscriber(List<IObserver<T>> observers, IObserver<T> observer)
             {

+ 1 - 1
Jint.Tests/Runtime/GenericMethodTests.cs

@@ -140,7 +140,7 @@ public class GenericMethodTests
 
     public class TestGenericBaseClass<T>
     {
-        private System.Collections.Generic.List<T> _list = new System.Collections.Generic.List<T>();
+        private readonly System.Collections.Generic.List<T> _list = new System.Collections.Generic.List<T>();
 
         public int Count
         {

+ 1 - 1
Jint.Tests/Runtime/InteropTests.MemberAccess.cs

@@ -200,7 +200,7 @@ namespace Jint.Tests.Runtime
 
         private class CustomDictionary<TValue> : IDictionary<string, TValue>
         {
-            Dictionary<string, TValue> _dictionary = new Dictionary<string, TValue>();
+            readonly Dictionary<string, TValue> _dictionary = new Dictionary<string, TValue>();
 
             public TValue this[string key]
             {

+ 1 - 1
Jint.Tests/Runtime/JsValueConversionTests.cs

@@ -5,7 +5,7 @@ namespace Jint.Tests.Runtime
 {
     public class JsValueConversionTests
     {
-        private Engine _engine;
+        private readonly Engine _engine;
 
         public JsValueConversionTests()
         {

+ 1 - 1
Jint/Agent.cs

@@ -7,7 +7,7 @@ namespace Jint;
 /// </summary>
 internal sealed class Agent
 {
-    private List<JsValue> _keptAlive = new();
+    private readonly List<JsValue> _keptAlive = new();
 
     public void AddToKeptObjects(JsValue target)
     {

+ 8 - 5
Jint/Native/Number/Dtoa/Bignum.cs

@@ -24,7 +24,7 @@ namespace Jint.Native.Number.Dtoa
         // grow. There are no checks if the stack-allocated space is sufficient.
         private const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
 
-        private uint[] bigits_ = new uint[kBigitCapacity];
+        private readonly uint[] bigits_ = new uint[kBigitCapacity];
 
         // The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize).
         private int exponent_;
@@ -429,7 +429,8 @@ namespace Jint.Native.Number.Dtoa
         internal void MultiplyByUInt64(ulong factor)
         {
             if (factor == 1) return;
-            if (factor == 0) {
+            if (factor == 0)
+            {
                 Zero();
                 return;
             }
@@ -437,7 +438,8 @@ namespace Jint.Native.Number.Dtoa
             ulong carry = 0;
             ulong low = factor & 0xFFFFFFFF;
             ulong high = factor >> 32;
-            for (int i = 0; i < used_digits_; ++i) {
+            for (int i = 0; i < used_digits_; ++i)
+            {
                 ulong product_low = low * bigits_[i];
                 ulong product_high = high * bigits_[i];
                 ulong tmp = (carry & kBigitMask) + product_low;
@@ -445,7 +447,8 @@ namespace Jint.Native.Number.Dtoa
                 carry = (carry >> kBigitSize) + (tmp >> kBigitSize) +
                         (product_high << (32 - kBigitSize));
             }
-            while (carry != 0) {
+            while (carry != 0)
+            {
                 EnsureCapacity(used_digits_ + 1);
                 bigits_[used_digits_] = (uint) (carry & kBigitMask);
                 used_digits_++;
@@ -655,4 +658,4 @@ namespace Jint.Native.Number.Dtoa
             Clamp();
         }
     }
-}
+}

+ 1 - 1
Jint/Runtime/Interpreter/JintStatementList.cs

@@ -19,7 +19,7 @@ namespace Jint.Runtime.Interpreter
 
         private Pair[]? _jintStatements;
         private bool _initialized;
-        private uint _index;
+        private readonly uint _index;
         private readonly bool _generator;
 
         public JintStatementList(IFunction function)

+ 1 - 1
Jint/Runtime/Modules/CyclicModuleRecord.cs

@@ -29,7 +29,7 @@ public abstract class CyclicModuleRecord : ModuleRecord
     protected bool _hasTLA;
     private bool _asyncEvaluation;
     private PromiseCapability _topLevelCapability;
-    private List<CyclicModuleRecord> _asyncParentModules;
+    private readonly List<CyclicModuleRecord> _asyncParentModules;
     private int _asyncEvalOrder;
     private int _pendingAsyncDependencies;