Skip to content
Closed
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
171 changes: 75 additions & 96 deletions src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
private const long MaxArrayLength = 9007199254740991L; // 2^53 - 1

public static void AddArrayMethods(IJsPropertyAccessor array, RealmState? realm = null,

Check failure on line 14 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fb&open=AZruRP3sagPv8a_hS7fb&pullRequest=212
JsObject? prototypeOverride = null)
{
// Once the shared Array prototype has been initialized, new arrays
Expand Down Expand Up @@ -52,7 +52,7 @@
{
Value = "lastIndexOf", Writable = false, Enumerable = false, Configurable = true
});
lastIndexOf.DefineProperty("length",

Check warning on line 55 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using this literal 'length' 47 times.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fY&open=AZruRP3sagPv8a_hS7fY&pullRequest=212
new PropertyDescriptor { Value = 1d, Writable = false, Enumerable = false, Configurable = true });
var lastIndexDescriptor = new PropertyDescriptor
{
Expand Down Expand Up @@ -102,7 +102,7 @@
DefineArrayIteratorFunction("keys", (_, _) => idx => (double)idx);

// values() - returns an iterator of values
var valuesFn = DefineArrayIteratorFunction("values", (accessor, _) => idx =>

Check warning on line 105 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unused local variable 'valuesFn'.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fe&open=AZruRP3sagPv8a_hS7fe&pullRequest=212
{
var key = idx.ToString(CultureInfo.InvariantCulture);
return GetElementOrUndefined(accessor, key);
Expand All @@ -121,7 +121,7 @@
return Math.Min(truncated, (double)MaxArrayLength); // 2^53 - 1
}

static object CreateArrayIterator(object? thisValue, IJsPropertyAccessor accessor,

Check warning on line 124 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused method parameter 'thisValue'.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fc&open=AZruRP3sagPv8a_hS7fc&pullRequest=212
Func<uint, object?> projector)
{
var iterator = new JsObject();
Expand All @@ -141,7 +141,7 @@
if (exhausted)
{
var doneResult = new JsObject();
doneResult.SetProperty("value", Symbol.Undefined);

Check warning on line 144 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using this literal 'value' 5 times.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fZ&open=AZruRP3sagPv8a_hS7fZ&pullRequest=212
doneResult.SetProperty("done", true);
return doneResult;
}
Expand Down Expand Up @@ -456,7 +456,7 @@
return InvokeDefaultObjectToString(target, realm);
}

private static object? ArrayIncludes(object? thisValue, IReadOnlyList<object?> args, RealmState? realm)

Check failure on line 459 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fd&open=AZruRP3sagPv8a_hS7fd&pullRequest=212
{
var accessor = EnsureArrayLikeReceiver(thisValue, "Array.prototype.includes", realm);

Expand Down Expand Up @@ -666,26 +666,7 @@
firstElement = firstValue;
}

for (long k = 1; k < length; k++)
{
var fromKey = ToIndexString(k);
var toKey = ToIndexString(k - 1);
var fromExists = TryGetExistingElement(accessor, fromKey, out var fromValue);
if (fromExists)
{
accessor.SetProperty(toKey, fromValue);
}
else
{
var toExists = HasProperty(accessor, toKey);
DeletePropertyOrThrow(objectLike, toKey, toExists, MethodName, realm);
}
}

var lastKey = ToIndexString(length - 1);
var lastExists = HasProperty(accessor, lastKey);
DeletePropertyOrThrow(objectLike, lastKey, lastExists, MethodName, realm);
accessor.SetProperty("length", (double)(length - 1));
MoveArrayRange(accessor, objectLike, MethodName, length, 1, length - 1, -1, -1, realm);
return firstElement;
}

Expand All @@ -698,34 +679,13 @@
var length = (long)ToLengthOrZero(lengthValue);
var argCount = args.Count;

if (length + argCount > MaxArrayLength)
{
throw ThrowTypeError("Array.prototype.unshift cannot exceed 2^53 - 1 elements", realm: realm);
}

for (long k = length - 1; k >= 0; k--)
{
var fromKey = ToIndexString(k);
var toKey = ToIndexString(k + argCount);
var fromExists = TryGetExistingElement(accessor, fromKey, out var fromValue);
if (fromExists)
{
accessor.SetProperty(toKey, fromValue);
}
else
{
var toExists = HasProperty(accessor, toKey);
DeletePropertyOrThrow(objectLike, toKey, toExists, MethodName, realm);
}
}

var newLength = MoveArrayRange(accessor, objectLike, MethodName, length, 0, length, argCount, argCount, realm,
overflowMessage: "Array.prototype.unshift cannot exceed 2^53 - 1 elements");
for (var j = 0; j < argCount; j++)
{
accessor.SetProperty(ToIndexString(j), args[j]);
}

var newLength = length + argCount;
accessor.SetProperty("length", (double)newLength);
return (double)newLength;
}

