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 * The js.dart library provides simple JavaScript invocation from Dart that | 6 * The js.dart library provides simple JavaScript invocation from Dart that |
7 * works on both Dartium and on other modern browsers via Dart2JS. | 7 * works on both Dartium and on other modern browsers via Dart2JS. |
8 * | 8 * |
9 * It provides a model based on scoped [Proxy] objects. Proxies give Dart | 9 * It provides a model based on scoped [Proxy] objects. Proxies give Dart |
10 * code access to JavaScript objects, fields, and functions as well as the | 10 * code access to JavaScript objects, fields, and functions as well as the |
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 // Enters a new scope in the JavaScript context. | 604 // Enters a new scope in the JavaScript context. |
605 function enterJavaScriptScope() { | 605 function enterJavaScriptScope() { |
606 proxiedObjectTable.enterScope(); | 606 proxiedObjectTable.enterScope(); |
607 } | 607 } |
608 | 608 |
609 // Enters a new scope in both the JavaScript and Dart context. | 609 // Enters a new scope in both the JavaScript and Dart context. |
610 var _dartEnterScopePort = null; | 610 var _dartEnterScopePort = null; |
611 function enterScope() { | 611 function enterScope() { |
612 enterJavaScriptScope(); | 612 enterJavaScriptScope(); |
613 if (!_dartEnterScopePort) { | 613 if (!_dartEnterScopePort) { |
614 _dartEnterScopePort = window.lookupPort('js-dart-enter-scope'); | 614 _dartEnterScopePort = window.lookupPort('js-dart-interop-enter-scope'); |
615 } | 615 } |
616 return _dartEnterScopePort.callSync([]); | 616 return _dartEnterScopePort.callSync([]); |
617 } | 617 } |
618 | 618 |
619 // Exits the current scope (and invalidate local IDs) in the JavaScript | 619 // Exits the current scope (and invalidate local IDs) in the JavaScript |
620 // context. | 620 // context. |
621 function exitJavaScriptScope() { | 621 function exitJavaScriptScope() { |
622 proxiedObjectTable.exitScope(); | 622 proxiedObjectTable.exitScope(); |
623 } | 623 } |
624 | 624 |
625 // Exits the current scope in both the JavaScript and Dart context. | 625 // Exits the current scope in both the JavaScript and Dart context. |
626 var _dartExitScopePort = null; | 626 var _dartExitScopePort = null; |
627 function exitScope(depth) { | 627 function exitScope(depth) { |
628 exitJavaScriptScope(); | 628 exitJavaScriptScope(); |
629 if (!_dartExitScopePort) { | 629 if (!_dartExitScopePort) { |
630 _dartExitScopePort = window.lookupPort('js-dart-exit-scope'); | 630 _dartExitScopePort = window.lookupPort('js-dart-interop-exit-scope'); |
631 } | 631 } |
632 return _dartExitScopePort.callSync([ depth ]); | 632 return _dartExitScopePort.callSync([ depth ]); |
633 } | 633 } |
634 | 634 |
635 makeGlobalPort('dart-js-context', context); | 635 makeGlobalPort('dart-js-interop-context', context); |
636 makeGlobalPort('dart-js-create', construct); | 636 makeGlobalPort('dart-js-interop-create', construct); |
637 makeGlobalPort('dart-js-proxy-count', proxyCount); | 637 makeGlobalPort('dart-js-interop-proxy-count', proxyCount); |
638 makeGlobalPort('dart-js-equals', proxyEquals); | 638 makeGlobalPort('dart-js-interop-equals', proxyEquals); |
639 makeGlobalPort('dart-js-instanceof', proxyInstanceof); | 639 makeGlobalPort('dart-js-interop-instanceof', proxyInstanceof); |
640 makeGlobalPort('dart-js-has-property', proxyHasProperty); | 640 makeGlobalPort('dart-js-interop-has-property', proxyHasProperty); |
641 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); | 641 makeGlobalPort('dart-js-interop-delete-property', proxyDeleteProperty); |
642 makeGlobalPort('dart-js-convert', proxyConvert); | 642 makeGlobalPort('dart-js-interop-convert', proxyConvert); |
643 makeGlobalPort('dart-js-enter-scope', enterJavaScriptScope); | 643 makeGlobalPort('dart-js-interop-enter-scope', enterJavaScriptScope); |
644 makeGlobalPort('dart-js-exit-scope', exitJavaScriptScope); | 644 makeGlobalPort('dart-js-interop-exit-scope', exitJavaScriptScope); |
645 makeGlobalPort('dart-js-globalize', function(data) { | 645 makeGlobalPort('dart-js-interop-globalize', function(data) { |
646 if (data[0] == "objref" || data[0] == "funcref") return proxiedObjectTable.g
lobalize(data[1]); | 646 if (data[0] == "objref" || data[0] == "funcref") return proxiedObjectTable.g
lobalize(data[1]); |
647 throw 'Illegal type: ' + data[0]; | 647 throw 'Illegal type: ' + data[0]; |
648 }); | 648 }); |
649 makeGlobalPort('dart-js-invalidate', function(data) { | 649 makeGlobalPort('dart-js-interop-invalidate', function(data) { |
650 if (data[0] == "objref" || data[0] == "funcref") return proxiedObjectTable.i
nvalidate(data[1]); | 650 if (data[0] == "objref" || data[0] == "funcref") return proxiedObjectTable.i
nvalidate(data[1]); |
651 throw 'Illegal type: ' + data[0]; | 651 throw 'Illegal type: ' + data[0]; |
652 }); | 652 }); |
653 })(); | 653 })(); |
654 """; | 654 """; |
655 | 655 |
656 // Injects JavaScript source code onto the page. | 656 // Injects JavaScript source code onto the page. |
657 // This is only used to load the bootstrapping code above. | 657 // This is only used to load the bootstrapping code above. |
658 void _inject(code) { | 658 void _inject(code) { |
659 final script = new ScriptElement(); | 659 final script = new ScriptElement(); |
(...skipping 19 matching lines...) Expand all Loading... |
679 // Global ports to manage communication from JS to Dart. | 679 // Global ports to manage communication from JS to Dart. |
680 ReceivePortSync _dartEnterDartScope = null; | 680 ReceivePortSync _dartEnterDartScope = null; |
681 ReceivePortSync _dartExitDartScope = null; | 681 ReceivePortSync _dartExitDartScope = null; |
682 | 682 |
683 // Initializes bootstrap code and ports. | 683 // Initializes bootstrap code and ports. |
684 void _initialize() { | 684 void _initialize() { |
685 if (_jsPortSync != null) return; | 685 if (_jsPortSync != null) return; |
686 | 686 |
687 // Test if the port is already defined. | 687 // Test if the port is already defined. |
688 try { | 688 try { |
689 _jsPortSync = window.lookupPort('dart-js-context'); | 689 _jsPortSync = window.lookupPort('dart-js-interop-context'); |
690 } catch (e) { | 690 } catch (e) { |
691 // TODO(vsm): Suppress the exception until dartbug.com/5854 is fixed. | 691 // TODO(vsm): Suppress the exception until dartbug.com/5854 is fixed. |
692 } | 692 } |
693 | 693 |
694 // If not, try injecting the script. | 694 // If not, try injecting the script. |
695 if (_jsPortSync == null) { | 695 if (_jsPortSync == null) { |
696 _inject(_JS_BOOTSTRAP); | 696 _inject(_JS_BOOTSTRAP); |
697 _jsPortSync = window.lookupPort('dart-js-context'); | 697 _jsPortSync = window.lookupPort('dart-js-interop-context'); |
698 } | 698 } |
699 | 699 |
700 _jsPortCreate = window.lookupPort('dart-js-create'); | 700 _jsPortCreate = window.lookupPort('dart-js-interop-create'); |
701 _jsPortProxyCount = window.lookupPort('dart-js-proxy-count'); | 701 _jsPortProxyCount = window.lookupPort('dart-js-interop-proxy-count'); |
702 _jsPortEquals = window.lookupPort('dart-js-equals'); | 702 _jsPortEquals = window.lookupPort('dart-js-interop-equals'); |
703 _jsPortInstanceof = window.lookupPort('dart-js-instanceof'); | 703 _jsPortInstanceof = window.lookupPort('dart-js-interop-instanceof'); |
704 _jsPortHasProperty = window.lookupPort('dart-js-has-property'); | 704 _jsPortHasProperty = window.lookupPort('dart-js-interop-has-property'); |
705 _jsPortDeleteProperty = window.lookupPort('dart-js-delete-property'); | 705 _jsPortDeleteProperty = window.lookupPort('dart-js-interop-delete-property'); |
706 _jsPortConvert = window.lookupPort('dart-js-convert'); | 706 _jsPortConvert = window.lookupPort('dart-js-interop-convert'); |
707 _jsEnterJavaScriptScope = window.lookupPort('dart-js-enter-scope'); | 707 _jsEnterJavaScriptScope = window.lookupPort('dart-js-interop-enter-scope'); |
708 _jsExitJavaScriptScope = window.lookupPort('dart-js-exit-scope'); | 708 _jsExitJavaScriptScope = window.lookupPort('dart-js-interop-exit-scope'); |
709 _jsGlobalize = window.lookupPort('dart-js-globalize'); | 709 _jsGlobalize = window.lookupPort('dart-js-interop-globalize'); |
710 _jsInvalidate = window.lookupPort('dart-js-invalidate'); | 710 _jsInvalidate = window.lookupPort('dart-js-interop-invalidate'); |
711 | 711 |
712 _dartEnterDartScope = new ReceivePortSync() | 712 _dartEnterDartScope = new ReceivePortSync() |
713 ..receive((_) => _enterScope()); | 713 ..receive((_) => _enterScope()); |
714 _dartExitDartScope = new ReceivePortSync() | 714 _dartExitDartScope = new ReceivePortSync() |
715 ..receive((args) => _exitScope(args[0])); | 715 ..receive((args) => _exitScope(args[0])); |
716 window.registerPort('js-dart-enter-scope', _dartEnterDartScope.toSendPort()); | 716 window.registerPort('js-dart-interop-enter-scope', _dartEnterDartScope.toSendP
ort()); |
717 window.registerPort('js-dart-exit-scope', _dartExitDartScope.toSendPort()); | 717 window.registerPort('js-dart-interop-exit-scope', _dartExitDartScope.toSendPor
t()); |
718 } | 718 } |
719 | 719 |
720 /** | 720 /** |
721 * Returns a proxy to the global JavaScript context for this page. | 721 * Returns a proxy to the global JavaScript context for this page. |
722 */ | 722 */ |
723 Proxy get context { | 723 Proxy get context { |
724 _enterScopeIfNeeded(); | 724 _enterScopeIfNeeded(); |
725 return _deserialize(_jsPortSync.callSync([])); | 725 return _deserialize(_jsPortSync.callSync([])); |
726 } | 726 } |
727 | 727 |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1389 // debugging / profiling purposes. | 1389 // debugging / profiling purposes. |
1390 void _proxyDebug([String message = '']) { | 1390 void _proxyDebug([String message = '']) { |
1391 print('Proxy status $message:'); | 1391 print('Proxy status $message:'); |
1392 var dartLive = proxyCount(dartOnly: true); | 1392 var dartLive = proxyCount(dartOnly: true); |
1393 var dartTotal = proxyCount(dartOnly: true, all: true); | 1393 var dartTotal = proxyCount(dartOnly: true, all: true); |
1394 var jsLive = proxyCount(jsOnly: true); | 1394 var jsLive = proxyCount(jsOnly: true); |
1395 var jsTotal = proxyCount(jsOnly: true, all: true); | 1395 var jsTotal = proxyCount(jsOnly: true, all: true); |
1396 print(' Dart objects Live : $dartLive (out of $dartTotal ever allocated).'); | 1396 print(' Dart objects Live : $dartLive (out of $dartTotal ever allocated).'); |
1397 print(' JS objects Live : $jsLive (out of $jsTotal ever allocated).'); | 1397 print(' JS objects Live : $jsLive (out of $jsTotal ever allocated).'); |
1398 } | 1398 } |
OLD | NEW |