From d1447458afeab640015113be363cdecb81a6e2a2 Mon Sep 17 00:00:00 2001 From: Matthijs ter Woord Date: Tue, 14 Jun 2016 13:51:16 -0400 Subject: [PATCH] Implement Object.MemberwiseClone --- .../Cosmos.Compiler.Tests.Bcl.csproj | 1 + Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs | 6 ++-- .../System/ObjectTests.cs | 33 +++++++++++++++++++ .../Cosmos.Core.Plugs.csproj | 1 + source/Cosmos.Core.Plugs/System/ObjectImpl.cs | 30 +++++++++++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Tests/Cosmos.Compiler.Tests.Bcl/System/ObjectTests.cs create mode 100644 source/Cosmos.Core.Plugs/System/ObjectImpl.cs diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/Cosmos.Compiler.Tests.Bcl.csproj b/Tests/Cosmos.Compiler.Tests.Bcl/Cosmos.Compiler.Tests.Bcl.csproj index f62eac904..b0a288621 100644 --- a/Tests/Cosmos.Compiler.Tests.Bcl/Cosmos.Compiler.Tests.Bcl.csproj +++ b/Tests/Cosmos.Compiler.Tests.Bcl/Cosmos.Compiler.Tests.Bcl.csproj @@ -93,6 +93,7 @@ + diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs b/Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs index a3f02f4e5..f2decb41f 100644 --- a/Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs +++ b/Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs @@ -8,7 +8,7 @@ using Sys = Cosmos.System; namespace Cosmos.Compiler.Tests.Bcl { using Cosmos.Compiler.Tests.Bcl.System; - + public class Kernel : Sys.Kernel { protected override void BeforeRun() @@ -23,7 +23,7 @@ namespace Cosmos.Compiler.Tests.Bcl mDebugger.Send("Run"); CSharp.WhileLoopTests.Execute(); - + ObjectTests.Execute(); StringTest.Execute(); ByteTest.Execute(); SByteTest.Execute(); @@ -54,4 +54,4 @@ namespace Cosmos.Compiler.Tests.Bcl } } } -} \ No newline at end of file +} diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/System/ObjectTests.cs b/Tests/Cosmos.Compiler.Tests.Bcl/System/ObjectTests.cs new file mode 100644 index 000000000..94165f52e --- /dev/null +++ b/Tests/Cosmos.Compiler.Tests.Bcl/System/ObjectTests.cs @@ -0,0 +1,33 @@ +using System; +using Cosmos.TestRunner; + +namespace Cosmos.Compiler.Tests.Bcl.System +{ + public class ObjectTests + { + public class MyType + { + public int IntField; + public object ReferenceField; + + public MyType Clone() + { + return (MyType)MemberwiseClone(); + } + } + + public static void Execute() + { + var xMyType = new MyType(); + xMyType.IntField = 42; + xMyType.ReferenceField = new object(); + + var xCloneType = xMyType.Clone(); + Assert.AreEqual(xMyType.IntField, xCloneType.IntField, "Cloned object has a different IntField value!"); + Assert.IsTrue(object.ReferenceEquals(xMyType.ReferenceField, xCloneType.ReferenceField), "References of field aren't the same!"); + xCloneType.IntField = 56; + Assert.AreEqual(xMyType.IntField, 42, "Cloned object is linked to original object!"); + + } + } +} diff --git a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj index 2142ba8cb..95e338b59 100644 --- a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj +++ b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj @@ -109,6 +109,7 @@ + diff --git a/source/Cosmos.Core.Plugs/System/ObjectImpl.cs b/source/Cosmos.Core.Plugs/System/ObjectImpl.cs new file mode 100644 index 000000000..e376455ee --- /dev/null +++ b/source/Cosmos.Core.Plugs/System/ObjectImpl.cs @@ -0,0 +1,30 @@ +using Cosmos.IL2CPU.Plugs; + +namespace Cosmos.Core.Plugs.System +{ + [Plug(Target = typeof(object))] + public class ObjectImpl + { + public static unsafe uint MemberwiseClone(uint aThis) + { + // pointers are handles! + + + var xThisPointer = (uint*)aThis; + + var xSize = xThisPointer[1]; + + var xResult = GCImplementionImpl.AllocNewObject(xSize); + + var xThatPointer = (uint*)xResult; + var xThatPointerByte = (byte*)xThatPointer[0]; + var xThisSimplePointer = (uint*)aThis; + var xThisPointerByte = (byte*)xThisSimplePointer[0]; + for (int i = 0; i < xSize; i++) + { + xThatPointerByte[i] = xThisPointerByte[i]; + } + return xResult; + } + } +} \ No newline at end of file