| 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 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 Loading... |
| 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; |
| 94 | 95 |
| 95 // Pretend we are always in checked mode as we aren't interested in users | 96 // Pretend we are always in checked mode as we aren't interested in users |
| 96 // running Dartium code outside of checked mode. | 97 // running Dartium code outside of checked mode. |
| 97 final bool CHECK_JS_INVOCATIONS = true; | 98 final bool CHECK_JS_INVOCATIONS = true; |
| 98 | 99 |
| 99 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); | 100 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); |
| 100 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); | 101 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); |
| 101 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); | 102 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); |
| 102 | 103 |
| 103 final _jsInterfaceTypes = new Set<Type>(); | 104 final _jsInterfaceTypes = new Set<Type>(); |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 * The properties of the JavaScript object are accessible via the `[]` and | 485 * The properties of the JavaScript object are accessible via the `[]` and |
| 485 * `[]=` operators. Methods are callable via [callMethod]. | 486 * `[]=` operators. Methods are callable via [callMethod]. |
| 486 */ | 487 */ |
| 487 class JsObject extends NativeFieldWrapperClass2 { | 488 class JsObject extends NativeFieldWrapperClass2 { |
| 488 JsObject.internal(); | 489 JsObject.internal(); |
| 489 | 490 |
| 490 /** | 491 /** |
| 491 * Constructs a new JavaScript object from [constructor] and returns a proxy | 492 * Constructs a new JavaScript object from [constructor] and returns a proxy |
| 492 * to it. | 493 * to it. |
| 493 */ | 494 */ |
| 494 factory JsObject(JsFunction constructor, [List arguments]) => | 495 factory JsObject(JsFunction constructor, [List arguments]) { |
| 495 _create(constructor, arguments); | 496 try { |
| 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 } |
| 496 | 503 |
| 497 static JsObject _create( | 504 static JsObject _create( |
| 498 JsFunction constructor, arguments) native "JsObject_constructorCallback"; | 505 JsFunction constructor, arguments) native "JsObject_constructorCallback"; |
| 499 | 506 |
| 500 _buildArgs(Invocation invocation) { | 507 _buildArgs(Invocation invocation) { |
| 501 if (invocation.namedArguments.isEmpty) { | 508 if (invocation.namedArguments.isEmpty) { |
| 502 return invocation.positionalArguments; | 509 return invocation.positionalArguments; |
| 503 } else { | 510 } else { |
| 504 var varArgs = new Map<String, Object>(); | 511 var varArgs = new Map<String, Object>(); |
| 505 invocation.namedArguments.forEach((symbol, val) { | 512 invocation.namedArguments.forEach((symbol, val) { |
| 506 varArgs[mirrors.MirrorSystem.getName(symbol)] = val; | 513 varArgs[mirrors.MirrorSystem.getName(symbol)] = val; |
| 507 }); | 514 }); |
| 508 return invocation.positionalArguments.toList() | 515 return invocation.positionalArguments.toList() |
| 509 ..add(new JsObject.jsify(varArgs)); | 516 ..add(new JsObject.jsify(varArgs)); |
| 510 } | 517 } |
| 511 } | 518 } |
| 512 | 519 |
| 513 /** | 520 /** |
| 514 * Constructs a [JsObject] that proxies a native Dart object; _for expert use | 521 * Constructs a [JsObject] that proxies a native Dart object; _for expert use |
| 515 * only_. | 522 * only_. |
| 516 * | 523 * |
| 517 * Use this constructor only if you wish to get access to JavaScript | 524 * Use this constructor only if you wish to get access to JavaScript |
| 518 * properties attached to a browser host object, such as a Node or Blob, that | 525 * properties attached to a browser host object, such as a Node or Blob, that |
| 519 * is normally automatically converted into a native Dart object. | 526 * is normally automatically converted into a native Dart object. |
| 520 * | 527 * |
| 521 * An exception will be thrown if [object] either is `null` or has the type | 528 * An exception will be thrown if [object] either is `null` or has the type |
| 522 * `bool`, `num`, or `String`. | 529 * `bool`, `num`, or `String`. |
| 523 */ | 530 */ |
| 524 factory JsObject.fromBrowserObject(object) { | 531 factory JsObject.fromBrowserObject(object) { |
| 525 if (object is num || object is String || object is bool || object == null) { | 532 if (object is num || object is String || object is bool || object == null) { |
| 526 throw new ArgumentError("object cannot be a num, string, bool, or null"); | 533 throw new ArgumentError("object cannot be a num, string, bool, or null"); |
| 527 } | 534 } |
| 528 return _fromBrowserObject(object); | 535 return _fromBrowserObject(object); |
| 529 } | 536 } |
| 530 | 537 |
| 531 /** | 538 /** |
| 532 * Recursively converts a JSON-like collection of Dart objects to a | 539 * Recursively converts a JSON-like collection of Dart objects to a |
| 533 * collection of JavaScript objects and returns a [JsObject] proxy to it. | 540 * collection of JavaScript objects and returns a [JsObject] proxy to it. |
| 534 * | 541 * |
| 535 * [object] must be a [Map] or [Iterable], the contents of which are also | 542 * [object] must be a [Map] or [Iterable], the contents of which are also |
| 536 * converted. Maps and Iterables are copied to a new JavaScript object. | 543 * converted. Maps and Iterables are copied to a new JavaScript object. |
| 537 * Primitives and other transferrable values are directly converted to their | 544 * Primitives and other transferrable values are directly converted to their |
| 538 * JavaScript type, and all other objects are proxied. | 545 * JavaScript type, and all other objects are proxied. |
| 539 */ | 546 */ |
| 540 factory JsObject.jsify(object) { | 547 factory JsObject.jsify(object) { |
| 541 if ((object is! Map) && (object is! Iterable)) { | 548 if ((object is! Map) && (object is! Iterable)) { |
| 542 throw new ArgumentError("object must be a Map or Iterable"); | 549 throw new ArgumentError("object must be a Map or Iterable"); |
| 543 } | 550 } |
| 544 return _jsify(object); | 551 return _jsify(object); |
| 545 } | 552 } |
| 546 | 553 |
| 547 static JsObject _jsify(object) native "JsObject_jsify"; | 554 static JsObject _jsify(object) native "JsObject_jsify"; |
| 548 | 555 |
| 549 static JsObject _fromBrowserObject( | 556 static JsObject _fromBrowserObject(object) => html.unwrap_jso(object); |
| 550 object) native "JsObject_fromBrowserObject"; | |
| 551 | 557 |
| 552 /** | 558 /** |
| 553 * Returns the value associated with [property] from the proxied JavaScript | 559 * Returns the value associated with [property] from the proxied JavaScript |
| 554 * object. | 560 * object. |
| 555 * | 561 * |
| 556 * The type of [property] must be either [String] or [num]. | 562 * The type of [property] must be either [String] or [num]. |
| 557 */ | 563 */ |
| 558 operator [](property) native "JsObject_[]"; | 564 operator [](property) { |
| 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_[]"; |
| 559 | 573 |
| 560 /** | 574 /** |
| 561 * Sets the value associated with [property] on the proxied JavaScript | 575 * Sets the value associated with [property] on the proxied JavaScript |
| 562 * object. | 576 * object. |
| 563 * | 577 * |
| 564 * The type of [property] must be either [String] or [num]. | 578 * The type of [property] must be either [String] or [num]. |
| 565 */ | 579 */ |
| 566 operator []=(property, value) native "JsObject_[]="; | 580 operator []=(property, value) { |
| 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_[]="; |
| 567 | 589 |
| 568 int get hashCode native "JsObject_hashCode"; | 590 int get hashCode native "JsObject_hashCode"; |
| 569 | 591 |
| 570 operator ==(other) => other is JsObject && _identityEquality(this, other); | 592 operator ==(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 } |
| 571 | 600 |
| 572 static bool _identityEquality( | 601 static bool _identityEquality( |
| 573 JsObject a, JsObject b) native "JsObject_identityEquality"; | 602 JsObject a, JsObject b) native "JsObject_identityEquality"; |
| 574 | 603 |
| 575 /** | 604 /** |
| 576 * Returns `true` if the JavaScript object contains the specified property | 605 * Returns `true` if the JavaScript object contains the specified property |
| 577 * either directly or though its prototype chain. | 606 * either directly or though its prototype chain. |
| 578 * | 607 * |
| 579 * This is the equivalent of the `in` operator in JavaScript. | 608 * This is the equivalent of the `in` operator in JavaScript. |
| 580 */ | 609 */ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 611 * Calls [method] on the JavaScript object with the arguments [args] and | 640 * Calls [method] on the JavaScript object with the arguments [args] and |
| 612 * returns the result. | 641 * returns the result. |
| 613 * | 642 * |
| 614 * The type of [method] must be either [String] or [num]. | 643 * The type of [method] must be either [String] or [num]. |
| 615 */ | 644 */ |
| 616 callMethod(String method, [List args]) { | 645 callMethod(String method, [List args]) { |
| 617 try { | 646 try { |
| 618 return _callMethod(method, args); | 647 return _callMethod(method, args); |
| 619 } catch (e) { | 648 } catch (e) { |
| 620 if (hasProperty(method)) { | 649 if (hasProperty(method)) { |
| 621 rethrow; | 650 // Return a DomException if DOM call returned an error. |
| 651 throw new html.DomException.jsInterop(e); |
| 622 } else { | 652 } else { |
| 623 throw new NoSuchMethodError(this, new Symbol(method), args, null); | 653 throw new NoSuchMethodError(this, new Symbol(method), args, null); |
| 624 } | 654 } |
| 625 } | 655 } |
| 626 } | 656 } |
| 627 | 657 |
| 628 noSuchMethod(Invocation invocation) { | 658 noSuchMethod(Invocation invocation) { |
| 629 throwError() { | 659 throwError() { |
| 630 throw new NoSuchMethodError(this, invocation.memberName, | 660 throw new NoSuchMethodError(this, invocation.memberName, |
| 631 invocation.positionalArguments, invocation.namedArguments); | 661 invocation.positionalArguments, invocation.namedArguments); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 throw new RangeError.range(end, start, cachedLength); | 777 throw new RangeError.range(end, start, cachedLength); |
| 748 } | 778 } |
| 749 } | 779 } |
| 750 | 780 |
| 751 // Methods required by ListMixin | 781 // Methods required by ListMixin |
| 752 | 782 |
| 753 E operator [](index) { | 783 E operator [](index) { |
| 754 if (index is int) { | 784 if (index is int) { |
| 755 _checkIndex(index); | 785 _checkIndex(index); |
| 756 } | 786 } |
| 757 return super[index]; | 787 |
| 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; |
| 758 } | 793 } |
| 759 | 794 |
| 760 void operator []=(index, E value) { | 795 void operator []=(index, E value) { |
| 761 if (index is int) { | 796 if (index is int) { |
| 762 _checkIndex(index); | 797 _checkIndex(index); |
| 763 } | 798 } |
| 764 super[index] = value; | 799 super[index] = value; |
| 765 } | 800 } |
| 766 | 801 |
| 767 int get length native "JsArray_length"; | 802 int get length native "JsArray_length"; |
| 768 | 803 |
| 769 void set length(int length) { | 804 set length(int length) { |
| 770 super['length'] = length; | 805 super['length'] = length; |
| 771 } | 806 } |
| 772 | 807 |
| 773 // Methods overriden for better performance | 808 // Methods overriden for better performance |
| 774 | 809 |
| 775 void add(E value) { | 810 void add(E value) { |
| 776 callMethod('push', [value]); | 811 callMethod('push', [value]); |
| 777 } | 812 } |
| 778 | 813 |
| 779 void addAll(Iterable<E> iterable) { | 814 void addAll(Iterable<E> iterable) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 | 863 |
| 829 /** | 864 /** |
| 830 * Returns a method that can be called with an arbitrary number (for n less | 865 * Returns a method that can be called with an arbitrary number (for n less |
| 831 * than 11) of arguments without violating Dart type checks. | 866 * than 11) of arguments without violating Dart type checks. |
| 832 */ | 867 */ |
| 833 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => | 868 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => |
| 834 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED, | 869 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED, |
| 835 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED, | 870 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED, |
| 836 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly( | 871 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly( |
| 837 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); | 872 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); |
| OLD | NEW |