| OLD | NEW |
| (Empty) | |
| 1 /* class = Mint (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Min
tMakerPromiseWithStubsTest.dart: 7) */ |
| 2 |
| 3 interface Mint$Proxy { |
| 4 Purse$Proxy createPurse(int balance); |
| 5 } |
| 6 |
| 7 class Mint$ProxyImpl extends Proxy implements Mint$Proxy { |
| 8 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
| 9 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } |
| 10 factory Mint$ProxyImpl.createIsolate() { |
| 11 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); |
| 12 return new Mint$ProxyImpl.forIsolate(isolate); |
| 13 } |
| 14 factory Mint$ProxyImpl.localProxy(Mint obj) { |
| 15 return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(n
ew Mint$Dispatcher(obj)))); |
| 16 } |
| 17 |
| 18 Purse$Proxy createPurse(int balance) { |
| 19 return new Purse$ProxyImpl(this.call(["createPurse", balance])); |
| 20 } |
| 21 } |
| 22 |
| 23 class Mint$Dispatcher extends Dispatcher<Mint> { |
| 24 Mint$Dispatcher(Mint thing) : super(thing) { } |
| 25 |
| 26 void process(var message, void reply(var response)) { |
| 27 String command = message[0]; |
| 28 if (command == "Mint") { |
| 29 } else if (command == "createPurse") { |
| 30 int balance = message[1]; |
| 31 Purse createPurse = target.createPurse(balance); |
| 32 SendPort port = Dispatcher.serve(new Purse$Dispatcher(createPurse)); |
| 33 reply(port); |
| 34 } else { |
| 35 // TODO(kasperl,benl): Somehow throw an exception instead. |
| 36 reply("Exception: command '" + command + "' not understood by Mint."); |
| 37 } |
| 38 } |
| 39 } |
| 40 |
| 41 class Mint$Dispatcher$Isolate extends Isolate { |
| 42 Mint$Dispatcher$Isolate() : super() { } |
| 43 |
| 44 void main() { |
| 45 this.port.receive(void _(var message, SendPort replyTo) { |
| 46 Mint thing = new Mint(); |
| 47 SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing)); |
| 48 Proxy proxy = new Proxy.forPort(replyTo); |
| 49 proxy.send([port]); |
| 50 }); |
| 51 } |
| 52 } |
| 53 |
| 54 /* class = Purse (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Mi
ntMakerPromiseWithStubsTest.dart: 15) */ |
| 55 |
| 56 interface Purse$Proxy { |
| 57 Promise<int> queryBalance(); |
| 58 |
| 59 Purse$Proxy sproutPurse(); |
| 60 |
| 61 Promise<int> deposit(int amount, Purse$Proxy source); |
| 62 } |
| 63 |
| 64 class Purse$ProxyImpl extends Proxy implements Purse$Proxy { |
| 65 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
| 66 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } |
| 67 factory Purse$ProxyImpl.createIsolate() { |
| 68 Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate()); |
| 69 return new Purse$ProxyImpl.forIsolate(isolate); |
| 70 } |
| 71 factory Purse$ProxyImpl.localProxy(Purse obj) { |
| 72 return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(
new Purse$Dispatcher(obj)))); |
| 73 } |
| 74 |
| 75 Promise<int> queryBalance() { |
| 76 return this.call(["queryBalance"]); |
| 77 } |
| 78 |
| 79 Purse$Proxy sproutPurse() { |
| 80 return new Purse$ProxyImpl(this.call(["sproutPurse"])); |
| 81 } |
| 82 |
| 83 Promise<int> deposit(int amount, Purse$Proxy source) { |
| 84 return this.call(["deposit", amount, source]); |
| 85 } |
| 86 } |
| 87 |
| 88 class Purse$Dispatcher extends Dispatcher<Purse> { |
| 89 Purse$Dispatcher(Purse thing) : super(thing) { } |
| 90 |
| 91 void process(var message, void reply(var response)) { |
| 92 String command = message[0]; |
| 93 if (command == "Purse") { |
| 94 } else if (command == "queryBalance") { |
| 95 int queryBalance = target.queryBalance(); |
| 96 reply(queryBalance); |
| 97 } else if (command == "sproutPurse") { |
| 98 Purse sproutPurse = target.sproutPurse(); |
| 99 SendPort port = Dispatcher.serve(new Purse$Dispatcher(sproutPurse)); |
| 100 reply(port); |
| 101 } else if (command == "deposit") { |
| 102 int amount = message[1]; |
| 103 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); |
| 104 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); |
| 105 Promise done = new Promise(); |
| 106 done.waitFor(promises, 1); |
| 107 done.addCompleteHandler((_) { |
| 108 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); |
| 109 int deposit = target.deposit(amount, source); |
| 110 reply(deposit); |
| 111 }); |
| 112 } else { |
| 113 // TODO(kasperl,benl): Somehow throw an exception instead. |
| 114 reply("Exception: command '" + command + "' not understood by Purse."); |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 class Purse$Dispatcher$Isolate extends Isolate { |
| 120 Purse$Dispatcher$Isolate() : super() { } |
| 121 |
| 122 void main() { |
| 123 this.port.receive(void _(var message, SendPort replyTo) { |
| 124 Purse thing = new Purse(); |
| 125 SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing)); |
| 126 Proxy proxy = new Proxy.forPort(replyTo); |
| 127 proxy.send([port]); |
| 128 }); |
| 129 } |
| 130 } |
| 131 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 132 // for details. All rights reserved. Use of this source code is governed by a |
| 133 // BSD-style license that can be found in the LICENSE file. |
| 134 |
| 135 // IsolateStubs=MintMakerPromiseWithStubsTest.dart:Mint,Purse |
| 136 |
| 137 interface Mint factory MintImpl { |
| 138 |
| 139 Mint(); |
| 140 |
| 141 Purse createPurse(int balance); |
| 142 |
| 143 } |
| 144 |
| 145 interface Purse factory PurseImpl { |
| 146 |
| 147 Purse(); |
| 148 |
| 149 int queryBalance(); |
| 150 Purse sproutPurse(); |
| 151 int deposit(int amount, Purse$Proxy source); |
| 152 |
| 153 } |
| 154 |
| 155 class MintImpl implements Mint { |
| 156 |
| 157 MintImpl() { } |
| 158 |
| 159 Purse createPurse(int balance) { |
| 160 PurseImpl purse = new PurseImpl(); |
| 161 purse.init(this, balance); |
| 162 |
| 163 return purse; |
| 164 } |
| 165 |
| 166 } |
| 167 |
| 168 class PurseImpl implements Purse { |
| 169 |
| 170 PurseImpl() { } |
| 171 // TODO(benl): implement stub constructors. |
| 172 // Note that this constructor should _not_ be in the Purse interface, |
| 173 // only this isolate is trusted to construct purses. |
| 174 //PurseImpl(this._mint, this._balance) { } |
| 175 void init(Mint mint, int balance) { |
| 176 this._mint = mint; |
| 177 this._balance = balance; |
| 178 } |
| 179 |
| 180 int queryBalance() { |
| 181 return _balance; |
| 182 } |
| 183 |
| 184 Purse sproutPurse() { |
| 185 return _mint.createPurse(0); |
| 186 } |
| 187 |
| 188 int deposit(int amount, Purse$Proxy proxy) { |
| 189 if (amount < 0) throw "Ha ha"; |
| 190 // Because we are in the same isolate as the other purse, we can |
| 191 // retrieve the proxy's local PurseImpl object and act on it |
| 192 // directly. Further, a forged purse will not be convertible, and |
| 193 // so an attempt to use it will fail. |
| 194 PurseImpl source = proxy.dynamic.local; |
| 195 if (source._balance < amount) throw "Not enough dough."; |
| 196 _balance += amount; |
| 197 source._balance -= amount; |
| 198 return _balance; |
| 199 } |
| 200 |
| 201 Mint _mint; |
| 202 int _balance; |
| 203 |
| 204 } |
| 205 |
| 206 class MintMakerPromiseWithStubsTest { |
| 207 |
| 208 static void testMain() { |
| 209 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); |
| 210 Purse$Proxy purse = mint.createPurse(100); |
| 211 expectEquals(100, purse.queryBalance()); |
| 212 |
| 213 Purse$Proxy sprouted = purse.sproutPurse(); |
| 214 expectEquals(0, sprouted.queryBalance()); |
| 215 |
| 216 // FIXME(benl): We should not have to manually order the calls |
| 217 // like this. |
| 218 Promise<int> result = sprouted.deposit(5, purse); |
| 219 expectEquals(5, result); |
| 220 result.addCompleteHandler((_) { |
| 221 expectEquals(0 + 5, sprouted.queryBalance()); |
| 222 expectEquals(100 - 5, purse.queryBalance()); |
| 223 |
| 224 result = sprouted.deposit(42, purse); |
| 225 expectEquals(5 + 42, result); |
| 226 result.addCompleteHandler((_) { |
| 227 expectEquals(0 + 5 + 42, sprouted.queryBalance()); |
| 228 expectEquals(100 - 5 - 42, purse.queryBalance()); |
| 229 expectDone(8); |
| 230 }); |
| 231 }); |
| 232 } |
| 233 |
| 234 static List<Promise> results; |
| 235 |
| 236 static void expectEquals(int expected, Promise<int> promise) { |
| 237 if (results === null) { |
| 238 results = new List<Promise>(); |
| 239 } |
| 240 results.add(promise.then((int actual) { |
| 241 //print("done $expected/$actual"); |
| 242 Expect.equals(expected, actual); |
| 243 })); |
| 244 } |
| 245 |
| 246 static void expectDone(int n) { |
| 247 if (results === null) { |
| 248 Expect.equals(0, n); |
| 249 print('##DONE##'); |
| 250 } else { |
| 251 Promise done = new Promise(); |
| 252 done.waitFor(results, results.length); |
| 253 done.then((ignored) { |
| 254 Expect.equals(n, results.length); |
| 255 print('##DONE##'); |
| 256 }); |
| 257 } |
| 258 } |
| 259 |
| 260 } |
| 261 |
| 262 main() { |
| 263 MintMakerPromiseWithStubsTest.testMain(); |
| 264 } |
| OLD | NEW |