OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // IsolateStubs=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse | 5 // IsolateStubs=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse |
6 | 6 |
7 #import("../../isolate/src/TestFramework.dart"); | 7 #import("../../isolate/src/TestFramework.dart"); |
8 | 8 |
9 /* class = Purse (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintM
akerFullyIsolatedTest.dart: 9) */ | 9 /* class = Purse (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintM
akerFullyIsolatedTest.dart: 9) */ |
10 | 10 |
11 interface Purse$Proxy { | 11 interface Purse$Proxy extends Proxy { |
12 Promise<int> queryBalance(); | 12 Promise<int> queryBalance(); |
13 | 13 |
14 Purse$Proxy sproutPurse(); | 14 Purse$Proxy sproutPurse(); |
15 | 15 |
16 Promise<int> deposit(int amount, Purse$Proxy source); | 16 Promise<int> deposit(int amount, Purse$Proxy source); |
17 } | 17 } |
18 | 18 |
19 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { | 19 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { |
20 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | 20 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
21 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } | 21 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } |
(...skipping 27 matching lines...) Expand all Loading... |
49 } else if (command == "queryBalance") { | 49 } else if (command == "queryBalance") { |
50 int queryBalance = target.queryBalance(); | 50 int queryBalance = target.queryBalance(); |
51 reply(queryBalance); | 51 reply(queryBalance); |
52 } else if (command == "sproutPurse") { | 52 } else if (command == "sproutPurse") { |
53 Purse$Proxy sproutPurse = target.sproutPurse(); | 53 Purse$Proxy sproutPurse = target.sproutPurse(); |
54 reply(sproutPurse); | 54 reply(sproutPurse); |
55 } else if (command == "deposit") { | 55 } else if (command == "deposit") { |
56 int amount = message[1]; | 56 int amount = message[1]; |
57 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | 57 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); |
58 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); | 58 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); |
59 Promise done = new Promise(); | 59 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); |
60 done.waitFor(promises, 1); | 60 Promise<int> deposit = target.deposit(amount, source); |
61 done.addCompleteHandler((_) { | 61 reply(deposit); |
62 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); | |
63 Promise<int> deposit = target.deposit(amount, source); | |
64 reply(deposit); | |
65 }); | |
66 } else { | 62 } else { |
67 // TODO(kasperl,benl): Somehow throw an exception instead. | 63 // TODO(kasperl,benl): Somehow throw an exception instead. |
68 reply("Exception: command '" + command + "' not understood by Purse."); | 64 reply("Exception: command '" + command + "' not understood by Purse."); |
69 } | 65 } |
70 } | 66 } |
71 } | 67 } |
72 | 68 |
73 class Purse$Dispatcher$Isolate extends Isolate { | 69 class Purse$Dispatcher$Isolate extends Isolate { |
74 Purse$Dispatcher$Isolate() : super() { } | 70 Purse$Dispatcher$Isolate() : super() { } |
75 | 71 |
76 void main() { | 72 void main() { |
77 this.port.receive(void _(var message, SendPort replyTo) { | 73 this.port.receive(void _(var message, SendPort replyTo) { |
78 Purse thing = new Purse(); | 74 Purse thing = new Purse(); |
79 SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing)); | 75 SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing)); |
80 Proxy proxy = new Proxy.forPort(replyTo); | 76 Proxy proxy = new Proxy.forPort(replyTo); |
81 proxy.send([port]); | 77 proxy.send([port]); |
82 }); | 78 }); |
83 } | 79 } |
84 } | 80 } |
85 | 81 |
86 /* class = PowerfulPurse (tests/stub-generator/src/MintMakerFullyIsolatedTest.da
rt/MintMakerFullyIsolatedTest.dart: 18) */ | 82 /* class = PowerfulPurse (tests/stub-generator/src/MintMakerFullyIsolatedTest.da
rt/MintMakerFullyIsolatedTest.dart: 18) */ |
87 | 83 |
88 interface PowerfulPurse$Proxy { | 84 interface PowerfulPurse$Proxy extends Proxy { |
89 void init(Mint$Proxy mint, int balance); | 85 void init(Mint$Proxy mint, int balance); |
90 | 86 |
91 Promise<int> grab(int amount); | 87 Promise<int> grab(int amount); |
92 | 88 |
93 Purse$Proxy weak(); | 89 Purse$Proxy weak(); |
94 } | 90 } |
95 | 91 |
96 class PowerfulPurse$ProxyImpl extends ProxyImpl implements PowerfulPurse$Proxy { | 92 class PowerfulPurse$ProxyImpl extends ProxyImpl implements PowerfulPurse$Proxy { |
97 PowerfulPurse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | 93 PowerfulPurse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
98 PowerfulPurse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.cal
l([null])) { } | 94 PowerfulPurse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.cal
l([null])) { } |
(...skipping 21 matching lines...) Expand all Loading... |
120 class PowerfulPurse$Dispatcher extends Dispatcher<PowerfulPurse> { | 116 class PowerfulPurse$Dispatcher extends Dispatcher<PowerfulPurse> { |
121 PowerfulPurse$Dispatcher(PowerfulPurse thing) : super(thing) { } | 117 PowerfulPurse$Dispatcher(PowerfulPurse thing) : super(thing) { } |
122 | 118 |
123 void process(var message, void reply(var response)) { | 119 void process(var message, void reply(var response)) { |
124 String command = message[0]; | 120 String command = message[0]; |
125 if (command == "PowerfulPurse") { | 121 if (command == "PowerfulPurse") { |
126 } else if (command == "init") { | 122 } else if (command == "init") { |
127 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | 123 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); |
128 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); | 124 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); |
129 int balance = message[2]; | 125 int balance = message[2]; |
130 Promise done = new Promise(); | 126 Mint$Proxy mint = new Mint$ProxyImpl(promises[0]); |
131 done.waitFor(promises, 1); | 127 target.init(mint, balance); |
132 done.addCompleteHandler((_) { | |
133 Mint$Proxy mint = new Mint$ProxyImpl(promises[0]); | |
134 target.init(mint, balance); | |
135 }); | |
136 } else if (command == "grab") { | 128 } else if (command == "grab") { |
137 int amount = message[1]; | 129 int amount = message[1]; |
138 int grab = target.grab(amount); | 130 int grab = target.grab(amount); |
139 reply(grab); | 131 reply(grab); |
140 } else if (command == "weak") { | 132 } else if (command == "weak") { |
141 Purse weak = target.weak(); | 133 Purse weak = target.weak(); |
142 SendPort port = Dispatcher.serve(new Purse$Dispatcher(weak)); | 134 SendPort port = Dispatcher.serve(new Purse$Dispatcher(weak)); |
143 reply(port); | 135 reply(port); |
144 } else { | 136 } else { |
145 // TODO(kasperl,benl): Somehow throw an exception instead. | 137 // TODO(kasperl,benl): Somehow throw an exception instead. |
(...skipping 10 matching lines...) Expand all Loading... |
156 PowerfulPurse thing = new PowerfulPurse(); | 148 PowerfulPurse thing = new PowerfulPurse(); |
157 SendPort port = Dispatcher.serve(new PowerfulPurse$Dispatcher(thing)); | 149 SendPort port = Dispatcher.serve(new PowerfulPurse$Dispatcher(thing)); |
158 Proxy proxy = new Proxy.forPort(replyTo); | 150 Proxy proxy = new Proxy.forPort(replyTo); |
159 proxy.send([port]); | 151 proxy.send([port]); |
160 }); | 152 }); |
161 } | 153 } |
162 } | 154 } |
163 | 155 |
164 /* class = Mint (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMa
kerFullyIsolatedTest.dart: 28) */ | 156 /* class = Mint (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMa
kerFullyIsolatedTest.dart: 28) */ |
165 | 157 |
166 interface Mint$Proxy { | 158 interface Mint$Proxy extends Proxy { |
167 Purse$Proxy createPurse(int balance); | 159 Purse$Proxy createPurse(int balance); |
168 | 160 |
169 PowerfulPurse$Proxy promote(Purse$Proxy purse); | 161 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse); |
170 } | 162 } |
171 | 163 |
172 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { | 164 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { |
173 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | 165 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
174 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } | 166 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } |
175 factory Mint$ProxyImpl.createIsolate() { | 167 factory Mint$ProxyImpl.createIsolate() { |
176 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); | 168 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); |
177 return new Mint$ProxyImpl.forIsolate(isolate); | 169 return new Mint$ProxyImpl.forIsolate(isolate); |
178 } | 170 } |
179 factory Mint$ProxyImpl.localProxy(Mint obj) { | 171 factory Mint$ProxyImpl.localProxy(Mint obj) { |
180 return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(n
ew Mint$Dispatcher(obj)))); | 172 return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(n
ew Mint$Dispatcher(obj)))); |
181 } | 173 } |
182 | 174 |
183 Purse$Proxy createPurse(int balance) { | 175 Purse$Proxy createPurse(int balance) { |
184 return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["createPurs
e", balance]))); | 176 return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["createPurs
e", balance]))); |
185 } | 177 } |
186 | 178 |
187 PowerfulPurse$Proxy promote(Purse$Proxy purse) { | 179 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) { |
188 return new PowerfulPurse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["pr
omote", purse]))); | 180 return new Promise<PowerfulPurse$Proxy>.fromValue(new PowerfulPurse$ProxyImp
l(new PromiseProxy<SendPort>(new PromiseProxy<SendPort>(this.call(["promote", pu
rse]))))); |
189 } | 181 } |
190 } | 182 } |
191 | 183 |
192 class Mint$Dispatcher extends Dispatcher<Mint> { | 184 class Mint$Dispatcher extends Dispatcher<Mint> { |
193 Mint$Dispatcher(Mint thing) : super(thing) { } | 185 Mint$Dispatcher(Mint thing) : super(thing) { } |
194 | 186 |
195 void process(var message, void reply(var response)) { | 187 void process(var message, void reply(var response)) { |
196 String command = message[0]; | 188 String command = message[0]; |
197 if (command == "Mint") { | 189 if (command == "Mint") { |
198 } else if (command == "createPurse") { | 190 } else if (command == "createPurse") { |
199 int balance = message[1]; | 191 int balance = message[1]; |
200 Purse$Proxy createPurse = target.createPurse(balance); | 192 Purse$Proxy createPurse = target.createPurse(balance); |
201 reply(createPurse); | 193 reply(createPurse); |
202 } else if (command == "promote") { | 194 } else if (command == "promote") { |
203 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | 195 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); |
204 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); | 196 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); |
205 Promise done = new Promise(); | 197 Purse$Proxy purse = new Purse$ProxyImpl(promises[0]); |
206 done.waitFor(promises, 1); | 198 Promise<PowerfulPurse$Proxy> promote = target.promote(purse); |
207 done.addCompleteHandler((_) { | 199 reply(promote); |
208 Purse$Proxy purse = new Purse$ProxyImpl(promises[0]); | |
209 PowerfulPurse$Proxy promote = target.promote(purse); | |
210 reply(promote); | |
211 }); | |
212 } else { | 200 } else { |
213 // TODO(kasperl,benl): Somehow throw an exception instead. | 201 // TODO(kasperl,benl): Somehow throw an exception instead. |
214 reply("Exception: command '" + command + "' not understood by Mint."); | 202 reply("Exception: command '" + command + "' not understood by Mint."); |
215 } | 203 } |
216 } | 204 } |
217 } | 205 } |
218 | 206 |
219 class Mint$Dispatcher$Isolate extends Isolate { | 207 class Mint$Dispatcher$Isolate extends Isolate { |
220 Mint$Dispatcher$Isolate() : super() { } | 208 Mint$Dispatcher$Isolate() : super() { } |
221 | 209 |
(...skipping 22 matching lines...) Expand all Loading... |
244 // Return an int so we can wait for it to complete. Shame we can't | 232 // Return an int so we can wait for it to complete. Shame we can't |
245 // have a Promise<void>. | 233 // have a Promise<void>. |
246 int grab(int amount); | 234 int grab(int amount); |
247 Purse weak(); | 235 Purse weak(); |
248 } | 236 } |
249 | 237 |
250 interface Mint factory MintImpl { | 238 interface Mint factory MintImpl { |
251 Mint(); | 239 Mint(); |
252 | 240 |
253 Purse$Proxy createPurse(int balance); | 241 Purse$Proxy createPurse(int balance); |
254 PowerfulPurse$Proxy promote(Purse$Proxy purse); | 242 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse); |
255 } | 243 } |
256 | 244 |
257 // Because promises can't be used as keys in maps until they have | 245 // Because promises can't be used as keys in maps until they have |
258 // completed, provide a wrapper. Note that if any key promise fails to | 246 // completed, provide a wrapper. Note that if any key promise fails to |
259 // resolve, then get()'s return may also fail to resolve. Also, | 247 // resolve, then get()'s return may also fail to resolve. Right now, a |
260 // although the logic is fine, this can't be used for a | 248 // Proxy can also be used since it has (kludgily) been made to inherit |
261 // ProxyMap. Perhaps both Proxy and Promise should inherit from | 249 // from Promise. Perhaps both Proxy and Promise should inherit from |
262 // Completable? | 250 // Completable? |
263 // NB: not tested and known to be buggy. Will fix in a future change. | 251 // Note that we cannot extend Set rather than Collection because, for |
| 252 // example, Set.remove() returns bool, whereas this will have to |
| 253 // return Promise<bool>. |
| 254 class PromiseSet<T extends Promise> implements Collection<T> { |
| 255 |
| 256 PromiseSet() { |
| 257 _set = new List<T>(); |
| 258 } |
| 259 |
| 260 PromiseSet.fromList(this._set); |
| 261 |
| 262 void add(T t) { |
| 263 print("ProxySet.add"); |
| 264 for (T x in _set) { |
| 265 if (x === t) |
| 266 return; |
| 267 } |
| 268 if (t.hasValue()) { |
| 269 for (T x in _set) { |
| 270 if (x.hasValue() && x == t) |
| 271 return; |
| 272 } |
| 273 } |
| 274 _set.add(t); |
| 275 t.addCompleteHandler((_) { |
| 276 // Remove any duplicates. |
| 277 _remove(t, 1); |
| 278 }); |
| 279 } |
| 280 |
| 281 void _remove(T t, int threshold) { |
| 282 print("PromiseSet.remove $threshold"); |
| 283 int count = 0; |
| 284 for (int n = 0; n < _set.length; ++n) |
| 285 if (_set[n].hasValue() && _set[n] == t) |
| 286 if (++count > threshold) { |
| 287 print(" remove $n"); |
| 288 _set.removeRange(n, 1); |
| 289 --n; |
| 290 } |
| 291 } |
| 292 |
| 293 void remove(T t) { |
| 294 t.addCompleteHandler((_) { |
| 295 _remove(t, 0); |
| 296 }); |
| 297 } |
| 298 |
| 299 int get length() => _set.length; |
| 300 void forEach(void f(T element)) { _set.forEach(f); } |
| 301 PromiseSet<T> filter(bool f(T element)) { |
| 302 return new PromiseSet<T>.fromList(_set.filter(f)); |
| 303 } |
| 304 bool every(bool f(T element)) => _set.every(f); |
| 305 bool some(bool f(T element)) => _set.some(f); |
| 306 bool isEmpty() => _set.isEmpty(); |
| 307 Iterator<T> iterator() => _set.iterator(); |
| 308 |
| 309 List<T> _set; |
| 310 |
| 311 } |
| 312 |
| 313 |
264 class PromiseMap<S extends Promise, T> { | 314 class PromiseMap<S extends Promise, T> { |
265 | 315 |
266 PromiseMap() { | 316 PromiseMap() { |
267 _map = new Map<S, T>(); | 317 _map = new Map<S, T>(); |
268 _incomplete = new Set<S>(); | 318 _incomplete = new PromiseSet<S>(); |
269 } | 319 } |
270 | 320 |
271 T add(S s, T t) { | 321 T add(S s, T t) { |
| 322 print("PromiseMap.add"); |
272 _incomplete.add(s); | 323 _incomplete.add(s); |
273 s.addCompleteHandler((_) { | 324 s.addCompleteHandler((_) { |
| 325 print("PromiseMap.add move to map"); |
274 _map[s] = t; | 326 _map[s] = t; |
275 _incomplete.remove(s); | 327 _incomplete.remove(s); |
276 }); | 328 }); |
277 return t; | 329 return t; |
278 } | 330 } |
279 | 331 |
280 Promise<T> find(S s) { | 332 Promise<T> find(S s) { |
281 T t = _map[s]; | 333 print("PromiseMap.find"); |
282 if (t != null) | 334 Promise<T> result = new Promise<T>(); |
283 return new Promise<T>.fromValue(t); | 335 s.addCompleteHandler((_) { |
284 Promise<T> p = new Promise<T>(); | 336 print("PromiseMap.find s completed"); |
285 int counter = _incomplete.length; | 337 T t = _map[s]; |
286 p.join(_incomplete, bool (S completed) { | 338 if (t != null) { |
287 if (completed != s) { | 339 print(" immediate"); |
288 if (--counter == 0) { | 340 result.complete(t); |
289 p.complete(null); | 341 return; |
290 return true; | 342 } |
| 343 // Otherwise, we need to wait for map[s] to complete... |
| 344 int counter = _incomplete.length; |
| 345 if (counter == 0) { |
| 346 print(" none incomplete"); |
| 347 result.complete(null); |
| 348 return; |
| 349 } |
| 350 result.join(_incomplete, bool (S completed) { |
| 351 if (completed != s) { |
| 352 if (--counter == 0) { |
| 353 print("PromiseMap.find failed"); |
| 354 result.complete(null); |
| 355 return true; |
| 356 } |
| 357 print("PromiseMap.find miss"); |
| 358 return false; |
291 } | 359 } |
292 return false; | 360 print("PromiseMap.find complete"); |
293 } | 361 result.complete(_map[s]); |
294 p.complete(_map[s]); | 362 return true; |
295 return true; | 363 }); |
296 }); | 364 }); |
297 return p; | 365 return result; |
298 } | 366 } |
299 | 367 |
300 Set<S> _incomplete; | 368 PromiseSet<S> _incomplete; |
301 Map<S, T> _map; | 369 Map<S, T> _map; |
302 | 370 |
303 } | 371 } |
304 | 372 |
| 373 |
305 class MintImpl implements Mint { | 374 class MintImpl implements Mint { |
306 | 375 |
307 MintImpl() { | 376 MintImpl() { |
308 //print('mint'); | 377 print('mint'); |
309 if (_power == null) | 378 if (_power == null) |
310 _power = new Map<Purse$Proxy, PowerfulPurse$Proxy>(); | 379 _power = new PromiseMap<Purse$Proxy, PowerfulPurse$Proxy>(); |
311 } | 380 } |
312 | 381 |
313 Purse$Proxy createPurse(int balance) { | 382 Purse$Proxy createPurse(int balance) { |
314 //print('createPurse'); | 383 print('createPurse'); |
315 PowerfulPurse$ProxyImpl purse = | 384 PowerfulPurse$ProxyImpl purse = |
316 new PowerfulPurse$ProxyImpl.createIsolate(); | 385 new PowerfulPurse$ProxyImpl.createIsolate(); |
317 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); | 386 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); |
318 purse.init(thisProxy, balance); | 387 purse.init(thisProxy, balance); |
319 | 388 |
320 Purse$Proxy weakPurse = purse.weak(); | 389 Purse$Proxy weakPurse = purse.weak(); |
321 weakPurse.addCompleteHandler(() { | 390 weakPurse.addCompleteHandler((_) { |
322 //print('cP1'); | 391 print('cP1'); |
323 _power[weakPurse] = purse; | 392 _power.add(weakPurse, purse); |
324 //print('cP2'); | 393 print('cP2'); |
325 }); | 394 }); |
326 return weakPurse; | 395 return weakPurse; |
327 } | 396 } |
328 | 397 |
329 PowerfulPurse$Proxy promote(Purse$Proxy purse) { | 398 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) { |
330 // FIXME(benl): we should be using a PromiseMap here. But we get | 399 print('promote $purse'); |
331 // away with it in this test for now. | 400 return _power.find(purse); |
332 //print('promote $purse/${_power[purse]}'); | |
333 return _power[purse]; | |
334 } | 401 } |
335 | 402 |
336 static Map<Purse$Proxy, PowerfulPurse$Proxy> _power; | 403 static PromiseMap<Purse$Proxy, PowerfulPurse$Proxy> _power; |
337 } | 404 } |
338 | 405 |
339 class PurseImpl implements PowerfulPurse { | 406 class PurseImpl implements PowerfulPurse { |
340 | 407 |
341 // FIXME(benl): autogenerate constructor, get rid of init(...). | 408 // FIXME(benl): autogenerate constructor, get rid of init(...). |
342 // Note that this constructor should not exist in the public interface | 409 // Note that this constructor should not exist in the public interface |
343 // PurseImpl(this._mint, this._balance) { } | 410 // PurseImpl(this._mint, this._balance) { } |
344 PurseImpl() { } | 411 PurseImpl() { } |
345 | 412 |
346 init(Mint$Proxy mint, int balance) { | 413 init(Mint$Proxy mint, int balance) { |
347 this._mint = mint; | 414 this._mint = mint; |
348 this._balance = balance; | 415 this._balance = balance; |
349 } | 416 } |
350 | 417 |
351 int queryBalance() { | 418 int queryBalance() { |
352 return _balance; | 419 return _balance; |
353 } | 420 } |
354 | 421 |
355 Purse$Proxy sproutPurse() { | 422 Purse$Proxy sproutPurse() { |
356 //print('sprout'); | 423 print('sprout'); |
357 return _mint.createPurse(0); | 424 return _mint.createPurse(0); |
358 } | 425 } |
359 | 426 |
360 Promise<int> deposit(int amount, Purse$Proxy proxy) { | 427 Promise<int> deposit(int amount, Purse$Proxy proxy) { |
361 //print('deposit'); | 428 print('deposit'); |
362 Promise<int> grabbed = _mint.promote(proxy).grab(amount); | 429 Promise<PowerfulPurse$Proxy> powerful = _mint.promote(proxy); |
363 Promise<int> done = new Promise<int>(); | 430 |
364 grabbed.then((int) { | 431 Promise<int> result = new Promise<int>(); |
365 //print("deposit done"); | 432 powerful.then((_) { |
366 _balance += amount; | 433 Promise<int> grabbed = powerful.value.grab(amount); |
367 done.complete(_balance); | 434 grabbed.then((int grabbedAmount) { |
| 435 _balance += grabbedAmount; |
| 436 result.complete(_balance); |
| 437 }); |
368 }); | 438 }); |
369 return done; | 439 |
| 440 return result; |
370 } | 441 } |
371 | 442 |
372 int grab(int amount) { | 443 int grab(int amount) { |
373 //print("grab"); | 444 print("grab"); |
374 if (_balance < amount) throw "Not enough dough."; | 445 if (_balance < amount) throw "Not enough dough."; |
375 _balance -= amount; | 446 _balance -= amount; |
376 return amount; | 447 return amount; |
377 } | 448 } |
378 | 449 |
379 Purse weak() { | 450 Purse weak() { |
380 return this; | 451 return this; |
381 } | 452 } |
382 | 453 |
383 Mint$Proxy _mint; | 454 Mint$Proxy _mint; |
(...skipping 27 matching lines...) Expand all Loading... |
411 | 482 |
412 done = sprouted.deposit(42, purse); | 483 done = sprouted.deposit(42, purse); |
413 expect.completesWithValue(done, 5 + 42); | 484 expect.completesWithValue(done, 5 + 42); |
414 Promise<int> d2 = done.then((val) { | 485 Promise<int> d2 = done.then((val) { |
415 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) | 486 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) |
416 .then((int value) => inner.complete(0)); | 487 .then((int value) => inner.complete(0)); |
417 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) | 488 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) |
418 .then((int value) => inner2.complete(0)); | 489 .then((int value) => inner2.complete(0)); |
419 }); | 490 }); |
420 expect.completes(d2); | 491 expect.completes(d2); |
| 492 |
| 493 return 0; |
421 }); | 494 }); |
422 expect.completes(d1); | 495 expect.completesWithValue(d1, 0); |
423 Promise<int> allDone = new Promise<int>(); | 496 Promise<int> allDone = new Promise<int>(); |
424 allDone.waitFor([d3, inner, inner2], 3); | 497 allDone.waitFor([d3, inner, inner2], 3); |
425 allDone.then((_) => expect.succeeded()); | 498 allDone.then((_) { |
| 499 expect.succeeded(); |
| 500 print("##DONE##"); |
| 501 }); |
426 } | 502 } |
427 | 503 |
428 } | 504 } |
429 | 505 |
430 main() { | 506 main() { |
431 runTests([MintMakerFullyIsolatedTest.testMain]); | 507 runTests([MintMakerFullyIsolatedTest.testMain]); |
432 } | 508 } |
OLD | NEW |