Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ReferenceEquals usage with is (not) null #1906

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Jint.Tests/Runtime/ExecutionConstraintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ function sleep(millisecondsTimeout) {

sleep(100);
waitHandle.Set();
sleep(5000);
sleep(1000);
sleep(1000);
sleep(1000);
sleep(1000);
sleep(1000);
");
}
}
Expand Down
8 changes: 4 additions & 4 deletions Jint/Native/Array/ArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
a = ArrayCreate(length);
}

var args = !ReferenceEquals(callable, null)
var args = callable is not null
? _engine._jsValueArrayPool.RentArray(2)
: null;

Expand All @@ -119,7 +119,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
for (uint i = 0; i < length; i++)
{
var value = source.Get(i);
if (!ReferenceEquals(callable, null))
if (callable is not null)
{
args![0] = value;
args[1] = i;
Expand All @@ -133,7 +133,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
n++;
}

if (!ReferenceEquals(callable, null))
if (callable is not null)
{
_engine._jsValueArrayPool.ReturnArray(args!);
}
Expand Down Expand Up @@ -165,7 +165,7 @@ protected override void ProcessItem(JsValue[] arguments, JsValue currentValue)
{
_index++;
JsValue jsValue;
if (!ReferenceEquals(_callable, null))
if (_callable is not null)
{
arguments[0] = currentValue;
arguments[1] = _index;
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public sealed override bool DefineOwnProperty(JsValue property, PropertyDescript
private bool DefineLength(PropertyDescriptor desc)
{
var value = desc.Value;
if (ReferenceEquals(value, null))
if (value is null)
{
return base.DefineOwnProperty(CommonProperties.Length, desc);
}
Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public ObjectOperations(ObjectInstance target) : base(target)
private double GetIntegerLength()
{
var descValue = _target.Get(CommonProperties.Length);
if (!ReferenceEquals(descValue, null))
if (descValue is not null)
{
return TypeConverter.ToInteger(descValue);
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public override uint GetLength()
}

var descValue = _target.Get(CommonProperties.Length);
if (!ReferenceEquals(descValue, null))
if (descValue is not null)
{
return (uint) TypeConverter.ToInteger(descValue);
}
Expand Down
6 changes: 3 additions & 3 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ private JsValue Every(JsValue thisObject, JsValue[] arguments)
args[0] = kvalue;
args[1] = k;
var testResult = callable.Call(thisArg, args);
if (false == TypeConverter.ToBoolean(testResult))
if (!TypeConverter.ToBoolean(testResult))
{
return JsBoolean.False;
}
Expand Down Expand Up @@ -1695,8 +1695,8 @@ private ArrayComparer(Engine? engine, ICallable? compare)

public int Compare(JsValue? x, JsValue? y)
{
var xIsNull = ReferenceEquals(x, null);
var yIsNull = ReferenceEquals(y, null);
var xIsNull = x is null;
var yIsNull = y is null;

if (xIsNull)
{
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Error/ErrorPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void Initialize()
public JsValue ToString(JsValue thisObject, JsValue[] arguments)
{
var o = thisObject.TryCast<ObjectInstance>();
if (ReferenceEquals(o, null))
if (o is null)
{
ExceptionHelper.ThrowTypeError(_realm);
}
Expand Down
8 changes: 4 additions & 4 deletions Jint/Native/JsArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override PropertyDescriptor GetOwnProperty(JsValue property)
{
EnsureInitialized();

if (!ReferenceEquals(ParameterMap, null))
if (ParameterMap is not null)
{
var desc = base.GetOwnProperty(property);
if (desc == PropertyDescriptor.Undefined)
Expand Down Expand Up @@ -182,7 +182,7 @@ public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc

EnsureInitialized();

if (!ReferenceEquals(ParameterMap, null))
if (ParameterMap is not null)
{
var map = ParameterMap;
var isMapped = map.GetOwnProperty(property);
Expand All @@ -201,7 +201,7 @@ public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc
else
{
var descValue = desc.Value;
if (!ReferenceEquals(descValue, null) && !descValue.IsUndefined())
if (descValue is not null && !descValue.IsUndefined())
{
map.Set(property, descValue, false);
}
Expand All @@ -223,7 +223,7 @@ public override bool Delete(JsValue property)
{
EnsureInitialized();

if (!ReferenceEquals(ParameterMap, null))
if (ParameterMap is not null)
{
var map = ParameterMap;
var isMapped = map.GetOwnProperty(property);
Expand Down
32 changes: 16 additions & 16 deletions Jint/Native/Object/ObjectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public bool TryGetValue(JsValue property, out JsValue value)
if (desc != PropertyDescriptor.Undefined)
{
var descValue = desc.Value;
if (desc.WritableSet && !ReferenceEquals(descValue, null))
if (desc.WritableSet && descValue is not null)
{
value = descValue;
return true;
Expand Down Expand Up @@ -600,7 +600,7 @@ internal bool CanPut(JsValue property)
if (desc.IsAccessorDescriptor())
{
var set = desc.Set;
if (ReferenceEquals(set, null) || set.IsUndefined())
if (set is null || set.IsUndefined())
{
return false;
}
Expand All @@ -611,7 +611,7 @@ internal bool CanPut(JsValue property)
return desc.Writable;
}

if (ReferenceEquals(Prototype, null))
if (Prototype is null)
{
return Extensible;
}
Expand All @@ -626,7 +626,7 @@ internal bool CanPut(JsValue property)
if (inherited.IsAccessorDescriptor())
{
var set = inherited.Set;
if (ReferenceEquals(set, null) || set.IsUndefined())
if (set is null || set.IsUndefined())
{
return false;
}
Expand Down Expand Up @@ -781,9 +781,9 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa

// 4. If every field in Desc is absent, return true.
if ((current._flags & (PropertyFlag.ConfigurableSet | PropertyFlag.EnumerableSet | PropertyFlag.WritableSet)) == PropertyFlag.None &&
ReferenceEquals(currentGet, null) &&
ReferenceEquals(currentSet, null) &&
ReferenceEquals(currentValue, null))
currentGet is null &&
currentSet is null &&
currentValue is null)
{
return true;
}
Expand All @@ -795,9 +795,9 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa
current.Configurable == desc.Configurable && current.ConfigurableSet == desc.ConfigurableSet &&
current.Writable == desc.Writable && current.WritableSet == desc.WritableSet &&
current.Enumerable == desc.Enumerable && current.EnumerableSet == desc.EnumerableSet &&
((ReferenceEquals(currentGet, null) && ReferenceEquals(descGet, null)) || (!ReferenceEquals(currentGet, null) && !ReferenceEquals(descGet, null) && SameValue(currentGet, descGet))) &&
((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && SameValue(currentSet, descSet))) &&
((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && currentValue == descValue))
((currentGet is null && descGet is null) || (currentGet is not null && descGet is not null && SameValue(currentGet, descGet))) &&
((currentSet is null && descSet is null) || (currentSet is not null && descSet is not null && SameValue(currentSet, descSet))) &&
((currentValue is null && descValue is null) || (currentValue is not null && descValue is not null && currentValue == descValue))
)
{
return true;
Expand Down Expand Up @@ -856,7 +856,7 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa

if (!current.Writable)
{
if (!ReferenceEquals(descValue, null) && !SameValue(descValue, currentValue!))
if (descValue is not null && !SameValue(descValue, currentValue!))
{
return false;
}
Expand All @@ -867,9 +867,9 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa
{
if (!current.Configurable)
{
if ((!ReferenceEquals(descSet, null) && !SameValue(descSet, currentSet ?? Undefined))
if ((descSet is not null && !SameValue(descSet, currentSet ?? Undefined))
||
(!ReferenceEquals(descGet, null) && !SameValue(descGet, currentGet ?? Undefined)))
(descGet is not null && !SameValue(descGet, currentGet ?? Undefined)))
{
return false;
}
Expand All @@ -879,7 +879,7 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa

if (o is not null)
{
if (!ReferenceEquals(descValue, null))
if (descValue is not null)
{
current.Value = descValue;
}
Expand All @@ -900,13 +900,13 @@ protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsVa
}

PropertyDescriptor? mutable = null;
if (!ReferenceEquals(descGet, null))
if (descGet is not null)
{
mutable = new GetSetPropertyDescriptor(mutable ?? current);
((GetSetPropertyDescriptor) mutable).SetGet(descGet);
}

if (!ReferenceEquals(descSet, null))
if (descSet is not null)
{
mutable = new GetSetPropertyDescriptor(mutable ?? current);
((GetSetPropertyDescriptor) mutable).SetSet(descSet);
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Object/ObjectPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
{
v = v.Prototype;

if (ReferenceEquals(v, null))
if (v is null)
{
return JsBoolean.False;
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/String/StringPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private JsValue ToStringString(JsValue thisObject, JsValue[] arguments)
}

var s = TypeConverter.ToObject(_realm, thisObject) as StringInstance;
if (ReferenceEquals(s, null))
if (s is null)
{
ExceptionHelper.ThrowTypeError(_realm);
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Pooling/ArgumentsInstancePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public JsArguments Rent(

public void Return(JsArguments instance)
{
if (ReferenceEquals(instance, null))
if (instance is null)
{
return;
}
Expand Down
8 changes: 4 additions & 4 deletions Jint/Pooling/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ internal T Allocate()
// We will interlock only when we have a candidate. in a worst case we may miss some
// recently returned objects. Not a big deal.
T inst = _firstItem;
if (!ReferenceEquals(inst, null))
if (inst is not null)
{
_firstItem = null;
return inst;
Expand All @@ -163,7 +163,7 @@ private T AllocateSlow()
for (int i = 0; i < items.Length; i++)
{
T inst = items[i].Value;
if (!ReferenceEquals(inst, null))
if (inst is not null)
{
items[i].Value = null;
return inst;
Expand All @@ -186,7 +186,7 @@ internal void Free(T obj)
Validate(obj);
ForgetTrackedObject(obj);

if (ReferenceEquals(_firstItem, null))
if (_firstItem is null)
{
// Intentionally not using interlocked here.
// In a worst case scenario two objects may be stored into same slot.
Expand Down Expand Up @@ -267,7 +267,7 @@ private void Validate(object obj)
for (int i = 0; i < items.Length; i++)
{
var value = items[i].Value;
if (ReferenceEquals(value, null))
if (value is null)
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions Jint/Runtime/Descriptors/PropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static JsValue FromPropertyDescriptor(Engine engine, PropertyDescriptor d
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsAccessorDescriptor()
{
return !ReferenceEquals(Get, null) || !ReferenceEquals(Set, null);
return Get is not null || Set is not null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -401,8 +401,8 @@ public bool IsDataDescriptor()
return false;
}
return (_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != PropertyFlag.None
|| (_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None && !ReferenceEquals(CustomValue, null)
|| !ReferenceEquals(_value, null);
|| (_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None && CustomValue is not null
|| _value is not null;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Jint/Runtime/Environments/JintEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ record = env;
return env.HasBinding(name);
}

while (!ReferenceEquals(record, null))
while (record is not null)
{
if (record.HasBinding(name))
{
Expand All @@ -46,7 +46,7 @@ record = env;
return ((GlobalEnvironment) env).TryGetBinding(name, out value);
}

while (!ReferenceEquals(record, null))
while (record is not null)
{
if (record.TryGetBinding(name, out value))
{
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Environments/ObjectEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal override JsValue GetBindingValue(Key name, bool strict)

internal override string[] GetAllBindingNames()
{
if (!ReferenceEquals(_bindingObject, null))
if (_bindingObject is not null)
{
var names = new List<string>(_bindingObject._properties?.Count ?? 0);
foreach (var name in _bindingObject.GetOwnProperties())
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interop/NamespaceReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)

var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();

if (ReferenceEquals(typeReference, null))
if (typeReference is null)
{
return Undefined;
}
Expand Down