| 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 package com.google.dart.compiler.backend.isolate; | 5 package com.google.dart.compiler.backend.isolate; |
| 6 | 6 |
| 7 import java.io.FileNotFoundException; | 7 import java.io.FileNotFoundException; |
| 8 import java.io.IOException; | 8 import java.io.IOException; |
| 9 import java.io.PrintStream; | 9 import java.io.PrintStream; |
| 10 import java.util.Collection; | 10 import java.util.Collection; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return true; | 92 return true; |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 | 95 |
| 96 private static boolean isPromise(DartTypeNode x) { | 96 private static boolean isPromise(DartTypeNode x) { |
| 97 if (!(x.getIdentifier() instanceof DartIdentifier)) | 97 if (!(x.getIdentifier() instanceof DartIdentifier)) |
| 98 return false; | 98 return false; |
| 99 String name = ((DartIdentifier)x.getIdentifier()).getTargetName(); | 99 String name = ((DartIdentifier)x.getIdentifier()).getTargetName(); |
| 100 return name.equals("Promise"); | 100 return name.equals("Promise"); |
| 101 } | 101 } |
| 102 |
| 103 private String getPromiseType(DartTypeNode x) { |
| 104 assert isPromise(x); |
| 105 List<DartTypeNode> types = x.getTypeArguments(); |
| 106 assert types.size() == 1; |
| 107 return ((DartIdentifier)types.get(0).getIdentifier()).getTargetName(); |
| 108 } |
| 109 |
| 110 private static boolean isPromiseForProxy(DartTypeNode x) { |
| 111 if (!isPromise(x)) |
| 112 return false; |
| 113 List<DartTypeNode> types = x.getTypeArguments(); |
| 114 if (types.size() != 1) |
| 115 return false; |
| 116 String name = ((DartIdentifier)types.get(0).getIdentifier()).getTargetName()
; |
| 117 return name.endsWith("$Proxy"); |
| 118 } |
| 102 | 119 |
| 103 private static boolean isVoid(DartTypeNode x) { | 120 private static boolean isVoid(DartTypeNode x) { |
| 104 if (!isSimpleType(x)) | 121 if (!isSimpleType(x)) |
| 105 return false; | 122 return false; |
| 106 return ((DartIdentifier)x.getIdentifier()).getTargetName().equals("void"); | 123 return ((DartIdentifier)x.getIdentifier()).getTargetName().equals("void"); |
| 107 } | 124 } |
| 108 | 125 |
| 109 private static boolean isProxyType(DartTypeNode x) { | 126 private static boolean isProxyType(DartTypeNode x) { |
| 110 if (!(x.getIdentifier() instanceof DartIdentifier)) | 127 if (!(x.getIdentifier() instanceof DartIdentifier)) |
| 111 return false; | 128 return false; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 * return new Purse$ProxyImpl(this.call(["sproutPurse"])); | 281 * return new Purse$ProxyImpl(this.call(["sproutPurse"])); |
| 265 * } | 282 * } |
| 266 * | 283 * |
| 267 * void deposit(int amount, Purse$Proxy source) { | 284 * void deposit(int amount, Purse$Proxy source) { |
| 268 * this.send(["deposit", amount, source]); | 285 * this.send(["deposit", amount, source]); |
| 269 * } | 286 * } |
| 270 * } | 287 * } |
| 271 */ | 288 */ |
| 272 private void generateProxyClass(DartClass clazz) { | 289 private void generateProxyClass(DartClass clazz) { |
| 273 String name = clazz.getClassName(); | 290 String name = clazz.getClassName(); |
| 274 p("interface " + name + "$Proxy {"); | 291 p("interface " + name + "$Proxy extends Proxy {"); |
| 275 printProxyInterfaceFunctions(clazz); | 292 printProxyInterfaceFunctions(clazz); |
| 276 p("}"); | 293 p("}"); |
| 277 nl(); | 294 nl(); |
| 278 nl(); | 295 nl(); |
| 279 | 296 |
| 280 p("class " + name + "$ProxyImpl extends ProxyImpl implements " + name + "$Pr
oxy {"); | 297 p("class " + name + "$ProxyImpl extends ProxyImpl implements " + name + "$Pr
oxy {"); |
| 281 nl(); | 298 nl(); |
| 282 p(" " + name + "$ProxyImpl(Promise<SendPort> port) : super.forReply(port) {
}"); | 299 p(" " + name + "$ProxyImpl(Promise<SendPort> port) : super.forReply(port) {
}"); |
| 283 nl(); | 300 nl(); |
| 284 p(" " + name | 301 p(" " + name |
| (...skipping 26 matching lines...) Expand all Loading... |
| 311 return false; | 328 return false; |
| 312 p(" {"); | 329 p(" {"); |
| 313 nl(); | 330 nl(); |
| 314 p(" "); | 331 p(" "); |
| 315 final DartFunction func = x.getFunction(); | 332 final DartFunction func = x.getFunction(); |
| 316 final DartTypeNode returnTypeNode = func.getReturnTypeNode(); | 333 final DartTypeNode returnTypeNode = func.getReturnTypeNode(); |
| 317 final boolean isVoid = isVoid(returnTypeNode); | 334 final boolean isVoid = isVoid(returnTypeNode); |
| 318 final boolean isSimple = isSimpleType(returnTypeNode); | 335 final boolean isSimple = isSimpleType(returnTypeNode); |
| 319 final boolean isProxy = isProxyType(returnTypeNode); | 336 final boolean isProxy = isProxyType(returnTypeNode); |
| 320 final boolean isPromise = isPromise(returnTypeNode); | 337 final boolean isPromise = isPromise(returnTypeNode); |
| 321 if (!isVoid) { | 338 final boolean isPromiseForProxy = isPromiseForProxy(returnTypeNode); |
| 322 p("return "); | 339 if (isPromiseForProxy) { |
| 323 if (!isSimple) { | 340 String type = getPromiseType(returnTypeNode); |
| 324 p("new "); | 341 // This horrific unpacking is because a Proxy is, in effect, a Promise
, but not quite, so |
| 325 accept(returnTypeNode); | 342 // a Promise<Proxy> ends up begin wrapped in two layers of SendPorts. |
| 326 if (!isProxy) { | 343 // FIXME(benl): unifying Promise and Proxy under Completable might wel
l reduce the |
| 327 p("$Proxy"); | 344 // complexity here. |
| 345 p("return new Promise<" + type + ">.fromValue(new " + type |
| 346 + "Impl(new PromiseProxy<SendPort>(new PromiseProxy<SendPort>("); |
| 347 } else { |
| 348 if (!isVoid) { |
| 349 p("return "); |
| 350 if (!isSimple) { |
| 351 p("new "); |
| 352 accept(returnTypeNode); |
| 353 if (!isProxy) { |
| 354 p("$Proxy"); |
| 355 } |
| 356 p("Impl("); |
| 328 } | 357 } |
| 329 p("Impl("); | |
| 330 } | 358 } |
| 331 } | 359 if (isProxy) { |
| 332 if (isProxy) { | 360 // Note that Promises are Proxies. |
| 333 // Note that Promises are Proxies. | 361 p("new PromiseProxy"); |
| 334 p("new PromiseProxy"); | 362 if (isPromise) { |
| 335 if (isPromise) { | 363 printTypeArguments(returnTypeNode); |
| 336 printTypeArguments(returnTypeNode); | 364 } else { |
| 337 } else { | 365 p("<SendPort>"); |
| 338 p("<SendPort>"); | 366 } |
| 367 p("("); |
| 339 } | 368 } |
| 340 p("("); | |
| 341 } | 369 } |
| 342 p("this."); | 370 p("this."); |
| 343 if (isVoid) { | 371 if (isVoid) { |
| 344 p("send"); | 372 p("send"); |
| 345 } else { | 373 } else { |
| 346 p("call"); | 374 p("call"); |
| 347 } | 375 } |
| 348 p("([\""); | 376 p("([\""); |
| 349 accept(x.getName()); | 377 accept(x.getName()); |
| 350 p("\""); | 378 p("\""); |
| 351 DartVisitor params = new DartVisitor() { | 379 DartVisitor params = new DartVisitor() { |
| 352 @Override | 380 @Override |
| 353 public boolean visit(DartIdentifier x, DartContext ctx) { | 381 public boolean visit(DartIdentifier x, DartContext ctx) { |
| 354 p(", "); | 382 p(", "); |
| 355 p(x.getTargetName()); | 383 p(x.getTargetName()); |
| 356 return false; | 384 return false; |
| 357 } | 385 } |
| 358 | 386 |
| 359 @Override | 387 @Override |
| 360 public boolean visit(DartParameter x, DartContext ctx) { | 388 public boolean visit(DartParameter x, DartContext ctx) { |
| 361 accept(x.getName()); | 389 accept(x.getName()); |
| 362 return false; | 390 return false; |
| 363 } | 391 } |
| 364 }; | 392 }; |
| 365 params.acceptList(func.getParams()); | 393 params.acceptList(func.getParams()); |
| 366 p("])"); | 394 p("])"); |
| 367 if (isProxy) { | 395 if (isPromiseForProxy) { |
| 368 p(")"); | 396 p("))))"); |
| 369 } | 397 } else { |
| 370 if (!isSimple) { | 398 if (isProxy) { |
| 371 p(")"); | 399 p(")"); |
| 400 } |
| 401 if (!isSimple) { |
| 402 p(")"); |
| 403 } |
| 372 } | 404 } |
| 373 p(";"); | 405 p(";"); |
| 374 nl(); | 406 nl(); |
| 375 | 407 |
| 376 p(" }"); | 408 p(" }"); |
| 377 nl(); | 409 nl(); |
| 378 | 410 |
| 379 return false; | 411 return false; |
| 380 } | 412 } |
| 381 | 413 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 404 if (first) { | 436 if (first) { |
| 405 p(" "); | 437 p(" "); |
| 406 } else { | 438 } else { |
| 407 p(" else "); | 439 p(" else "); |
| 408 } | 440 } |
| 409 p("if (command == \""); | 441 p("if (command == \""); |
| 410 printFunctionName(member); | 442 printFunctionName(member); |
| 411 p("\") {"); | 443 p("\") {"); |
| 412 nl(); | 444 nl(); |
| 413 int proxies = unpackParams(member); | 445 int proxies = unpackParams(member); |
| 414 String extra = ""; | |
| 415 if (proxies != 0) { | 446 if (proxies != 0) { |
| 416 p(" Promise done = new Promise();"); | 447 // FIXME(benl): we don't need to gather them anymore, could just pass th
em directly. Too |
| 417 nl(); | 448 // lazy right now. |
| 418 p(" done.waitFor(promises, " + proxies + ");"); | |
| 419 nl(); | |
| 420 p(" done.addCompleteHandler((_) {"); | |
| 421 nl(); | |
| 422 gatherProxies(member); | 449 gatherProxies(member); |
| 423 extra = " "; | |
| 424 } | 450 } |
| 425 callTarget((DartMethodDefinition)member, extra); | 451 callTarget((DartMethodDefinition)member, ""); |
| 426 if (proxies != 0) { | |
| 427 p(" });"); | |
| 428 nl(); | |
| 429 } | |
| 430 p(" }"); | 452 p(" }"); |
| 431 first = false; | 453 first = false; |
| 432 } | 454 } |
| 433 p(" else {"); | 455 p(" else {"); |
| 434 nl(); | 456 nl(); |
| 435 p(" // TODO(kasperl,benl): Somehow throw an exception instead."); | 457 p(" // TODO(kasperl,benl): Somehow throw an exception instead."); |
| 436 nl(); | 458 nl(); |
| 437 p(" reply(\"Exception: command '\" + command + \"' not understood by "
+ dispatcherName + ".\");"); | 459 p(" reply(\"Exception: command '\" + command + \"' not understood by "
+ dispatcherName + ".\");"); |
| 438 nl(); | 460 nl(); |
| 439 p(" }"); | 461 p(" }"); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 accept(param); | 663 accept(param); |
| 642 } | 664 } |
| 643 return false; | 665 return false; |
| 644 } | 666 } |
| 645 | 667 |
| 646 @Override | 668 @Override |
| 647 public boolean visit(DartParameter x, DartContext ctx) { | 669 public boolean visit(DartParameter x, DartContext ctx) { |
| 648 boolean isSimpleType = isSimpleType(x.getTypeNode()); | 670 boolean isSimpleType = isSimpleType(x.getTypeNode()); |
| 649 | 671 |
| 650 if (!isSimpleType) { | 672 if (!isSimpleType) { |
| 651 p(" "); | 673 p(" "); |
| 652 accept(x.getTypeNode()); | 674 accept(x.getTypeNode()); |
| 653 p(" "); | 675 p(" "); |
| 654 accept(x.getName()); | 676 accept(x.getName()); |
| 655 p(" = new "); | 677 p(" = new "); |
| 656 accept(x.getTypeNode()); | 678 accept(x.getTypeNode()); |
| 657 p("Impl(promises[" + proxies + "]);"); | 679 p("Impl(promises[" + proxies + "]);"); |
| 658 ++proxies; | 680 ++proxies; |
| 659 nl(); | 681 nl(); |
| 660 } | 682 } |
| 661 return false; | 683 return false; |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 } | 869 } |
| 848 p(" "); | 870 p(" "); |
| 849 visitor.accept(x.getName()); | 871 visitor.accept(x.getName()); |
| 850 p("("); | 872 p("("); |
| 851 printParams(func.getParams()); | 873 printParams(func.getParams()); |
| 852 p(")"); | 874 p(")"); |
| 853 | 875 |
| 854 return true; | 876 return true; |
| 855 } | 877 } |
| 856 } | 878 } |
| OLD | NEW |