OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // --------------------------------------------------------------------------- | 5 // --------------------------------------------------------------------------- |
6 // Support for JS interoperability | 6 // Support for JS interoperability |
7 // --------------------------------------------------------------------------- | 7 // --------------------------------------------------------------------------- |
8 function SendPortSync() { | 8 function SendPortSync() { |
9 } | 9 } |
10 | 10 |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 proxiedObjectTable.add(proxy, id); | 461 proxiedObjectTable.add(proxy, id); |
462 return proxy; | 462 return proxy; |
463 } | 463 } |
464 } | 464 } |
465 | 465 |
466 // Remote handler to construct a new JavaScript object given its | 466 // Remote handler to construct a new JavaScript object given its |
467 // serialized constructor and arguments. | 467 // serialized constructor and arguments. |
468 function construct(args) { | 468 function construct(args) { |
469 args = args.map(deserialize); | 469 args = args.map(deserialize); |
470 var constructor = args[0]; | 470 var constructor = args[0]; |
471 args = Array.prototype.slice.call(args, 1); | |
472 | 471 |
473 // Until 10 args, the 'new' operator is used. With more arguments we use a | 472 // The following code solves the problem of invoking a JavaScript |
474 // generic way that may not work, particularly when the constructor does not | 473 // constructor with an unknown number arguments. |
475 // have an "apply" method. | 474 // First bind the constructor to the argument list using bind.apply(). |
476 var ret = null; | 475 // The first argument to bind() is the binding of 'this', make it 'null' |
alexandre.ardhuin
2013/10/13 18:49:41
A line has been dropped compared to js_dart2js.dar
justinfagnani
2013/10/15 22:18:08
It's slightly different, since this args list alre
| |
477 if (args.length === 0) { | 476 // After that, use the JavaScript 'new' operator which overrides any binding |
478 ret = new constructor(); | 477 // of 'this' with the new instance. |
479 } else if (args.length === 1) { | 478 args[0] = null; |
480 ret = new constructor(args[0]); | 479 var factoryFunction = constructor.bind.apply(constructor, args); |
481 } else if (args.length === 2) { | 480 return serialize(new factoryFunction()); |
482 ret = new constructor(args[0], args[1]); | |
483 } else if (args.length === 3) { | |
484 ret = new constructor(args[0], args[1], args[2]); | |
485 } else if (args.length === 4) { | |
486 ret = new constructor(args[0], args[1], args[2], args[3]); | |
487 } else if (args.length === 5) { | |
488 ret = new constructor(args[0], args[1], args[2], args[3], args[4]); | |
489 } else if (args.length === 6) { | |
490 ret = new constructor(args[0], args[1], args[2], args[3], args[4], | |
491 args[5]); | |
492 } else if (args.length === 7) { | |
493 ret = new constructor(args[0], args[1], args[2], args[3], args[4], | |
494 args[5], args[6]); | |
495 } else if (args.length === 8) { | |
496 ret = new constructor(args[0], args[1], args[2], args[3], args[4], | |
497 args[5], args[6], args[7]); | |
498 } else if (args.length === 9) { | |
499 ret = new constructor(args[0], args[1], args[2], args[3], args[4], | |
500 args[5], args[6], args[7], args[8]); | |
501 } else if (args.length === 10) { | |
502 ret = new constructor(args[0], args[1], args[2], args[3], args[4], | |
503 args[5], args[6], args[7], args[8], args[9]); | |
504 } else { | |
505 // Dummy Type with correct constructor. | |
506 var Type = function(){}; | |
507 Type.prototype = constructor.prototype; | |
508 | |
509 // Create a new instance | |
510 var instance = new Type(); | |
511 | |
512 // Call the original constructor. | |
513 ret = constructor.apply(instance, args); | |
514 ret = Object(ret) === ret ? ret : instance; | |
515 } | |
516 return serialize(ret); | |
517 } | 481 } |
518 | 482 |
519 // Remote handler to return the top-level JavaScript context. | 483 // Remote handler to return the top-level JavaScript context. |
520 function context(data) { | 484 function context(data) { |
521 return serialize(globalContext); | 485 return serialize(globalContext); |
522 } | 486 } |
523 | 487 |
524 // Return true if a JavaScript proxy is instance of a given type (instanceof). | 488 // Return true if a JavaScript proxy is instance of a given type (instanceof). |
525 function proxyInstanceof(args) { | 489 function proxyInstanceof(args) { |
526 var obj = deserialize(args[0]); | 490 var obj = deserialize(args[0]); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
564 port.receive(f); | 528 port.receive(f); |
565 window.registerPort(name, port.toSendPort()); | 529 window.registerPort(name, port.toSendPort()); |
566 } | 530 } |
567 | 531 |
568 makeGlobalPort('dart-js-context', context); | 532 makeGlobalPort('dart-js-context', context); |
569 makeGlobalPort('dart-js-create', construct); | 533 makeGlobalPort('dart-js-create', construct); |
570 makeGlobalPort('dart-js-instanceof', proxyInstanceof); | 534 makeGlobalPort('dart-js-instanceof', proxyInstanceof); |
571 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); | 535 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); |
572 makeGlobalPort('dart-js-convert', proxyConvert); | 536 makeGlobalPort('dart-js-convert', proxyConvert); |
573 })(); | 537 })(); |
OLD | NEW |