[dartdevc] fix #36372, make JS Array constructor property usable from JS

The `new array.constructor()` pattern is occasionally used in JS
(presumably, to get the array subtype if one is available). GopherJS in
particular does this. This fixes DDC's generic `JSArray<T>` to support
this pattern, for all values of T. This also makes JSArray work like
all other "native" types in DDC, where the "constructor" property
is the JS constructor for that native type.

Change-Id: I5270bd648d3d60cf07b8548fcd8c9b9d1a3018d4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99940
Auto-Submit: Jenny Messerly <jmesserly@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Jenny Messerly <jmesserly@google.com>
diff --git a/pkg/dev_compiler/lib/src/analyzer/code_generator.dart b/pkg/dev_compiler/lib/src/analyzer/code_generator.dart
index 8591d9c..39f7c88f 100644
--- a/pkg/dev_compiler/lib/src/analyzer/code_generator.dart
+++ b/pkg/dev_compiler/lib/src/analyzer/code_generator.dart
@@ -1430,6 +1430,15 @@
                   throw Error("use `new " + #.typeName(#.getReifiedType(this)) +
                       ".new(...)` to create a Dart object");
               }''', [runtimeModule, runtimeModule])));
+    } else if (classElem == _jsArray) {
+      // Provide access to the Array constructor property, so it works like
+      // other native types (rather than calling the Dart Object "constructor"
+      // above, which throws).
+      //
+      // This will become obsolete when
+      // https://github.com/dart-lang/sdk/issues/31003 is addressed.
+      jsMethods.add(JS.Method(
+          propertyName('constructor'), js.fun(r'function() { return []; }')));
     } else if (classElem.isEnum) {
       // Generate Enum.toString()
       var fields = classElem.fields.where((f) => f.type == type).toList();
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart
index 1c88754..b6c1925 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart
@@ -1586,6 +1586,15 @@
                   throw Error("use `new " + #.typeName(#.getReifiedType(this)) +
                       ".new(...)` to create a Dart object");
               }''', [runtimeModule, runtimeModule])));
+    } else if (c == _jsArrayClass) {
+      // Provide access to the Array constructor property, so it works like
+      // other native types (rather than calling the Dart Object "constructor"
+      // above, which throws).
+      //
+      // This will become obsolete when
+      // https://github.com/dart-lang/sdk/issues/31003 is addressed.
+      jsMethods.add(JS.Method(
+          propertyName('constructor'), js.fun(r'function() { return []; }')));
     }
 
     Set<Member> redirectingFactories;
diff --git a/tests/lib_2/js/array_test.dart b/tests/lib_2/js/array_test.dart
new file mode 100644
index 0000000..30bae97
--- /dev/null
+++ b/tests/lib_2/js/array_test.dart
@@ -0,0 +1,40 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+@JS()
+library array_test;
+
+import 'package:expect/expect.dart';
+import 'package:js/js.dart';
+import 'package:js/js_util.dart' as js;
+
+main() {
+  testArrayConstructor();
+}
+
+/// Test that we can access .constructor() on a JS Array instance, regardless
+/// of the reified generic type.
+///
+/// Regression test for https://github.com/dart-lang/sdk/issues/36372
+testArrayConstructor() {
+  var list = <int>[1, 2, 3];
+  testArray = list;
+
+  // Call the consturctor with `new`.
+  var array = js.callConstructor(js.getProperty(testArray, 'constructor'), []);
+  var list2 = array as List;
+  Expect.listEquals(list2, []);
+  Expect.notEquals(list, list2, '$list2 should be a new list');
+
+  // We could return a reified type here, but currently does not to match
+  // dart2js, and because the Array is being returned to JS.
+  Expect.isFalse(list2 is List<int>,
+      '$list2 should not have a reified generic type (it was allocated by JS)');
+
+  list2.addAll([1, 2, 3]);
+  Expect.listEquals(list, list2);
+}
+
+external Object get testArray;
+external set testArray(value);