Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 // Flags: --harmony | |
| 29 | |
| 30 // Test for-of syntax. | |
| 31 | |
| 32 "use strict"; | |
| 33 | |
| 34 | |
| 35 // First, some helpers. | |
| 36 | |
| 37 function* values() { | |
| 38 for (var i = 0; i < arguments.length; i++) { | |
| 39 yield arguments[i]; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 function integers_until(max) { | |
| 44 function next() { | |
| 45 var ret = { value: this.n, done: this.n == max }; | |
| 46 this.n++; | |
| 47 return ret; | |
| 48 } | |
| 49 return { next: next, n: 0 } | |
| 50 } | |
| 51 | |
| 52 function results(results) { | |
| 53 var i = 0; | |
| 54 function next() { | |
| 55 return results[i++]; | |
| 56 } | |
| 57 return { next: next } | |
| 58 } | |
| 59 | |
| 60 function* integers_from(n) { | |
| 61 while (1) yield n++; | |
| 62 } | |
| 63 | |
| 64 // A destructive append. | |
| 65 function append(x, tail) { | |
| 66 tail[tail.length] = x; | |
| 67 return tail; | |
| 68 } | |
| 69 | |
| 70 function sum(x, tail) { | |
| 71 return x + tail; | |
| 72 } | |
| 73 | |
| 74 function fold(cons, seed, iter) { | |
| 75 for (var x of iter) { | |
| 76 seed = cons(x, seed); | |
| 77 } | |
| 78 return seed; | |
| 79 } | |
| 80 | |
| 81 function* take(iter, n) { | |
| 82 if (n == 0) return; | |
| 83 for (let x of iter) { | |
| 84 yield x; | |
| 85 if (--n == 0) break; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 function nth(iter, n) { | |
| 90 for (let x of iter) { | |
| 91 if (n-- == 0) return x; | |
| 92 } | |
| 93 throw "unreachable"; | |
| 94 } | |
| 95 | |
| 96 function* skip_every(iter, n) { | |
| 97 var i = 0; | |
| 98 for (let x of iter) { | |
| 99 if (++i % n == 0) continue; | |
| 100 yield x; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 function* iter_map(iter, f) { | |
| 105 for (var x of iter) { | |
| 106 yield f(x); | |
| 107 } | |
| 108 } | |
| 109 function nested_fold(cons, seed, iter) { | |
| 110 var visited = [] | |
| 111 for (let x of iter) { | |
| 112 for (let y of x) { | |
| 113 seed = cons(y, seed); | |
| 114 } | |
| 115 } | |
| 116 return seed; | |
| 117 } | |
| 118 | |
| 119 function* unreachable(iter) { | |
| 120 for (let x of iter) { | |
| 121 throw "not reached"; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 function one_time_getter(o, prop, val) { | |
| 126 function set_never() { throw "unreachable"; } | |
| 127 var gotten = false; | |
| 128 function get_once() { | |
| 129 if (gotten) throw "got twice"; | |
| 130 gotten = true; | |
| 131 return val; | |
| 132 } | |
| 133 Object.defineProperty(o, prop, {get: get_once, set: set_never}) | |
| 134 return o; | |
| 135 } | |
| 136 | |
| 137 function never_getter(o, prop) { | |
| 138 function never() { throw "unreachable"; } | |
| 139 Object.defineProperty(o, prop, {get: never, set: never}) | |
| 140 return o; | |
| 141 } | |
| 142 | |
| 143 function remove_next_after(iter, n) { | |
| 144 function next() { | |
| 145 if (n-- == 0) delete this.next; | |
| 146 return iter.next(); | |
| 147 } | |
| 148 return { next: next } | |
| 149 } | |
| 150 | |
| 151 function poison_next_after(iter, n) { | |
| 152 function next() { | |
| 153 return iter.next(); | |
| 154 } | |
| 155 function next_getter() { | |
| 156 if (n-- < 0) | |
| 157 throw "poisoned"; | |
| 158 return next; | |
| 159 } | |
| 160 var o = {}; | |
| 161 Object.defineProperty(o, 'next', { get: next_getter }); | |
| 162 return o; | |
| 163 } | |
| 164 | |
| 165 // Now, the tests. | |
| 166 | |
| 167 // Non-generator iterators. | |
| 168 assertEquals(45, fold(sum, 0, integers_until(10))); | |
| 169 // Generator iterators. | |
| 170 assertEquals([1, 2, 3], fold(append, [], values(1, 2, 3))); | |
| 171 // Break. | |
| 172 assertEquals(45, fold(sum, 0, take(integers_from(0), 10))); | |
| 173 // Continue. | |
| 174 assertEquals(90, fold(sum, 0, take(skip_every(integers_from(0), 2), 10))); | |
| 175 // Return. | |
| 176 assertEquals(10, nth(integers_from(0), 10)); | |
| 177 // Nested for-of. | |
| 178 assertEquals([0, 0, 1, 0, 1, 2, 0, 1, 2, 3], | |
| 179 nested_fold(append, | |
| 180 [], | |
| 181 iter_map(integers_until(5), integers_until))); | |
| 182 // Result objects with sparse fields. | |
| 183 assertEquals([undefined, 1, 2], | |
| 184 fold(append, [], | |
| 185 results([{ done: false }, | |
| 186 { value: 1, done: false }, | |
| 187 // A missing "done" is the same as undefined, which | |
| 188 // is false. | |
| 189 { value: 2 }, | |
| 190 { done: true }]))); | |
| 191 // Results that are not objects. | |
| 192 assertEquals([undefined, undefined, undefined], | |
| 193 fold(append, [], | |
| 194 results([10, "foo", /qux/, { value: 37, done: true }]))); | |
| 195 // Getters (shudder). | |
| 196 assertEquals([1, 2], | |
| 197 fold(append, [], | |
| 198 results([one_time_getter({ value: 1 }, 'done', false), | |
| 199 one_time_getter({ done: false }, 'value', 2), | |
| 200 { value: 37, done: true }, | |
| 201 never_getter(never_getter({}, 'done'), 'value')]))); | |
| 202 | |
| 203 // Null and undefined do not cause an error. | |
| 204 assertEquals(0, fold(sum, 0, unreachable(null))); | |
| 205 assertEquals(0, fold(sum, 0, unreachable(undefined))); | |
| 206 | |
| 207 // Other non-iterators do cause an error. | |
| 208 assertThrows('fold(sum, 0, unreachable({}))', TypeError); | |
| 209 assertThrows('fold(sum, 0, unreachable("foo"))', TypeError); | |
| 210 assertThrows('fold(sum, 0, unreachable(37))', TypeError); | |
| 211 | |
| 212 // "next" is looked up each time. | |
| 213 assertThrows('fold(sum, 0, remove_next_after(integers_until(10), 5))', | |
| 214 TypeError); | |
| 215 // It is not called at any other time. | |
| 216 assertEquals(45, | |
| 217 fold(sum, 0, remove_next_after(integers_until(10), 10))); | |
| 218 // It is not looked up too many times. | |
| 219 assertEquals(45, | |
| 220 fold(sum, 0, poison_next_after(integers_until(10), 10))); | |
| 221 | |
| 222 function labelled_continue(iter) { | |
| 223 var n = 0; | |
| 224 outer: | |
| 225 while (true) { | |
| 226 n++; | |
| 227 for (var x of iter) continue outer; | |
| 228 break; | |
| 229 } | |
| 230 return n; | |
| 231 } | |
| 232 assertEquals(11, labelled_continue(integers_until(10))); | |
| 233 | |
| 234 function labelled_break(iter) { | |
| 235 var n = 0; | |
| 236 outer: | |
| 237 while (true) { | |
| 238 n++; | |
| 239 for (var x of iter) break outer; | |
| 240 } | |
| 241 return n; | |
| 242 } | |
| 243 assertEquals(1, labelled_break(integers_until(10))); | |
| 244 | |
| 245 // Test continue/break in catch. | |
| 246 function catch_control(iter, k) { | |
| 247 var n = 0; | |
| 248 for (var x of iter) { | |
| 249 try { | |
| 250 return k(x); | |
| 251 } catch (e) { | |
| 252 if (e == "continue") continue; | |
| 253 else if (e == "break") break; | |
| 254 else throw e; | |
| 255 } | |
| 256 } while (false); | |
| 257 return false; | |
| 258 } | |
| 259 assertEquals(false, | |
| 260 catch_control(integers_until(10), | |
| 261 function() { throw "break" })); | |
| 262 assertEquals(false, | |
| 263 catch_control(integers_until(10), | |
| 264 function() { throw "continue" })); | |
| 265 assertEquals(5, | |
| 266 catch_control(integers_until(10), | |
| 267 function(x) { | |
| 268 if (x == 5) return x; | |
| 269 throw "continue"; | |
| 270 })); | |
| 271 | |
| 272 // Test continue/break in try. | |
| 273 function try_control(iter, k) { | |
| 274 var n = 0; | |
| 275 for (var x of iter) { | |
| 276 try { | |
| 277 var e = k(x); | |
| 278 if (e == "continue") continue; | |
| 279 else if (e == "break") break; | |
| 280 return e; | |
| 281 } catch (e) { | |
| 282 throw e; | |
| 283 } | |
| 284 } while (false); | |
| 285 return false; | |
| 286 } | |
| 287 assertEquals(false, | |
| 288 try_control(integers_until(10), | |
| 289 function() { return "break" })); | |
| 290 assertEquals(false, | |
| 291 try_control(integers_until(10), | |
| 292 function() { return "continue" })); | |
| 293 assertEquals(5, | |
| 294 try_control(integers_until(10), | |
| 295 function(x) { return (x == 5) ? x : "continue" })); | |
| 296 | |
| 297 // Proxies and getters, together at last. | |
|
rossberg
2013/06/07 09:29:52
Thanks. :) Sorry to pester, but can we also have a
wingo
2013/06/07 10:07:15
Done. However it doesn't work for generators, as
rossberg
2013/06/07 10:43:52
Yeah, you just hit the core of the problem re prox
| |
| 298 function transparent_proxy(x) { | |
| 299 return Proxy.create({ | |
| 300 get: function(receiver, name) { return x[name]; } | |
| 301 }); | |
| 302 } | |
| 303 assertEquals([1, 2], | |
| 304 fold(append, [], | |
| 305 results([one_time_getter({ value: 1 }, 'done', false), | |
| 306 one_time_getter({ done: false }, 'value', 2), | |
| 307 { value: 37, done: true }, | |
| 308 never_getter(never_getter({}, 'done'), 'value')] | |
| 309 .map(transparent_proxy)))); | |
| OLD | NEW |