Chromium Code Reviews| 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'; |
| 8 | 8 |
| 9 // TODO(conradw): Implement other strong operators | 9 // TODO(conradw): Implement other strong operators |
| 10 let strong_arith = [ | 10 let strong_binops = [ |
|
arv (Not doing code reviews)
2015/04/24 15:39:42
FYI. Our js style does not allow underscores in va
conradw
2015/04/27 11:20:47
Hah! I should at least try and be consistent, sinc
| |
| 11 "-", | 11 "-", |
| 12 "*", | 12 "*", |
| 13 "/", | 13 "/", |
| 14 "%" | 14 "%", |
| 15 ] | 15 "|", |
| 16 "&", | |
| 17 "^", | |
| 18 "<<", | |
| 19 ">>", | |
| 20 ">>>", | |
| 21 ]; | |
| 16 | 22 |
| 17 let nonnumber_values = [ | 23 let strong_unops = [ |
| 18 "{}", | 24 "~" |
| 19 "'foo'", | 25 ]; |
| 20 "(function(){})", | |
| 21 "[]", | |
| 22 "'0'", | |
| 23 "'NaN'", | |
| 24 "(class Foo {})" | |
| 25 ] | |
| 26 | 26 |
| 27 let number_values = [ | 27 let nonNumberValues = [ |
| 28 "0", | 28 "{}", |
| 29 "(-0)", | 29 "'foo'", |
| 30 "1", | 30 "(function(){})", |
| 31 "0.79", | 31 "[]", |
| 32 "(-0.79)", | 32 "'0'", |
| 33 "4294967295", | 33 "'NaN'", |
| 34 "4294967296", | 34 "(class Foo {})" |
| 35 "(-4294967295)", | 35 ]; |
| 36 "(-4294967296)", | 36 |
| 37 "9999999999999", | 37 let numberValues = [ |
| 38 "(-9999999999999)", | 38 "0", |
| 39 "1.5e10", | 39 "(-0)", |
| 40 "(-1.5e10)", | 40 "1", |
| 41 "0xFFF", | 41 "0.79", |
| 42 "(-0xFFF)", | 42 "(-0.79)", |
| 43 "NaN", | 43 "4294967295", |
| 44 "Infinity", | 44 "4294967296", |
| 45 "(-Infinity)" | 45 "(-4294967295)", |
| 46 ] | 46 "(-4294967296)", |
| 47 "9999999999999", | |
| 48 "(-9999999999999)", | |
| 49 "1.5e10", | |
| 50 "(-1.5e10)", | |
| 51 "0xFFF", | |
| 52 "(-0xFFF)", | |
| 53 "NaN", | |
| 54 "Infinity", | |
| 55 "(-Infinity)" | |
| 56 ]; | |
| 47 | 57 |
| 48 function sub_strong(x, y) { | 58 function sub_strong(x, y) { |
| 49 "use strong"; | 59 "use strong"; |
| 50 let v = x - y; | 60 return x - y; |
| 51 return v; | |
| 52 } | 61 } |
| 53 | 62 |
| 54 function mul_strong(x, y) { | 63 function mul_strong(x, y) { |
| 55 "use strong"; | 64 "use strong"; |
| 56 let v = x * y; | 65 return x * y; |
| 57 return v; | |
| 58 } | 66 } |
| 59 | 67 |
| 60 function div_strong(x, y) { | 68 function div_strong(x, y) { |
| 61 "use strong"; | 69 "use strong"; |
| 62 let v = x / y; | 70 return x / y; |
| 63 return v; | |
| 64 } | 71 } |
| 65 | 72 |
| 66 function mod_strong(x, y) { | 73 function mod_strong(x, y) { |
| 67 "use strong"; | 74 "use strong"; |
| 68 let v = x % y; | 75 return x % y; |
| 69 return v; | |
| 70 } | 76 } |
| 71 | 77 |
| 72 let strong_funcs = [sub_strong, mul_strong, div_strong, mod_strong]; | 78 function or_strong(x, y) { |
| 79 "use strong"; | |
| 80 return x | y; | |
| 81 } | |
| 82 | |
| 83 function and_strong(x, y) { | |
| 84 "use strong"; | |
| 85 return x & y; | |
| 86 } | |
| 87 | |
| 88 function xor_strong(x, y) { | |
| 89 "use strong"; | |
| 90 return x ^ y; | |
| 91 } | |
| 92 | |
| 93 function shl_strong(x, y) { | |
| 94 "use strong"; | |
| 95 return x << y; | |
| 96 } | |
| 97 | |
| 98 function shr_strong(x, y) { | |
| 99 "use strong"; | |
| 100 return x >> y; | |
| 101 } | |
| 102 | |
| 103 function sar_strong(x, y) { | |
| 104 "use strong"; | |
| 105 return x >>> y; | |
| 106 } | |
| 107 | |
| 108 function not_strong(x, y) { | |
|
arv (Not doing code reviews)
2015/04/24 15:39:42
Not clear why you need this function. You are test
conradw
2015/04/27 11:20:47
Yeah, it was a mistake to put this here.
| |
| 109 "use strong"; | |
| 110 let v1 = ~x; | |
| 111 let v2 = ~y; | |
| 112 return v1 | v2; | |
| 113 } | |
| 114 | |
| 115 let strong_funcs = [sub_strong, mul_strong, div_strong, mod_strong, or_strong, | |
| 116 and_strong, xor_strong, shl_strong, shr_strong, sar_strong, | |
| 117 not_strong]; | |
| 73 | 118 |
| 74 function inline_sub_strong(x, y) { | 119 function inline_sub_strong(x, y) { |
| 75 "use strong"; | 120 "use strong"; |
| 76 let v = x - y; | 121 let v = x - y; |
| 77 return v; | 122 return v; |
| 78 } | 123 } |
| 79 | 124 |
| 80 function inline_outer(x, y) { | 125 function inline_outer(x, y) { |
| 81 return inline_sub_strong(x, y); | 126 return inline_sub_strong(x, y); |
| 82 } | 127 } |
| 83 | 128 |
| 84 function inline_sub(x, y) { | 129 function inline_sub(x, y) { |
| 85 let v = x - y; | 130 let v = x - y; |
| 86 return v; | 131 return v; |
| 87 } | 132 } |
| 88 | 133 |
| 89 function inline_outer_strong(x, y) { | 134 function inline_outer_strong(x, y) { |
| 90 "use strong"; | 135 "use strong"; |
| 91 return inline_sub(x, y); | 136 return inline_sub(x, y); |
| 92 } | 137 } |
| 93 | 138 |
| 94 for (let op of strong_arith) { | 139 for (let op of strong_binops) { |
| 95 for (let left of number_values) { | 140 for (let left of numberValues) { |
| 96 for (let right of number_values) { | 141 for (let right of numberValues) { |
| 97 let expr = "(" + left + op + right + ")"; | 142 let expr = "(" + left + op + right + ")"; |
| 98 assertEquals(eval(expr), eval("'use strong';" + expr)); | 143 assertEquals(eval(expr), eval("'use strong';" + expr)); |
| 99 assertDoesNotThrow("'use strong'; " + expr + ";"); | 144 assertDoesNotThrow("'use strong'; " + expr + ";"); |
| 100 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); | 145 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); |
| 101 } | 146 } |
| 102 } | 147 } |
| 103 for (let left of number_values) { | 148 for (let left of numberValues) { |
| 104 for (let right of nonnumber_values) { | 149 for (let right of nonNumberValues) { |
| 105 let expr = "(" + left + op + right + ")"; | 150 let expr = "(" + left + op + right + ")"; |
| 106 assertDoesNotThrow("'use strict'; " + expr + ";"); | 151 assertDoesNotThrow("'use strict'; " + expr + ";"); |
| 107 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | 152 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); |
| 108 assertThrows("'use strong'; " + expr + ";", TypeError); | 153 assertThrows("'use strong'; " + expr + ";", TypeError); |
| 109 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | 154 assertThrows("'use strong'; let v = " + expr + ";", TypeError); |
| 110 } | 155 } |
| 111 } | 156 } |
| 112 for (let left of nonnumber_values) { | 157 for (let left of nonNumberValues) { |
| 113 for (let right of number_values.concat(nonnumber_values)) { | 158 for (let right of numberValues.concat(nonNumberValues)) { |
| 114 let expr = "(" + left + op + right + ")"; | 159 let expr = "(" + left + op + right + ")"; |
| 115 assertDoesNotThrow("'use strict'; " + expr + ";"); | 160 assertDoesNotThrow("'use strict'; " + expr + ";"); |
| 116 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | 161 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); |
| 117 assertThrows("'use strong'; " + expr + ";", TypeError); | 162 assertThrows("'use strong'; " + expr + ";", TypeError); |
| 118 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | 163 assertThrows("'use strong'; let v = " + expr + ";", TypeError); |
| 119 } | 164 } |
| 120 } | 165 } |
| 121 } | 166 } |
| 122 | 167 |
| 168 for (let op of strong_unops) { | |
| 169 for (let value of numberValues) { | |
| 170 let expr = "(" + op + value + ")"; | |
| 171 assertEquals(eval(expr), eval("'use strong';" + expr)); | |
| 172 assertDoesNotThrow("'use strong'; " + expr + ";"); | |
| 173 assertDoesNotThrow("'use strong'; let v = " + expr + ";"); | |
| 174 } | |
| 175 for (let value of nonNumberValues) { | |
| 176 let expr = "(" + op + value + ")"; | |
| 177 assertDoesNotThrow("'use strict'; " + expr + ";"); | |
| 178 assertDoesNotThrow("'use strict'; let v = " + expr + ";"); | |
| 179 assertThrows("'use strong'; " + expr + ";", TypeError); | |
| 180 assertThrows("'use strong'; let v = " + expr + ";", TypeError); | |
| 181 } | |
| 182 } | |
| 183 | |
| 123 for (let func of strong_funcs) { | 184 for (let func of strong_funcs) { |
| 124 let a = func(4, 5); | 185 let a = func(4, 5); |
| 125 let b = func(4, 5); | 186 let b = func(4, 5); |
| 126 assertTrue(a === b); | 187 assertTrue(a === b); |
| 127 %OptimizeFunctionOnNextCall(func); | 188 %OptimizeFunctionOnNextCall(func); |
| 128 let c = func(4, 5); | 189 let c = func(4, 5); |
| 129 assertOptimized(func); | 190 assertOptimized(func); |
| 130 assertTrue(b === c); | 191 assertTrue(b === c); |
| 131 %DeoptimizeFunction(func); | 192 %DeoptimizeFunction(func); |
| 132 let d = func(4, 5); | 193 let d = func(4, 5); |
| 133 assertTrue(c === d); | 194 assertTrue(c === d); |
| 134 %DeoptimizeFunction(func); | 195 %DeoptimizeFunction(func); |
| 135 %ClearFunctionTypeFeedback(func); | 196 %ClearFunctionTypeFeedback(func); |
| 136 } | 197 } |
| 137 | 198 |
| 138 for (let func of strong_funcs) { | 199 for (let func of strong_funcs) { |
| 139 try { | 200 try { |
| 140 let a = func(2, 3); | 201 let a = func(2, 3); |
| 141 let b = func(2, 3); | 202 let b = func(2, 3); |
| 142 assertTrue(a === b); | 203 assertTrue(a === b); |
| 143 %OptimizeFunctionOnNextCall(func); | 204 %OptimizeFunctionOnNextCall(func); |
| 144 let c = func(2, "foo"); | 205 let c = func(2, "foo"); |
| 145 assertUnreachable(); | 206 assertUnreachable(); |
| 146 } catch(e) { | 207 } catch (e) { |
| 147 assertTrue(e instanceof TypeError); | 208 assertInstanceof(e, TypeError); |
| 148 assertUnoptimized(func); | 209 assertUnoptimized(func); |
| 149 assertThrows(function(){func(2, "foo");}, TypeError); | 210 assertThrows(function(){func(2, "foo");}, TypeError); |
| 150 assertDoesNotThrow(function(){func(2, 3);}); | 211 assertDoesNotThrow(function(){func(2, 3);}); |
| 151 } | 212 } |
| 152 } | 213 } |
| 153 | 214 |
| 154 assertThrows(function(){inline_outer(1, {})}, TypeError); | 215 assertThrows(function(){inline_outer(1, {})}, TypeError); |
| 155 for (var i = 0; i < 100; i++) { | 216 for (var i = 0; i < 100; i++) { |
| 156 inline_outer(1, 2); | 217 inline_outer(1, 2); |
| 157 } | 218 } |
| 158 assertThrows(function(){inline_outer(1, {})}, TypeError); | 219 assertThrows(function(){inline_outer(1, {})}, TypeError); |
| 159 | 220 |
| 160 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); | 221 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); |
| 161 for (var i = 0; i < 100; i++) { | 222 for (var i = 0; i < 100; i++) { |
| 162 inline_outer_strong(1, 2); | 223 inline_outer_strong(1, 2); |
| 163 } | 224 } |
| 164 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); | 225 assertDoesNotThrow(function(){inline_outer_strong(1, {})}); |
| OLD | NEW |