Expand Down Expand Up @@ -772,12 +732,6 @@
actualDeleteCount = (long)bounded;
}

var newLength = length - actualDeleteCount + insertCount;
if (newLength > MaxArrayLength)
{
throw ThrowRangeError("Array length exceeds 2^53 - 1", realm: realm);
}

var result = ArraySpeciesCreate(thisValue, actualDeleteCount, realm);
for (long k = 0; k < actualDeleteCount; k++)
{
Expand All @@ -786,53 +740,11 @@

SetArrayLikeLength(result, actualDeleteCount);

if (insertCount < actualDeleteCount)
{
for (long k = actualStart; k < length - actualDeleteCount; k++)
{
var from = k + actualDeleteCount;
var to = k + insertCount;
var fromKey = ToIndexString(from);
var toKey = ToIndexString(to);

if (TryGetExistingElement(accessor, fromKey, out var fromValue))
{
accessor.SetProperty(toKey, fromValue);
}
else
{
var toExists = HasProperty(accessor, toKey);
DeletePropertyOrThrow(objectLike, toKey, toExists, MethodName, realm);
}
}

for (long k = length; k > length - (actualDeleteCount - insertCount); k--)
{
var key = ToIndexString(k - 1);
var existed = HasProperty(accessor, key);
DeletePropertyOrThrow(objectLike, key, existed, MethodName, realm);
}
}
else if (insertCount > actualDeleteCount)
{
for (long k = length - actualDeleteCount; k > actualStart; k--)
{
var from = k + actualDeleteCount - 1;
var to = k + insertCount - 1;
var fromKey = ToIndexString(from);
var toKey = ToIndexString(to);

if (TryGetExistingElement(accessor, fromKey, out var fromValue))
{
accessor.SetProperty(toKey, fromValue);
}
else
{
var toExists = HasProperty(accessor, toKey);
DeletePropertyOrThrow(objectLike, toKey, toExists, MethodName, realm);
}
}
}
var lengthDelta = insertCount - actualDeleteCount;
var tailStart = actualStart + actualDeleteCount;
var tailCount = Math.Max(length - tailStart, 0);
var newLength = MoveArrayRange(accessor, objectLike, MethodName, length, tailStart, tailCount, lengthDelta,
lengthDelta, realm, overflowIsRangeError: true);

for (var j = 0; j < insertCount; j++)
{
Expand All @@ -843,7 +755,7 @@
return result;
}

private static object? ArrayConcat(object? thisValue, IReadOnlyList<object?> args, RealmState? realm)

Check failure on line 758 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7ff&open=AZruRP3sagPv8a_hS7ff&pullRequest=212
{
var accessor = EnsureArrayLikeReceiver(thisValue, "Array.prototype.concat", realm);
var result = ArraySpeciesCreate(thisValue, 0, realm);
Expand Down Expand Up @@ -873,7 +785,7 @@
var toKey = ToIndexString(resultIndex);
if (TryGetExistingElement(spreadAccessor, fromKey, out var value))
{
result.SetProperty(toKey, value);

Check warning on line 788 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 788 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'result' is null on at least one execution path.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fi&open=AZruRP3sagPv8a_hS7fi&pullRequest=212
}
else if (result is not null)
{
Expand All @@ -890,11 +802,11 @@
throw ThrowTypeError("Array length exceeds 2^53 - 1", realm: realm);
}

result.SetProperty(ToIndexString(resultIndex++), sourceValue);

Check warning on line 805 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 805 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'result' is null on at least one execution path.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fj&open=AZruRP3sagPv8a_hS7fj&pullRequest=212
}
}

SetArrayLikeLength(result, resultIndex);

