Skip to content

Commit

Permalink
Add Array.Clear(Array) plug and test since .net 6 implementation does…
Browse files Browse the repository at this point in the history
… not work with cosmos

Cleanup StringImpl.cs and fix DictionaryTest.cs line endings
  • Loading branch information
quajak committed Jun 1, 2022
1 parent 400e651 commit 387a6ac
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ public static unsafe void Execute()
Array.Clear(xByteResult, 0, xByteResult.Length);
for (int i = 0; i < 8; i++)
{
Assert.IsTrue(xByteResult[i] == 0, "Array.Clear works");
Assert.IsTrue(xByteResult[i] == 0, "Array.Clear(byte[], int, int) works");
}
xByteResult[1] = 1;
Assert.IsTrue(xByteResult[1] == 1, "Array.Clear does not break the array");

xByteResult = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.AreEqual(2, xByteResult[1], "Setting byte array to new array object works");
Array.Clear(xByteResult);
for (int i = 0; i < 8; i++)
{
Assert.AreEqual(0, xByteResult[i], "Array.Clear(byte[]) works");
}

// Single[] Test
float[] xSingleResult = { 1.25f, 2.50f, 3.51f, 4.31f, 9.28f, 18.56f };
float[] xSingleExpectedResult = { 1.25f, 2.598f, 5.39f, 4.31f, 9.28f, 18.56f };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cosmos.TestRunner;


namespace Cosmos.Compiler.Tests.Bcl.System.Collections.Generic
{
public static class DictionaryTest
{
public static void Execute()
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cosmos.TestRunner;


namespace Cosmos.Compiler.Tests.Bcl.System.Collections.Generic
{
public static class DictionaryTest
{
public static void Execute()
{
{
var dictionary = new Dictionary<string, string>
{
{"a", "a"},
{"b", "b" },
{"c", "c"}
var dictionary = new Dictionary<string, string>
{
{"a", "a"},
{"b", "b" },
{"c", "c"}
};

Assert.IsTrue(dictionary.ContainsKey("a"), "Dictionary<string, string> ContainsKey does not work1");
Expand Down Expand Up @@ -63,14 +63,14 @@ public static void Execute()
dictionary.Add("b", "basds");
Assert.IsTrue(dictionary.Count == 1, "Dictionary<string, string>().Clear prevents correctly adding values again");
Assert.IsTrue(dictionary["b"] == "basds", "Dictionary<string, string>().Clear prevents correctly adding values again");
}

}

{
var dictionary2 = new Dictionary<string, int>
{
{ "One", 1 },
{ "Two", 2},
{ "Three", 3 },
var dictionary2 = new Dictionary<string, int>
{
{ "One", 1 },
{ "Two", 2},
{ "Three", 3 },
};

Assert.IsTrue(dictionary2.ContainsKey("One"), "Dictionary<string, int> ContainsKey does not work1");
Expand Down Expand Up @@ -566,6 +566,6 @@ public static void Execute()

//TODO: Add GUID test once newGUID returns something other than a zero initialized guid.

}
}
}
}
}
}
44 changes: 44 additions & 0 deletions source/Cosmos.Core_Asm/Array/ArrayClearAsm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using IL2CPU.API;
using XSharp;
using XSharp.Assembler;
using XSharp.Assembler.x86;
using static XSharp.XSRegisters;

namespace Cosmos.Core_Asm
{
public class ArrayClearAsm : AssemblerMethod
{
private const int SourceArrayDisplacement = 12;

public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
{
// load element size into eax
// load length into ebx
// calculate entire size into eax and move to exc
// load start into edi
// clear ecx bytes starting at edi

// load element size into eax
XS.Set(EAX, EBP, sourceDisplacement: SourceArrayDisplacement);
XS.Add(EAX, 8);
XS.Set(EAX, EAX, sourceIsIndirect: true);

// load length into ebx
XS.Set(EBX, EBP, sourceDisplacement: SourceArrayDisplacement);
XS.Add(EBX, 12);
XS.Set(EBX, EBX, sourceIsIndirect: true);

// calculate size in bytes and move to ecx
XS.Multiply(EBX);
XS.Set(ECX, EAX);

// load start into esi
XS.Set(EDI, EBP, sourceDisplacement: SourceArrayDisplacement);
XS.Add(EDI, 16);

// clear eax bytes starting at esi
XS.Set(EAX, 0);
XS.LiteralCode("rep stosb");
}
}
}
6 changes: 6 additions & 0 deletions source/Cosmos.Core_Asm/ArrayImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ public static void Copy(Array sourceArray, int sourceIndex, Array destinationArr
{
throw new NotImplementedException();
}

[PlugMethod(Assembler = typeof(ArrayClearAsm))]
public static void Clear(Array array)
{
throw new NotImplementedException();
}
}
}
76 changes: 4 additions & 72 deletions source/Cosmos.Core_Plugs/System/StringImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -677,78 +677,10 @@ public static int LastIndexOfAny(string aThis, char[] aChars, int aStartIndex, i
return -1;
}

