| 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 class Fields { | 5 class Fields { |
| 6 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} | 6 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} |
| 7 int fld1; | 7 int fld1; |
| 8 final int fld2; | 8 final int fld2; |
| 9 static int fld3; | 9 static int fld3; |
| 10 static final int fld4 = 10; | 10 static final int fld4 = 10; |
| (...skipping 1243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1254 } | 1254 } |
| 1255 Expect.equals(true, encounteredException); | 1255 Expect.equals(true, encounteredException); |
| 1256 | 1256 |
| 1257 this.port.receive((ignored, replyTo) { | 1257 this.port.receive((ignored, replyTo) { |
| 1258 replyTo.send("foo", null); | 1258 replyTo.send("foo", null); |
| 1259 this.port.close(); | 1259 this.port.close(); |
| 1260 }); | 1260 }); |
| 1261 } | 1261 } |
| 1262 } | 1262 } |
| 1263 | 1263 |
| 1264 interface Mint default MintImpl { | |
| 1265 | |
| 1266 Mint(); | |
| 1267 | |
| 1268 Purse createPurse(int balance); | |
| 1269 | |
| 1270 } | |
| 1271 | |
| 1272 | |
| 1273 class MintImpl implements Mint { | |
| 1274 | |
| 1275 MintImpl() { } | |
| 1276 | |
| 1277 Purse createPurse(int balance) { | |
| 1278 return new PurseImpl(this, balance); | |
| 1279 } | |
| 1280 | |
| 1281 } | |
| 1282 | |
| 1283 | |
| 1284 interface Purse { | |
| 1285 | |
| 1286 int queryBalance(); | |
| 1287 Purse sproutPurse(); | |
| 1288 void deposit(int amount, Purse$Proxy source); | |
| 1289 | |
| 1290 } | |
| 1291 | |
| 1292 | |
| 1293 class PurseImpl implements Purse { | |
| 1294 | |
| 1295 PurseImpl(this._mint, this._balance) { } | |
| 1296 | |
| 1297 int queryBalance() { | |
| 1298 return _balance; | |
| 1299 } | |
| 1300 | |
| 1301 Purse sproutPurse() { | |
| 1302 return _mint.createPurse(0); | |
| 1303 } | |
| 1304 | |
| 1305 void deposit(int amount, Purse$Proxy purse) { | |
| 1306 Purse$ProxyImpl impl = purse.dynamic; | |
| 1307 PurseImpl source = impl.local; | |
| 1308 if (source._balance < amount) throw "Not enough dough."; | |
| 1309 _balance += amount; | |
| 1310 source._balance -= amount; | |
| 1311 } | |
| 1312 | |
| 1313 Mint _mint; | |
| 1314 int _balance; | |
| 1315 | |
| 1316 } | |
| 1317 | |
| 1318 | |
| 1319 class MintMakerPromiseTest { | |
| 1320 | |
| 1321 static void testMain() { | |
| 1322 Mint$Proxy mint = createMint(); | |
| 1323 Purse$Proxy purse = mint.createPurse(100); | |
| 1324 expectEquals(100, purse.queryBalance()); | |
| 1325 | |
| 1326 Purse$Proxy sprouted = purse.sproutPurse(); | |
| 1327 expectEquals(0, sprouted.queryBalance()); | |
| 1328 | |
| 1329 sprouted.deposit(5, purse); | |
| 1330 expectEquals(0 + 5, sprouted.queryBalance()); | |
| 1331 expectEquals(100 - 5, purse.queryBalance()); | |
| 1332 | |
| 1333 sprouted.deposit(42, purse); | |
| 1334 expectEquals(0 + 5 + 42, sprouted.queryBalance()); | |
| 1335 expectEquals(100 - 5 - 42, purse.queryBalance()); | |
| 1336 | |
| 1337 expectDone(6); | |
| 1338 } | |
| 1339 | |
| 1340 static Mint$Proxy createMint() { | |
| 1341 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); | |
| 1342 return new Mint$ProxyImpl(isolate); | |
| 1343 } | |
| 1344 | |
| 1345 | |
| 1346 static List<Promise> results; | |
| 1347 | |
| 1348 static void expectEquals(int expected, Promise<int> promise) { | |
| 1349 if (results === null) { | |
| 1350 results = new List<Promise>(); | |
| 1351 } | |
| 1352 results.add(promise.then((int actual) { | |
| 1353 Expect.equals(expected, actual); | |
| 1354 })); | |
| 1355 } | |
| 1356 | |
| 1357 static void expectDone(int n) { | |
| 1358 if (results === null) { | |
| 1359 Expect.equals(0, n); | |
| 1360 } else { | |
| 1361 Promise done = new Promise(); | |
| 1362 done.waitFor(results, results.length); | |
| 1363 done.then((ignored) { | |
| 1364 Expect.equals(n, results.length); | |
| 1365 }); | |
| 1366 } | |
| 1367 } | |
| 1368 | |
| 1369 } | |
| 1370 | |
| 1371 | 1264 |
| 1372 // --------------------------------------------------------------------------- | 1265 // --------------------------------------------------------------------------- |
| 1373 // THE REST OF THIS FILE COULD BE AUTOGENERATED | 1266 // THE REST OF THIS FILE COULD BE AUTOGENERATED |
| 1374 // --------------------------------------------------------------------------- | 1267 // --------------------------------------------------------------------------- |
| 1375 | 1268 |
| 1376 interface Mint$Proxy { | |
| 1377 | |
| 1378 Purse$Proxy createPurse(int balance); // Promise<int> balance. | |
| 1379 | |
| 1380 } | |
| 1381 | |
| 1382 | |
| 1383 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { | |
| 1384 | |
| 1385 Mint$ProxyImpl(Proxy isolate) : super.forReply(isolate.call([null])) {} | |
| 1386 | |
| 1387 Purse$Proxy createPurse(int balance) { | |
| 1388 return new Purse$ProxyImpl(this.call([balance])); | |
| 1389 } | |
| 1390 | |
| 1391 } | |
| 1392 | |
| 1393 | |
| 1394 class Mint$Dispatcher extends Dispatcher<Mint> { | |
| 1395 | |
| 1396 Mint$Dispatcher(Mint mint) : super(mint) { } | |
| 1397 | |
| 1398 void process(var message, void reply(var response)) { | |
| 1399 int balance = message[0]; | |
| 1400 Purse purse = target.createPurse(balance); | |
| 1401 SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse)); | |
| 1402 reply(port); | |
| 1403 } | |
| 1404 | |
| 1405 } | |
| 1406 | |
| 1407 | |
| 1408 class Mint$Dispatcher$Isolate extends Isolate { | |
| 1409 | |
| 1410 Mint$Dispatcher$Isolate() : super() { } | |
| 1411 | |
| 1412 void main() { | |
| 1413 this.port.receive((var message, SendPort replyTo) { | |
| 1414 Mint mint = new Mint(); | |
| 1415 SendPort port = Dispatcher.serve(new Mint$Dispatcher(mint)); | |
| 1416 Proxy proxy = new Proxy.forPort(replyTo); | |
| 1417 proxy.send([port]); | |
| 1418 }); | |
| 1419 } | |
| 1420 | |
| 1421 } | |
| 1422 | |
| 1423 | |
| 1424 interface Purse$Proxy { | |
| 1425 | |
| 1426 Promise<int> queryBalance(); | |
| 1427 Purse$Proxy sproutPurse(); | |
| 1428 void deposit(int amount, Purse$Proxy source); // Promise<int> amount. | |
| 1429 | |
| 1430 } | |
| 1431 | |
| 1432 | |
| 1433 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { | |
| 1434 | |
| 1435 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 1436 | |
| 1437 Promise<int> queryBalance() { | |
| 1438 return this.call(["balance"]); | |
| 1439 } | |
| 1440 | |
| 1441 void deposit(int amount, Purse$Proxy source) { | |
| 1442 this.send(["deposit", amount, source]); | |
| 1443 } | |
| 1444 | |
| 1445 Purse$Proxy sproutPurse() { | |
| 1446 return new Purse$ProxyImpl(this.call(["sprout"])); | |
| 1447 } | |
| 1448 | |
| 1449 } | |
| 1450 | |
| 1451 | |
| 1452 class Purse$Dispatcher extends Dispatcher<Purse> { | |
| 1453 | |
| 1454 Purse$Dispatcher(Purse purse) : super(purse) { } | |
| 1455 | |
| 1456 void process(var message, void reply(var response)) { | |
| 1457 String command = message[0]; | |
| 1458 if (command == "balance") { | |
| 1459 int balance = target.queryBalance(); | |
| 1460 reply(balance); | |
| 1461 } else if (command == "deposit") { | |
| 1462 int amount = message[1]; | |
| 1463 Promise<SendPort> port = new Promise<SendPort>.fromValue(message[2]); | |
| 1464 Purse$Proxy source = new Purse$ProxyImpl(port); | |
| 1465 target.deposit(amount, source); | |
| 1466 } else if (command == "sprout") { | |
| 1467 Purse purse = target.sproutPurse(); | |
| 1468 SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse)); | |
| 1469 reply(port); | |
| 1470 } else { | |
| 1471 // TODO: Send an exception back. | |
| 1472 reply("Exception: Command not understood"); | |
| 1473 } | |
| 1474 } | |
| 1475 | |
| 1476 } | |
| 1477 | |
| 1478 | 1269 |
| 1479 // ImportOptions=IsolateTestFramework | 1270 // ImportOptions=IsolateTestFramework |
| 1480 | 1271 |
| 1481 class SpawnTest { | 1272 class SpawnTest { |
| 1482 | 1273 |
| 1483 static void testMain() { | 1274 static void testMain() { |
| 1484 spawnIsolate(); | 1275 spawnIsolate(); |
| 1485 } | 1276 } |
| 1486 | 1277 |
| 1487 static void spawnIsolate() { | 1278 static void spawnIsolate() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1558 // simple messages. | 1349 // simple messages. |
| 1559 // ImportOptions=IsolateTestFramework | 1350 // ImportOptions=IsolateTestFramework |
| 1560 | 1351 |
| 1561 class IsolateTest { | 1352 class IsolateTest { |
| 1562 | 1353 |
| 1563 static void testMain() { | 1354 static void testMain() { |
| 1564 _waitingForDoneCount = 0; | 1355 _waitingForDoneCount = 0; |
| 1565 IsolateTestFramework.waitForDone(); | 1356 IsolateTestFramework.waitForDone(); |
| 1566 RequestReplyTest.test(); | 1357 RequestReplyTest.test(); |
| 1567 CountTest.test(); | 1358 CountTest.test(); |
| 1568 PromiseBasedTest.test(); | |
| 1569 StaticStateTest.test(); | 1359 StaticStateTest.test(); |
| 1570 } | 1360 } |
| 1571 | 1361 |
| 1572 static void waitForDone() { | 1362 static void waitForDone() { |
| 1573 _waitingForDoneCount++; | 1363 _waitingForDoneCount++; |
| 1574 } | 1364 } |
| 1575 | 1365 |
| 1576 static void done() { | 1366 static void done() { |
| 1577 if (--_waitingForDoneCount == 0) { | 1367 if (--_waitingForDoneCount == 0) { |
| 1578 IsolateTestFramework.done(); | 1368 IsolateTestFramework.done(); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1709 } | 1499 } |
| 1710 | 1500 |
| 1711 Expect.equals(count, message); | 1501 Expect.equals(count, message); |
| 1712 count++; | 1502 count++; |
| 1713 replyTo.send(message * 2, null); | 1503 replyTo.send(message * 2, null); |
| 1714 }); | 1504 }); |
| 1715 } | 1505 } |
| 1716 } | 1506 } |
| 1717 | 1507 |
| 1718 | 1508 |
| 1719 // --------------------------------------------------------------------------- | |
| 1720 // Promise-based test. | |
| 1721 // --------------------------------------------------------------------------- | |
| 1722 | |
| 1723 class PromiseBasedTest { | |
| 1724 | |
| 1725 static void test() { | |
| 1726 IsolateTest.waitForDone(); | |
| 1727 Proxy proxy = new Proxy.forIsolate(new PromiseIsolate()); | |
| 1728 proxy.send([42]); // Seed the isolate. | |
| 1729 proxy.call([87]).then((int value) { | |
| 1730 Expect.equals(42 + 87, value); | |
| 1731 return 99; | |
| 1732 }).then((int value) { | |
| 1733 Expect.equals(99, value); | |
| 1734 IsolateTest.done(); | |
| 1735 }); | |
| 1736 } | |
| 1737 | |
| 1738 } | |
| 1739 | |
| 1740 | |
| 1741 class PromiseIsolate extends Isolate { | |
| 1742 | |
| 1743 PromiseIsolate() : super() { } | |
| 1744 | |
| 1745 void main() { | |
| 1746 int seed = 0; | |
| 1747 this.port.receive((var message, SendPort replyTo) { | |
| 1748 if (seed == 0) { | |
| 1749 seed = message[0]; | |
| 1750 } else { | |
| 1751 Promise<int> response = new Promise<int>(); | |
| 1752 var proxy = new Proxy.forPort(replyTo); | |
| 1753 proxy.send([response]); | |
| 1754 response.complete(seed + message[0]); | |
| 1755 this.port.close(); | |
| 1756 } | |
| 1757 }); | |
| 1758 } | |
| 1759 | |
| 1760 } | |
| 1761 | |
| 1762 | 1509 |
| 1763 // --------------------------------------------------------------------------- | 1510 // --------------------------------------------------------------------------- |
| 1764 // State state test. | 1511 // State state test. |
| 1765 // --------------------------------------------------------------------------- | 1512 // --------------------------------------------------------------------------- |
| 1766 | 1513 |
| 1767 class StaticStateTest { | 1514 class StaticStateTest { |
| 1768 | 1515 |
| 1769 static String state; | 1516 static String state; |
| 1770 | 1517 |
| 1771 static void test() { | 1518 static void test() { |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2118 } | 1865 } |
| 2119 | 1866 |
| 2120 void shutdown() { | 1867 void shutdown() { |
| 2121 _out.then((SendPort p) { | 1868 _out.then((SendPort p) { |
| 2122 p.send(MandelIsolateTest.TERMINATION_MESSAGE, null); | 1869 p.send(MandelIsolateTest.TERMINATION_MESSAGE, null); |
| 2123 }); | 1870 }); |
| 2124 } | 1871 } |
| 2125 | 1872 |
| 2126 MandelbrotState _state; | 1873 MandelbrotState _state; |
| 2127 int _id; | 1874 int _id; |
| 2128 Promise<SendPort> _out; | 1875 Future<SendPort> _out; |
| 2129 | 1876 |
| 2130 } | 1877 } |
| 2131 | 1878 |
| 2132 | 1879 |
| 2133 class LineProcessor extends Isolate { | 1880 class LineProcessor extends Isolate { |
| 2134 | 1881 |
| 2135 LineProcessor() : super() { } | 1882 LineProcessor() : super() { } |
| 2136 | 1883 |
| 2137 void main() { | 1884 void main() { |
| 2138 this.port.receive((message, SendPort replyTo) { | 1885 this.port.receive((message, SendPort replyTo) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2189 }); | 1936 }); |
| 2190 }); | 1937 }); |
| 2191 } | 1938 } |
| 2192 | 1939 |
| 2193 void main() { | 1940 void main() { |
| 2194 this.port.receive((ignored, replyTo) { | 1941 this.port.receive((ignored, replyTo) { |
| 2195 replyTo.send("foo", null); | 1942 replyTo.send("foo", null); |
| 2196 }); | 1943 }); |
| 2197 } | 1944 } |
| 2198 } | 1945 } |
| OLD | NEW |