OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --strong-mode --allow-natives-syntax | 5 // Flags: --strong-mode --allow-natives-syntax |
6 | 6 |
7 'use strict'; | 7 'use strict'; |
arv (Not doing code reviews)
2015/04/27 14:02:49
Use double quotes here too for consistency.
conradw
2015/04/27 20:41:57
Done.
| |
8 | 8 |
9 // TODO(conradw): Implement other strong operators | 9 // TODO(conradw): Implement other strong operators |
10 let strong_arith = [ | 10 let strongBinops = [ |
11 "-", | 11 "-", |
12 "*", | 12 "*", |
13 "/", | 13 "/", |
14 "%" | 14 "%", |
15 ] | 15 "|", |
16 "&", | |
17 "^", | |
18 "<<", | |
19 ">>", | |
20 ">>>", | |
21 ]; | |
16 | 22 |
17 let nonnumber_values = [ | 23 let strongUnops = [ |
18 "{}", | 24 "~", |
19 "'foo'", | 25 "+", |
20 "(function(){})", | 26 "-" |
21 "[]", | 27 ]; |
22 "'0'", | |
23 "'NaN'", | |
24 "(class Foo {})" | |
25 ] | |
26 | 28 |
27 let number_values = [ | 29 let nonNumberValues = [ |
28 "0", | 30 "{}", |
29 "(-0)", | 31 "'foo'", |
30 "1", | 32 "(function(){})", |
31 "0.79", | 33 "[]", |
32 "(-0.79)", | 34 "'0'", |
33 "4294967295", | 35 "'NaN'", |
34 "4294967296", | 36 "(class Foo {})" |
35 "(-4294967295)", | 37 ]; |
36 "(-4294967296)", | 38 |
37 "9999999999999", | 39 let numberValues = [ |
38 "(-9999999999999)", | 40 "0", |
39 "1.5e10", | 41 "(-0)", |
40 "(-1.5e10)", | 42 "1", |
41 "0xFFF", | 43 "0.79", |
42 "(-0xFFF)", | 44 "(-0.79)", |
43 "NaN", | 45 "4294967295", |
44 "Infinity", | 46 "4294967296", |
45 "(-Infinity)" | 47 "(-4294967295)", |
46 ] | 48 "(-4294967296)", |
49 "9999999999999", | |
50 "(-9999999999999)", | |
51 "1.5e10", | |
52 "(-1.5e10)", | |
53 "0xFFF", | |
54 "(-0xFFF)", | |
55 "NaN", | |
56 "Infinity", | |
57 "(-Infinity)" | |
58 ]; | |
47 | 59 |
48 function sub_strong(x, y) { | 60 function sub_strong(x, y) { |
49 "use strong"; | 61 "use strong"; |
50 let v = x - y; | 62 return x - y; |
51 return v; | |
52 } | 63 } |
53 | 64 |
54 function mul_strong(x, y) { | 65 function mul_strong(x, y) { |
55 "use strong"; | 66 "use strong"; |
56 let v = x * y; | 67 return x * y; |
57 return v; | |
58 } | 68 } |
59 | 69 |
60 function div_strong(x, y) { | 70 function div_strong(x, y) { |
61 "use strong"; | 71 "use strong"; |
62 let v = x / y; | 72 return x / y; |
63 return v; | |
64 } | 73 } |
65 | 74 |
66 function mod_strong(x, y) { | 75 function mod_strong(x, y) { |
67 "use strong"; | 76 "use strong"; |
68 let v = x % y; | 77 return x % y; |
69 return v; | |
70 } | 78 } |
71 | 79 |
72 let strong_funcs = [sub_strong, mul_strong, div_strong, mod_strong]; | 80 function or_strong(x, y) { |
81 "use strong"; | |
82 return x | y; | |
83 } | |
84 | |
85 function and_strong(x, y) { | |
86 "use strong"; | |
87 return x & y; | |
88 } | |
89 | |
90 function xor_strong(x, y) { | |
91 "use strong"; | |
92 return x ^ y; | |
93 } | |
94 | |
95 function shl_strong(x, y) { | |
96 "use strong"; | |
97 return x << y; | |
98 } | |
99 | |
100 function shr_strong(x, y) { | |
101 "use strong"; | |
102 return x >> y; | |
103 } | |
104 | |
105 function sar_strong(x, y) { | |
106 "use strong"; | |
107 return x >>> y; | |
108 } | |
109 | |
110 function typed_sub_strong(x, y) { | |
111 "use strong"; | |
112 return (+x) - (+y); | |
113 } | |
114 | |
115 function typed_mul_strong(x, y) { | |
116 "use strong"; | |
117 return (+x) * (+y); | |
118 } | |
119 | |
120 function typed_div_strong(x, y) { | |
121 "use strong"; | |
122 return (+x) / (+y); | |
123 } | |
124 | |
125 function typed_mod_strong(x, y) { | |
126 "use strong"; | |
127 return (+x) % (+y); | |
128 } | |
129 | |
130 function typed_or_strong(x, y) { | |
131 "use strong"; | |
132 return (+x) | (+y); | |
133 } | |
134 | |
135 function typed_and_strong(x, y) { | |
136 "use strong"; | |
137 return (+x) & (+y); | |
138 } | |
139 | |
140 function typed_xor_strong(x, y) { | |
141 "use strong"; | |
142 return (+x) ^ (+y); | |
143 } | |
144 | |
145 function typed_shl_strong(x, y) { | |
146 "use strong"; | |
147 return (+x) << (+y); | |
148 } | |
149 | |
150 function typed_shr_strong(x, y) { | |
151 "use strong"; | |
152 return (+x) >> (+y); | |
153 } | |
154 | |
155 function typed_sar_strong(x, y) { | |
156 "use strong"; | |
157 return (+x) >>> (+y); | |
158 } | |
159 | |
160 let strongFuncs = [sub_strong, mul_strong, div_strong, mod_strong, or_strong, | |
161 and_strong, xor_strong, shl_strong, shr_strong, sar_strong, | |
162 typed_sub_strong, typed_mul_strong, typed_div_strong, | |
163 typed_mod_strong, typed_or_strong, typed_and_strong, | |
164 typed_xor_strong, typed_shl_strong, typed_shr_strong, | |
165 typed_sar_strong]; | |
73 | 166 |
74 function inline_sub_strong(x, y) { | 167 function inline_sub_strong(x, y) { |
75 "use strong"; | 168 "use strong"; |
76 let v = x - y; | 169 let v = x - y; |
77 return v; | 170 return v; |
78 } | 171 } |
79 | 172 |
80 function inline_outer(x, y) { | 173 function inline_outer(x, y) { |
81 return inline_sub_strong(x, y); | 174 return inline_sub_strong(x, y); |
82 } | 175 } |
83 | 176 |
84 function inline_sub(x, y) { | 177 function inline_sub(x, y) { |
85 let v = x - y; | 178 let v = x - y; |
86 return v; | 179 return v; |
87 } | 180 } |
88 | 181 |
89 function inline_outer_strong(x, y) { | 182 function inline_outer_strong(x, y) { |
90 "use strong"; | 183 "use strong"; |
91 return inline_sub(x, y); | 184 return inline_sub(x, y); |
92 } | 185 } |
93 | 186 |
94 for (let op of strong_arith) { | 187 for (let op of strongBinops) { |
95 for (let left of number_values) { | 188 for (let left of numberValues) { |
96 for (let right of number_values) { | 189 for (let right of numberValues) { |
97 let expr = "(" + left + op + right + ")"; | 190 let expr = "(" + left + op + right + ")"; |
98 assertEquals(eval(expr), eval("'use strong';" + expr)); | 191 assertEquals(eval(expr), eval("'use strong';" + expr)); |
99 assertDoesNotThrow("'use strong'; " + expr + ";"); | 192 assertDoesNotThrow("'use strong'; " + expr + ";"); |
100 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); | 193 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); |
101 } | 194 } |
102 } | 195 } |
103 for (let left of number_values) { | 196 for (let left of numberValues) { |
104 for (let right of nonnumber_values) { | 197 for (let right of nonNumberValues) { |
105 let expr = "(" + left + op + right + ")"; | 198 let expr = "(" + left + op + right + ")"; |
106 assertDoesNotThrow("'use strict'; " + expr + ";"); | 199 assertDoesNotThrow("'use strict'; " + expr + ";"); |
107 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | 200 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); |
108 assertThrows("'use strong'; " + expr + ";", TypeError); | 201 assertThrows("'use strong'; " + expr + ";", TypeError); |
109 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | 202 assertThrows("'use strong'; let v = " + expr + ";", TypeError); |
110 } | 203 } |
111 } | 204 } |
112 for (let left of nonnumber_values) { | 205 for (let left of nonNumberValues) { |
113 for (let right of number_values.concat(nonnumber_values)) { | 206 for (let right of numberValues.concat(nonNumberValues)) { |
114 let expr = "(" + left + op + right + ")"; | 207 let expr = "(" + left + op + right + ")"; |
115 assertDoesNotThrow("'use strict'; " + expr + ";"); | 208 assertDoesNotThrow("'use strict'; " + expr + ";"); |
116 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | 209 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); |
117 assertThrows("'use strong'; " + expr + ";", TypeError); | 210 assertThrows("'use strong'; " + expr + ";", TypeError); |
118 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | 211 assertThrows("'use strong'; let v = " + expr + ";", TypeError); |
119 } | 212 } |
120 } | 213 } |
121 } | 214 } |
122 | 215 |
123 for (let func of strong_funcs) { | 216 for (let op of strongUnops) { |
217 for (let value of numberValues) { | |
218 let expr = "(" + op + value + ")"; | |
219 assertEquals(eval(expr), eval("'use strong';" + expr)); | |
220 assertDoesNotThrow("'use strong'; " + expr + ";"); | |
221 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); | |
222 } | |
223 for (let value of nonNumberValues) { | |
224 let expr = "(" + op + value + ")"; | |
225 assertDoesNotThrow("'use strict'; " + expr + ";"); | |
226 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | |
227 assertThrows("'use strong'; " + expr + ";", TypeError); | |
228 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | |
229 } | |
230 } | |
231 | |
232 for (let func of strongFuncs) { | |
124 let a = func(4, 5); | 233 let a = func(4, 5); |
125 let b = func(4, 5); | 234 let b = func(4, 5); |
126 assertTrue(a === b); | 235 assertTrue(a === b); |
127 %OptimizeFunctionOnNextCall(func); | 236 %OptimizeFunctionOnNextCall(func); |
128 let c = func(4, 5); | 237 let c = func(4, 5); |
129 assertOptimized(func); | 238 assertOptimized(func); |
130 assertTrue(b === c); | 239 assertTrue(b === c); |
131 %DeoptimizeFunction(func); | 240 %DeoptimizeFunction(func); |
132 let d = func(4, 5); | 241 let d = func(4, 5); |
133 assertTrue(c === d); | 242 assertTrue(c === d); |
134 %DeoptimizeFunction(func); | 243 %DeoptimizeFunction(func); |
135 %ClearFunctionTypeFeedback(func); | 244 %ClearFunctionTypeFeedback(func); |
136 } | 245 } |
137 | 246 |
138 for (let func of strong_funcs) { | 247 for (let func of strongFuncs) { |
139 try { | 248 try { |
140 let a = func(2, 3); | 249 let a = func(2, 3); |
141 let b = func(2, 3); | 250 let b = func(2, 3); |
142 assertTrue(a === b); | 251 assertTrue(a === b); |
143 %OptimizeFunctionOnNextCall(func); | 252 %OptimizeFunctionOnNextCall(func); |
144 let c = func(2, "foo"); | 253 let c = func(2, "foo"); |
145 assertUnreachable(); | 254 assertUnreachable(); |
146 } catch(e) { | 255 } catch (e) { |
147 assertTrue(e instanceof TypeError); | 256 assertInstanceof(e, TypeError); |
148 assertUnoptimized(func); | 257 assertUnoptimized(func); |
149 assertThrows(function(){func(2, "foo");}, TypeError); | 258 assertThrows(function(){func(2, "foo");}, TypeError); |
150 assertDoesNotThrow(function(){func(2, 3);}); | 259 assertDoesNotThrow(function(){func(2, 3);}); |
151 } | 260 } |
152 } | 261 } |
153 | 262 |
154 assertThrows(function(){inline_outer(1, {})}, TypeError); | 263 assertThrows(function(){inline_outer(1, {})}, TypeError); |
155 for (var i = 0; i < 100; i++) { | 264 for (var i = 0; i < 100; i++) { |
156 inline_outer(1, 2); | 265 inline_outer(1, 2); |
157 } | 266 } |
158 assertThrows(function(){inline_outer(1, {})}, TypeError); | 267 assertThrows(function(){inline_outer(1, {})}, TypeError); |
159 | 268 |
160 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); | 269 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); |
161 for (var i = 0; i < 100; i++) { | 270 for (var i = 0; i < 100; i++) { |
162 inline_outer_strong(1, 2); | 271 inline_outer_strong(1, 2); |
163 } | 272 } |
164 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); | 273 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); |
OLD | NEW |