//public static int nativeCompareOrdinalEx(string aStrA, int aIndexA, string aStrB, int aIndexB, int aCount)
//{
// mDebugger.SendInternal($"nativeCompareOrdinalEx : aStrA|aIndexA = {aStrA}|{aIndexA}, aStrB|aIndexB = {aStrB}|{aIndexB}, aCount = {aCount}");
// if (aCount < 0)
// {
// throw new ArgumentOutOfRangeException(nameof(aCount));
// }

// if (aIndexA < 0 || aIndexA > aStrA.Length)
// {
// throw new ArgumentOutOfRangeException(nameof(aIndexA));
// }

// if (aIndexB < 0 || aIndexB > aStrB.Length)
// {
// throw new ArgumentOutOfRangeException(nameof(aIndexB));
// }

// if (aStrA == null)
// {
// mDebugger.SendInternal("nativeCompareOrdinalEx : aStrA is null");
// if (aStrB == null)
// {
// mDebugger.SendInternal($"nativeCompareOrdinalEx : aStrB is null");
// mDebugger.SendInternal($"nativeCompareOrdinalEx : returning 0");
// return 0;
// }
// mDebugger.SendInternal($"nativeCompareOrdinalEx : aStrB is not null");
// mDebugger.SendInternal($"nativeCompareOrdinalEx : returning -1");
// return -1;
// }
// if (aStrB == null)
// {
// mDebugger.SendInternal("nativeCompareOrdinalEx : aStrA is not null");
// mDebugger.SendInternal($"nativeCompareOrdinalEx : aStrB is null");
// mDebugger.SendInternal($"nativeCompareOrdinalEx : returning 1");
// return 1;
// }
// int xLengthA = Math.Min(aStrA.Length, aCount - aIndexA);
// int xLengthB = Math.Min(aStrB.Length, aCount - aIndexB);
// //mDebugger.SendInternal($"nativeCompareOrdinalEx : xLengthA = {xLengthA}");
// //mDebugger.SendInternal($"nativeCompareOrdinalEx : xLengthB = {xLengthB}");

// if (xLengthA == xLengthB && aIndexA == aIndexB && ReferenceEquals(aStrA, aStrB))
// {
// mDebugger.SendInternal("nativeCompareOrdinalEx : xLengthA == xLengthB && aIndexA == aIndexB && aStrA is the same object asaStrB, returning 0");
// return 0;
// }

// int xResult = 0;
// if (xLengthA != xLengthB)
// {
// xResult = xLengthA - xLengthB;
// mDebugger.SendInternal("nativeCompareOrdinalEx : xLengthA != xLengthB, returning " + xResult);
// }

// for (int i = 0; i < xLengthA; i++)
// {
// if (aStrA != aStrB)
// {
// xResult = (byte)aStrA[i] - (byte)aStrB[i];
// mDebugger.SendInternal("nativeCompareOrdinalEx : aStrA[i] != aStrB[i], returning " + xResult);
// return xResult;
// }
// }

// mDebugger.SendInternal("nativeCompareOrdinalEx (end of func) : aStrA[i] != aStrB[i], returning " + xResult);
// return xResult;
//}

public static bool StartsWith(string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture) =>
aThis.StartsWith(aSubStr, aIgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
public static bool StartsWith(string aThis, string aSubStr, bool aIgnoreCase, CultureInfo aCulture)
{
return aThis.StartsWith(aSubStr, aIgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}

public static string Replace(string aThis, string oldValue, string newValue)
{
Expand Down

0 comments on commit 387a6ac

Please sign in to comment.