From 9c2075be49d0537cd4664922df021b6c3091caba Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Tue, 26 May 2020 16:04:15 +0000 Subject: [PATCH] Migrate language_2/interceptor to NNBD. Change-Id: Ibbc89b68ef980e1ff9913c299c05d5117b249477 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148949 Auto-Submit: Bob Nystrom Commit-Queue: Erik Ernst Reviewed-by: Erik Ernst --- .../interceptor/interceptor2_test.dart | 25 +++++++++++++ .../interceptor/interceptor3_test.dart | 17 +++++++++ .../interceptor/interceptor4_test.dart | 14 ++++++++ .../interceptor/interceptor5_test.dart | 11 ++++++ .../interceptor/interceptor7_test.dart | 16 +++++++++ .../interceptor/interceptor8_test.dart | 12 +++++++ .../interceptor/interceptor9_test.dart | 32 +++++++++++++++++ .../interceptor/interceptor_test.dart | 35 +++++++++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 tests/language/interceptor/interceptor2_test.dart create mode 100644 tests/language/interceptor/interceptor3_test.dart create mode 100644 tests/language/interceptor/interceptor4_test.dart create mode 100644 tests/language/interceptor/interceptor5_test.dart create mode 100644 tests/language/interceptor/interceptor7_test.dart create mode 100644 tests/language/interceptor/interceptor8_test.dart create mode 100644 tests/language/interceptor/interceptor9_test.dart create mode 100644 tests/language/interceptor/interceptor_test.dart diff --git a/tests/language/interceptor/interceptor2_test.dart b/tests/language/interceptor/interceptor2_test.dart new file mode 100644 index 00000000000..06260c82eff --- /dev/null +++ b/tests/language/interceptor/interceptor2_test.dart @@ -0,0 +1,25 @@ +// Copyright (c) 2012, 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. + +import "package:expect/expect.dart"; + +// Regression test for issue http://dartbug.com/6903: dart2js used to +// not generate an interceptor forwarder when a getter call and a +// method call on an intercepted method were both used. + +class A { + get iterator => () => 499; +} + +main() { + var a = [ + new A(), + [1, 1] + ]; + Expect.equals(499, a[0].iterator()); + Expect.equals(499, (a[0].iterator)()); + for (var i in a[1]) { + Expect.equals(1, i); + } +} diff --git a/tests/language/interceptor/interceptor3_test.dart b/tests/language/interceptor/interceptor3_test.dart new file mode 100644 index 00000000000..b22b26d1c19 --- /dev/null +++ b/tests/language/interceptor/interceptor3_test.dart @@ -0,0 +1,17 @@ +// Copyright (c) 2012, 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. + +import "package:expect/expect.dart"; + +// Test that code motion in the presence of interceptors work in dart2js. + +main() { + var a = [2, '2']; + var b = a[1]; + if (a[0] == 2 && b is String) { + Expect.isTrue(b.contains('2')); + } else { + b.isEven(); + } +} diff --git a/tests/language/interceptor/interceptor4_test.dart b/tests/language/interceptor/interceptor4_test.dart new file mode 100644 index 00000000000..00bac2837b6 --- /dev/null +++ b/tests/language/interceptor/interceptor4_test.dart @@ -0,0 +1,14 @@ +// Copyright (c) 2012, 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. + +import "package:expect/expect.dart"; + +// Test that dart2js gets the right interceptor for an int. + +main() { + var a = [1]; + var b = a[0]; + Expect.equals('1', b.toString()); + Expect.isTrue(b.isOdd); +} diff --git a/tests/language/interceptor/interceptor5_test.dart b/tests/language/interceptor/interceptor5_test.dart new file mode 100644 index 00000000000..41b76e98dae --- /dev/null +++ b/tests/language/interceptor/interceptor5_test.dart @@ -0,0 +1,11 @@ +// Copyright (c) 2012, 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. + +import "package:expect/expect.dart"; + +get X => [() => 123]; + +main() { + Expect.equals(123, X.last()); +} diff --git a/tests/language/interceptor/interceptor7_test.dart b/tests/language/interceptor/interceptor7_test.dart new file mode 100644 index 00000000000..7e88bcdff73 --- /dev/null +++ b/tests/language/interceptor/interceptor7_test.dart @@ -0,0 +1,16 @@ +// Copyright (c) 2013, 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. + +// Test that dart2js uses the right interceptor when call a method on +// something that has type number. + +import 'package:expect/expect.dart'; + +var array = []; + +main() { + array.add(false); + dynamic x = array[0] ? 1.5 : 2; + Expect.isTrue(x.isEven); +} diff --git a/tests/language/interceptor/interceptor8_test.dart b/tests/language/interceptor/interceptor8_test.dart new file mode 100644 index 00000000000..089012452d0 --- /dev/null +++ b/tests/language/interceptor/interceptor8_test.dart @@ -0,0 +1,12 @@ +// Copyright (c) 2013, 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. + +// Regression test for dart2js whose codegen used to not consider a double +// could be instantiated when doing int / int. + +var a = [5, 2]; + +main() { + print(a[0] / a[1]); +} diff --git a/tests/language/interceptor/interceptor9_test.dart b/tests/language/interceptor/interceptor9_test.dart new file mode 100644 index 00000000000..07525a57d27 --- /dev/null +++ b/tests/language/interceptor/interceptor9_test.dart @@ -0,0 +1,32 @@ +// Copyright (c) 2014, 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. + +// Regression test for dart2js whose codegen in some cases did not take is +// tests into account when computing the set of classes for interceptors. +// See http://dartbug.com/17325 + +import "package:expect/expect.dart"; +import "dart:typed_data"; + +confuse(x, [y = null]) => new DateTime.now().day == 42 ? y : x; + +boom() { + var x = confuse(new Uint8List(22), ""); + Expect.isTrue(x is Uint8List); + x.startsWith("a"); + x.endsWith("u"); +} + +main() { + try { + var f; + if (confuse(true)) { + // prevent inlining + f = boom; + } + f(); + } catch (e) { + if (e is ExpectException) throw e; + } +} diff --git a/tests/language/interceptor/interceptor_test.dart b/tests/language/interceptor/interceptor_test.dart new file mode 100644 index 00000000000..7faf1746342 --- /dev/null +++ b/tests/language/interceptor/interceptor_test.dart @@ -0,0 +1,35 @@ +// Copyright (c) 2012, 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. + +import "package:expect/expect.dart"; + +// Test that interceptors (that is, methods in classes implemented as +// JavaScript primitives) in dart2js work. + +class A { + codeUnitAt(a) => a; +} + +main() { + var res = [[], 1, 'foo', new A()]; + Expect.throws(() => res[0].codeUnitAt(1)); + Expect.throws(() => (res[0].codeUnitAt)(1)); + + Expect.throws(() => res[1].codeUnitAt(1)); + Expect.throws(() => (res[1].codeUnitAt)(1)); + + Expect.equals(111, res[2].codeUnitAt(1)); + Expect.equals(111, (res[2].codeUnitAt)(1)); + Expect.throws(() => res[2].codeUnitAt(1, 4)); + Expect.throws(() => res[2].codeUnitAt()); + Expect.throws(() => (res[2].codeUnitAt)(1, 4)); + Expect.throws(() => (res[2].codeUnitAt)()); + + Expect.equals(1, res[3].codeUnitAt(1)); + Expect.equals(1, (res[3].codeUnitAt)(1)); + Expect.throws(() => res[3].codeUnitAt(1, 4)); + Expect.throws(() => res[3].codeUnitAt()); + Expect.throws(() => (res[3].codeUnitAt)(1, 4)); + Expect.throws(() => (res[3].codeUnitAt)()); +}