Fixed njs_iterator_to_array() with sparse arrays.

This closes #524 issue on Github.
diff --git a/src/njs_iterator.c b/src/njs_iterator.c
index 043e448..9309677 100644
--- a/src/njs_iterator.c
+++ b/src/njs_iterator.c
@@ -686,7 +686,8 @@
         return NULL;
     }
 
-    args.data = njs_array_alloc(vm, 1, length, 0);
+    args.data = njs_array_alloc(vm, 0, 0,
+                                njs_min(length, NJS_ARRAY_LARGE_OBJECT_LENGTH));
     if (njs_slow_path(args.data == NULL)) {
         return NULL;
     }
@@ -708,10 +709,9 @@
 njs_iterator_to_array_handler(njs_vm_t *vm, njs_iterator_args_t *args,
     njs_value_t *value, int64_t index)
 {
-    njs_array_t  *array;
+    njs_value_t  array;
 
-    array = args->data;
-    array->start[index] = *value;
+    njs_set_array(&array, args->data);
 
-    return NJS_OK;
+    return njs_value_property_i64_set(vm, &array, index, value);
 }
diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c
index 9c19745..46197cd 100644
--- a/src/test/njs_unit_test.c
+++ b/src/test/njs_unit_test.c
@@ -11299,6 +11299,14 @@
     { njs_str("let e = AggregateError([1, 2, 3], 'm'); e"),
       njs_str("AggregateError: m") },
 
+    { njs_str("var v = Object.defineProperty([], 1025, {get: () => 1});"
+              "AggregateError(v).errors[23]"),
+      njs_str("undefined") },
+
+    { njs_str("var v = Object.defineProperty([], 2**20, {get: () => 1});"
+              "AggregateError(v).errors[2**19]"),
+      njs_str("undefined") },
+
     /* Memory object is immutable. */
 
     { njs_str("var e = MemoryError('e'); e.name = 'E'"),