Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: test/mjsunit/strong/implicit-conversions.js

Issue 1092353002: [strong] Disallow implicit conversions for binary arithmetic operations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: cl feedback 5 Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Flags: --strong-mode --allow-natives-syntax
6
7 'use strict';
8
9 // TODO(conradw): Implement other strong operators
10 let strong_arith = [
11 "-",
arv (Not doing code reviews) 2015/04/24 14:14:23 Don't mix ' and " in the same file. ' is preferred
arv (Not doing code reviews) 2015/04/24 14:14:23 Array and Object literals should be indented 2 spa
conradw 2015/04/24 14:42:32 Will add to my followup https://codereview.chromiu
conradw 2015/04/24 14:42:32 I've been trying to be consistent in using ", whic
12 "*",
13 "/",
14 "%"
15 ]
arv (Not doing code reviews) 2015/04/24 14:14:23 semicolon
conradw 2015/04/24 14:42:32 followup
16
17 let nonnumber_values = [
arv (Not doing code reviews) 2015/04/24 14:14:23 nonNumberValues https://google-styleguide.googlec
conradw 2015/04/24 14:42:32 followup
18 "{}",
19 "'foo'",
20 "(function(){})",
21 "[]",
22 "'0'",
23 "'NaN'",
24 "(class Foo {})"
25 ]
26
27 let number_values = [
28 "0",
29 "(-0)",
30 "1",
31 "0.79",
32 "(-0.79)",
33 "4294967295",
34 "4294967296",
35 "(-4294967295)",
36 "(-4294967296)",
37 "9999999999999",
38 "(-9999999999999)",
39 "1.5e10",
40 "(-1.5e10)",
41 "0xFFF",
42 "(-0xFFF)",
43 "NaN",
44 "Infinity",
45 "(-Infinity)"
46 ]
47
48 function sub_strong(x, y) {
49 "use strong";
50 let v = x - y;
51 return v;
arv (Not doing code reviews) 2015/04/24 14:14:23 why not just `return x - y`? Is this to prevent i
conradw 2015/04/24 14:42:32 there's no particular reason for it, will change i
52 }
53
54 function mul_strong(x, y) {
55 "use strong";
56 let v = x * y;
57 return v;
58 }
59
60 function div_strong(x, y) {
61 "use strong";
62 let v = x / y;
63 return v;
64 }
65
66 function mod_strong(x, y) {
67 "use strong";
68 let v = x % y;
69 return v;
70 }
71
72 let strong_funcs = [sub_strong, mul_strong, div_strong, mod_strong];
73
74 function inline_sub_strong(x, y) {
75 "use strong";
76 let v = x - y;
77 return v;
78 }
79
80 function inline_outer(x, y) {
81 return inline_sub_strong(x, y);
82 }
83
84 function inline_sub(x, y) {
85 let v = x - y;
86 return v;
87 }
88
89 function inline_outer_strong(x, y) {
90 "use strong";
91 return inline_sub(x, y);
92 }
93
94 for (let op of strong_arith) {
95 for (let left of number_values) {
96 for (let right of number_values) {
97 let expr = "(" + left + op + right + ")";
98 assertEquals(eval(expr), eval("'use strong';" + expr));
99 assertDoesNotThrow("'use strong'; " + expr + ";");
100 assertDoesNotThrow("'use strong'; let v = " + expr + ";");
101 }
102 }
103 for (let left of number_values) {
104 for (let right of nonnumber_values) {
105 let expr = "(" + left + op + right + ")";
106 assertDoesNotThrow("'use strict'; " + expr + ";");
107 assertDoesNotThrow("'use strict'; let v = " + expr + ";");
108 assertThrows("'use strong'; " + expr + ";", TypeError);
109 assertThrows("'use strong'; let v = " + expr + ";", TypeError);
arv (Not doing code reviews) 2015/04/24 14:14:23 These things might be easier to read with template
conradw 2015/04/24 14:42:32 Probably. It doesn't sound like a bad idea for me
110 }
111 }
112 for (let left of nonnumber_values) {
113 for (let right of number_values.concat(nonnumber_values)) {
114 let expr = "(" + left + op + right + ")";
115 assertDoesNotThrow("'use strict'; " + expr + ";");
116 assertDoesNotThrow("'use strict'; let v = " + expr + ";");
117 assertThrows("'use strong'; " + expr + ";", TypeError);
118 assertThrows("'use strong'; let v = " + expr + ";", TypeError);
119 }
120 }
121 }
122
123 for (let func of strong_funcs) {
124 let a = func(4, 5);
125 let b = func(4, 5);
126 assertTrue(a === b);
127 %OptimizeFunctionOnNextCall(func);
128 let c = func(4, 5);
129 assertOptimized(func);
130 assertTrue(b === c);
131 %DeoptimizeFunction(func);
132 let d = func(4, 5);
133 assertTrue(c === d);
134 %DeoptimizeFunction(func);
135 %ClearFunctionTypeFeedback(func);
136 }
137
138 for (let func of strong_funcs) {
139 try {
140 let a = func(2, 3);
141 let b = func(2, 3);
142 assertTrue(a === b);
143 %OptimizeFunctionOnNextCall(func);
144 let c = func(2, "foo");
145 assertUnreachable();
146 } catch(e) {
arv (Not doing code reviews) 2015/04/24 14:14:23 catch (e) Just like if, for etc
conradw 2015/04/24 14:42:32 followup
147 assertTrue(e instanceof TypeError);
arv (Not doing code reviews) 2015/04/24 14:14:23 or assertInstanceof
conradw 2015/04/24 14:42:32 followup
148 assertUnoptimized(func);
149 assertThrows(function(){func(2, "foo");}, TypeError);
150 assertDoesNotThrow(function(){func(2, 3);});
151 }
152 }
153
154 assertThrows(function(){inline_outer(1, {})}, TypeError);
155 for (var i = 0; i < 100; i++) {
156 inline_outer(1, 2);
157 }
158 assertThrows(function(){inline_outer(1, {})}, TypeError);
159
160 assertDoesNotThrow(function(){inline_outer_strong(1, {})});
161 for (var i = 0; i < 100; i++) {
162 inline_outer_strong(1, 2);
163 }
164 assertDoesNotThrow(function(){inline_outer_strong(1, {})});
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698