Check warning on line 809 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'target' in 'void StandardLibrary.SetArrayLikeLength(IJsPropertyAccessor target, long length)'.
return result;
}

Expand Down Expand Up @@ -939,7 +851,7 @@
continue;
}

if (lowerExists && !upperExists)

Check warning on line 854 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Change this condition so that it does not always evaluate to 'False'.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fk&open=AZruRP3sagPv8a_hS7fk&pullRequest=212
{
DeletePropertyOrThrow(objectLike, lowerKey, lowerExists, MethodName, realm);
accessor.SetProperty(upperKey, lowerValue);
Expand All @@ -953,7 +865,7 @@
return accessor;
}

private static object? ArraySort(object? thisValue, IReadOnlyList<object?> args, RealmState? realm)

Check failure on line 868 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fg&open=AZruRP3sagPv8a_hS7fg&pullRequest=212
{
var accessor = EnsureArrayLikeReceiver(thisValue, "Array.prototype.sort", realm);
var objectLike = accessor as IJsObjectLike;
Expand Down Expand Up @@ -986,7 +898,7 @@
return 0;
}

return d > 0 ? 1 : d < 0 ? -1 : 0;

Check warning on line 901 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fh&open=AZruRP3sagPv8a_hS7fh&pullRequest=212
}

var aStr = JsValueToString(a);
Expand Down Expand Up @@ -1204,7 +1116,7 @@
return target;
}

private static object? ArrayToSorted(object? thisValue, IReadOnlyList<object?> args, RealmState? realm)

Check failure on line 1119 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fl&open=AZruRP3sagPv8a_hS7fl&pullRequest=212
{
var accessor = EnsureArrayLikeReceiver(thisValue, "Array.prototype.toSorted", realm);
var lengthValue = accessor.TryGetProperty("length", out var lenVal) ? lenVal : 0d;
Expand Down Expand Up @@ -1234,7 +1146,7 @@
return 0;
}

return d > 0 ? 1 : d < 0 ? -1 : 0;

Check warning on line 1149 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fm&open=AZruRP3sagPv8a_hS7fm&pullRequest=212

});
}
Expand Down Expand Up @@ -1262,7 +1174,7 @@
result?.Delete(key);
}

SetArrayLikeLength(result, length);

Check warning on line 1177 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'target' in 'void StandardLibrary.SetArrayLikeLength(IJsPropertyAccessor target, long length)'.
return result;
}

Expand Down Expand Up @@ -1455,7 +1367,7 @@
}

var asNumber = JsOps.ToNumber(lengthVal);
if (double.IsNaN(asNumber) || !(asNumber > 0))

Check warning on line 1370 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the opposite operator ('<=') instead.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fn&open=AZruRP3sagPv8a_hS7fn&pullRequest=212
{
return 0;
}
Expand Down Expand Up @@ -1501,7 +1413,7 @@
}
}

public static HostFunction CreateArrayConstructor(RealmState realm)

Check failure on line 1416 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 29 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fr&open=AZruRP3sagPv8a_hS7fr&pullRequest=212
{
JsObject? arrayPrototype = null;

Expand Down Expand Up @@ -1663,7 +1575,7 @@
return arrayConstructor;
}

private static object? ArrayOf(HostFunction host, object? thisValue, IReadOnlyList<object?> args, RealmState? realm)

Check warning on line 1578 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused method parameter 'host'.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7ft&open=AZruRP3sagPv8a_hS7ft&pullRequest=212
{
const string MethodName = "Array.of";
var len = args.Count;
Expand Down Expand Up @@ -1698,7 +1610,7 @@
}
}

private static object? ArrayFrom(HostFunction host, object? thisValue, IReadOnlyList<object?> args,

Check failure on line 1613 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fp&open=AZruRP3sagPv8a_hS7fp&pullRequest=212
RealmState? realm)
{
const string MethodName = "Array.from";
Expand Down Expand Up @@ -1750,7 +1662,7 @@

var key = ToIndexString(k);
var value = GetElementOrUndefined(arrayLike, key);
var mapped = mapping && mapper is not null

Check warning on line 1665 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Change this condition so that it does not always evaluate to 'True'.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fu&open=AZruRP3sagPv8a_hS7fu&pullRequest=212
? InvokeArrayFromMapper(mapper, host, thisArg, value, k)
: value;
CreateDataPropertyOrThrow(result, key, mapped, realm, MethodName);
Expand Down Expand Up @@ -1838,7 +1750,7 @@
return promise.JsObject;
}

