kevingadd/JSIL — Issue #63: Elements in arrays of structs are not initialized
Author: markusjohnsson
Created: 2012-01-30 22:16:49+00:00
State: open

The following test currently fails because the elements of the array are null, which is not a valid value for structs:

```csharp

using System;

public struct CustomType {
public int Value;

public CustomType (int value) {
Value = value;
}
}

public static class Program {
public static void Main (string[] args) {
var a = new CustomType[5];

for (var i = 0; i < a.Length; i++)
Console.WriteLine(a[i].Value);
}
}

```

This modified JSIL.Array.New fixes the problem:

```csharp
JSIL.Array.New = function (type, sizeOrInitializer) {
if (Array.isArray(sizeOrInitializer)) {
// If non-numeric, assume array initializer
var result = new Array(sizeOrInitializer.length);
for (var i = 0; i < sizeOrInitializer.length; i++)
result[i] = sizeOrInitializer[i];
} else {
var size = Number(sizeOrInitializer);
var result = new Array(size);

var defaultValue = null;
if (type.__IsNumeric__)
defaultValue = 0;
else if (type.__IsReferenceType__ === false)
defaultValue = new type();

for (var i = 0; i < size; i++)
result[i] = defaultValue;
}

/* Even worse, doing this deoptimizes all uses of the array in TraceMonkey. AUGH
// Can't do this the right way, because .prototype for arrays in JS is insanely busted
result.__FullName__ = type.__FullName__ + "[]";
result.toString = System.Object.prototype.toString;
*/

return result;
};
```