OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // VMOptions=--throw_on_javascript_int_overflow --print_stacktrace_at_throw --op
timization_counter_threshold=10 --no-use-osr | |
6 | |
7 | |
8 import "package:expect/expect.dart"; | |
9 import 'dart:typed_data'; | |
10 | |
11 | |
12 double dti_arg; | |
13 int double_to_int() { | |
14 return dti_arg.toInt(); | |
15 } | |
16 | |
17 | |
18 int ia_arg1; | |
19 int ia_arg2; | |
20 int integer_add() { | |
21 return ia_arg1 + ia_arg2; | |
22 } | |
23 | |
24 | |
25 int is_arg; | |
26 int integer_shift() { | |
27 return is_arg << 1; | |
28 } | |
29 | |
30 | |
31 int max_add_throws() { | |
32 return 0x20000000000000 + 1; | |
33 } | |
34 | |
35 | |
36 int min_sub_throws() { | |
37 return -0x20000000000000 - 1; | |
38 } | |
39 | |
40 | |
41 int n_arg; | |
42 int negate() { | |
43 return -n_arg; | |
44 } | |
45 | |
46 | |
47 int max_literal() { | |
48 return 0x20000000000000; | |
49 } | |
50 | |
51 | |
52 int min_literal() { | |
53 var min_literal = -0x20000000000000; | |
54 return min_literal; | |
55 } | |
56 | |
57 | |
58 int doNotThrow1(a, b) { | |
59 return (a << b) & 0xFFFFFFFF; | |
60 } | |
61 | |
62 int doNotThrow2(a, b) { | |
63 return (a << b) & 0xFFFFFFFF; | |
64 } | |
65 | |
66 | |
67 int doNotThrow3(a, b) { | |
68 return (a << b) & 0x7FFFFFFF; | |
69 } | |
70 | |
71 | |
72 int doNotThrow4(a, b) { | |
73 return (a << b) & 0x7FFFFFFF; | |
74 } | |
75 | |
76 | |
77 // We don't test for the _JavascriptIntegerOverflowError since it's not visible. | |
78 // It should not be visible since it doesn't exist on dart2js. | |
79 bool isJavascriptIntError(e) => | |
80 e is Error && "$e".startsWith("Javascript Integer Overflow:"); | |
81 | |
82 main() { | |
83 Expect.equals(0x20000000000000, max_literal()); | |
84 Expect.equals(-0x20000000000000, min_literal()); | |
85 | |
86 // Run the tests once before optimizations. | |
87 dti_arg = 1.9e17; | |
88 Expect.throws(double_to_int, isJavascriptIntError); | |
89 | |
90 ia_arg1 = (1 << 53); | |
91 ia_arg2 = (1 << 53); | |
92 Expect.throws(integer_add, isJavascriptIntError); | |
93 | |
94 n_arg = -0x20000000000000; | |
95 Expect.equals(0x20000000000000, negate()); | |
96 | |
97 is_arg = (1 << 53); | |
98 Expect.throws(integer_shift, isJavascriptIntError); | |
99 | |
100 Expect.throws(max_add_throws, isJavascriptIntError); | |
101 Expect.throws(min_sub_throws, isJavascriptIntError); | |
102 | |
103 for (int i = 0; i < 20; i++) { | |
104 dti_arg = i.toDouble(); | |
105 // Expect.throws calls through the closure, so we have to here, too. | |
106 var f = double_to_int; | |
107 Expect.equals(i, f()); | |
108 | |
109 ia_arg1 = i; | |
110 ia_arg2 = i; | |
111 f = integer_add; | |
112 Expect.equals(i + i, f()); | |
113 | |
114 n_arg = i; | |
115 f = negate; | |
116 Expect.equals(-i, f()); | |
117 | |
118 is_arg = i; | |
119 f = integer_shift; | |
120 Expect.equals(i << 1, f()); | |
121 } | |
122 | |
123 // The optimized functions should now deoptimize and throw the error. | |
124 dti_arg = 1.9e17; | |
125 Expect.throws(double_to_int, isJavascriptIntError); | |
126 | |
127 ia_arg1 = (1 << 53); | |
128 ia_arg2 = (1 << 53); | |
129 Expect.throws(integer_add, isJavascriptIntError); | |
130 | |
131 n_arg = -0x20000000000000; | |
132 Expect.equals(0x20000000000000, negate()); | |
133 | |
134 is_arg = (1 << 53); | |
135 Expect.throws(integer_shift, isJavascriptIntError); | |
136 | |
137 Expect.throws(max_add_throws, isJavascriptIntError); | |
138 Expect.throws(min_sub_throws, isJavascriptIntError); | |
139 | |
140 for (int i = 0; i < 20; i++) { | |
141 Expect.equals(0xAFAFA000, doNotThrow1(0xFAFAFA, 12)); | |
142 Expect.equals(0x2FAFA000, doNotThrow3(0xFAFAFA, 12)); | |
143 Expect.equals(0xABABA000, doNotThrow2(0xFAFAFAABABA, 12)); | |
144 Expect.equals(0x2BABA000, doNotThrow4(0xFAFAFAABABA, 12)); | |
145 } | |
146 for (int i = 0; i < 20; i++) { | |
147 Expect.equals(0xABABA000, doNotThrow1(0xFAFAFAABABA, 12)); | |
148 Expect.equals(0x2BABA000, doNotThrow3(0xFAFAFAABABA, 12)); | |
149 } | |
150 | |
151 } | |
OLD | NEW |