private static object? ArrayFromIterable(HostFunction host, object? thisValue, object? items,

Check failure on line 1753 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fo&open=AZruRP3sagPv8a_hS7fo&pullRequest=212
IJsCallable iteratorMethod, IJsCallable? mapper, bool mapping, object? thisArg, RealmState? realm)

Check warning on line 1754 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Method has 8 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fs&open=AZruRP3sagPv8a_hS7fs&pullRequest=212
{
const string MethodName = "Array.from";
Expand Down Expand Up @@ -1916,7 +1828,7 @@
}
}

private static bool TryAwaitPromiseLike(object? candidate, RealmState? realm, Action<object?> onFulfilled,

Check failure on line 1831 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fq&open=AZruRP3sagPv8a_hS7fq&pullRequest=212
Action<object?> onRejected)
{
if (candidate is JsObject jsObject &&
Expand Down Expand Up @@ -2048,8 +1960,8 @@
private bool _awaitIteratorResult;
private IJsPropertyAccessor? _arrayLike;

public ArrayFromAsyncOperation(HostFunction host, RealmState realm, JsPromise promise, IJsObjectLike result,
bool mapping, IJsCallable? mapper, object? thisArg, string methodName)

Check warning on line 1964 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Constructor has 8 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fv&open=AZruRP3sagPv8a_hS7fv&pullRequest=212
{
_host = host;
_realm = realm;
Expand Down Expand Up @@ -2385,7 +2297,7 @@
private static readonly string SymbolIsConcatSpreadableKey =
$"@@symbol:{TypedAstSymbol.For("Symbol.isConcatSpreadable").GetHashCode()}";

private static IJsObjectLike ArraySpeciesCreate(object? original, long length, RealmState? realm)

Check failure on line 2300 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fw&open=AZruRP3sagPv8a_hS7fw&pullRequest=212
{
length = Math.Max(length, 0);

Expand Down Expand Up @@ -2446,7 +2358,7 @@
var proto = ResolveConstructPrototype(callable, callable, realm);
IJsObjectLike receiver;

if (callable is HostFunction hostFunction && realm?.ArrayConstructor is not null &&

Check warning on line 2361 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unnecessary check for null.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fz&open=AZruRP3sagPv8a_hS7fz&pullRequest=212
ReferenceEquals(hostFunction, realm.ArrayConstructor))
{
receiver = new JsArray(realm);
Expand Down Expand Up @@ -2498,7 +2410,7 @@
private static IJsObjectLike CreateArrayLikeReceiverForConstructor(IJsCallable constructor, RealmState? realm,
long length)
{
var proto = ResolveConstructPrototype(constructor, constructor, realm);

Check warning on line 2413 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'realmState' in 'JsObject? StandardLibrary.ResolveConstructPrototype(IJsCallable newTarget, IJsCallable target, RealmState realmState)'.
IJsObjectLike receiver;

if (constructor is HostFunction hostFunction && realm?.ArrayConstructor is not null &&
Expand All @@ -2522,6 +2434,73 @@
return receiver;
}

private static long MoveArrayRange(IJsPropertyAccessor accessor, IJsObjectLike? objectLike, string methodName,

Check failure on line 2437 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fx&open=AZruRP3sagPv8a_hS7fx&pullRequest=212
long length, long sourceStart, long sourceCount, long destinationOffset, long lengthDelta, RealmState? realm,
bool overflowIsRangeError = false, string? overflowMessage = null, bool updateLength = true)

Check warning on line 2439 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Method has 12 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fy&open=AZruRP3sagPv8a_hS7fy&pullRequest=212
{
var newLength = length + lengthDelta;
if (newLength > MaxArrayLength)
{
var errorMessage = overflowMessage ?? "Array length exceeds 2^53 - 1";
if (overflowIsRangeError)
{
throw ThrowRangeError(errorMessage, realm: realm);
}

throw ThrowTypeError(errorMessage, realm: realm);
}

if (sourceCount > 0 && destinationOffset != 0)
{
if (destinationOffset > 0)
{
for (long k = sourceStart + sourceCount - 1; k >= sourceStart; k--)
{
MoveSingleIndex(k, destinationOffset);
}
}
else
{
for (long k = sourceStart; k < sourceStart + sourceCount; k++)
{
MoveSingleIndex(k, destinationOffset);
}
}
}

if (lengthDelta < 0)
{
for (long k = length; k > newLength; k--)
{
var key = ToIndexString(k - 1);
var existed = HasProperty(accessor, key);
DeletePropertyOrThrow(objectLike, key, existed, methodName, realm);
}
}

if (updateLength)
{
accessor.SetProperty("length", (double)newLength);
}

return newLength;

void MoveSingleIndex(long fromIndex, long offset)
{
var toIndex = fromIndex + offset;
var fromKey = ToIndexString(fromIndex);
var toKey = ToIndexString(toIndex);
if (TryGetExistingElement(accessor, fromKey, out var value))
{
accessor.SetProperty(toKey, value);
return;
}

var toExisted = HasProperty(accessor, toKey);
DeletePropertyOrThrow(objectLike, toKey, toExisted, methodName, realm);
}
}

private static void DeletePropertyOrThrow(IJsObjectLike? objectLike, string propertyKey, bool propertyExisted,
string methodName, RealmState? realm)
{
Expand Down Expand Up @@ -2643,8 +2622,8 @@
}
}

private static long FlattenIntoArray(IJsPropertyAccessor target, IJsPropertyAccessor source, long sourceLength,
long targetIndex, long depth, IJsCallable? mapper, object? thisArg, RealmState? realm, string operation)

Check warning on line 2626 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Method has 9 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7f0&open=AZruRP3sagPv8a_hS7f0&pullRequest=212
{
for (long k = 0; k < sourceLength; k++)
{
Expand Down Expand Up @@ -2742,7 +2721,7 @@
return 0;
}

if (double.IsInfinity(number) || number == 0)

Check warning on line 2724 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not check floating point equality with exact values, use a range instead.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7f1&open=AZruRP3sagPv8a_hS7f1&pullRequest=212
{
return number;
}
Expand Down Expand Up @@ -2772,7 +2751,7 @@
return integer > length ? length : integer;
}

internal static object? ReduceLike(object? thisValue, IReadOnlyList<object?> args, RealmState? realm,

Check failure on line 2754 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 39 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7f2&open=AZruRP3sagPv8a_hS7f2&pullRequest=212
string methodName, bool fromRight)
{
var accessor = EnsureArrayLikeReceiver(thisValue, methodName, realm);
Expand Down Expand Up @@ -2865,7 +2844,7 @@
return accumulatorGeneric;
}

internal static object? SomeLike(object? thisValue, IReadOnlyList<object?> args, RealmState? realm,

Check failure on line 2847 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7f3&open=AZruRP3sagPv8a_hS7f3&pullRequest=212
string methodName)
{
var accessor = EnsureArrayLikeReceiver(thisValue, methodName, realm);
Expand Down Expand Up @@ -2931,7 +2910,7 @@
return JsOps.StrictEquals(x, y);
}

private static IJsPropertyAccessor EnsureArrayLikeReceiver(object? receiver, string methodName, RealmState? realm)

Check failure on line 2913 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7f4&open=AZruRP3sagPv8a_hS7f4&pullRequest=212
{
if (receiver is null || ReferenceEquals(receiver, Symbol.Undefined))
{
Expand All @@ -2942,7 +2921,7 @@
{
case IJsPropertyAccessor accessor when accessor is not TypedAstSymbol:
{
if (accessor is not JsObject jsObj || !jsObj.TryGetProperty("__value__", out var inner) ||

Check warning on line 2924 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using this literal '__value__' 4 times.

See more on https://sonarcloud.io/project/issues?id=asynkron_Asynkron.JsEngine&issues=AZruRP3sagPv8a_hS7fa&open=AZruRP3sagPv8a_hS7fa&pullRequest=212
inner is not string sInner)
{
return accessor;
Expand Down Expand Up @@ -3198,7 +3177,7 @@
objectPrototype.TryGetProperty("toString", out var toStringValue) &&
toStringValue is IJsCallable callable)
{
return callable.Invoke([], target);

Check warning on line 3180 in src/Asynkron.JsEngine/StdLib/StandardLibrary.Array.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference return.
}

return "[object Object]";
Expand Down
Loading