Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit b16dc3f

Browse files
committed
Array.Empty correct implementation and test added.
1 parent 9c4344f commit b16dc3f

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

JSIL.Libraries/Includes/Bootstrap/Core/Classes/System.Array.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,19 @@ JSIL.ImplementExternals("System.Array", function ($) {
176176
$.Method({ Static: true, Public: true }, "Empty",
177177
new JSIL.MethodSignature($jsilcore.TypeRef("System.Array", ["!!0"]), [], ["T"]),
178178
function Empty(T) {
179-
return [];
179+
return $jsilcore.System.Array_EmptyArray$b1.Of(T).Value;
180180
}
181181
);
182-
});
182+
});
183+
184+
185+
JSIL.MakeStaticClass("System.Array+EmptyArray`1", false, ["T"], function($) {
186+
$.Field({ Static: true, Public: true, ReadOnly: true }, "Value", $jsilcore.TypeRef("System.Array", [$.GenericParameter("T")]));
187+
188+
$.Method({ Static: true, Public: false }, ".cctor",
189+
JSIL.MethodSignature.Void,
190+
function EmptyArray$b1__cctor() {
191+
this.Value = JSIL.Array.New(this.T, 0);
192+
}
193+
);
194+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
3+
public static class Program {
4+
public static void Main (string[] args)
5+
{
6+
var emptyObj1 = Array.Empty<object>();
7+
var emptyObj2 = Array.Empty<object>();
8+
var intObj1 = Array.Empty<int>();
9+
var intObj2= Array.Empty<int>();
10+
11+
Console.WriteLine(emptyObj1.GetType());
12+
Console.WriteLine(intObj1.GetType());
13+
Console.WriteLine(emptyObj1 == emptyObj2 ? "true" : "false");
14+
Console.WriteLine(intObj1 == intObj2 ? "true" : "false");
15+
Console.WriteLine(Equals(emptyObj1, intObj1) ? "true" : "false");
16+
}
17+
}
18+
19+
public static class A
20+
{
21+
public static T[] Empty<T>()
22+
{
23+
return EmptyArray<T>.Value;
24+
}
25+
26+
internal static class EmptyArray<T>
27+
{
28+
public static readonly T[] Value = new T[0];
29+
}
30+
}

Tests/SimpleTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<None Include="SimpleTestCases\ReflectionPositionalGenericNames_Issue509.cs" />
135135
<None Include="SimpleTestCases\ArrayType.cs" />
136136
<None Include="SimpleTestCases\Issue844.cs" />
137+
<None Include="SimpleTestCases\ArrayEmpty.cs" />
137138
<Compile Include="SimpleTests.cs" />
138139
<None Include="SimpleTestCases\BaseAutoProperties.cs" />
139140
<None Include="SimpleTestCases\OverloadedVirtualMethods.cs" />

0 commit comments

Comments
 (0)