Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: sdk/lib/js/dartium/js_dartium.dart

Issue 1327083002: Revert "Patched in Dartium JsInterop" (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/js/dart2js/js_dart2js.dart ('k') | sdk/lib/svg/dart2js/svg_dart2js.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 interoperating with JavaScript. 6 * Support for interoperating with JavaScript.
7 * 7 *
8 * This library provides access to JavaScript objects from Dart, allowing 8 * This library provides access to JavaScript objects from Dart, allowing
9 * Dart code to get and set properties, and call methods of JavaScript objects 9 * Dart code to get and set properties, and call methods of JavaScript objects
10 * and invoke JavaScript functions. The library takes care of converting 10 * and invoke JavaScript functions. The library takes care of converting
11 * between Dart and JavaScript objects where possible, or providing proxies if 11 * between Dart and JavaScript objects where possible, or providing proxies if
12 * conversion isn't possible. 12 * conversion isn't possible.
13 * 13 *
14 * This library does not yet make Dart objects usable from JavaScript, their 14 * This library does not yet make Dart objects usable from JavaScript, their
15 * methods and proeprties are not accessible, though it does allow Dart 15 * methods and proeprties are not accessible, though it does allow Dart
16 * functions to be passed into and called from JavaScript. 16 * functions to be passed into and called from JavaScript.
17 * 17 *
18 * [JsObject] is the core type and represents a proxy of a JavaScript object. 18 * [JsObject] is the core type and represents a proxy of a JavaScript object.
19 * JsObject gives access to the underlying JavaScript objects properties and 19 * JsObject gives access to the underlying JavaScript objects properties and
20 * methods. `JsObject`s can be acquired by calls to JavaScript, or they can be 20 * methods. `JsObject`s can be acquired by calls to JavaScript, or they can be
21 * created from proxies to JavaScript constructors. 21 * created from proxies to JavaScript constructors.
22 * 22 *
23 * The top-level getter [context] provides a [JsObject] that represents the 23 * The top-level getter [context] provides a [JsObject] that represents the
24 * global object in JavaScript, usually `window`. 24 * global object in JavaScript, usually `window`.
25 * 25 *
26 * The following example shows an alert dialog via a JavaScript call to the 26 * The following example shows an alert dialog via a JavaScript call to the
27 * global function `alert()`: 27 * global function `alert()`:
28 * 28 *
29 * import 'dart:js'; 29 * import 'dart:js';
30 * 30 *
31 * main() => context.callMethod('alert', ['Hello from Dart!']); 31 * main() => context.callMethod('alert', ['Hello from Dart!']);
32 * 32 *
33 * This example shows how to create a [JsObject] from a JavaScript constructor 33 * This example shows how to create a [JsObject] from a JavaScript constructor
34 * and access its properties: 34 * and access its properties:
35 * 35 *
36 * import 'dart:js'; 36 * import 'dart:js';
37 * 37 *
38 * main() { 38 * main() {
39 * var object = new JsObject(context['Object']); 39 * var object = new JsObject(context['Object']);
40 * object['greeting'] = 'Hello'; 40 * object['greeting'] = 'Hello';
41 * object['greet'] = (name) => "${object['greeting']} $name"; 41 * object['greet'] = (name) => "${object['greeting']} $name";
42 * var message = object.callMethod('greet', ['JavaScript']); 42 * var message = object.callMethod('greet', ['JavaScript']);
43 * context['console'].callMethod('log', [message]); 43 * context['console'].callMethod('log', [message]);
44 * } 44 * }
45 * 45 *
46 * ## Proxying and automatic conversion 46 * ## Proxying and automatic conversion
47 * 47 *
48 * When setting properties on a JsObject or passing arguments to a Javascript 48 * When setting properties on a JsObject or passing arguments to a Javascript
49 * method or function, Dart objects are automatically converted or proxied to 49 * method or function, Dart objects are automatically converted or proxied to
50 * JavaScript objects. When accessing JavaScript properties, or when a Dart 50 * JavaScript objects. When accessing JavaScript properties, or when a Dart
51 * closure is invoked from JavaScript, the JavaScript objects are also 51 * closure is invoked from JavaScript, the JavaScript objects are also
52 * converted to Dart. 52 * converted to Dart.
53 * 53 *
54 * Functions and closures are proxied in such a way that they are callable. A 54 * Functions and closures are proxied in such a way that they are callable. A
55 * Dart closure assigned to a JavaScript property is proxied by a function in 55 * Dart closure assigned to a JavaScript property is proxied by a function in
56 * JavaScript. A JavaScript function accessed from Dart is proxied by a 56 * JavaScript. A JavaScript function accessed from Dart is proxied by a
57 * [JsFunction], which has a [apply] method to invoke it. 57 * [JsFunction], which has a [apply] method to invoke it.
(...skipping 15 matching lines...) Expand all
73 * ## Converting collections with JsObject.jsify() 73 * ## Converting collections with JsObject.jsify()
74 * 74 *
75 * To create a JavaScript collection from a Dart collection use the 75 * To create a JavaScript collection from a Dart collection use the
76 * [JsObject.jsify] constructor, which converts Dart [Map]s and [Iterable]s 76 * [JsObject.jsify] constructor, which converts Dart [Map]s and [Iterable]s
77 * into JavaScript Objects and Arrays. 77 * into JavaScript Objects and Arrays.
78 * 78 *
79 * The following expression creats a new JavaScript object with the properties 79 * The following expression creats a new JavaScript object with the properties
80 * `a` and `b` defined: 80 * `a` and `b` defined:
81 * 81 *
82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); 82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2});
83 * 83 *
84 * This expression creates a JavaScript array: 84 * This expression creates a JavaScript array:
85 * 85 *
86 * var jsArray = new JsObject.jsify([1, 2, 3]); 86 * var jsArray = new JsObject.jsify([1, 2, 3]);
87 */ 87 */
88 library dart.js; 88 library dart.js;
89 89
90 import 'dart:collection' show ListMixin; 90 import 'dart:collection' show ListMixin;
91 import 'dart:nativewrappers'; 91 import 'dart:nativewrappers';
92 import 'dart:math' as math; 92 import 'dart:math' as math;
93 import 'dart:mirrors' as mirrors; 93 import 'dart:mirrors' as mirrors;
94 import 'dart:html' as html;
95 94
96 // Pretend we are always in checked mode as we aren't interested in users 95 // Pretend we are always in checked mode as we aren't interested in users
97 // running Dartium code outside of checked mode. 96 // running Dartium code outside of checked mode.
98 final bool CHECK_JS_INVOCATIONS = true; 97 final bool CHECK_JS_INVOCATIONS = true;
99 98
100 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); 99 final _allowedMethods = new Map<Symbol, _DeclarationSet>();
101 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); 100 final _allowedGetters = new Map<Symbol, _DeclarationSet>();
102 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); 101 final _allowedSetters = new Map<Symbol, _DeclarationSet>();
103 102
104 final _jsInterfaceTypes = new Set<Type>(); 103 final _jsInterfaceTypes = new Set<Type>();
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 * The properties of the JavaScript object are accessible via the `[]` and 484 * The properties of the JavaScript object are accessible via the `[]` and
486 * `[]=` operators. Methods are callable via [callMethod]. 485 * `[]=` operators. Methods are callable via [callMethod].
487 */ 486 */
488 class JsObject extends NativeFieldWrapperClass2 { 487 class JsObject extends NativeFieldWrapperClass2 {
489 JsObject.internal(); 488 JsObject.internal();
490 489
491 /** 490 /**
492 * Constructs a new JavaScript object from [constructor] and returns a proxy 491 * Constructs a new JavaScript object from [constructor] and returns a proxy
493 * to it. 492 * to it.
494 */ 493 */
495 factory JsObject(JsFunction constructor, [List arguments]) { 494 factory JsObject(JsFunction constructor, [List arguments]) =>
496 try { 495 _create(constructor, arguments);
497 return _create(constructor, arguments);
498 } catch (e) {
499 // Re-throw any errors (returned as a string) as a DomException.
500 throw new html.DomException.jsInterop(e);
501 }
502 }
503 496
504 static JsObject _create( 497 static JsObject _create(
505 JsFunction constructor, arguments) native "JsObject_constructorCallback"; 498 JsFunction constructor, arguments) native "JsObject_constructorCallback";
506 499
507 _buildArgs(Invocation invocation) { 500 _buildArgs(Invocation invocation) {
508 if (invocation.namedArguments.isEmpty) { 501 if (invocation.namedArguments.isEmpty) {
509 return invocation.positionalArguments; 502 return invocation.positionalArguments;
510 } else { 503 } else {
511 var varArgs = new Map<String, Object>(); 504 var varArgs = new Map<String, Object>();
512 invocation.namedArguments.forEach((symbol, val) { 505 invocation.namedArguments.forEach((symbol, val) {
513 varArgs[mirrors.MirrorSystem.getName(symbol)] = val; 506 varArgs[mirrors.MirrorSystem.getName(symbol)] = val;
514 }); 507 });
515 return invocation.positionalArguments.toList() 508 return invocation.positionalArguments.toList()
516 ..add(new JsObject.jsify(varArgs)); 509 ..add(new JsObject.jsify(varArgs));
517 } 510 }
518 } 511 }
519 512
520 /** 513 /**
521 * Constructs a [JsObject] that proxies a native Dart object; _for expert use 514 * Constructs a [JsObject] that proxies a native Dart object; _for expert use
522 * only_. 515 * only_.
523 * 516 *
524 * Use this constructor only if you wish to get access to JavaScript 517 * Use this constructor only if you wish to get access to JavaScript
525 * properties attached to a browser host object, such as a Node or Blob, that 518 * properties attached to a browser host object, such as a Node or Blob, that
526 * is normally automatically converted into a native Dart object. 519 * is normally automatically converted into a native Dart object.
527 * 520 *
528 * An exception will be thrown if [object] either is `null` or has the type 521 * An exception will be thrown if [object] either is `null` or has the type
529 * `bool`, `num`, or `String`. 522 * `bool`, `num`, or `String`.
530 */ 523 */
531 factory JsObject.fromBrowserObject(object) { 524 factory JsObject.fromBrowserObject(object) {
532 if (object is num || object is String || object is bool || object == null) { 525 if (object is num || object is String || object is bool || object == null) {
533 throw new ArgumentError("object cannot be a num, string, bool, or null"); 526 throw new ArgumentError("object cannot be a num, string, bool, or null");
534 } 527 }
535 return _fromBrowserObject(object); 528 return _fromBrowserObject(object);
536 } 529 }
537 530
538 /** 531 /**
539 * Recursively converts a JSON-like collection of Dart objects to a 532 * Recursively converts a JSON-like collection of Dart objects to a
540 * collection of JavaScript objects and returns a [JsObject] proxy to it. 533 * collection of JavaScript objects and returns a [JsObject] proxy to it.
541 * 534 *
542 * [object] must be a [Map] or [Iterable], the contents of which are also 535 * [object] must be a [Map] or [Iterable], the contents of which are also
543 * converted. Maps and Iterables are copied to a new JavaScript object. 536 * converted. Maps and Iterables are copied to a new JavaScript object.
544 * Primitives and other transferrable values are directly converted to their 537 * Primitives and other transferrable values are directly converted to their
545 * JavaScript type, and all other objects are proxied. 538 * JavaScript type, and all other objects are proxied.
546 */ 539 */
547 factory JsObject.jsify(object) { 540 factory JsObject.jsify(object) {
548 if ((object is! Map) && (object is! Iterable)) { 541 if ((object is! Map) && (object is! Iterable)) {
549 throw new ArgumentError("object must be a Map or Iterable"); 542 throw new ArgumentError("object must be a Map or Iterable");
550 } 543 }
551 return _jsify(object); 544 return _jsify(object);
552 } 545 }
553 546
554 static JsObject _jsify(object) native "JsObject_jsify"; 547 static JsObject _jsify(object) native "JsObject_jsify";
555 548
556 static JsObject _fromBrowserObject(object) => html.unwrap_jso(object); 549 static JsObject _fromBrowserObject(
550 object) native "JsObject_fromBrowserObject";
557 551
558 /** 552 /**
559 * Returns the value associated with [property] from the proxied JavaScript 553 * Returns the value associated with [property] from the proxied JavaScript
560 * object. 554 * object.
561 * 555 *
562 * The type of [property] must be either [String] or [num]. 556 * The type of [property] must be either [String] or [num].
563 */ 557 */
564 operator [](property) { 558 operator [](property) native "JsObject_[]";
565 try {
566 return _operator_getter(property);
567 } catch (e) {
568 // Re-throw any errors (returned as a string) as a DomException.
569 throw new html.DomException.jsInterop(e);
570 }
571 }
572 _operator_getter(property) native "JsObject_[]";
573 559
574 /** 560 /**
575 * Sets the value associated with [property] on the proxied JavaScript 561 * Sets the value associated with [property] on the proxied JavaScript
576 * object. 562 * object.
577 * 563 *
578 * The type of [property] must be either [String] or [num]. 564 * The type of [property] must be either [String] or [num].
579 */ 565 */
580 operator []=(property, value) { 566 operator []=(property, value) native "JsObject_[]=";
581 try {
582 _operator_setter(property, value);
583 } catch (e) {
584 // Re-throw any errors (returned as a string) as a DomException.
585 throw new html.DomException.jsInterop(e);
586 }
587 }
588 _operator_setter(property, value) native "JsObject_[]=";
589 567
590 int get hashCode native "JsObject_hashCode"; 568 int get hashCode native "JsObject_hashCode";
591 569
592 operator ==(other) { 570 operator ==(other) => other is JsObject && _identityEquality(this, other);
593 var is_JsObject = other is JsObject;
594 if (!is_JsObject) {
595 other = html.unwrap_jso(other);
596 is_JsObject = other is JsObject;
597 }
598 return is_JsObject && _identityEquality(this, other);
599 }
600 571
601 static bool _identityEquality( 572 static bool _identityEquality(
602 JsObject a, JsObject b) native "JsObject_identityEquality"; 573 JsObject a, JsObject b) native "JsObject_identityEquality";
603 574
604 /** 575 /**
605 * Returns `true` if the JavaScript object contains the specified property 576 * Returns `true` if the JavaScript object contains the specified property
606 * either directly or though its prototype chain. 577 * either directly or though its prototype chain.
607 * 578 *
608 * This is the equivalent of the `in` operator in JavaScript. 579 * This is the equivalent of the `in` operator in JavaScript.
609 */ 580 */
(...skipping 30 matching lines...) Expand all
640 * Calls [method] on the JavaScript object with the arguments [args] and 611 * Calls [method] on the JavaScript object with the arguments [args] and
641 * returns the result. 612 * returns the result.
642 * 613 *
643 * The type of [method] must be either [String] or [num]. 614 * The type of [method] must be either [String] or [num].
644 */ 615 */
645 callMethod(String method, [List args]) { 616 callMethod(String method, [List args]) {
646 try { 617 try {
647 return _callMethod(method, args); 618 return _callMethod(method, args);
648 } catch (e) { 619 } catch (e) {
649 if (hasProperty(method)) { 620 if (hasProperty(method)) {
650 // Return a DomException if DOM call returned an error. 621 rethrow;
651 throw new html.DomException.jsInterop(e);
652 } else { 622 } else {
653 throw new NoSuchMethodError(this, new Symbol(method), args, null); 623 throw new NoSuchMethodError(this, new Symbol(method), args, null);
654 } 624 }
655 } 625 }
656 } 626 }
657 627
658 noSuchMethod(Invocation invocation) { 628 noSuchMethod(Invocation invocation) {
659 throwError() { 629 throwError() {
660 throw new NoSuchMethodError(this, invocation.memberName, 630 throw new NoSuchMethodError(this, invocation.memberName,
661 invocation.positionalArguments, invocation.namedArguments); 631 invocation.positionalArguments, invocation.namedArguments);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 throw new RangeError.range(end, start, cachedLength); 747 throw new RangeError.range(end, start, cachedLength);
778 } 748 }
779 } 749 }
780 750
781 // Methods required by ListMixin 751 // Methods required by ListMixin
782 752
783 E operator [](index) { 753 E operator [](index) {
784 if (index is int) { 754 if (index is int) {
785 _checkIndex(index); 755 _checkIndex(index);
786 } 756 }
787 757 return super[index];
788 // Lazily create the Dart class that wraps the JS object when the object in
789 // a list if fetched.
790 var wrap_entry = html.wrap_jso(super[index]);
791 super[index] = wrap_entry;
792 return wrap_entry;
793 } 758 }
794 759
795 void operator []=(index, E value) { 760 void operator []=(index, E value) {
796 if (index is int) { 761 if (index is int) {
797 _checkIndex(index); 762 _checkIndex(index);
798 } 763 }
799 super[index] = value; 764 super[index] = value;
800 } 765 }
801 766
802 int get length native "JsArray_length"; 767 int get length native "JsArray_length";
803 768
804 set length(int length) { 769 void set length(int length) {
805 super['length'] = length; 770 super['length'] = length;
806 } 771 }
807 772
808 // Methods overriden for better performance 773 // Methods overriden for better performance
809 774
810 void add(E value) { 775 void add(E value) {
811 callMethod('push', [value]); 776 callMethod('push', [value]);
812 } 777 }
813 778
814 void addAll(Iterable<E> iterable) { 779 void addAll(Iterable<E> iterable) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 828
864 /** 829 /**
865 * Returns a method that can be called with an arbitrary number (for n less 830 * Returns a method that can be called with an arbitrary number (for n less
866 * than 11) of arguments without violating Dart type checks. 831 * than 11) of arguments without violating Dart type checks.
867 */ 832 */
868 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => 833 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) =>
869 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED, 834 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED,
870 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED, 835 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED,
871 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly( 836 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly(
872 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); 837 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]));
OLDNEW
« no previous file with comments | « sdk/lib/js/dart2js/js_dart2js.dart ('k') | sdk/lib/svg/dart2js/svg_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698