Skip to content

Commit

Permalink
Merge pull request #2442 from CosmosOS/feature/create_array_plug
Browse files Browse the repository at this point in the history
Add Array.CreateInstance plug and support range indexing for arrays
  • Loading branch information
MishaTy authored Oct 17, 2022
2 parents 5e6332e + c013ad2 commit f80528b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Tests/Kernels/SimpleStructsAndArraysTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ private void TestRemainder3Structs()
Assert.AreEqual(local.C, 77, "Three byte struct third value read correctly");
}

private void TestArrayRange()
{
byte[] array = new byte[16];
for (int i = 0; i < 16; i++)
{
array[i] = (byte)i;
}
byte[] slice = array[1..5];
Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, slice, "Taking the slice of a byte array works");
}

protected override void Run()
{
TestStep1();
Expand All @@ -289,6 +300,7 @@ protected override void Run()
ConstrainedTest.MutateStructTest();
Assert.IsTrue(true, "After MutateTestStruct");
TestRemainder3Structs();
TestArrayRange();

TestController.Completed();
}
Expand Down
6 changes: 6 additions & 0 deletions source/Cosmos.Core_Plugs/System/ArrayImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Cosmos.Core;
using Cosmos.Debug.Kernel;
using IL2CPU.API;
using IL2CPU.API.Attribs;
Expand Down Expand Up @@ -147,5 +148,10 @@ public static object InternalGetValue(Array aThis, IntPtr aIntPtr)
{
return aThis.GetValue((int)aIntPtr);
}

public static Array CreateInstance(Type type, int size)
{
return GCImpl.CreateNewArray((int)VTablesImpl.GetSize(((CosmosRuntimeType)type).mTypeId), size);
}
}
}
2 changes: 0 additions & 2 deletions source/Cosmos.Core_Plugs/System/GCImplCreateNewArrayAsm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
XS.Set(ECX, EBP, sourceDisplacement: 8); // size
XS.Set(EDX, EBP, sourceDisplacement: 12); // length



XS.Push(ECX); // size of element
XS.Set(EAX, ECX);
XS.Multiply(EDX); // total element size
Expand Down

0 comments on commit f80528b

Please sign in to comment.