Skip to content

Commit 8d35cde

Browse files
matra774kg
authored andcommitted
Implemented set_Item for collections (List<T>,ArrayList). List<T> now implements IList<T>
1 parent 0724097 commit 8d35cde

5 files changed

Lines changed: 140 additions & 7 deletions

File tree

Libraries/JSIL.Bootstrap.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,18 +1059,32 @@ $jsilcore.$ListExternals = function ($, T, type) {
10591059
}
10601060
);
10611061

1062-
$.Method({Static:false, Public:true }, "get_Item",
1063-
new JSIL.MethodSignature(T, [mscorlib.TypeRef("System.Int32")], []),
1064-
function (index) {
1062+
var rangeCheckImpl = function (index,size) {
10651063
if (index < 0)
10661064
throw new System.ArgumentOutOfRangeException("index");
1067-
else if (index >= this._size)
1065+
else if (index >= size)
10681066
throw new System.ArgumentOutOfRangeException("index");
1067+
}
1068+
1069+
$.Method({Static:false, Public:true }, "get_Item",
1070+
new JSIL.MethodSignature(T, [mscorlib.TypeRef("System.Int32")], []),
1071+
function (index) {
1072+
rangeCheckImpl(index, this._size);
1073+
return this._items[index];
1074+
}
1075+
1076+
);
1077+
1078+
$.Method({Static: false, Public: true }, "set_Item",
1079+
new JSIL.MethodSignature(null, [mscorlib.TypeRef("System.Int32"), T], []),
1080+
function (index, value) {
1081+
rangeCheckImpl(index, this._size);
1082+
this._items[index]=value;
1083+
}
10691084

1070-
return this._items[index];
1071-
}
10721085
);
10731086

1087+
10741088
var getEnumeratorImpl = function () {
10751089
// Detect whether we are a List<T> or an ArrayList.
10761090
if (typeof(this.$thisEnumeratorType) === "undefined") {
@@ -1505,7 +1519,8 @@ JSIL.MakeClass("System.Object", "System.Collections.Generic.List`1", true, ["T"]
15051519
$.ImplementInterfaces(
15061520
$jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.List`1")]),
15071521
"System.Collections.IEnumerable",
1508-
$jsilcore.TypeRef("System.Collections.Generic.ICollection`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.List`1")])
1522+
$jsilcore.TypeRef("System.Collections.Generic.ICollection`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.List`1")]),
1523+
$jsilcore.TypeRef("System.Collections.Generic.IList`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.List`1")])
15091524
);
15101525
});
15111526

Libraries/JSIL.Core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,18 @@ JSIL.MakeInterface(
49434943
$jsilcore.TypeRef("System.Collections.IEnumerable")]
49444944
);
49454945

4946+
JSIL.MakeInterface(
4947+
"System.Collections.Generic.IList`1", true, ["T"], {
4948+
"get_Item": Function,
4949+
"set_Item": Function,
4950+
"IndexOf": Function,
4951+
"Insert": Function,
4952+
"RemoveAt": Function,
4953+
"Item" : Property
4954+
},
4955+
[$jsilcore.TypeRef("System.Collections.Generic.ICollection`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.IList`1")]),
4956+
$jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", [new JSIL.GenericParameter("T", "System.Collections.Generic.IList`1")]),
4957+
$jsilcore.TypeRef("System.Collections.IEnumerable")]);
49464958

49474959
JSIL.ImplementExternals("System.Array", function ($) {
49484960
$.RawMethod(true, "CheckType", JSIL.IsArray);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public static class Program {
6+
public static void Main (string[] args) {
7+
8+
var al = new ArrayList();
9+
10+
al.Add("zero");
11+
al.Add("one");
12+
al.Add("two");
13+
14+
al.IndexOf("zero");
15+
Console.WriteLine(al.Count);
16+
Console.WriteLine(al[0]);
17+
Console.WriteLine(al[1]);
18+
Console.WriteLine(al[2]);
19+
20+
al[1] = "one-updated";
21+
Console.WriteLine(al[1]); // this used to fail
22+
23+
}
24+
25+
public static void PrintBool(bool b)
26+
{
27+
Console.WriteLine(b ? 1 : 0);
28+
}
29+
30+
}

Tests/SimpleTestCases/ListIList.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public static class Program
5+
{
6+
7+
8+
public static void Main(string[] args)
9+
{
10+
11+
12+
var slist = new List<string> { "zero", "one", "two", "three" };
13+
14+
15+
16+
// collection test are already implemented in ListiCollection.cs
17+
18+
// NOTE: JSIL is quite forgiving when implementing the interface - it does not take into account the returning parameter type
19+
// so "object get_Item(i)" can also implement "T get_Item(int)". The listExternals implementation as a little bit
20+
// loosey goosey. To be strict, different types of collection (ArrayList, List<T> should really have slightly
21+
// different method signatures for get_item, set_Item and others - this is currenlty only partially covverred in JSIL.Bootsrap.js)
22+
Console.WriteLine("----- interface ----");
23+
24+
var list = (IList<string>) slist;
25+
26+
Console.WriteLine(list.Count);
27+
Console.WriteLine(list[0]);
28+
Console.WriteLine(list[1]);
29+
Console.WriteLine(list[2]);
30+
Console.WriteLine(list[3]);
31+
32+
Console.WriteLine(list.IndexOf("two"));
33+
34+
list.Insert(1,"inserted");
35+
Console.WriteLine(list.Count);
36+
Console.WriteLine(list[4]);
37+
38+
list.RemoveAt(2);
39+
Console.WriteLine(list.Count);
40+
Console.WriteLine(list[3]);
41+
42+
43+
list[2] = "modified";
44+
Console.WriteLine(list[2]);
45+
46+
// also check item acecssor throw class (not the interface)
47+
slist = new List<string> { "zero", "one", "two", "three" };
48+
49+
Console.WriteLine("----- class ----");
50+
51+
Console.WriteLine(slist.Count);
52+
Console.WriteLine(slist[0]);
53+
Console.WriteLine(slist[1]);
54+
Console.WriteLine(slist[2]);
55+
Console.WriteLine(slist[3]);
56+
57+
Console.WriteLine(slist.IndexOf("two"));
58+
59+
slist.Insert(1,"inserted");
60+
Console.WriteLine(slist.Count);
61+
Console.WriteLine(slist[4]);
62+
63+
slist.RemoveAt(2);
64+
Console.WriteLine(slist.Count);
65+
Console.WriteLine(slist[3]);
66+
67+
68+
slist[2] = "modified";
69+
Console.WriteLine(slist[2]);
70+
71+
}
72+
73+
74+
}

Tests/Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
<None Include="SimpleTestCases\ListIndexOf.cs" />
134134
<None Include="SimpleTestCases\ListInsert.cs" />
135135
<None Include="TestCases\EnumAnonymousMethod.cs" />
136+
<Compile Include="SimpleTestCases\ArrayListIndexer.cs" />
137+
<None Include="SimpleTestCases\ListIList.cs" />
136138
<Compile Include="XMLTests.cs" />
137139
<Compile Include="ReflectionTests.cs" />
138140
<Compile Include="DependencyTests.cs" />

0 commit comments

Comments
 (0)